遇到的小问题,有点意思啊。估计很多初学者都会遇到的,大家都看看.
import java.awt.*;
import java.awt.event.*;
public class DrawPictures extends Frame {
public Graphics g;
public int xp, yp, x, y;
public DrawPictures(String title){
super(title);
setVisible(true);
setSize(500,500);
g=this.getGraphics();
//g.setColor(Color.red);
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
x=e.getX(); y=e.getY();
g.drawLine(xp,yp,x,y);
xp=x; yp=y;
}
});
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
xp=e.getX(); yp=e.getY();
}
});
}
public static void main(String[] args){
new DrawPictures("画布");
}
}
//下面这个程序和上面的一样,只是将那两个监听程序换了。
import java.awt.*;
import java.awt.event.*;
public class DrawPictures extends Frame {
public Graphics g;
public int xp, yp, x, y;
public DrawPictures(String title){
super(title);
setVisible(true);
setSize(500,500);
g=this.getGraphics();
//g.setColor(Color.red);
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
xp=e.getX(); yp=e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
x=e.getX(); y=e.getY();
g.drawLine(xp,yp,x,y);
xp=x; yp=y;
}
});
}
public static void main(String[] args){
new DrawPictures("画布");
}
}
//我觉得应该是现有pessed 再有drag啊?但是?
//帮个忙啊