Hi Forum
Auf einer Fläche habe ich viele schwarze Kreise mit einem Durchmesser von 20 px. Diese gemalten Punkte stecken in einer ArrayList vom Typ Point. Per Klick möchte ich nun einen Kreis löschen, sofern der Klick innerhalb der Kreisfläche stattgefunden hat. Ich wollte fragen warum das bei mir nicht klappt, sondern in einer Exception Meldung endet.
Vielleicht hilft auch die Exception-Meldung?
Wie immer danke ich für Eure Unterstützung.
EDIT: Einen HInweis habe ich schon. Aber ich weiß nicht wie man damit umgehen soll um das zu lösen:
Viele Grüße,
Semo
Auf einer Fläche habe ich viele schwarze Kreise mit einem Durchmesser von 20 px. Diese gemalten Punkte stecken in einer ArrayList vom Typ Point. Per Klick möchte ich nun einen Kreis löschen, sofern der Klick innerhalb der Kreisfläche stattgefunden hat. Ich wollte fragen warum das bei mir nicht klappt, sondern in einer Exception Meldung endet.
Java:
import java.util.ArrayList;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JPanel;
/**
* Node ist eine Unterklasse von MyGraphEditor, mit dem im Program die Knoten
* gezeichnet, geschoben und geloescht werden koennen (sollen)
*
* @author carino
* @version 1.6.0_20-b02
*
*/
public class NodePanel extends JPanel {
private static final long serialVersionUID = 6887540707408099142L;
private ArrayList<Point> AllNodes;
Point p;
final int dia = 20;
public NodePanel() {
super();
setBackground(Color.WHITE);
AllNodes = new ArrayList<Point>();
this.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
if (Contents.selectedItem == 2) {
if (AllNodes.contains(e.getPoint())) {
// e.translatePoint(5, 5);
// System.out.println(p.toString());
// repaint();
}
}
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseClicked(MouseEvent e) {
if (Contents.selectedItem == 1) {
p = e.getPoint();
AllNodes.add(p);
System.out.println(p.toString());
paintComponent(getGraphics());
} else if (Contents.selectedItem == 3) {
p = e.getPoint();
for (Point q : AllNodes) {
if (p.distance(q) <= dia) {
AllNodes.remove(q);
paintComponent(getGraphics());
}
}
}
}
});
}
/*
* Diese Methode zeichnet die Kreise in die Komponente JPanel
*
* @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
try {
g2D.setColor(Color.BLACK);
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (Point p : AllNodes) {
g2D.fillOval(p.x - 10, p.y - 10, dia, dia);
}
} finally {
g2D.dispose();
}
}
}
Vielleicht hilft auch die Exception-Meldung?
Java:
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at NodePanel$1.mouseClicked(NodePanel.java:70)
at java.awt.Component.processMouseEvent(Component.java:6266)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4247)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Wie immer danke ich für Eure Unterstützung.
EDIT: Einen HInweis habe ich schon. Aber ich weiß nicht wie man damit umgehen soll um das zu lösen:
For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances. Some Iterator implementations (including those of all the general purpose collection implementations provided by the JRE) may choose to throw this exception if this behavior is detected. Iterators that do this are known as fail-fast iterators, as they fail quickly and cleanly, rather that risking arbitrary, non-deterministic behavior at an undetermined time in the future.
Viele Grüße,
Semo