0

我有一个 Java 绘画程序,我有两个问题与它有关。这两个问题都比较简单,只看鼠标输入是怎么处理的,图片是怎么用颜色的。这是应用程序的照片:

在此处输入图像描述

所以这是我的第一个问题:

如您所见,从应用程序的外观来看,油漆区域上有一些点。这些点中的每一个都是鼠标单击。该程序无法识别用户何时按住鼠标按钮,因此您必须单独单击。

这显然适得其反,对用户不友好且无法接受。现在,我如何解决这个问题,我不确定。我试过使用永久while (true)循环,但这不起作用。我如何做到这一点,而不是每次都单击,每次按住鼠标时它都会喷出点?

第二个问题是点的颜色。如您所见,在底部,有颜色按钮。这些功能,但是有一个问题:每当我改变颜色时,当前屏幕上的所有点都会改变颜色。颜色由一个名为的变量运行,该变量currentColor由底部面板上所有颜色按钮控制的 actionListener 运行。如何确保已经放置在屏幕上的颜色不再受到影响?

我相信可以解决这两个问题的所有代码都在我的自定义JPanel中,用于程序绘制。我将在下面发布整个课程,如果您有任何其他问题,请告诉我。

int xCord, yCord;

public class PaintPanel extends JPanel implements MouseListener {
    // default serial whatever...
    private static final long serialVersionUID = -6514297510194472060L;

    public PaintPanel() {
        addMouseListener(this);
    }

    ArrayList<Point> points = new ArrayList<Point>();

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Point point : points) {
            g.setColor(currentColor);
            g.fillOval(point.x, point.y, 12, 12);

        }
        repaint();
    }

    @Override
    public void mouseClicked(MouseEvent m) {
    }

    @Override
    public void mouseEntered(MouseEvent m) {
    }

    @Override
    public void mouseExited(MouseEvent m) {
    }

    @Override
    public void mousePressed(MouseEvent m) {

        if (paintPanel.contains(m.getPoint())) {
                points.add(m.getPoint());
                xCord = m.getX();
                yCord = m.getY();
                System.out.println("x: " + xCord + " y: " + yCord);
        }

    }

    @Override
    public void mouseReleased(MouseEvent m) {
    }

}
4

1 回答 1

1

Swing 中的绘画具有破坏性。

也就是说,当 Swing 请求对组件进行重绘时,组件应该清除之前绘制的内容并自行更新。

您的颜色问题的问题是您只指定了一种颜色。

一种可能的解决方案是绘制到后备缓冲区(如BufferedImage)而不是依赖paintComponent.

而不是每次paintComponent调用时都重新绘制所有点,您只需绘制BufferedImage代替。

至于你的鼠标问题,你需要实现 a MouseMotionListener,这将允许你检测鼠标何时被拖过表面,画出一串点

用非常基本的例子更新

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SimplePaint04 {

    public static void main(String[] args) {
        new SimplePaint04();
    }

    public SimplePaint04() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private PaintPane paintPane;

        public TestPane() {
            setLayout(new BorderLayout());
            add((paintPane = new PaintPane()));
            add(new ColorsPane(paintPane), BorderLayout.SOUTH);
        }
    }

    public class ColorsPane extends JPanel {

        public ColorsPane(PaintPane paintPane) {
            add(new JButton(new ColorAction(paintPane, "Red", Color.RED)));
            add(new JButton(new ColorAction(paintPane, "Green", Color.GREEN)));
            add(new JButton(new ColorAction(paintPane, "Blue", Color.BLUE)));
        }

        public class ColorAction extends AbstractAction {

            private PaintPane paintPane;
            private Color color;

            private ColorAction(PaintPane paintPane, String name, Color color) {
                putValue(NAME, name);
                this.paintPane = paintPane;
                this.color = color;
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                paintPane.setForeground(color);
            }

        }

    }

    public class PaintPane extends JPanel {

        private BufferedImage background;

        public PaintPane() {
            setBackground(Color.WHITE);
            setForeground(Color.BLACK);
            MouseAdapter handler = new MouseAdapter() {

                @Override
                public void mousePressed(MouseEvent e) {
                    drawDot(e.getPoint());
                }

                @Override
                public void mouseDragged(MouseEvent e) {
                    drawDot(e.getPoint());
                }

            };
            addMouseListener(handler);
            addMouseMotionListener(handler);
        }

        protected void drawDot(Point p) {
            if (background == null) {
                updateBuffer();;
            }

            if (background != null) {
                Graphics2D g2d = background.createGraphics();
                g2d.setColor(getForeground());
                g2d.fillOval(p.x - 5, p.y - 5, 10, 10);
                g2d.dispose();
            }
            repaint();
        }

        @Override
        public void invalidate() {
            super.invalidate();
            updateBuffer();
        }

        protected void updateBuffer() {

            if (getWidth() > 0 && getHeight() > 0) {
                BufferedImage newBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
                Graphics2D g2d = newBuffer.createGraphics();
                g2d.setColor(Color.WHITE);
                g2d.fillRect(0, 0, getWidth(), getHeight());
                if (background != null) {
                    g2d.drawImage(background, 0, 0, this);
                }
                g2d.dispose();
                background = newBuffer;
            }

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            if (background == null) {
                updateBuffer();
            }
            g2d.drawImage(background, 0, 0, this);
            g2d.dispose();
        }
    }
}
于 2013-06-04T03:41:54.957 回答