面板类:
为什么要继承JFrame类?
答:考虑到重绘图形时需要在JFrame对象上重绘出数组中的图形,所以我们让面板类继承JFrame类成为它的一个子类,以便之后重写paint()方法。
package drawingboard;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
/*
* 画板类
*/
public class Drawing extends JFrame {
private String str[] = new String[] { "直线", "多边形", "图片", "文字" };
private Color c[] = new Color[] { Color.black, Color.green, Color.yellow, Color.blue };
private Graphics g;
private ArrayList<Shape> listshape=new ArrayList();
/*
* 初始化界面函数
*/
public void initUI() {
this.setTitle("画板"); //标题
this.setSize(700, 600); //大小
this.setLocationRelativeTo(null); //居中
this.setDefaultCloseOperation(3); //关闭
this.setLayout(new FlowLayout()); //流式布局
DrawingListener dl = new DrawingListener(); //实例化接口对象
this.addMouseListener(dl); //给面板对象加监听方法addMouseListener,指定监听接口对象为dl
for (int i = 0; i < str.length; i++) { //循环添加按钮
JButton jb = new JButton(str[i]);
this.add(jb);
jb.addActionListener(dl);
}
for (int i = 0; i < c.length; i++) {
JButton jb1 = new JButton();
jb1.setBackground(c[i]);
jb1.setPreferredSize(new Dimension(30, 30));
this.add(jb1);
jb1.addActionListener(dl);
}
this.setVisible(true); //可见
g = this.getGraphics(); //取画笔,注意只能在窗体可见之后
dl.setg(g); //传画笔
dl.setlist(listshape); //传数组
}
public void paint(Graphics g) {
super.paint(g); //调用父类paint方法
for (int i = 0; i < listshape.size(); i++) { //取出listshape数组中的元素进行重绘
Shape shape = listshape.get(i);
if (shape != null) {
shape.drawshape(g);
} else {
break;
}
}
}
/*
* 主函数
*/
public static void main(String[] args) {
Drawing dr = new Drawing(); //实例化窗体类对象dr
dr.initUI(); //调用dr对象的初始化窗体的方法
}
}
监听接口类:
为什么用MouseAdapter类而不用MouseListener接口?
答:使用MouseAdapter类不需要重写其中所有的方法,而使用接口要重写其中所有的抽象方法。
package drawingboard;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
/*
* 监听类
*/
public class DrawingListener extends MouseAdapter implements ActionListener {
private int x1, y1, x2, y2, x3, y3; // 用于存储坐标值,注意窗体界面y往下为大,往上为小
private String xingzhuang;
private JFrame jf;
private Graphics g;
private Color color;
private boolean flag = true; // 标志位,注意方法执行顺序,按下-释放-点击
private ArrayList<Shape> listshape;
/*
* 监听类构造函数,传递参数的作用
*/
public void setg(Graphics g) { //传画笔
this.g = g;
}
public void setlist(ArrayList<Shape> listshape) {//传数组
this.listshape = listshape;
}
@Override
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if (s.equals("")) { //颜色按钮
JButton jb = (JButton) e.getSource();
color = jb.getBackground();
g.setColor(color); //给画笔上色
} else {
xingzhuang = s; //形状按钮
}
}
@Override
public void mouseClicked(MouseEvent e) {
x3 = e.getX();
y3 = e.getY();
if (xingzhuang.equals("多边形")) {
g.drawLine(x2, y2, x3, y3);
x2 = x3;
y2 = y3;
if (e.getClickCount() == 2) {
g.drawLine(x1, y1, x3, y3);
}
} else if (xingzhuang.equals("文字")) {
g.setFont(new Font("宋体", Font.BOLD, 50));
String ss = "熊哥!";
g.drawString(ss, x3, y3);
Shape shape = new Shape(); //实例化Shape类的对象
shape.setShape(x1, y1, x2, y2, x3, y3, xingzhuang, color);//给Shape类的对象传参
listshape.add(shape); //把对象添加到数组当中
}
}
@Override
public void mousePressed(MouseEvent e) {
if (flag) {
x1 = e.getX();
y1 = e.getY();
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (flag) {
x2 = e.getX();
y2 = e.getY();
}
if (xingzhuang.equals("直线") && flag) {
g.drawLine(x1, y1, x2, y2);
Shape shape = new Shape();
shape.setShape(x1, y1, x2, y2, x3, y3, xingzhuang, color);
listshape.add(shape);
} else if (xingzhuang.equals("多边形") && flag) {
g.drawLine(x1, y1, x2, y2);
flag = !flag;
} else if (xingzhuang.equals("图片")) {
ImageIcon t = new ImageIcon("C:\\Users\\lcz\\Desktop\\***\\IMG_0456.JPG");
g.drawImage(t.getImage(), Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2), null);
Shape shape = new Shape();
shape.setShape(x1, y1, x2, y2, x3, y3, xingzhuang, color);
listshape.add(shape);
}
}
}
形状类:
为什么我们要定义一个形状类?
答:方便我们把形状对象添加到ArrayList数组当中,而且进行实例化的时候也非常的方便。
package drawingboard;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.ImageIcon;
public class Shape {
private int x1, x2, y1, y2, x3, y3;
private Color color;
private String xingzhuang;
private ImageIcon t = new ImageIcon("C:\\Users\\lcz\\Desktop\\***\\IMG_0456.JPG");
public void setShape(int x1, int y1, int x2, int y2, int x3, int y3, String xingzhuang, Color color) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
this.x3 = x3;
this.y3 = y3;
this.xingzhuang = xingzhuang;
this.color = color;
}
public void drawshape(Graphics g) {
switch (xingzhuang) {
case "直线":
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
break;
case "图片":
g.setColor(color);
g.drawImage(t.getImage(), Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2), null);
case "文字":
g.setColor(color);
g.setFont(new Font("宋体", Font.BOLD, 50));
String ss = "熊哥!";
g.drawString(ss, x3, y3);
}
}
}
多边形的重绘方法我没有写呀,感兴趣的小伙伴可以自己去考虑。