1 import javax.swing.*; 2 import java.awt.*; 3 4 public class MelodyPanel extends JPanel{ 5 6 private int length; 7 private Chord[] chords; 8 private boolean erase; 9 private Melody melody; 10 11 public MelodyPanel(){ 12 setLength(16); 13 setErase(true); 14 } 15 16 17 // Actual methods 18 19 public void paintComponent(Graphics g){ 20 super.paintComponent(g); 21 if(!getErase()){ 22 drawRectangles(g, this.getWidth(), this.getHeight()); 23 } else { 24 g.setColor(new Color(0,0,0,0)); 25 g.fillRect(0,0, this.getWidth(), this.getHeight()); 26 } 27 } 28 29 private void drawRectangles(Graphics g, int width, int height){ 30 int clear = (width/4)/(getLength()+1); // width of the space in between rectangles 31 int rect = (width*3/4)/getLength(); // width of each rectangle 32 // draw small rectangles and note names (if available) for every beat 33 for(int i = 1; i <= getLength(); i++){ 34 g.setColor(Color.BLACK); 35 g.drawRoundRect(clear*i+rect*(i-1), height/3, rect, height/3, 10, 10); 36 if(getMelody().getMelody()[i-1] != null){ 37 if(getMelody().findKeyNoteIndex(getMelody().getMelody()[i-1]) != -1){ // note == keyNote 38 g.setColor(Main.keyNoteColors[getMelody().findKeyNoteIndex(getMelody().getMelody()[i-1])]); 39 } else { // note == extraNote 40 g.setColor(Color.LIGHT_GRAY); 41 } 42 g.drawString(getMelody().getMelody()[i-1], (clear*i+rect*(i-1)+rect/2)-(g.getFontMetrics().stringWidth(getMelody().getMelody()[i-1])/2)+1, height/2+(g.getFontMetrics().getHeight()/4)); // place the note name at the center of the boxes 43 } 44 } 45 // draw large rectangles for every chord 46 int beginningIndex = 0; 47 Chord currentChord = getChords()[beginningIndex]; 48 Chord compareChord; 49 int drawStartingXCoordinate; 50 int drawWidth; 51 for(int i = 0; i < getLength(); i++){ 52 if(i < getLength()-1){ 53 compareChord = getChords()[i+1]; 54 } else { 55 compareChord = new Chord("#", false); // create a compareChord that will always be different from the currentChord -> needed to include the last beat 56 } 57 if(currentChord != null){ 58 if(!Chord.chordsEqual(currentChord, compareChord)){ 59 if(getMelody().findKeyNoteIndex(Melody.extractActualNoteName(currentChord.getRootNote())) != -1){ // rootNote == keyNote 60 g.setColor(Main.keyNoteColors[getMelody().findKeyNoteIndex(Melody.extractActualNoteName(currentChord.getRootNote()))]); 61 } else { // rootNote == extraNote 62 g.setColor(Color.LIGHT_GRAY); 63 } 64 drawStartingXCoordinate = clear*(beginningIndex+1)+rect*(beginningIndex); 65 drawWidth = clear*(i-beginningIndex)+rect*(i-beginningIndex+1); 66 g.drawRoundRect(drawStartingXCoordinate, height*2/3+1, drawWidth, height/8, 10, 10); 67 g.drawString(currentChord.getRootNote(), drawStartingXCoordinate+(drawWidth/2)-(g.getFontMetrics().stringWidth(currentChord.getRootNote())/2),height*2/3+1+(g.getFontMetrics().getHeight()*3/4)); 68 if(i < getLength()-1){ 69 beginningIndex = i+1; 70 currentChord = getChords()[beginningIndex]; 71 } 72 } 73 } else { 74 if(compareChord != null){ 75 if(i < getLength()-1){ 76 beginningIndex = i+1; 77 currentChord = getChords()[beginningIndex]; 78 } 79 } 80 } 81 } 82 } 83 84 85 // Getters & Setters 86 87 public int getLength(){ 88 return this.length; 89 } 90 91 public void setLength(int length){ 92 this.length = length; 93 setChords(new Chord[getLength()]); 94 setMelody(new Melody(getLength())); 95 } 96 97 public Chord[] getChords(){ 98 return this.chords; 99 } 100 101 public void setChords(Chord[] chords){ 102 this.chords = chords; 103 } 104 105 public boolean getErase(){ 106 return this.erase; 107 } 108 109 public void setErase(boolean erase){ 110 this.erase = erase; 111 } 112 113 public Melody getMelody(){ 114 return this.melody; 115 } 116 117 public void setMelody(Melody melody){ 118 this.melody = melody; 119 } 120 } 121