Error drawing multiple lines on JPanel

I am trying to draw some lines on JPanel. My code adds each line to ArrayList, and then the for loop should go through it to draw each line. But instead, I get this odd conclusion.

This is my code:

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);
        }  
    }
}

And this is what he draws on the panel when I move the cursor in a circle. This is a bunch of lines connected at a central point. But I want him to be able to draw several separate lines. [1]

And this is the type of line that I would like to draw, but a few of them, if the previous drawn lines did not disappear. Therefore, why did I use ArrayList to redraw the lines to keep them in the panel.

[2]

+4
source share
1 answer

, , , . .

   @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);
    }  
}

, , . , , .

+1

All Articles