图形的重绘

窗体的重绘
之前已经学习了窗体的的一些组件以及窗体的一些特有的性质,画图板等等一些窗体小工具。但是在学习画图板的时候当我们在画图板上画上一些图案当我们将窗口进行缩放,最大化最小化的时候,再看我们的额画图板会发现我们之前画的一些图案全部都消失了。这是为什么呢?原来当我们将窗体进行缩放、最大化最小化的时候窗体都进行了重绘操作,也就是窗体下的paint()函数。我们可以这样理解,当我们进行将画图板拖拽的时候,显示屏会将原来地方的窗体擦去,在新的地方重新绘制一个我们想要的画图板。对于放大缩小等操作都是一样的。
这样问题就来了,当我们进行这些操作的时候原来的画图板被擦去,我们在画图板上面所绘制的图像也理所当然的被一起擦去了,在绘制我们想要的新的画图板的时候paint()函数不会将我们的图像也一起绘制。这样一来我们之前画的东西就全部消失了。
当然问题还要从根源下手,既然全部都是paint函数惹的祸那我们就在新的窗体对象中重写这个paint函数,让它可以在重绘的时候将我们之前绘制的图形也一同绘制出来。既然需要我们将我们之前绘制的图像重新画出来,就必须先将我们之前所绘制的图像保存起来,在重绘的时候将我们之前保存的图像在paint函数中重新调用画图函数将我们的图像重新在相同的位置画出来。这样就出现了两个问题:1.将我们之前绘制的图像保存起来 2.在重绘函数中重新画出图形。
1.将之前画的图形保存起来:
我们知道我们所画的图形并不是一个而是多个,当我们需要保存很多东西的时候一般用到了什么?数组?队列?这可能还要取决于我们想要保存的东西的类型。我们这次想要保存的就是图像,其中就包括了图像的类型(直线、圆、矩形、还是其它的图像类型)、图像的位置(x坐标,y坐标,还是否需要长宽等参数)、图像的颜色等等。当我们需要封装这些数据的时候就不难想到类了。但是我们有很多个这样的对象,那么我们就可以用到图像的队列来进行存储。
private ArrayList list = new ArrayList();
这里的shape就是我们我们所定义的图像的类,专门来存储我们所画的图像。但是我们会有直线、圆等等一系列不同的图形,这样就要对这个类更加细化,这样我们就不难联想到类的继承。我们定义一系列的类来继承这个shape类,然后在这些子类中进行图形参数的存储。
package com.lsh1002;

import java.awt.Color;
import java.awt.Graphics;

public abstract class Shape {

private int x1,x2,y1,y2;
private Color color;

public Shape(int x1, int y1, int x2, int y2, Color color) {
	this.x1 = x1;
	this.y1 = y1;
	this.x2 = x2;
	this.y2 = y2;
	this.color = color;
}


public abstract void draw(Graphics g);

public int getx1(){
	return x1;
}
public int getx2(){
	return x2;
}
public int gety1(){
	return y1;
}
public int gety2(){
	return y2;
}
public Color getColor(){
	return color;
}
public void setx1(int n){
	this.x1=n;
}
public void sety1(int n){
	this.y1=n;
}
public void setx2(int n){
	this.x2=n;
}
public void sety2(int n){
	this.y2=n;
}
public void setColor(Color n){
	this.color=n;
}

}

直线类:
package com.lsh1002;

import java.awt.Color;
import java.awt.Graphics;

public class ShapeLine extends Shape {

public ShapeLine(int x1, int y1, int x2, int y2, Color color) {
	super(x1, y1, x2, y2, color);
}



public void draw(Graphics g)
{
	g.setColor(getColor());
	g.drawLine(getx1(), gety1(), getx2(), gety2());
}

}
圆类:
package com.lsh1002;

import java.awt.Color;
import java.awt.Graphics;

public class ShapeOval extends Shape{

public ShapeOval(int x1, int y1, int x2, int y2, Color color) {
	super(x1, y1, x2, y2, color);
	
}

public void draw(Graphics g) {
	g.setColor(getColor());
	int x = Math.min(getx1(), getx2());
	int y = Math.min(gety1(), gety2());
	int width = Math.abs(getx1() - getx2());
	int height = Math.abs(gety1() - gety2());
	g.fillOval(x, y, width, height);
}

}
矩形类:
package com.lsh1002;

import java.awt.Color;
import java.awt.Graphics;

public class ShapeRect extends Shape{

public ShapeRect(int x1, int y1, int x2, int y2, Color color) {
	super(x1, y1, x2, y2, color);

}

public void draw(Graphics g) {
	g.setColor(getColor());
	int x = Math.min(getx1(), getx2());
	int y = Math.min(gety1(), gety2());
	int width = Math.abs(getx1() - getx2());
	int height = Math.abs(gety1() - gety2());
	g.drawRect(x, y, width, height);
}

}
这样我们在画图形的时候顺便在主函数中加上把画的图形加入这个队列进行存储。
public void mouseReleased(MouseEvent e){

	System.out.println("释放");
	x2 = e.getX();
	y2 = e.getY();
	    	
	if (type.equals("直线")) {
		s = new ShapeLine(x1, y1, x2, y2, color);
	} else if (type.equals("矩形")) {
		s = new ShapeRect(x1, y1, x2, y2, color);
	} else if (type.equals("圆")) {
		s = new ShapeOval(x1, y1, x2, y2, color);
	}
	if (s != null) {
		s.draw(g);
		list.add(s);
	}

2.进行重绘
进行重绘的时候我们就需要将paint函数进行重写,将我们存入的list队列中的所有图形全部都重新绘制一遍。我们在每个图形的类中都进行了一个函数的书写就是按照存入的信息画出这个图像。那么我们在重绘函数中直接调用这个函数就行了。
public void paint(Graphics g){
super.paint(g);

	for(int i=0;i < list.size() ; i++){
		Shape s =list.get(i);
		s.draw(g);
	}
}

这样之后我们的画图板放大或缩小之后我们所画的图像就不会消失啦。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值