import java.awt.*;
import java.awt.event.*;
public class Draw3 extends Frame{
Graphics g;
int x,y, ox, oy;
public Draw3(String title){
super(title);
setSize(200,200);
setVisible(true);
g=this.getGraphics();
g.setColor(Color.red);
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
x=e.getX(); y=e.getY();
g.drawLine(ox, oy, x, y);
ox=x; oy=y;\\搞不懂为什么还要这一句?
}
});
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
ox=e.getX(); oy=e.getY();
}
});
}
public static void main(String[] args){
Frame f=new Draw3("绘制图形");
}
}
import java.awt.event.*;
public class Draw3 extends Frame{
Graphics g;
int x,y, ox, oy;
public Draw3(String title){
super(title);
setSize(200,200);
setVisible(true);
g=this.getGraphics();
g.setColor(Color.red);
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
x=e.getX(); y=e.getY();
g.drawLine(ox, oy, x, y);
ox=x; oy=y;\\搞不懂为什么还要这一句?
}
});
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
ox=e.getX(); oy=e.getY();
}
});
}
public static void main(String[] args){
Frame f=new Draw3("绘制图形");
}
}
先假设这是个只有三个像素的屏幕,三个像素分别为A、B、C鼠标点击的初始坐标为ox、oy那么我画一条线(其实就是点亮另一个像素)AB,那么鼠标就是从A到B,然后紧接着再画出BC,这时候你应该只能看到ABC这一条线.鼠标在A点点下时,那么这时这一点的坐标就为ox、oy
如果当你在画完AB后没有将B点坐标赋值给ox、oy,那么在画BC时(AB画好后鼠标按键没有松开),就不是BC了,而是AB(因为初始点ox、oy还在A点)。那么你看到的就不是ABC这一条线,而是AB、AC两条线
不好意思,刚刚打错字母了....不能修改原帖真麻烦
相互学习吧MSN有,但基本没上过...- -!
QQ可以不?