Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Java
public class xy extends Applet implements MouseListener,ActionListener
{	Button b;
	int x1, y1,x2,y2;
	public void init()
	{	b=new Button("Draw Line");	
		add(b);		
		x1=y1=x2=y2=0;
		b.addActionListener(this);
	}
	public void actionPerformed(ActionEvent ae)
{
	if(ae.getSource()==b){
	addMouseListener(this);}
}
	public void mouseClicked(MouseEvent me)
	{
		showStatus( "(" + x1 + "," + y1+ ")" );	
	}
	public void mousePressed(MouseEvent me)
	{
	x1=me.getX();
	y1=me.getY();
	}
	public void mouseReleased(MouseEvent me)
	{
	x2=me.getX();
	y2=me.getY();
	Graphics g = getGraphics();
	me.consume();	
	repaint();
	
	}
	public void mouseExited(MouseEvent me)
	{}
	public void mouseEntered(MouseEvent me)
	{}
	public void paint(Graphics g)
	{
		update(g);
	}
	public void update(Graphics g)
	{
		g.drawLine( x1,y1,x2,y2);
	}

}
Posted

1 solution

When you minimize and maximize your applet (in applet viewer or webbrowser), a fresh call to update is made after clearing the entire screen. So in update, you only paint the last line... therefore all your lines disappear. Think of some alternate mechanism such as storing all lines in a stack and repainting them everytime update is called.

You can also use swing components other that awt ones, but remember to override the appropriate method.

Good Luck
 
Share this answer
 
v2
Comments
AG007 2-Jun-13 11:55am    
thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900