用Java制作一个简易的画图程序,要求有七种颜色,有矩形,直线,椭圆
时间: 2024-03-22 11:42:51 浏览: 91
好的,以下是一个简单的Java画图程序,包含七种颜色,可以绘制矩形、直线、椭圆。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimplePaint extends JFrame implements ActionListener, MouseListener, MouseMotionListener {
private int startX, startY, endX, endY;
private int currentShape;
private int currentColor;
private JButton[] colorButtons;
private JButton[] shapeButtons;
private JPanel colorPanel;
private JPanel shapePanel;
private JPanel controlPanel;
private DrawingArea drawingArea;
public SimplePaint() {
setTitle("Simple Paint");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
// 初始化画布和颜色、形状按钮
drawingArea = new DrawingArea();
colorButtons = new JButton[7];
colorButtons[0] = new JButton("Black");
colorButtons[1] = new JButton("Red");
colorButtons[2] = new JButton("Orange");
colorButtons[3] = new JButton("Yellow");
colorButtons[4] = new JButton("Green");
colorButtons[5] = new JButton("Blue");
colorButtons[6] = new JButton("Purple");
for (int i = 0; i < colorButtons.length; i++) {
colorButtons[i].addActionListener(this);
}
shapeButtons = new JButton[3];
shapeButtons[0] = new JButton("Rectangle");
shapeButtons[1] = new JButton("Line");
shapeButtons[2] = new JButton("Oval");
for (int i = 0; i < shapeButtons.length; i++) {
shapeButtons[i].addActionListener(this);
}
// 设置颜色、形状按钮面板
colorPanel = new JPanel();
colorPanel.setLayout(new GridLayout(1, colorButtons.length));
for (int i = 0; i < colorButtons.length; i++) {
colorPanel.add(colorButtons[i]);
}
shapePanel = new JPanel();
shapePanel.setLayout(new GridLayout(1, shapeButtons.length));
for (int i = 0; i < shapeButtons.length; i++) {
shapePanel.add(shapeButtons[i]);
}
// 设置控制面板
controlPanel = new JPanel();
controlPanel.setLayout(new BorderLayout());
controlPanel.add(colorPanel, BorderLayout.NORTH);
controlPanel.add(shapePanel, BorderLayout.SOUTH);
// 添加组件
add(drawingArea, BorderLayout.CENTER);
add(controlPanel, BorderLayout.NORTH);
// 添加鼠标事件监听器
drawingArea.addMouseListener(this);
drawingArea.addMouseMotionListener(this);
}
public void actionPerformed(ActionEvent e) {
// 处理颜色按钮事件
for (int i = 0; i < colorButtons.length; i++) {
if (e.getSource() == colorButtons[i]) {
currentColor = i;
return;
}
}
// 处理形状按钮事件
for (int i = 0; i < shapeButtons.length; i++) {
if (e.getSource() == shapeButtons[i]) {
currentShape = i;
return;
}
}
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
startX = e.getX();
startY = e.getY();
}
public void mouseReleased(MouseEvent e) {
endX = e.getX();
endY = e.getY();
// 绘制形状
Graphics g = drawingArea.getGraphics();
g.setColor(getColor(currentColor));
switch (currentShape) {
case 0: // 矩形
g.drawRect(Math.min(startX, endX), Math.min(startY, endY), Math.abs(endX - startX), Math.abs(endY - startY));
break;
case 1: // 直线
g.drawLine(startX, startY, endX, endY);
break;
case 2: // 椭圆
g.drawOval(Math.min(startX, endX), Math.min(startY, endY), Math.abs(endX - startX), Math.abs(endY - startY));
break;
}
g.dispose();
}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
private Color getColor(int index) {
switch (index) {
case 0: // 黑色
return Color.BLACK;
case 1: // 红色
return Color.RED;
case 2: // 橙色
return Color.ORANGE;
case 3: // 黄色
return Color.YELLOW;
case 4: // 绿色
return Color.GREEN;
case 5: // 蓝色
return Color.BLUE;
case 6: // 紫色
return new Color(128, 0, 128);
default:
return Color.BLACK;
}
}
public static void main(String[] args) {
SimplePaint paint = new SimplePaint();
paint.setVisible(true);
}
// 画布面板
class DrawingArea extends JPanel {
public DrawingArea() {
setPreferredSize(new Dimension(400, 300));
setBackground(Color.WHITE);
}
}
}
```
这个程序使用了Java的Swing图形库,创建了一个窗口,在窗口中添加了一个画布和颜色、形状按钮。通过鼠标事件监听器实现了绘制矩形、直线、椭圆的功能,通过颜色按钮实现了七种颜色的选择。您可以根据需要对程序进行修改和完善。
阅读全文