0

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!

user1725940
  • 93
  • 4
  • 10
  • 1
    where is the code you've tried? – vishal_aim Jan 02 '13 at 05:50
  • Can you please refer this tutorial??? I think this is more than enough if you read this much :) http://beginwithjava.blogspot.in/2008/07/in-most-basic-graphics-app-we-had.html – Azzy Jan 02 '13 at 05:51
  • "Currently I am overriding the paintComponent function on a jpanel, and calling that function every 125ms" You should not call `paintComponent` method manually – vishal_aim Jan 02 '13 at 05:51
  • @vishal_aim I have added the code I am using. Let me know if it doesn't make sense. Also, I was mistaken, I am calling the paint function, which I understand calls the paintComponent function. – user1725940 Jan 02 '13 at 06:34
  • even you should not call paint, check my answer – vishal_aim Jan 02 '13 at 06:52

2 Answers2

0

override the paintComponent function of your JPanel

public void paintComponent(Graphics g) {
        super.paintComponent(g)

        g.drawLine(this.x1, this.y1, this.x2, this.y2);
    }
Vahid Haratian
  • 708
  • 2
  • 6
  • 23
0

you should not call paint or paintComponent method manually.

For better results you can try invalidate() and after that repaint() method on your panel after every 125ms

you may also be interested in repaint and invalidate

Community
  • 1
  • 1
vishal_aim
  • 7,636
  • 1
  • 20
  • 23