我想的效果是每次更改JFrame窗口的大小都能保留原来画的所有矩形,请问该怎么实现?

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窗口的大小都能保留原来画的所有矩形,而以上代码只能实现保留最后一次的画的矩形!请哪位高手赐教啊,小弟着这里谢谢了!

不负相思意
浏览 97回答 2
2回答

莫回无

super.paint()的作用是把当前的区域清空,每次resize的时候就会自动调用paint()方法,paint()方法里先调用了super.paint()清空当前区域,再画一个矩型筐,当然每次只有一个了。另外你的算法也有漏洞,当你想从右上角拉到左下角画矩形的时候是没有反应的。下面这个程序修改了只画一个的错误,改进了右上角拉到左下角的漏洞,还增加了拖动的中间过程。没时间给你写注释,自己看吧。import java.awt.*;import java.awt.event.*;import java.util.Vector;import javax.swing.*;public class Hello extends JFrame implements MouseListener,MouseMotionListener{int x1,y1;Vector<Rectangle> rectangles=null;public Hello(){this.setTitle("画图小程序");this.addMouseListener(this);this.addMouseMotionListener(this);this.setSize(400,400);rectangles=new Vector<Rectangle>();this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public void paint(Graphics g){super.paint(g);g.setColor(Color.BLUE);for(int i=0;i<rectangles.size();i++){Rectangle rec=rectangles.get(i);int tmp_x=rec.x;int tmp_y=rec.y;int tmp_w=rec.width;int tmp_h=rec.height;g.drawRect(tmp_x,tmp_y,tmp_w,tmp_h);}}public static void main(String[] args) {new Hello();}public void mouseClicked(MouseEvent e) {}public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e) {}public void mousePressed(MouseEvent e) {x1=e.getX();y1=e.getY();}public void mouseReleased(MouseEvent e) {int x2=e.getX();int y2=e.getY();int x=0;int y=0;Graphics g=this.getGraphics();g.setColor(Color.BLUE);int tmp_w=0;int tmp_h=0;if(x1<x2){x=x1;tmp_w=x2-x1;}else if(x1>x2){x=x2;tmp_w=x1-x2;}if(y1<y2){y=y1;tmp_h=y2-y1;}else if(y1>y2){y=y2;tmp_h=y1-y2;}rectangles.add(new Rectangle(x,y,tmp_w,tmp_h));this.repaint();}public void mouseDragged(MouseEvent e) {int x2=e.getX();int y2=e.getY();int x=0;int y=0;Graphics g=this.getGraphics();g.setColor(Color.BLUE);int tmp_w=0;int tmp_h=0;if(x1<x2){x=x1;tmp_w=x2-x1;}else if(x1>x2){x=x2;tmp_w=x1-x2;}if(y1<y2){y=y1;tmp_h=y2-y1;}else if(y1>y2){y=y2;tmp_h=y1-y2;}paint(g);g.drawRect(x,y,tmp_w,tmp_h);}public void mouseMoved(MouseEvent e) {}}

BIG阳

你要建一个矩形类,把每次画的矩形保存在一个List中,每次刷新的时候重画List中所有的矩形
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java