Reputation: 357
I have a list containing lines that I draw on gui. I want to be able to select a line and delete it by clicking on the line. I can select and delete but the issue is the repaint method erase all the drawn lines when I do that. When I start drawing a new line, the lines minus the previously deleted line reappear.
I feel like it might be because of the iterator but I'm not entirely sure. Can anyone help with a workaround for this? Or am I doing it wrong?
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class LinesDemo extends JPanel {
private List<Line2D> linesList = new ArrayList<>();
private List<Line2D> lineCollection = new ArrayList<>();
private Line2D line = null;
Point pointStart = null;
Point pointEnd = null;
// Width and height of rectangular region around mouse
// pointer to use for hit detection on lines
private static final int HIT_BOX_SIZE = 4;
/**
* Create the panel.
*/
public LinesDemo() {
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
pointStart = e.getPoint();
line = new Line2D.Double(e.getPoint(), e.getPoint());
linesList.add(line);
}
@Override
public void mouseReleased(MouseEvent e) {
pointStart = null;
}
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
getClickedLine(x, y);
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
pointEnd = e.getPoint();
}
@Override
public void mouseDragged(MouseEvent e) {
pointEnd = e.getPoint();
line.setLine(pointStart, pointEnd);
lineCollection.add(line);
repaint();
}
});
}
private Shape getClickedLine(int x, int y) {
int boxX = x - HIT_BOX_SIZE / 2;
int boxY = y - HIT_BOX_SIZE / 2;
int width = HIT_BOX_SIZE;
int height = HIT_BOX_SIZE;
for(Line2D selectedLine:lineCollection) {
if (selectedLine.intersects(boxX, boxY, width, height)) {
System.out.println("intersects!");
removeLine(selectedLine);
return selectedLine;
}
}
return null;
}
private void removeLine(Line2D line) {
Iterator<Line2D> it = lineCollection.iterator();
while(it.hasNext()) {
Line2D selectedLine = it.next();
if(selectedLine.equals(line)) {
it.remove();
repaint();
}
}
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if(pointStart != null) {
g2d.setPaint(Color.RED);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for(Shape content : lineCollection){
g2d.draw(content);
}
g2d.dispose();
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Moving and Scaling");
LinesDemo m = new LinesDemo();
frame.add(m);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Upvotes: 1
Views: 4009
Reputation: 251
UPD2:
Here is your code fixed. There have been many other problems, like: you've been adding a point into a list everytime the mouse is dragged, etc. It's far from perfect yet, but enough changes already for you to analyze.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class LinesDemo extends JPanel {
private List<Line2D> linesList = new ArrayList<>();
private Line2D line = new Line2D.Double();
Point pointStart = null;
Point pointEnd = null;
// Width and height of rectangular region around mouse
// pointer to use for hit detection on lines
private static final int HIT_BOX_SIZE = 4;
/**
* Create the panel.
*/
public LinesDemo() {
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
pointStart = e.getPoint();
pointEnd = e.getPoint();
line = new Line2D.Double(pointStart, pointEnd);
linesList.add(line);
}
@Override
public void mouseReleased(MouseEvent e) {
line.setLine(pointStart, pointEnd);
pointStart = null;
}
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
getClickedLine(x, y);
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
pointEnd = e.getPoint();
}
@Override
public void mouseDragged(MouseEvent e) {
pointEnd = e.getPoint();
line.setLine(pointStart, pointEnd);
repaint();
}
});
}
private Shape getClickedLine(int x, int y) {
int boxX = x - HIT_BOX_SIZE / 2;
int boxY = y - HIT_BOX_SIZE / 2;
int width = HIT_BOX_SIZE;
int height = HIT_BOX_SIZE;
for(Line2D selectedLine:linesList) {
if (selectedLine.intersects(boxX, boxY, width, height)) {
System.out.println("intersects!");
removeLine(selectedLine);
return selectedLine;
}
}
return null;
}
private void removeLine(Line2D line) {
Iterator<Line2D> it = linesList.iterator();
while(it.hasNext()) {
Line2D selectedLine = it.next();
if(selectedLine.equals(line)) {
it.remove();
repaint();
}
}
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(Color.RED);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for(Shape content : linesList){
g2d.draw(content);
}
g2d.dispose();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Moving and Scaling");
LinesDemo m = new LinesDemo();
frame.add(m);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Original: I can't run your program, but can you try replacing
while(it.hasNext()) {
Line2D selectedLine = it.next();
if(selectedLine.equals(line)) {
it.remove();
repaint();
}
}
with
boolean repaintNeeded = false;
while(it.hasNext()) {
Line2D selectedLine = it.next();
if(selectedLine.equals(line)) {
it.remove();
repaintNeeded = true;
}
}
if (repaintNeeded) {
repaint();
}
UPD1: I was able to run your code. There are many things to improve actually. For your question specifically, these 2 things lead to your lines not being drawn:
@Override
public void mouseReleased(MouseEvent e) {
pointStart = null;
}
and
if(pointStart != null) {
So every time mouse is released, your code will not draw anything.
Upvotes: 1
Reputation: 6618
The reason the lines disappear is that you explicitly suppress their drawing in paintComponent()
:
if (pointStart != null) {
pointStart
has been set to null
in mouseReleased
, so none of the lines get drawn. Remove that check, and your code should work as you intended.
As a side note, you should create the GUI in the event dispatch thread.
Upvotes: 3