I have never created a GUI or done anything with drawing in Java and am need of help drawing lines efficiently. In my app, the user will be provided with 8 buttons, and the idea is when a button is pressed, a line should be drawn on the window corresponding to the time the button is held down. Currently I am overriding the paintComponent function on a jpanel, and calling the paint() function every 125ms(I only have ~240 pixels wide to draw on, and I would like to keep 30 seconds worth of recording on that 240px), but the hardware it is being run on cannot keep up and it looks terrible/extremely choppy. Here is the code I am using:
jPanel1 = new javax.swing.JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Something cur = manager.getSomething(getCurrentState());
for(int j=0; j<cur.getNumItems(); j++) {
Item i1 = cur.getItem(j);
for(int i = 0; i<i1.getLength(); i++) {
int start = i1.getStartTime(i);
int len = Math.max(0, (Math.min(i1.getStopTime(i), pix) - start));
g.fillRect(start,j*22+5,len,5);
}
}
g.drawLine(pix, 0, pix, 170);
}
};
where: getItem(int) will return the information for a given button. getStartTime(int) will return the start time of a given period of time when the button was pressed down. getStopTime(int) will return the stop time of a given period of time when the button was pressed down. pix = the current pixel we are at on the panel(so, if we are 15 seconds into the 30 seconds of the timeline, pix would equal 120)
Finally I have a timer which calls jpanel1.paint() every 125ms.
Is there a better way to do this, or does anyone have any other suggestions? Please provide detailed info/sources as I do not have any real drawing experience. Thanks in advance!