ChordsPanel.java
1    import javax.swing.*;
2    import java.awt.*;
3    import java.awt.font.GlyphVector;
4    
5    public class ChordsPanel extends JPanel{
6    
7        private int length;
8        private Chord[] chords;
9        private boolean erase;
10       private Melody calcMelody;
11   
12       public ChordsPanel(Melody calcMelody){
13           setLength(16);
14           setErase(false);
15           setCalcMelody(calcMelody);
16       }
17   
18   
19       // Actual methods
20   
21       @Override
22       public Dimension getPreferredSize(){
23           return new Dimension(Main.OTHER_FRAME_WIDTH, Main.OTHER_FRAME_HEIGHT);
24       }
25   
26       public void paintComponent(Graphics g){
27           super.paintComponent(g);
28           if(!getErase()){
29               drawRectangles(g, this.getWidth(), this.getHeight());
30           } else {
31               g.setColor(new Color(0,0,0,0));
32               g.fillRect(0,0, this.getWidth(), this.getHeight());
33           }
34       }
35   
36       private void drawRectangles(Graphics g, int width, int height){
37           int clear = (width/4)/(getLength()+1); // width of the space in between rectangles
38           int rect = (width*3/4)/getLength(); // width of each rectangle
39           // draw small rectangles for every beat
40           g.setColor(Color.BLACK);
41           for(int i = 1; i <= getLength(); i++){
42               g.drawRoundRect(clear*i+rect*(i-1), height/3, rect, height/3, 10, 10);
43           }
44           // draw large rectangles for every chord
45           int beginningIndex = 0;
46           Chord currentChord = getChords()[beginningIndex];
47           Chord compareChord;
48           int drawStartingXCoordinate;
49           int drawWidth;
50           for(int i = 0; i < getLength(); i++){
51               if(i < getLength()-1){
52                   compareChord = getChords()[i+1];
53               } else {
54                   compareChord = new Chord("#", false); // create a compareChord that will always be different from the currentChord -> needed in order to include the last beat
55               }
56               if(currentChord != null){
57                   if(!Chord.chordsEqual(currentChord, compareChord)){
58                       if(getCalcMelody().findKeyNoteIndex(Melody.extractActualNoteName(currentChord.getRootNote())) != -1){ // rootNote == keyNote
59                           g.setColor(Main.keyNoteColors[getCalcMelody().findKeyNoteIndex(Melody.extractActualNoteName(currentChord.getRootNote()))]);
60                       } else { // rootNote == extraNote
61                           g.setColor(Color.LIGHT_GRAY);
62                       }
63                       drawStartingXCoordinate = clear*(beginningIndex+1)+rect*(beginningIndex);
64                       drawWidth = clear*(i-beginningIndex)+rect*(i-beginningIndex+1);
65                       g.drawRoundRect(drawStartingXCoordinate, height/2-height/15, drawWidth, height/8, 10, 10);
66                       g.drawString(currentChord.getRootNote(), drawStartingXCoordinate+(drawWidth/2)-(g.getFontMetrics().stringWidth(currentChord.getRootNote())/2),height/2+(g.getFontMetrics().getHeight()/4));
67   
68                       // create black border around rootNote String
69                       // drawTextBorder((Graphics2D)g, currentChord.getRootNote(), Color.BLACK, drawStartingXCoordinate+(drawWidth/2)-(g.getFontMetrics().stringWidth(currentChord.getRootNote())/2), height/2+(g.getFontMetrics().getHeight()/4));
70   
71                       if(i < getLength()-1){
72                           beginningIndex = i+1;
73                           currentChord = getChords()[beginningIndex];
74                       }
75                   }
76               } else {
77                   if(compareChord != null){
78                       if(i < getLength()-1){
79                           beginningIndex = i+1;
80                           currentChord = getChords()[beginningIndex];
81                       }
82                   }
83               }
84           }
85       }
86   
87       private void drawTextBorder(Graphics2D g2d, String text, Color borderColor, int x, int y){
88           g2d.translate(x, y); // TODO: fix this
89           // create glyph vector from text
90           GlyphVector glyphVector = getFont().createGlyphVector(g2d.getFontRenderContext(), text);
91           // get shape object
92           Shape textShape = glyphVector.getOutline();
93           // activate anti aliasing for text rendering (looks nicer)
94           g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
95           g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
96           // draw outline
97           g2d.setColor(borderColor);
98           g2d.setStroke(new BasicStroke(0.5f));
99           g2d.draw(textShape);
100          /* 
101          // fill shape 
102          if(getCalcMelody().findKeyNoteIndex(Melody.extractActualNoteName(text)) != -1){ // rootNote == keyNote 
103              g2d.setColor(Main.keyNoteColors[getCalcMelody().findKeyNoteIndex(Melody.extractActualNoteName(text))]); 
104          } else { 
105              g2d.setColor(Color.LIGHT_GRAY); // rootNote == extraNote 
106          } 
107          g2d.fill(textShape); 
108          */
109      }
110  
111  
112      // Getters & Setters
113  
114      public int getLength(){
115          return this.length;
116      }
117  
118      public void setLength(int length){
119          this.length = length;
120          setChords(new Chord[getLength()]);
121      }
122  
123      public Chord[] getChords(){
124          return this.chords;
125      }
126  
127      public void setChords(Chord[] chords){
128          this.chords = chords;
129      }
130  
131      public boolean getErase(){
132          return this.erase;
133      }
134  
135      public void setErase(boolean erase){
136          this.erase = erase;
137      }
138  
139      public Melody getCalcMelody(){
140          return this.calcMelody;
141      }
142  
143      public void setCalcMelody(Melody calcMelody){
144          this.calcMelody = calcMelody;
145      }
146  }
147