用Java制作一个简易的画图程序,要求有七种颜色,有矩形,直线,椭圆,画笔,清除画板橡皮擦等功能
时间: 2024-03-23 16:35:52 浏览: 82
以下是一个Java制作的简易画图程序,包含七种颜色,矩形,直线,椭圆,画笔,清除画板和橡皮擦等功能。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimplePaintApp extends JFrame implements ActionListener, MouseListener, MouseMotionListener {
private static final long serialVersionUID = 1L;
private JButton clearBtn, eraserBtn;
private JButton lineBtn, rectBtn, ovalBtn, penBtn;
private JButton redBtn, greenBtn, blueBtn, cyanBtn, magentaBtn, yellowBtn, blackBtn;
private JPanel btnPanel, drawPanel;
private int startX, startY, endX, endY;
private boolean isDrawing = false;
private Color currentColor = Color.BLACK;
private int penSize = 2;
private Graphics2D g2;
public SimplePaintApp() {
setTitle("Simple Paint App");
setDefaultCloseOperation(EXIT_ON_CLOSE);
// 初始化按钮
clearBtn = new JButton("Clear");
eraserBtn = new JButton("Eraser");
lineBtn = new JButton("Line");
rectBtn = new JButton("Rectangle");
ovalBtn = new JButton("Oval");
penBtn = new JButton("Pen");
redBtn = new JButton();
redBtn.setBackground(Color.RED);
greenBtn = new JButton();
greenBtn.setBackground(Color.GREEN);
blueBtn = new JButton();
blueBtn.setBackground(Color.BLUE);
cyanBtn = new JButton();
cyanBtn.setBackground(Color.CYAN);
magentaBtn = new JButton();
magentaBtn.setBackground(Color.MAGENTA);
yellowBtn = new JButton();
yellowBtn.setBackground(Color.YELLOW);
blackBtn = new JButton();
blackBtn.setBackground(Color.BLACK);
// 添加按钮监听器
clearBtn.addActionListener(this);
eraserBtn.addActionListener(this);
lineBtn.addActionListener(this);
rectBtn.addActionListener(this);
ovalBtn.addActionListener(this);
penBtn.addActionListener(this);
redBtn.addActionListener(this);
greenBtn.addActionListener(this);
blueBtn.addActionListener(this);
cyanBtn.addActionListener(this);
magentaBtn.addActionListener(this);
yellowBtn.addActionListener(this);
blackBtn.addActionListener(this);
// 初始化按钮面板
btnPanel = new JPanel();
btnPanel.setLayout(new GridLayout(4, 4));
btnPanel.add(clearBtn);
btnPanel.add(eraserBtn);
btnPanel.add(lineBtn);
btnPanel.add(rectBtn);
btnPanel.add(ovalBtn);
btnPanel.add(penBtn);
btnPanel.add(redBtn);
btnPanel.add(greenBtn);
btnPanel.add(blueBtn);
btnPanel.add(cyanBtn);
btnPanel.add(magentaBtn);
btnPanel.add(yellowBtn);
btnPanel.add(blackBtn);
// 初始化画板
drawPanel = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
};
drawPanel.setBackground(Color.WHITE);
drawPanel.addMouseListener(this);
drawPanel.addMouseMotionListener(this);
// 添加面板
getContentPane().add(btnPanel, BorderLayout.NORTH);
getContentPane().add(drawPanel, BorderLayout.CENTER);
setSize(600, 400);
setVisible(true);
}
public static void main(String[] args) {
new SimplePaintApp();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == clearBtn) {
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, drawPanel.getWidth(), drawPanel.getHeight());
g2.setColor(currentColor);
} else if (e.getSource() == eraserBtn) {
g2.setColor(Color.WHITE);
penSize = 20;
penBtn.setSelected(false);
lineBtn.setSelected(false);
rectBtn.setSelected(false);
ovalBtn.setSelected(false);
} else if (e.getSource() == lineBtn) {
penSize = 2;
penBtn.setSelected(false);
lineBtn.setSelected(true);
rectBtn.setSelected(false);
ovalBtn.setSelected(false);
} else if (e.getSource() == rectBtn) {
penSize = 2;
penBtn.setSelected(false);
lineBtn.setSelected(false);
rectBtn.setSelected(true);
ovalBtn.setSelected(false);
} else if (e.getSource() == ovalBtn) {
penSize = 2;
penBtn.setSelected(false);
lineBtn.setSelected(false);
rectBtn.setSelected(false);
ovalBtn.setSelected(true);
} else if (e.getSource() == penBtn) {
penSize = 2;
penBtn.setSelected(true);
lineBtn.setSelected(false);
rectBtn.setSelected(false);
ovalBtn.setSelected(false);
} else if (e.getSource() == redBtn) {
currentColor = Color.RED;
g2.setColor(currentColor);
} else if (e.getSource() == greenBtn) {
currentColor = Color.GREEN;
g2.setColor(currentColor);
} else if (e.getSource() == blueBtn) {
currentColor = Color.BLUE;
g2.setColor(currentColor);
} else if (e.getSource() == cyanBtn) {
currentColor = Color.CYAN;
g2.setColor(currentColor);
} else if (e.getSource() == magentaBtn) {
currentColor = Color.MAGENTA;
g2.setColor(currentColor);
} else if (e.getSource() == yellowBtn) {
currentColor = Color.YELLOW;
g2.setColor(currentColor);
} else if (e.getSource() == blackBtn) {
currentColor = Color.BLACK;
g2.setColor(currentColor);
}
}
@Override
public void mousePressed(MouseEvent e) {
startX = e.getX();
startY = e.getY();
isDrawing = true;
}
@Override
public void mouseReleased(MouseEvent e) {
if (isDrawing) {
endX = e.getX();
endY = e.getY();
if (penBtn.isSelected()) {
g2.setStroke(new BasicStroke(penSize, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2.drawLine(startX, startY, endX, endY);
} else if (lineBtn.isSelected()) {
g2.setStroke(new BasicStroke(penSize, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2.drawLine(startX, startY, endX, endY);
} else if (rectBtn.isSelected()) {
g2.setStroke(new BasicStroke(penSize, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2.drawRect(Math.min(startX, endX), Math.min(startY, endY), Math.abs(endX - startX), Math.abs(endY - startY));
} else if (ovalBtn.isSelected()) {
g2.setStroke(new BasicStroke(penSize, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2.drawOval(Math.min(startX, endX), Math.min(startY, endY), Math.abs(endX - startX), Math.abs(endY - startY));
}
}
isDrawing = false;
}
@Override
public void mouseDragged(MouseEvent e) {
if (isDrawing) {
endX = e.getX();
endY = e.getY();
if (penBtn.isSelected()) {
g2.setStroke(new BasicStroke(penSize, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2.drawLine(startX, startY, endX, endY);
startX = endX;
startY = endY;
} else if (eraserBtn.isSelected()) {
g2.setStroke(new BasicStroke(penSize, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2.setColor(Color.WHITE);
g2.drawLine(startX, startY, endX, endY);
g2.setColor(currentColor);
startX = endX;
startY = endY;
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
}
}
```
该程序使用了 `JFrame` 和 `JPanel` 等 Swing 组件,通过按钮的监听器来实现不同功能的绘图,包括清除画板、橡皮擦、直线、矩形、椭圆和画笔,同时提供了七种颜色选择。程序中使用了 `Graphics2D` 对象来进行绘图,并通过鼠标事件监听器来捕获鼠标动作,实现画图功能。
阅读全文