RhythmPanel.java
1    import javax.swing.*;
2    import java.awt.*;
3    
4    public class RhythmPanel extends JPanel{
5    
6        private int length;
7        public boolean[] rhythm;
8        private boolean erase;
9    
10       public RhythmPanel(){
11          setLength(16);
12          setErase(false);
13       }
14   
15   
16       // Actual methods
17   
18       @Override
19       public Dimension getPreferredSize(){
20           return new Dimension(Main.OTHER_FRAME_WIDTH, Main.OTHER_FRAME_HEIGHT);
21       }
22   
23       public void paintComponent(Graphics g){
24           super.paintComponent(g);
25           if(!getErase()){
26               drawRectangles(g, this.getWidth(), this.getHeight());
27           } else {
28               g.setColor(new Color(0,0,0,0));
29               g.fillRect(0,0, this.getWidth(), this.getHeight());
30           }
31       }
32   
33       private void drawRectangles(Graphics g, int width, int height){
34           int clear = (width/4)/(getLength()+1); // width of the space in between rectangles
35           int rect = (width*3/4)/getLength(); // width of each rectangle
36           g.setColor(Color.BLACK);
37           for(int i = 1; i <= getLength(); i++){
38               g.drawRoundRect(clear*i+rect*(i-1), height/3, rect, height/3, 10, 10);
39           }
40           int xPos;
41           for(int i = 1; i <= getLength(); i++){
42               xPos = clear*i+rect*(i-1);
43               if(getRhythm()[i-1]){
44                   g.setColor(Color.CYAN);
45                   g.fillRoundRect(xPos+1, height/3+1, rect-1, height/3-1, 10, 10);
46               } else {
47                   g.setColor(new Color(0,0,0,0));
48                   g.fillRoundRect(xPos+1, height/3+1, rect-1, height/3-1, 10, 10);
49                   g.setColor(Color.BLACK);
50                   g.drawRoundRect(xPos, height/3, rect, height/3, 10, 10);
51               }
52           }
53       }
54   
55   
56       // Getters & Setters
57   
58       public int getLength(){
59           return this.length;
60       }
61   
62       public void setLength(int length){
63           this.length = length;
64           setRhythm(new boolean[getLength()]);
65       }
66   
67       public boolean[] getRhythm(){
68           return this.rhythm;
69       }
70   
71       public void setRhythm(boolean[] rhythm){
72           this.rhythm = rhythm;
73       }
74   
75       public boolean getErase(){
76           return this.erase;
77       }
78   
79       public void setErase(boolean erase){
80           this.erase = erase;
81       }
82   }
83