我是java初学者,在使用按钮单击在小程序中绘制线条时遇到问题。请帮助我解决这个问题。ps-我不知道摇摆或其他高级部分。它正在绘制并发线,同样在最大化小程序时,只有最后绘制的线是可见的。也请帮助解决这个问题。单击另一点时,先前绘制的线条会消失。我想制作一个程序,在单击按钮时画线并且它们不应该消失。
//program to draw lines
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code=xy width=200 height=200>
</applet>
*/
public class xy extends Applet implements MouseListener,ActionListener
{ Button b;
int x1, y1,x2,y2;
public void init()
{b=new Button();
add(b);
x1=y1=x2=y2=0;
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b) //button click
{
addMouseListener(this);
}}
public void mouseClicked(MouseEvent me)
{
x1=me.getX();
y1=me.getY();
showStatus( "(" + x1 + "," + y1+ ")" );
}
public void mousePressed(MouseEvent me)
{}
public void mouseReleased(MouseEvent me)
{
x2=me.getX();
y2=me.getY();
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);
}}