简单的 Java Swing 画图 Demo,包含基本的绘图功能(直线、矩形、椭圆)和颜色选择功能。这个示例代码结构清晰,适合初学者理解 Swing 绘图的基本原理
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class SimpleDrawingDemo {
public static void main(String[] args) {
// 创建并设置GUI
JFrame frame = new JFrame("简易绘图工具");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
// 创建绘图面板
DrawingPanel drawingPanel = new DrawingPanel();
// 创建工具栏
JPanel toolPanel = new JPanel();
JButton lineBtn = new JButton("直线");
JButton rectBtn = new JButton("矩形");
JButton ovalBtn = new JButton("椭圆");
JButton clearBtn = new JButton("清除");
// 添加按钮到工具栏
toolPanel.add(lineBtn);
toolPanel.add(rectBtn);
toolPanel.add(ovalBtn);
toolPanel.add(clearBtn);
// 添加颜色选择器
JButton colorBtn = new JButton("颜色");
toolPanel.add(colorBtn);
// 按钮事件监听
lineBtn.addActionListener(e -> drawingPanel.setShapeType(ShapeType.LINE));
rectBtn.addActionListener(e -> drawingPanel.setShapeType(ShapeType.RECTANGLE));
ovalBtn.addActionListener(e -> drawingPanel.setShapeType(ShapeType.OVAL));
clearBtn.addActionListener(e -> drawingPanel.clear());
// 颜色选择事件
colorBtn.addActionListener(e -> {
Color newColor = JColorChooser.showDialog(frame, "选择颜色", drawingPanel.getColor());
if (newColor != null) {
drawingPanel.setColor(newColor);
}
});
// 添加组件到窗口
frame.add(toolPanel, BorderLayout.NORTH);
frame.add(drawingPanel, BorderLayout.CENTER);
// 显示窗口
frame.setVisible(true);
}
}
// 绘图形状枚举
enum ShapeType {
LINE, RECTANGLE, OVAL
}
// 绘图形状类
class Shape {
private int x1, y1, x2, y2;
private Color color;
private ShapeType type;
public Shape(int x1, int y1, int x2, int y2, Color color, ShapeType type) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
this.type = type;
}
public void draw(Graphics g) {
g.setColor(color);
switch (type) {
case LINE:
g.drawLine(x1, y1, x2, y2);
break;
case RECTANGLE:
g.drawRect(Math.min(x1, x2), Math.min(y1, y2),
Math.abs(x2 - x1), Math.abs(y2 - y1));
break;
case OVAL:
g.drawOval(Math.min(x1, x2), Math.min(y1, y2),
Math.abs(x2 - x1), Math.abs(y2 - y1));
break;
}
}
}
// 绘图面板类
class DrawingPanel extends JPanel {
private ArrayList<Shape> shapes = new ArrayList<>();
private ShapeType currentShapeType = ShapeType.LINE;
private Color currentColor = Color.BLACK;
private int startX, startY;
private Shape currentShape;
public DrawingPanel() {
setBackground(Color.WHITE);
// 添加鼠标监听器
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
startX = e.getX();
startY = e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
int endX = e.getX();
int endY = e.getY();
shapes.add(new Shape(startX, startY, endX, endY, currentColor, currentShapeType));
repaint();
}
});
// 添加鼠标移动监听器
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
int endX = e.getX();
int endY = e.getY();
currentShape = new Shape(startX, startY, endX, endY, currentColor, currentShapeType);
repaint();
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// 绘制所有已保存的形状
for (Shape shape : shapes) {
shape.draw(g);
}
// 绘制当前正在绘制的形状
if (currentShape != null) {
currentShape.draw(g);
}
}
public void setShapeType(ShapeType type) {
this.currentShapeType = type;
}
public void setColor(Color color) {
this.currentColor = color;
}
public Color getColor() {
return currentColor;
}
public void clear() {
shapes.clear();
repaint();
}
}
代码功能说明:
- 主窗口(SimpleDrawingDemo):创建窗口和工具栏,添加各种绘图工具按钮。
- 绘图面板(DrawingPanel):核心绘图区域,处理鼠标事件和图形绘制。
- 形状类(Shape):封装绘图形状的属性(位置、颜色、类型)和绘制方法。
- 形状类型枚举(ShapeType):定义支持的绘图形状类型(直线、矩形、椭圆)。
使用方法:
- 选择绘图工具(直线、矩形、椭圆)。
- 点击 "颜色" 按钮选择绘图颜色。
- 在白色区域按住鼠标左键并拖动进行绘图。
- 点击 "清除" 按钮可以清空画布。
这个 Demo 包含了 Swing 绘图的基本要素:鼠标事件处理、图形绘制、组件布局等。你可以在此基础上添加更多功能,如填充颜色、线条粗细调整、撤销操作等
3807

被折叠的 条评论
为什么被折叠?



