im 尝试在网格上随机单击的点和点列表中的给定点之间绘制垂直和水平线。我的新手风格编码似乎对应用程序造成了影响,该应用程序在我单击网格上的随机点后会崩溃。然后它抛出以下 OOME。到目前为止,我已经尝试增加堆大小。这根本无济于事..任何想法??这是导致它的代码片段。到目前为止,我只为列表中某个点以东的 Click 事件编写了代码
@Override
public void mouseClicked(MouseEvent e) {
boolean crossingLines = false;
Line l;
Point p = new Point(e.getX(), e.getY());
for (int i = 0; i < pointList.size(); i++) {
Rectangle rec = new Rectangle(pointList.get(i).x
* GAPBETWEENPOINTS
+ GAP
- TOLERANCE, pointList.y
* GAPBETWEENPOINTS
+ GAP
- TOLERANCE,TOLERANCE * 2,
CLICKTOLERANCE * 2);
if (rec.contains(p)) {
if (clickedEast(p, pointList.get(i)) == true) {
for (int j = i + 1; j < pointList.size(); j++) {
if (pointList.get(j).y
* GAPBETWEENPOINTS
+ GAP == pointList.get(i).y
* GAPBETWEENPOINTS
+ GAP
&& pointList.get(j).x
* GAPBETWEENPOINTS
+ GAP > pointList.get(i).x
* GAPBETWEENPOINTS
+ GAP) {
l = new Line(pointList.get(i),
pointList.get(j));
if (!lineList.contains(b)) {
for (int k = 0; k < lineList.size(); k++) {
if (lineList.get(k).getHorizontal() == false) {
if (pointList.get(i).x
* GAPBETWEENPOINTS
+ GAP < lineList
.get(k).getStartPoint().x
&& lineList.get(k)
.getStartPoint().x < pointList.get(j).x
* GAPBETWEENPOINTS
+ GAP
&& lineList.get(k)
.getStartPoint().y < pointList.get(i)).y
* GAPBETWEENPOINTS
+ GAP
&& pointList.get(k).y
* GAPBETWEENPOINTS
+ GAP < lineList
.get(k).getEndPoint().y) {
crossinglines = true;
}
}
if (crossingLines == false) {
lineList.add(b);
}
}
}
validate();
repaint();
}
}
}
}
}
}
这是例外:
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java
heap space
at java.util.Arrays.copyOf(Arrays.java:2245)
at java.util.Arrays.copyOf(Arrays.java:2219)
at java.util.ArrayList.grow(ArrayList.java:242)
at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:216)
at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:208)
at java.util.ArrayList.add(ArrayList.java:440)
at GUIs.GameField.mouseClicked(GameField.java:326)
at java.awt.Component.processMouseEvent(Component.java:6519)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4501)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at
java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.
java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.
java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.
java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
问题似乎出在GameField.mouseClicked()
内最内层的循环中。当crossingLines
计算结果为false
时,你可能会将很多项目添加到lineList
中,因为lineList.add()
在lineList
本身的循环中。因此,似乎crossingLines
目前从未评估为true
并且列表不断扩展,直到内存不足。
要解决此问题,您应该以某种方式确保lineList.add()
不会连续调用。此外,我建议您重新考虑mouseClicked()
的结构。目前,该方法中有三个 (!) 嵌套的 for 循环,并且还在中间循环中调用 repaint()
,这可能会导致一些不必要的闪烁,具体取决于pointList
的大小。