这是一个使用 Java Swing 库构建的图形绘制应用程序,它允许用户绘制各种形状,并保存和加载绘制结果。

用户界面包括一个绘制区域和一个控制面板。绘制区域是一个自定义的 JPanel 组件,用于绘制所有的图形。控制面板包括以下控件:

  • 形状选择下拉框: 选择要绘制的形状,例如直线、矩形和椭圆。
  • 颜色选择下拉框: 选择要绘制的形状的颜色。
  • 填充复选框: 选择是否填充所绘制的形状。
  • 保存按钮: 将当前绘制结果保存到文件。
  • 打开按钮: 从文件中加载已保存的绘制结果。

当用户选择新的形状、颜色或填充选项时,程序会更新相应的变量。当用户单击保存按钮时,程序会打开一个文件选择对话框,让用户选择文件保存绘制结果。程序会将所有的图形对象保存到一个文件中。当用户单击打开按钮时,程序会打开一个文件选择对话框,让用户选择要打开的文件。程序会从文件中读取所有的图形对象,并将它们显示在绘制区域中。

绘制区域是一个自定义的 JPanel 组件,用于绘制所有的图形。它包括一个 Shapes 对象,用于保存所有的图形对象。当用户在绘制区域上单击鼠标时,程序会根据当前的形状、颜色和填充选项创建一个新的图形对象,并将它添加到 Shapes 对象中。然后程序会调用 repaint() 方法来刷新绘制区域,以便显示新的图形对象。

Shapes 对象是一个自定义的类,用于保存所有的图形对象。它包括一个 List 对象,用于保存所有的图形对象。它还实现了 Serializable 接口,以便可以将它保存到文件中。当程序从文件中读取 Shapes 对象时,它会将所有的图形对象重新绘制到绘制区域中。

绘制图形的方法都是通过 Java AWT 和 Java 2D 库来实现的。我们创建了几个不同的 Shape 对象来表示不同的图形形状,包括直线 (Line2D.Double)、矩形 (Rectangle2D.Double) 和椭圆 (Ellipse2D.Double)。我们还使用了 Graphics2D 对象来设置颜色、填充和绘制线条或填充区域。

以下是使用 Java 语言编写的绘图应用程序的代码:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class DrawApp extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    private JPanel drawPanel;
    private JComboBox<String> shapeBox;
    private JComboBox<String> colorBox;
    private JCheckBox fillBox;
    private JButton saveButton;
    private JButton openButton;
    private Color color;
    private Shape shape;
    private boolean fill;

    public DrawApp() {
        super('DrawApp');
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800, 600);
        setLocationRelativeTo(null);

        drawPanel = new DrawPanel();
        add(drawPanel, BorderLayout.CENTER);

        JPanel controlPanel = new JPanel(new FlowLayout());

        String[] shapes = {'Line', 'Rectangle', 'Oval'};
        shapeBox = new JComboBox<String>(shapes);
        shapeBox.addActionListener(this);
        controlPanel.add(shapeBox);

        String[] colors = {'Black', 'Red', 'Green', 'Blue'};
        colorBox = new JComboBox<String>(colors);
        colorBox.addActionListener(this);
        controlPanel.add(colorBox);

        fillBox = new JCheckBox('Fill');
        fillBox.addActionListener(this);
        controlPanel.add(fillBox);

        saveButton = new JButton('Save');
        saveButton.addActionListener(this);
        controlPanel.add(saveButton);

        openButton = new JButton('Open');
        openButton.addActionListener(this);
        controlPanel.add(openButton);

        add(controlPanel, BorderLayout.NORTH);
    }

    public static void main(String[] args) {
        DrawApp app = new DrawApp();
        app.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == shapeBox) {
            String shapeStr = (String) shapeBox.getSelectedItem();
            if (shapeStr.equals('Line')) {
                shape = new Line2D.Double();
            } else if (shapeStr.equals('Rectangle')) {
                shape = new Rectangle2D.Double();
            } else if (shapeStr.equals('Oval')) {
                shape = new Ellipse2D.Double();
            }
        } else if (e.getSource() == colorBox) {
            String colorStr = (String) colorBox.getSelectedItem();
            if (colorStr.equals('Black')) {
                color = Color.BLACK;
            } else if (colorStr.equals('Red')) {
                color = Color.RED;
            } else if (colorStr.equals('Green')) {
                color = Color.GREEN;
            } else if (colorStr.equals('Blue')) {
                color = Color.BLUE;
            }
        } else if (e.getSource() == fillBox) {
            fill = fillBox.isSelected();
        } else if (e.getSource() == saveButton) {
            saveToFile();
        } else if (e.getSource() == openButton) {
            openFromFile();
        }
    }

    private void saveToFile() {
        JFileChooser fileChooser = new JFileChooser();
        int returnVal = fileChooser.showSaveDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file))) {
                out.writeObject(drawPanel.getShapes());
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, 'Error saving file: ' + ex.getMessage());
            }
        }
    }

    private void openFromFile() {
        JFileChooser fileChooser = new JFileChooser();
        int returnVal = fileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) {
                drawPanel.setShapes((Shapes) in.readObject());
            } catch (IOException | ClassNotFoundException ex) {
                JOptionPane.showMessageDialog(this, 'Error opening file: ' + ex.getMessage());
            }
        }
    }

    private class DrawPanel extends JPanel {

        private static final long serialVersionUID = 1L;
        private Shapes shapes;

        public DrawPanel() {
            setBackground(Color.WHITE);
            shapes = new Shapes();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            for (Shape shape : shapes.getShapes()) {
                g2d.setColor(shape.getColor());
                if (shape.isFilled()) {
                    g2d.fill(shape);
                } else {
                    g2d.draw(shape);
                }
            }
        }

        public void addShape(Shape shape) {
            shape.setColor(color);
            shape.setFilled(fill);
            shapes.addShape(shape);
            repaint();
        }

        public Shapes getShapes() {
            return shapes;
        }

        public void setShapes(Shapes shapes) {
            this.shapes = shapes;
            repaint();
        }
    }

    private class Shapes implements Serializable {

        private static final long serialVersionUID = 1L;
        private List<Shape> shapes;

        public Shapes() {
            shapes = new ArrayList<Shape>();
        }

        public void addShape(Shape shape) {
            shapes.add(shape);
        }

        public List<Shape> getShapes() {
            return shapes;
        }
    }
}
Java 图形绘制应用程序:创建、保存和加载图形

原文地址: https://www.cveoy.top/t/topic/nWTe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录