Drawline deletes previous lines

Asked

Viewed 72 times

0

How do I draw more than one line with drawline? It always deletes the previous one when drawing a new one, but I want to keep it. The code is as follows

public class DrawLines extends JPanel{
    private final int[] origemXY = new int[2];
    private final int[] destinoXY = new int[2];
    Graphics2D g2;

    @Override
    public void paintComponent(Graphics g){
        super.paintComponents(g);
        drawLinha(g);        
    }

    public void drawLinha(Graphics g){
        g2 = (Graphics2D) g;
        g2.setStroke(new BasicStroke(2)); 
        g2.setColor(Color.red);
        g2.drawLine(origemXY[0], origemXY[1], destinoXY[0], destinoXY[1]);        
    }

    public void drawLinha(int origemXY[], int destinoXY[]){
        this.origemXY[0] = origemXY[0];
        this.origemXY[1] = origemXY[1];
        this.destinoXY[0] = destinoXY[0];
        this.destinoXY[1] = destinoXY[1]; 
        repaint();
    }
}

From my other class I send the strings in the vectors and the line is drawn, but if I send other coordinates to draw another line, the previous line is deleted. I think the repaint does this to erase what you have and draw again, but if you take it nothing is drawn. I need to draw more than one, thanks.

  • It would not be a solution to store the values obtained in the method drawLinha(int origemXY[], int destinoXY[]) in other variables? Something like origemXYAtual for the chains and origemXYAnterior to the old?

  • Yes maybe I would, but I think there are ways to keep what has already been drawn, and in my case I wish that each time called already draw

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.