画图板的重绘就是将我们在画布上所画的图形,在窗体发生变化后依然存在,这就要求我们将画过的图形保存下来,这个可以通过创建一个形状类,这个形状类是所有的形状的基类,也就是说具体的形状类,例如:直线类,矩形类。我们可以通过继承形状类来创建,在创建一个形状类的队列,这样我们就可以直接保存所画的图形,接下来就是重写paint方法,paint方法就是重绘的方法,但是我们要注意我们的画布Graphics对象是加在那个容器中的,如果是JFrame,就重写JFrame中的paint方法,如果是JPanel,就重写JPanel中的paint方法。
源代码:
界面代码:
package com.paint;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GUI extends JFrame {
//属性
private Graphics g;
private ShapeList shapes=new ShapeList();
//构造方法
public GUI(){
this.setTitle("画图板");
this.setBounds(200,200,500,400);
this.setDefaultCloseOperation(3);
this.setLayout(new BorderLayout());
//按钮面板
JPanel jp_north=new JPanel();
jp_north.setPreferredSize(new Dimension(500,100));
//创建按钮面板的按钮并加监听器
//形状按钮
JButton jb_line=new JButton("LINE");
JButton jb_rect=new JButton("RECT");
JButton jb_oval=new JButton("OVAL");
JButton jb_red=new JButton("RED");
JButton jb_blue=new JButton("BLUE");
JButton jb_green=new JButton("GREEN");
JButton jb_colorchose=new JButton("CHOSE COLOR");
Mylistener m1=new Mylistener();
jb_line.addActionListener(m1);
jb_rect.addActionListener(m1);
jb_oval.addActionListener(m1);
//颜色按钮
Colorlistener color=new Colorlistener();
jb_red.addActionListener(color);
jb_blue.addActionListener(color);
jb_green.addActionListener(color);
jb_colorchose.addActionListener(color);
//添加
jp_north.add(jb_line);
jp_north.add(jb_rect);
jp_north.add(jb_oval);
jp_north.add(jb_red);
jp_north.add(jb_blue);
jp_north.add(jb_green);
jp_north.add(jb_colorchose);
//画布面板
MyJPanel jp_south=new MyJPanel(shapes);
jp_south.setPreferredSize(new Dimension(500,300));
jp_north.setBackground(Color.GREEN);
//添加面板
this.add(jp_north,BorderLayout.NORTH);
this.add(jp_south,BorderLayout.SOUTH);
this.setVisible(true);
g=jp_south.getGraphics();
Mymouselistener m=new Mymouselistener(g, m1,color,shapes);
jp_south.addMouseListener(m);
jp_south.addMouseMotionListener(m);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new GUI();
}
}
按钮的监听器代码:
package com.paint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Mylistener implements ActionListener {
private String type="LINE";
public String gettype(){
String s=type;
return s;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
type=e.getActionCommand();
}
}
package com.paint;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JColorChooser;
import javax.swing.plaf.synth.ColorType;
public class Colorlistener implements ActionListener {
private Color color=Color.black;
public Color getcolor(){
return color;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String colortype=e.getActionCommand();
if(colortype.equals("RED")){
color=Color.red;
}else if(colortype.equals("GREEN")){
color=Color.green;
}else if(colortype.equals("BLUE")){
color=Color.blue;
}else if(colortype.equals("CHOSE COLOR")){
color=JColorChooser.showDialog(null, "请选择颜色", Color.black);
}
}
}
鼠标监听器:
package com.paint;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Mymouselistener extends MouseAdapter {
private Graphics g;
private Mylistener m1;
private Colorlistener color;
private int x1,x2,x3,y1,y2,y3;
private int tempx,tempy;
private ShapeList shapes;
public Mymouselistener(Graphics g,Mylistener m1,Colorlistener color,ShapeList shapes){
this.shapes=shapes;
this.g=g;
this.m1=m1;
this.color=color;
}
public void mousePressed(MouseEvent e) {
x1=e.getX();
y1=e.getY();
tempx=x1;
tempy=y1;
}
public void mouseReleased(MouseEvent e) {
String command=m1.gettype();
Color color1=color.getcolor();
x2=e.getX();
y2=e.getY();
g.setColor(color1);
NetJavaShape shape=new ImpLine(x1, y1, x2, y2, color1);
if(command.equals("LINE")){
shape=new ImpLine(x1, y1, x2, y2, color1);
}else if(command.equals("RECT")){
shape=new ImpRect(x1, y1, x2, y2, color1);
}else if(command.equals("OVAL")){
shape=new ImpOval(x1, y1, x2, y2, color1);
}
shape.draw(g);
shapes.add(shape);
}
public void mouseDragged(MouseEvent e){
x3=e.getX();
y3=e.getY();
String command=m1.gettype();
Color color1=color.getcolor();
if(command.equals("LINE")){
g.setColor(Color.white);
g.drawLine(x1, y1, tempx, tempy);
g.setColor(color1);
g.drawLine(x1, y1, x3, y3);
tempx=x3;
tempy=y3;
}else if(command.equals("RECT")){
g.setColor(Color.white);
g.drawRect(Math.min(x1, tempx), Math.min(y1, tempy),Math.abs(x1-tempx),Math.abs(y1-tempy));
g.setColor(color1);
g.drawRect(Math.min(x1, x3), Math.min(y1, y3),Math.abs(x1-x3),Math.abs(y1-y3));
tempx=x3;
tempy=y3;
}else if(command.equals("OVAL")){
g.setColor(Color.white);
g.drawOval(Math.min(x1, tempx), Math.min(y1, tempy),Math.abs(x1-tempx),Math.abs(y1-tempy));
g.setColor(color1);
g.drawOval(Math.min(x1, x3), Math.min(y1, y3),Math.abs(x1-x3),Math.abs(y1-y3));
tempx=x3;
tempy=y3;
}
}
}
package com.paint;
import java.awt.Color;
import java.awt.Graphics;
public class NetJavaShape {
private Color color;
private int x1,x2,y1,y2;
//方法
public void draw(Graphics g){
}
}
package com.paint;
import java.awt.Color;
import java.awt.Graphics;
public class ImpLine extends NetJavaShape{
private int x1,x2,y1,y2;
private Color color;
//构造方法
public ImpLine(int x1,int y1,int x2,int y2,Color color){
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
this.color=color;
}
//方法
public void draw(Graphics g){
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}
}
package com.paint;
import java.awt.Color;
import java.awt.Graphics;
public class ImpOval extends NetJavaShape {
private int x1,x2,y1,y2;
private Color color;
//构造方法
public ImpOval(int x1,int y1,int x2,int y2,Color color){
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
this.color=color;
}
//方法
public void draw(Graphics g){
g.setColor(color);
g.drawOval(Math.min(x1, x2), Math.min(y1, y2),Math.abs(x1-x2),Math.abs(y1-y2));
}
}
import java.awt.Color;
import java.awt.Graphics;
public class ImpRect extends NetJavaShape {
private int x1,x2,y1,y2;
private Color color;
public ImpRect(int x1,int y1,int x2,int y2,Color color){
this.color=color;
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
}
public void draw(Graphics g){
g.setColor(color);
g.drawRect(Math.min(x2, x1), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
}
}
形状类的队列:
package com.paint;
public class ShapeList {
NetJavaShape[] src=new NetJavaShape[0];
//添加
public void add(NetJavaShape s){
NetJavaShape[] agr=new NetJavaShape[src.length+1];
agr[src.length]=s;
for(int i=0;i<src.length;i++){
agr[i]=src[i];
}
src=agr;
}
public NetJavaShape get(int index){
NetJavaShape k=src[index];
return k;
}
public int getsize(){
int n=src.length;
return n;
}
}
重绘paint:
package com.paint;
import java.awt.Graphics;
import javax.swing.JPanel;
public class MyJPanel extends JPanel {
private ShapeList shapes;
public MyJPanel(ShapeList shapes){
this.shapes=shapes;
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
for(int i=0;i<shapes.getsize();i++){
NetJavaShape shape=shapes.get(i);
shape.draw(g);
}
}
}
形状类以及它的几个具体行政的子类: