import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class huatu extends JFrame implements MouseListener {
int x1,x2,y1,y2;
Graphics x;
public huatu(){
this.setTitle("画图小程序");
this.addMouseListener(this);
this.setSize(400,400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics x){
super.paint(x);
x.setColor(Color.BLUE);
if(x1<x2)
x.drawRect(x1,y1,x2-x1,y2-y1);
if(x1>x2)
x.drawRect(x2,y2,x1-x2,y1-y2);
}
public static void main(String[] args) {
new huatu();
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
// TODO 自动生成方法存根
}
public void mouseExited(MouseEvent e) {
// TODO 自动生成方法存根
}
public void mousePressed(MouseEvent e) {
x1=e.getX();y1=e.getY();
}
public void mouseReleased(MouseEvent e) {
x2=e.getX();y2=e.getY();
x=this.getGraphics();
x.setColor(Color.BLUE);
if(x1<x2)
x.drawRect(x1,y1,x2-x1,y2-y1);
if(x1>x2)
x.drawRect(x2,y2,x1-x2,y1-y2);
}
}
我想的效果是每次更改JFrame窗口的大小都能保留原来画的所有矩形,而以上代码只能实现保留最后一次的画的矩形!请哪位高手赐教啊,小弟着这里谢谢了!
莫回无
BIG阳
相关分类