0

I'm trying to do rubber banding, let me explain more, that each time I draw a line and draw another one, it clear the panel and never shows me the first one drawn, I want to save all lines drawn without using Lines2D I want to use ArrayList one for the first point and second for the second point.

Here's my code :

package rubberlines;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class RubberLinesPanel extends JPanel {
    private Point point1 = null, point2 = null;
    private ArrayList<Point> pt1 = new ArrayList<Point>();
    private ArrayList<Point> pt2 = new ArrayList<Point>();
//    private ArrayList<Point> pt3 = new ArrayList<Point>();


    RubberLinesPanel(){
        LineListener listener = new LineListener();
        addMouseListener(listener);
        addMouseMotionListener(listener);
        setBackground(Color.black);
    }

    public void paintComponent(Graphics page){
        super.paintComponent(page);
        page.setColor(Color.green);
        for(int i=0; i<pt1.size(); i++){
        if(point1 != null && point2 != null){
            page.drawLine(point1.x, point1.y, point2.x, point2.y);
        }
        }
    }

    private class LineListener implements MouseListener, MouseMotionListener{
        public void mousePressed(MouseEvent event){
            point1 = event.getPoint();
            pt1.add(point1);
        }

        public void mouseDragged(MouseEvent event){
//            point2 = event.getPoint();
//            repaint();
        }

    public void mouseClicked(MouseEvent event){}
    public void mouseReleased(MouseEvent event){
        point2 = event.getPoint();
        pt2.add(point2);
        repaint();
//            for(int i=0; i<pt2.size(); i++){
//            pt2.get(i);
//            }            
        }
    public void mouseEntered(MouseEvent event){}
    public void mouseExited(MouseEvent event){}
    public void mouseMoved(MouseEvent event){}

    }

}
Elie
  • 23
  • 4
  • You mean something like [this for example](https://stackoverflow.com/questions/22645172/java-draws-rectangle-one-way-not-both/22645343#22645343). One thing you need to know about painting (in general) is it is destructive, so you will need to ensure that when ever `paintComponent` is called, you can completely reconstruct what ever you want to paint – MadProgrammer Dec 04 '19 at 22:54
  • So based on you code, you should be walking through a single `List` of points, draw a line between them – MadProgrammer Dec 04 '19 at 22:56
  • *I want to use ArrayList one for the first point and second for the second point.* - well then you need to actually use the `get(...)` method to get each point from the ArrayList before you attempt to draw a line between the two points. – camickr Dec 04 '19 at 23:02

2 Answers2

1

I want to use ArrayList one for the first point and second for the second point.

Well then you need to actually use the get(...) method to get each point from the ArrayList before you attempt to draw a line between the two points.

Something like:

for(int i = 0; i < pt1.size(); i++)
{
    Point point1 = pt1.get(i);
    Point point2 = pt2.get(i);

    if(point1 != null && point2 != null)
    {
        page.drawLine(point1.x, point1.y, point2.x, point2.y);
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
1

So, what you basically need is a single List of points. Then, when paintComponent is called, you simply want to loop through these points, draw a line between each point...

The basic concept might look something like...

List<Point> drawPoints = new ArrayList<>(points);
Point startPoint = drawPoints.remove(0);
while (!drawPoints.isEmpty()) {
    Point nextPoint = drawPoints.remove(0);
    g2d.draw(new Line2D.Double(startPoint, nextPoint));
    startPoint = nextPoint;
}

This basically starts with an initial point, gets the next point, draws a line between them. It then assigns the next point to the initial point and repeats.

Runnable example...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new RubberLinesPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class RubberLinesPanel extends JPanel {

        private ArrayList<Point> points = new ArrayList<Point>();
        private Point dragPoint;

        RubberLinesPanel() {
            LineListener listener = new LineListener();
            addMouseListener(listener);
            addMouseMotionListener(listener);
            setBackground(Color.black);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

        @Override
        protected void paintComponent(Graphics page) {
            super.paintComponent(page);
            if (points.isEmpty()) {
                return;
            }
            Graphics2D g2d = (Graphics2D) page.create();
            g2d.setColor(Color.green);

            List<Point> drawPoints = new ArrayList<>(points);
            Point startPoint = drawPoints.remove(0);
            while (!drawPoints.isEmpty()) {
                Point nextPoint = drawPoints.remove(0);
                g2d.draw(new Line2D.Double(startPoint, nextPoint));
                startPoint = nextPoint;
            }

            if (dragPoint != null) {
                g2d.setColor(Color.LIGHT_GRAY);
                g2d.draw(new Line2D.Double(startPoint, dragPoint));
            }

            g2d.dispose();
        }

        private class LineListener extends MouseAdapter {

            @Override
            public void mousePressed(MouseEvent event) {
                points.add(event.getPoint());
                repaint();
            }

            @Override
            public void mouseDragged(MouseEvent event) {
                dragPoint = event.getPoint();
                repaint();
            }

            @Override
            public void mouseReleased(MouseEvent event) {
                points.add(event.getPoint());
                dragPoint = null;
                repaint();
            }

        }

    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366