上次我们已经写了一个画图板了,细心的小伙伴可能已经发现上一个画图板有一个bug了,每当我们改变我们的窗口的大小,以前画的图形就不见了,也就是说它不能将我们已经画的图形保存下来。今天,我们就来解决这一个问题。
用什么来存储
那么现在又有一个问题产生了,我们要怎样将图形信息保存下来呢?其实数组、数组队列、链表等数据结构都可以用来保存这些图形的数据的,今天我们就用数组来保存,相信这也是大多数人最熟悉的一种数据结构了。
存储哪些信息
既然要保存图形的信息,那我们需要保存哪些信息呢?不同的图形所含有的信息是不一样的,比如:直线只有两个端点的坐标,圆除了有x,y坐标还有高度和宽度…那么我们应该如何区分这些图形呢?其实我们只需要在画图的时候记录下图形的类型(是一个圆、直线、还是其他的等等),然后在重绘时判断一下图形的类型,然后提取出与当前图形有关的信息就可以了。
重绘
前面我们已经将准备工作做好了,那么我们究竟该如何实现重绘呢?这就需要我们重写JFrame中的paint() 方法了。在paint() 方法中将我们保存的图形重新画出来。
代码实现
界面类
(用于显示我们所画的图形)
package com0715;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
public class UI {
public void showUI(){
//注意: 此处是用新类(继承了JFrame的类)创建的对象 而不是直接用JFrame创建的对象,因为需要重写父类的 paint 方法
NewFrame Login = new NewFrame();
Login.setTitle("画图板2.0");
Login.setSize(1000, 600);
Login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Login.setLocationRelativeTo(null);
//设置流式布局
Login.setLayout(new FlowLayout());
//创建监听器对象
UiListener UL = new UiListener();
//添加监听器
Login.addMouseListener(UL);
//设置按钮数组
String[] btn = {"直线","圆"};
//创建按钮并添加到容器中(可创建多个按钮)
for(int i=0;i<btn.length;i++){
JButton button = new JButton(btn[i]);
Login.add(button);
//在按钮上添加动作事件
button.addActionListener(UL);
}
//显示可见
Login.setVisible(true);
//获取画布
Graphics g =Login.getGraphics();
UL.setGraphics(g);
//将监听器中的数组传给 NewFrame 中
Login.setShape(UL.shapes);
}
public static void main(String[] args){
UI ui = new UI();
ui.showUI();
}
}
NewFrame类
(继承了JFrame类,并且重写了paint() 方法)
package com0715;
import java.awt.Graphics;
import javax.swing.JFrame;
public class NewFrame extends JFrame{
Shape[] shapes;
public void setShape(Shape[] shapes){
this.shapes = shapes;
}
public void paint(Graphics g){
//继承父类,重画组件
super.paint(g);
//重画自己画的图形
for(int i=0;i<shapes.length;i++){
if(shapes[i]!=null){
if("直线".equals(shapes[i].type)){
shapes[i].draw(g);
}else if("圆".equals(shapes[i].type)){
shapes[i].draw(g);
}
}
}
}
}
图形类
(用于保存图形的信息,并提供绘图方法)
package com0715;
import java.awt.Color;
import java.awt.Graphics;
public class Shape {
int x1,y1,x2,y2;
int height,width;
String type;
Color color;
//初始化
public Shape(int x1,int y1,int x2,int y2,int width,int height,Color color,String type){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.height = height;
this.width = width;
this.color = color;
this.type = type;
}
public void draw(Graphics g){
if(type.equals("直线")){
g.drawLine(x1, y1, x2, y2);
}else if(type.equals("圆")){
g.drawOval(x1, y1, width, height);
}
}
}
监听器类
(用于实现画图的操作)
package com0715;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class UiListener implements MouseListener,ActionListener{
//点坐标
int x1,y1,x2,y2;
int height,width;
//定义画布
Graphics g;
//记录图形信息
String ShapeType = "";
//定义数组,储存图形数据
Shape[] shapes = new Shape[1000];
//记录画的个数
int index=0;
//传画布
public void setGraphics(Graphics g){
this.g = g;
}
public void actionPerformed(ActionEvent e){
String action = e.getActionCommand();
ShapeType = action;
//System.out.println(action);
}
public void mousePressed(MouseEvent e){
x1 = e.getX();
y1 = e.getY();
}
public void mouseReleased(MouseEvent e){
x2 = e.getX();
y2 = e.getY();
if(ShapeType.equals("直线")){
g.drawLine(x1, y1, x2, y2);
}else if(ShapeType.equals("圆")){
g.drawOval(x1, y1,x2-x1, y2-y1);
this.width = x2-x1;
this.height = y2-y1;
}
Shape shape = new Shape(x1,y1,x2,y2,width,height,Color.RED,ShapeType);
shapes[index++] = shape;
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
下面就是画图板2.0的效果啦
改变窗口大小之前:
改变窗口大小之后:
可承接各种项目,有意者加QQ:1217898975