您的位置:首页 > 编程语言 > Java开发

JAVA画图

2011-10-09 13:36 204 查看
package gui;

import java.awt.*;
import java.awt.event.*;

public class PaintFrame extends Frame {

private static final long serialVersionUID = 1L;

private int _x, _y, x, y;
private boolean bps = false;

public void paint(Graphics g) {
if (bps) {
Color c = g.getColor();
g.setColor(Color.RED);
g.drawLine(_x, _y, x, y);
g.setColor(c);
}
}

public PaintFrame() {
this.setBounds(230, 100, 600, 500);
this.setTitle("画图");
this.setResizable(false);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
_x = e.getX();
_y = e.getY();
}

public void mouseReleased(MouseEvent e) {
bps = false;
}
});

this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent arg0) {
bps = true;

}

public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
PaintFrame.this.paint(PaintFrame.this.getGraphics());
_x = x;
_y = y;
}
});

this.setVisible(true);
}

public static void main(String[] args) {
new PaintFrame();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: