how to update?

I have a canvas frame where when user clicks 1st time I store that point in an array...when user clicks 2nd time I store the 2nd point again so on ...and I call repaint method in the 2nd click mouse clicked().

Now in paint I display the lines...

But till the time I press the mouse for a third click the first line(comprising of first two mouse clicks) doesnot print.

Any 1 please reply

addMouseListener(new MouseAdapter()

{

public void mouseClicked(MouseEvent e)

{

if(rtclick==false)

{

/*if(mp.gate_place==true)

{

gate_place=false;

alen++;

and[alen].x=x;

and[alen].y=y;

repaint();

System.out.println("AND");

repaint();

return;

}*/

if(clicked==false)//1st click

{

linelength++;

line[linelength].sx=e.getX();

line[linelength].sy=e.getY();

clicked=true;//1 click has happened

repaint();

}

else

{//2nd click thus target

line[linelength].tx=e.getX();

line[linelength].ty=e.getY();

myframe m = (myframe)e.getSource();

m.repaint();

e.consume();

//repaint();

clicked=false;//2 click has happened

}

System.out.println("in center clkd"+e.getX()+" "+e.getY());

}

}//end of mouseClicked()

});

public void paint(Graphics g)

{

int i,j;

super.paint(g);

for(j=0;j<linelength;j++)

{

g.drawLine(line[j].sx,line[j].sy,line[j].tx,line[j].ty);

}

}

}//end of class myframe>

[1578 byte] By [catchmea] at [2007-11-26 20:54:20]
# 1

The paint method will draw whatever you tell it to. It does not remember what you asked it to draw on previous trips through its method body. You can save previously created lines in (generally) one of two ways:

1 — make a List/ArrayList and save each newly–created line in it. In the painting method loop through your list and draw each line it contains. So the list preserves the state that you want to draw each time.

2 — create an offscreen image (BufferedImage works well for this) and add your new line to it each time you get a "second click" in your mouse code. The paint method draws this image; that's all it needs to do. The offscreen image saves the state of the lines.

The idea is to have the enclosing class (the one that has the painting method you are drawing with) keep the state and draw it each time it is asked to render itself. Your component must be prepared to draw any or all of its state at any time. Putting things together this way will keep you from getting caught with your pants down by (uncontrollable) System repaint events.

crwooda at 2007-7-10 2:21:11 > top of Java-index,Security,Cryptography...