摆动:我通过鼠标拖动绘制的任何线条仅在下次拖动鼠标时显示

时间:2016-04-04 19:35:25

标签: java swing repaint line-drawing

我还在学习Java的基础知识,很抱歉,如果这个问题是基本的。我尝试做的是将点和线存储到ArrayLists中,然后让paintComponent()方法通过它们并在每次调用时绘制它们。由于我对我的点使用相同的ArrayList,因此每次按下鼠标时都会清除它,因此新行不会与前一行连接。

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.util.ArrayList;


public class SwingPaintDemo2 {

    public static void main(String[] args) {
        JFrame f = new JFrame("Swing Paint Demo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(300,300);
        f.add(new MyPanel());
        f.setBackground(Color.WHITE);
        f.setVisible(true);
    }
}

class MyPanel extends JPanel {

    private int x;
    private int y;
    private int x2;
    private int y2;

   private ArrayList<Point> points = new ArrayList<Point>();
   private ArrayList<Curves> curvedLines = new ArrayList<Curves>();

    public MyPanel() {

        setBorder(BorderFactory.createLineBorder(Color.black));

        addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                points.add(new Point(e.getX(), e.getY()));
                repaint();
            }
        });

        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                points.clear();
                points.add(new Point(e.getX(), e.getY()));
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                ArrayList<Point> newPoints = new ArrayList<Point>();
                for (int i = 0; i < points.size(); i++) {
                    newPoints.add(points.get(i));
                }
                curvedLines.add(new Curves(newPoints));
            }
        });
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLACK);


            for (int c = 0; c < points.size() - 1; c++) {
                x = (int) points.get(c).getX();
                y = (int) points.get(c).getY();
                x2 = (int) points.get(c + 1).getX();
                y2 = (int) points.get(c + 1).getY();
                g.drawLine(x, y, x2, y2);
            }


            for (int t = 0; t < curvedLines.size(); t++) {
                ArrayList<Point> iterator = curvedLines.get(t).points;
                for (int c = 0; c < iterator.size() - 1; c++) {
                    x = (int) iterator.get(c).getX();
                    y = (int) iterator.get(c).getY();
                    x2 = (int) iterator.get(c + 1).getX();
                    y2 = (int) iterator.get(c + 1).getY();
                    g.drawLine(x, y, x2, y2);
                }
            }

    }


        class Curves {
            private ArrayList<Point> points;

            public Curves(ArrayList<Point> points) {
                this.points = new ArrayList<Point>();
                this.points = points;
            }
        }
    }

问题在于,当我拖动鼠标时,没有绘制任何线条,但是当我再次拖动鼠标时出现该线条时,其点仍然存储。造成这种明显延迟的原因是什么?

2 个答案:

答案 0 :(得分:1)

在我的原始评论中,您被要求将上一个问题中给出的工作代码与您在此处发布的代码进行比较,以了解其中的区别。

不同之处在于您在paintComponent()方法中添加了if / else语句。

  

我添加了if / else语句,因为paintComponenet不会绘制任何内容

这没有任何意义。当你添加一个if / else语句时,这意味着只能绘制一种类型的绘画,可以绘制曲线或绘制点,但是你永远不能同时绘制它们。

你总是想要同时做到这两点:

  1. 绘制在mouseDragged()事件中添加的点
  2. 绘制已添加到曲线中的所有旧点。
  3. 所以摆脱if / else语句。

    此外,将来当您发布适当的SSCCE se代码时,我们可以执行代码。这里发布的代码没有main()方法或JFrame,因此无法进行编译和测试。

答案 1 :(得分:0)

基于camickr的推荐,它对我来说很好用

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SwingPaintDemo2 {

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

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

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

    class MyPanel extends JPanel {

        private int x;
        private int y;
        private int x2;
        private int y2;

        private ArrayList<Point> points = new ArrayList<Point>();
        private ArrayList<Curves> curvedLines = new ArrayList<Curves>();

        public MyPanel() {

            setBorder(BorderFactory.createLineBorder(Color.black));

            addMouseMotionListener(new MouseAdapter() {
                @Override
                public void mouseDragged(MouseEvent e) {
                    points.add(new Point(e.getX(), e.getY()));
                    repaint();
                }
            });

            addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    points.clear();
                    points.add(new Point(e.getX(), e.getY()));
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    ArrayList<Point> newPoints = new ArrayList<Point>();
                    for (int i = 0; i < points.size(); i++) {
                        newPoints.add(points.get(i));
                    }
                    curvedLines.add(new Curves(newPoints));
                    repaint();
                }
            });
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLACK);

            for (int c = 0; c < points.size() - 1; c++) {
                x = (int) points.get(c).getX();
                y = (int) points.get(c).getY();
                x2 = (int) points.get(c + 1).getX();
                y2 = (int) points.get(c + 1).getY();
                g.drawLine(x, y, x2, y2);
            }
            for (int t = 0; t < curvedLines.size(); t++) {
                ArrayList<Point> iterator = curvedLines.get(t).points;
                for (int c = 0; c < iterator.size() - 1; c++) {
                    x = (int) iterator.get(c).getX();
                    y = (int) iterator.get(c).getY();
                    x2 = (int) iterator.get(c + 1).getX();
                    y2 = (int) iterator.get(c + 1).getY();
                    g.drawLine(x, y, x2, y2);
                }
            }
        }

        class Curves {

            private ArrayList<Point> points;

            public Curves(ArrayList<Point> points) {
                this.points = new ArrayList<Point>();
                this.points = points;
            }
        }
    }
}