Java小程序之高级画板重绘篇I
前言:我们完成的高级画板在最小化后,然后最大化,画板上绘制的图形全都会消失不见了。原因已在前面的博客中解释,这里不再累赘;今天就让我们一起来解决这个小bug吧!
画板重绘思路:
1、定义直线,椭圆,矩形,圆角矩形等类;
2、在绘制图形的时候生成相应的对象;
3、利用容器将对象进行保存;
4、利用重绘方法,将容器中的对象拿出来,进行重绘;
难点:容器中装了各种各样的对象,如何判断取出来的是什么对象?
利用instanceof关键字解决
重绘时如何把颜色和粗细进行重绘?
在保存对象的时候,把画笔的颜色和、粗细一并保存到对象中
附:工具中的图片以上传到我的资源;需要的可以自行下载;
源代码:
Line类:
package com.huaxin.zhou;
import java.awt.Color;
import java.awt.Stroke;
public class Line {
public int x1, y1, x2, y2;
public Color color;
public Stroke s;
public Line(int x1, int y1, int x2, int y2,Color color,Stroke s){
//保存直线的坐标
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.color=color;//保存直线的颜色
this.s=s;//保存直线的粗细
}
}
Rect类:
package com.huaxin.zhou;
import java.awt.Color;
import java.awt.Stroke;
public class Rect {
public int x1, y1, x2, y2;
public Color color;
public Stroke s;
public Rect(int x1, int y1, int x2, int y2,Color color, Stroke s){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
this.color=color;
this.s=s;
}
}
RonundRect类:
package com.huaxin.zhou;
import java.awt.Color;
import java.awt.Stroke;
public class RoundRect {
public int x1, y1, x2, y2,i,j;
public Color color;
public Stroke s;
public RoundRect(int x1, int y1, int x2, int y2,int i,int j,Color color, Stroke s){
this.x1=x1;
this.y1=y1;
this.x2=x2;
thi