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