java学习之Swing篇(三)——简单画板的实现v1.0
程序员文章站
2024-01-31 13:11:52
...
画板功能实现:
1、直线、矩形、椭圆、多边形、曲线等基本图形的绘制;
2、多种颜色可选;
3、橡皮檫采用加粗画笔,另添加一键清屏功能;
4、重绘功能;
5、多边形采用两种方式自动闭合:一种鼠标双击闭合,一种为切换其它图形自动闭合;
6、采用边界布局。
图形效果:
具体代码如下:
主界面程序
package paint0701;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PainterUI extends JPanel {
private static final long serialVersionUID = 1L;
private Shape[] shapeParameter = new Shape[20000];
public void initUI() {
// 新建窗体并命名
JFrame jf = new JFrame("画板");
// 设置窗体大小
jf.setSize(1100, 700);
// 窗体设置居中
jf.setLocationRelativeTo(null);
// 设置窗体关闭
jf.setDefaultCloseOperation(3);
// 设置窗体边界布局
jf.setLayout(new BorderLayout());
// 添加3个JPanel容器
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
// 将JPanel布局到窗体中
jf.add(this, BorderLayout.CENTER);
jf.add(jp2, BorderLayout.WEST);
jf.add(jp3, BorderLayout.EAST);
// 设置jp1
this.setPreferredSize(new Dimension(900, 700));
this.setBackground(Color.white);
// 创建事件监听器对象
DrawListener dl = new DrawListener();
// 给画布添加监听器
this.addMouseListener(dl);
this.addMouseMotionListener(dl);
// 设置jp2
jp2.setPreferredSize(new Dimension(100, 700));
jp2.setBackground(Color.LIGHT_GRAY);
// 设置jp3
jp3.setPreferredSize(new Dimension(100, 700));
jp3.setBackground(Color.LIGHT_GRAY);
// 添加图形按钮
String[] shapeArray = { "直线", "矩形", "椭圆", "多边形", "画笔", "橡皮檫", "清屏" };
for (int i = 0; i < shapeArray.length; i++) {
// 创建图形按钮
JButton jbu1 = new JButton(shapeArray[i]);
// 设置按钮大小
jbu1.setPreferredSize(new Dimension(100, 40));
// 将按钮添加到jp2容器中
jp2.add(jbu1);
// 给按钮注册监听器
jbu1.addActionListener(dl);
}
// 设置颜色按钮
Color[] colorArray = { Color.red, Color.pink, Color.orange, Color.yellow, Color.green, Color.blue, Color.cyan,
Color.black, Color.gray, Color.white };
for (int i = 0; i < colorArray.length; i++) {
JButton jbu2 = new JButton();
jbu2.setBackground(colorArray[i]);
jbu2.setPreferredSize(new Dimension(100, 40));
jp3.add(jbu2);
jbu2.addActionListener(dl);
}
// 设置窗体可见
jf.setVisible(true);
// 获取画笔
Graphics g = this.getGraphics();
// 将画笔传递过去
dl.setGr(g);
// 将图形数组传递过去
dl.setSp(shapeParameter);
}
// 重写父类方法
public void paint(Graphics g) {
super.paint(g);
//遍历图形数组,重绘图形
for (int i = 0; i < shapeParameter.length; i++) {
Shape shape = shapeParameter[i];
if (shapeParameter[i] != null) {
shape.drawShape(g);
}
}
}
//主函数
public static void main(String[] args) {
PainterUI pui = new PainterUI();
pui.initUI();
}
}
监听器
package paint0701;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
public class DrawListener implements MouseListener, MouseMotionListener, ActionListener {
private int x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;
private Boolean flag1 = true, flag2 = false;
private String name;
private Color color;
private Graphics g;
private Shape[] shapeArray;
private int index = 0;
// 初始化画笔
public void setGr(Graphics g) {
this.g = g;
}
// 初始化图形数组
public void setSp(Shape[] shapeArray) {
this.shapeArray = shapeArray;
}
//鼠标点击
public void mouseClicked(java.awt.event.MouseEvent e) {
flag2 = true;
if (!flag1) {
x5 = e.getX();
y5 = e.getY();
g.drawLine(x4, y4, x5, y5);
//将直线存入数组
Shape line = new Line(x4, y4, x5, y5, name, color);
shapeArray[index++] = line;
x4 = x5;
y4 = y5;
}
//双击自动完成多边形闭合
if (e.getClickCount() == 2) {
g.drawLine(x5, y5, x3, y3);
Shape line = new Line(x5, y5, x3, y3, name, color);
shapeArray[index++] = line;
flag1 = true;
}
}
//鼠标按下
public void mousePressed(java.awt.event.MouseEvent e) {
{
x1 = e.getX();
y1 = e.getY();
}
}
//鼠标释放
public void mouseReleased(java.awt.event.MouseEvent e) {
{
x2 = e.getX();
y2 = e.getY();
// 绘制直线
if ("直线".equals(name)) {
g.drawLine(x1, y1, x2, y2);
Shape line = new Line(x1, y1, x2, y2, name, color);
shapeArray[index++] = line;
}
if ("矩形".equals(name)) {
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1));
Shape rect = new Rect(x1, y1, x2, y2, name, color);
shapeArray[index++] = rect;
}
if ("椭圆".equals(name)) {
g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1));
Shape oval = new Oval(x1, y1, x2, y2, name, color);
shapeArray[index++] = oval;
}
if ("多边形".equals(name) && flag1) {
g.drawLine(x1, y1, x2, y2);
Shape line = new Line(x1, y1, x2, y2, name, color);
shapeArray[index++] = line;
x3 = x1;
y3 = y1;
x4 = x2;
y4 = y2;
flag1 = false;
}
}
}
public void mouseEntered(java.awt.event.MouseEvent e) {
}
public void mouseExited(java.awt.event.MouseEvent e) {
}
//鼠标拖动
public void mouseDragged(java.awt.event.MouseEvent e) {
// 画笔重载需注意内存
if ("画笔".equals(name)) {
x2 = e.getX();
y2 = e.getY();
g.drawLine(x1, y1, x2, y2);
Shape line = new Line(x1, y1, x2, y2, name, color);
shapeArray[index++] = line;
x1 = x2;
y1 = y2;
}
if ("橡皮檫".equals(name)) {
color = Color.white;
g.setColor(color);
//设置线宽
((Graphics2D) g).setStroke(new BasicStroke(20));
x2 = e.getX();
y2 = e.getY();
g.drawLine(x1, y1, x2, y2);
Shape eraser = new Eraser(x1, y1, x2, y2, name, color);
shapeArray[index++] = eraser;
x1 = x2;
y1 = y2;
color = Color.black;
g.setColor(color);
((Graphics2D) g).setStroke(new BasicStroke(1));
}
}
public void mouseMoved(java.awt.event.MouseEvent e) {
}
public void actionPerformed(ActionEvent e) {
if ("".equals(e.getActionCommand())) {
// 获取当前事件源,并强制转换
JButton jb = (JButton) e.getSource();
// 将按钮背景色赋值给color
color = jb.getBackground();
// 设置画笔背景色
// 注意:不能直接写成g.setColor(jb.getBackground());后面重绘时需用到color参数;
g.setColor(color);
} else {
name = e.getActionCommand();
if ("清屏".equals(name)) {
color = Color.white;
g.setColor(color);
x1 = 0;
y1 = 0;
x2 = 900;
y2 = 700;
g.fillRect(x1, y1, x2, y2);
// 重置多边形最后一条线段数据
x3 = 0;
y3 = 0;
x5 = 0;
y5 = 0;
Shape fillrect = new FillRect(x1, y1, x2, y2, name, color);
shapeArray[index++] = fillrect;
color = Color.black;
g.setColor(color);
}
}
// 多边形切换设置
flag1 = true;
// 点击非清屏按钮,先完成多边形绘制
if (!"".equals(e.getActionCommand()) && flag2) {
g.drawLine(x5, y5, x3, y3);
Shape line = new Line(x5, y5, x3, y3, name, color);
shapeArray[index++] = line;
flag2 = false;
}
// 点击颜色按钮继续画图
if ("".equals(e.getActionCommand()) && flag2) {
flag1 = false;
}
}
}
Shape父类
package paint0701;
import java.awt.Color;
import java.awt.Graphics;
public abstract class Shape {
public int x1, y1, x2, y2;
public String name;
public Color color;
public Shape() {
};
public Shape(int x1, int y1, int x2, int y2, String name, Color color) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.name = name;
this.color = color;
}
public void drawShape(Graphics g) {
}
}
直线子类
package paint0701;
import java.awt.Color;
import java.awt.Graphics;
public class Line extends Shape {
public Line() {
};
public Line(int x1, int y1, int x2, int y2, String name, Color color) {
super(x1, y1, x2, y2, name, color);
}
public void drawShape(Graphics g) {
g.setColor(color);
g.drawLine(x1, y1, x2, y2);
}
}
矩形子类
package paint0701;
import java.awt.Color;
import java.awt.Graphics;
public class Rect extends Shape {
public Rect(){};
public Rect(int x1,int y1,int x2,int y2,String name,Color color){
super(x1,y1,x2,y2,name,color);
}
public void drawShape(Graphics g){
g.setColor(color);
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1));
}
}
椭圆子类
package paint0701;
import java.awt.Color;
import java.awt.Graphics;
public class Oval extends Shape {
public Oval(){};
public Oval(int x1,int y1,int x2,int y2,String name,Color color){
super(x1,y1,x2,y2,name,color);
}
public void drawShape(Graphics g){
g.setColor(color);
g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1));
}
}
清屏(绘制实心矩形)子类
package paint0701;
import java.awt.Color;
import java.awt.Graphics;
public class FillRect extends Shape {
public FillRect() {
};
public FillRect(int x1, int y1, int x2, int y2, String name, Color color) {
super(x1, y1, x2, y2, name, color);
}
public void drawShape(Graphics g) {
g.setColor(color);
g.fillRect(x1, y1, x2, y2);
}
}
橡皮檫(绘制白色画笔)子类
package paint0701;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
public class Eraser extends Shape {
public Eraser() {
};
public Eraser(int x1, int y1, int x2, int y2, String name, Color color) {
super(x1, y1, x2, y2, name, color);
}
public void drawShape(Graphics g) {
g.setColor(color);
((Graphics2D) g).setStroke(new BasicStroke(20));
g.drawLine(x1, y1, x2, y2);
((Graphics2D) g).setStroke(new BasicStroke(1));
}
}
此画图板缺陷:
1、未增加调整线宽功能;
2、设计布局不够简洁;
3、为了完成多边形切换图形自动绘制新引入了多个变量,并且带来了一个相对明显的bug。
(绘图时切换颜色按钮后,鼠标双击会有意外惊喜~)
上一篇: 网页设计技巧谈:走出路径的困惑
下一篇: Kafka基础命令