0

I have implemented the DDA algorithm to draw a line in Canvas. This is the listener for drawing the line:

panel.addMouseMotionListener(new MouseAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            final double startX = previousX / (panel.getWidth() - 1.0); //k zamysleni: proc 1.0 a ne 1?
            final double startY = 1 - previousY / (panel.getHeight() - 1.0);
            final double endX = e.getX() / (panel.getWidth() - 1.0);
            final double endY = 1 - e.getY() / (panel.getHeight() - 1.0);
            clear(); // zkuste zakomentovat
            rasterImage = liner.rasterizeLine(rasterImage,
                    startX, startY, endX, endY,
                    0xffff00);
            panel.repaint();
        }
    });

It works perfectly but everytime I draw another line, the first line dissappear because of the clear() method. I need to keep the start and end points in a list so I could redraw the line after clearing. However I don't know exactly how to do that. How to keep the 4 points in a list?

  • The accepted answer here should help you : https://stackoverflow.com/questions/5801734/how-to-draw-lines-in-java – Arnaud Oct 31 '17 at 09:15
  • @Berger In the answer, he was able to make a list of Lines. However I don't have any Line Class. How to store the line in my case please? – pajicekkralicek Oct 31 '17 at 10:07
  • Just copy the Line class from the answer and use it . – Arnaud Oct 31 '17 at 10:13
  • Isn't there some other way? Like storing just the points? @Berger – pajicekkralicek Oct 31 '17 at 10:35
  • The problem is that you want to store _pairs_ of points. If you don't want to create a wrapper like a Line object representing a pair of points, you may try a list of entries like `List> = new ArrayList<>()` . – Arnaud Oct 31 '17 at 10:40

0 Answers0