java – 在JPanel上绘制多行时出错

我正在尝试在JPanel上绘制多行.我的代码将每一行添加到ArrayList,然后for循环应该遍历它以绘制每一行.但我得到了这个奇怪的输出.

这是我的代码:

public class DrawPanel extends JPanel {

    /** Generated serial ID for the program. */
    private static final long serialVersionUID = 1697489704611349844L;

    /** The width of the panel. */
    private static final int WIDTH = 600;

    /** The height of the panel. */
    private static final int HEIGHT = 300;

    /** The stroke width in pixels. */
    private static final int STROKE_WIDTH = 1;

    /** x-coordinate when mouse is first clicked. */
    private int myX;

    /** y-coordinate when mouse is first clicked. */
    private int myY;

    /** x-coordinate when mouse is clicked for a second time. */
    private int myXEnd;

    /** y-coordinate when mouse clicked for a second time. */
    private int myYEnd;

    /** ArrayList of lines drawn. */
    private List<Line2D> myLines = new ArrayList<Line2D>();

    /** ArrayList of coordinates to draw with a pencil. */
    private List<MouseEvent> myPoints = new ArrayList<MouseEvent>();


    /**
     * Constructs a new ellipse panel.
     */
    public DrawPanel() {
        super();
        setBackground(Color.WHITE);
        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        addMouseListener(myMouseHandler);
        addMouseMotionListener(myMouseMotionHandler);
        setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
    }

    /**
     * MouseMotionListener for drawing a shape.
     */
    private final MouseMotionListener myMouseMotionHandler = new MouseMotionAdapter() {

        @Override
        public void mouseDragged(final MouseEvent theEvent) {
            myXEnd = theEvent.getX();
            myYEnd = theEvent.getY();
            myPoints.add(theEvent);
            repaint(); 
        }

//        @Override
//        public void mouseMoved(MouseEvent e) {          
//        }

    };

    /**
     * MouseListener for drawing a shape.
     */
    private final MouseListener myMouseHandler = new MouseAdapter() {
        @Override
        public void mousePressed(final MouseEvent theEvent) {
            myX = theEvent.getX();
            myY = theEvent.getY();
            myXEnd = theEvent.getX();
            myYEnd = theEvent.getY();
            repaint();

        }

        @Override
        public void mouseReleased(final MouseEvent theEvent) {
            myXEnd = theEvent.getX();
            myYEnd = theEvent.getY();

            myPoints.add(theEvent);
            repaint();            
        }
    };

    /**
     * Draws line with drawLine method.
     */
    @Override
    public void paintComponent(final Graphics theGraphics) {
        super.paintComponent(theGraphics);
        final Graphics2D g2d = (Graphics2D) theGraphics;

        // for better graphics display
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);

        g2d.setPaint(new Color(51, 0, 111));
        g2d.setStroke(new BasicStroke(STROKE_WIDTH));

        myLines.add(new Line2D.Double(myX, myY, myXEnd, myYEnd));

        for (Line2D l : myLines) {
            g2d.draw(l);
        }  
    }
}

这是我将光标移动到圆圈时在面板上绘制的内容.这是一串连接在中心点的线.但我希望它能够绘制多个单独的行.
[

这是我想绘制的线条类型,但是没有先前绘制线条的多条线条消失了.因此,为什么我实现了一个ArrayList来重绘线条以保持它们在面板上.

[

最佳答案 我认为线条消失的原因未在此代码中显示.这是我如何更新它.

   @Override
    public void mouseDragged(final MouseEvent theEvent) {
        myXEnd = theEvent.getX();
        myYEnd = theEvent.getY();
        repaint(); 
    }

    @Override
    public void mousePressed(final MouseEvent theEvent) {
        myX = theEvent.getX();
        myY = theEvent.getY();
    }

    @Override
    public void mouseReleased(final MouseEvent theEvent) {
        myXEnd = theEvent.getX();
        myYEnd = theEvent.getY();
        myLines.add(new Line2D.Double(myX, myY, myXEnd, myYEnd));
        repaint();            
    }
};

/**
 * Draws line with drawLine method.
 */
@Override
public void paintComponent(final Graphics theGraphics) {
    super.paintComponent(theGraphics);
    final Graphics2D g2d = (Graphics2D) theGraphics;

    // for better graphics display
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                         RenderingHints.VALUE_ANTIALIAS_ON);

    g2d.setPaint(new Color(51, 0, 111));
    g2d.setStroke(new BasicStroke(STROKE_WIDTH));

    g2d.draw(new Line2D.Double(myX, myY, myXEnd, myYEnd));

    for (Line2D l : myLines) {
        g2d.draw(l);
    }  
}

这将在两个点之间绘制一条线,并且在释放鼠标之前不会存储该线.如果您确实希望它像铅笔一样绘制,也可以在鼠标拖动方法中添加新行.

点赞