0

我有一个使用自定义 JPanel 进行绘画的 Java 绘画程序。当单击 JPanel 时会绘制一个小椭圆(或圆形,如果您愿意的话),但每次单击另一个位置时椭圆会消失。坐标也会更新,但椭圆不会停留,它会移动到用户接下来单击的任何位置...这是自定义 JPanel 的代码:

int xCord, yCord;

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

        // initial values
        int xCord = -10;
        int yCord = -10;

        public PaintPanel() {
            addMouseListener(this);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(ProgramUI.currentColor);
            g.fillOval(xCord, yCord, 8, 8);
            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())) {
                xCord = m.getX();
                yCord = m.getY();
                System.out.println("x: " + xCord + " y: " + yCord);
            }
        }

        @Override
        public void mouseReleased(MouseEvent m) {
        }

    }

我需要按住鼠标来连续绘制椭圆,直到松开鼠标按钮。这里唯一的问题是鼠标椭圆更新,但不保存它的原始位置。我该如何解决?

4

3 回答 3

5

fillOval绘制一个椭圆,因为该方法中只有一个语句绘制了一个椭圆,paintComponent因此该语句

super.paintComponent(g);

导致任何以前的绘画被清除一次repaint被调用。

要绘制多个椭圆,您可以List<Point>按照自定义绘制方法中所述绘制组件

不要repaint从内部调用paintComponent。这会产生无限循环并降低性能。如果需要定期更新,请改为repaintSwing Timer调用。ActionListener

于 2013-06-03T19:58:04.250 回答
0

那是因为组件会重新绘制自身,为了使更改永久化,您应该拍摄 jpanel 的图像,并在每次完成绘制后将其设置为背景...

于 2013-06-03T19:58:54.803 回答
0

您只绘制用户每次单击的最后一个位置。相反,您需要收集过去的点击并每次都绘制它们。

此代码将执行您想要的操作:

package com.sandbox;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

public class SwingSandbox {


    public static void main(String[] args) {

        JFrame frame = buildFrame();
        frame.add(new PaintPanel());
    }

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

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

        public PaintPanel() {
            addMouseListener(this);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(new Color(250));
            for (Point point : points) {
                g.fillOval(point.x, point.y, 8, 8);
            }
            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 (this.contains(m.getPoint())) {
                points.add(m.getPoint());
            }
        }

        @Override
        public void mouseReleased(MouseEvent m) {
        }

    }


    private static JFrame buildFrame() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setVisible(true);
        return frame;
    }


}
于 2013-06-03T20:00:23.430 回答