Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用于循环检查通过矩形阵列列表的交点_Java_Swing_For Loop_Arraylist - Fatal编程技术网

Java 用于循环检查通过矩形阵列列表的交点

Java 用于循环检查通过矩形阵列列表的交点,java,swing,for-loop,arraylist,Java,Swing,For Loop,Arraylist,这是我在Stack上的第一个问题,因此,如果有任何关于如何下次做得更好的建议,我将不胜感激:) 我创建了一个由几个矩形组成的列表。我正在尝试循环浏览此列表,以检查列表中的矩形与此矩形上拖动并释放的JLabel之间的交点。我的做法如下: public void mouseReleased(MouseEvent e) { Component comp = (Component) e.getSource(); Point locOnScreen = e.getLocationOnScr

这是我在Stack上的第一个问题,因此,如果有任何关于如何下次做得更好的建议,我将不胜感激:) 我创建了一个由几个矩形组成的列表。我正在尝试循环浏览此列表,以检查列表中的矩形与此矩形上拖动并释放的JLabel之间的交点。我的做法如下:

public void mouseReleased(MouseEvent e) {
    Component comp = (Component) e.getSource();
    Point locOnScreen = e.getLocationOnScreen();
    int x = locOnScreen.x - initialLocOnScreen.x + initialLoc.x;
    int y = locOnScreen.y - initialLocOnScreen.y + initialLoc.y;
    boundsSet(x, y, comp);//method to limit dragging space in contentPane 

    List<Rectangle> placeHolder = new ArrayList<Rectangle>();

    placeHolder.add(leftDesk);
    placeHolder.add(leftPainting);
    placeHolder.add(underBed);
    placeHolder.add(onBed);
    placeHolder.add(centerPainting);
    placeHolder.add(window);
    placeHolder.add(wardrobe);

    for (Rectangle holder : placeHolder) {
        if (holder.intersects(comp.getBounds())) {

            JOptionPane.showMessageDialog(null, "Correct place !");
            GameStatus.points += 10;
            GameStatus.nrOfItems--;
            if (GameStatus.points == 50)
                GameStatus.level++;

        } else
            comp.setLocation(initialLoc);
    }
}
public void mouseereleased(MouseEvent e){
组件comp=(组件)e.getSource();
点位置屏幕=e.getLocationOnScreen();
int x=locOnScreen.x-initialLocOnScreen.x+initialLoc.x;
int y=locOnScreen.y-initialLocOnScreen.y+initialLoc.y;
boundsSet(x,y,comp);//在contentPane中限制拖动空间的方法
列表占位符=新的ArrayList();
占位符.add(leftDesk);
占位符。添加(左绘制);
占位符。添加(底部);
占位符。添加(在床上);
占位符。添加(中心绘制);
占位符。添加(窗口);
占位符。添加(衣柜);
用于(矩形保持架:占位符){
if(holder.intersects(comp.getBounds())){
showMessageDialog(null,“正确的位置!”);
GameStatus.points+=10;
GameStatus.nrOfItems--;
如果(GameStatus.points==50)
GameStatus.level++;
}否则
组件设置位置(初始位置);
}
}

我已经将适当的坐标设置为矩形(检查了数百次)。问题是它只检测到与列表中第一个矩形的交点。。。如果我将标签拖动到另一个放置的矩形上,它将不会检测到它。有什么想法吗?

我认为你需要按如下方式改变周期:

boolean found = false;

for (Rectangle holder : placeHolder) {
    if (holder.intersects(comp.getBounds())) {

        JOptionPane.showMessageDialog(null, "Correct place !");
        GameStatus.points += 10;
        GameStatus.nrOfItems--;
        if (GameStatus.points == 50)
            GameStatus.level++;
        found = true;
        break;
    }
}
if (!found) {
    comp.setLocation(initialLoc);
}

看起来你只需要用一个矩形来检查它,而不是所有的。<代码>……所以,建议下次如何做得更好。“”——请考虑创建和发布一个有效的。或者,你真的需要<代码>其他<代码>分支吗?我不明白它为什么在这里。在第一次检查后,这似乎会改变组件的位置。我认为您的程序会按照您告诉它的那样执行:当它与列表中的第一个矩形不相交时,标签将放在其原始位置(
else
),因此它永远不会与另一个矩形相交。试着删除这个
else
,看看它是否有效。就是这样。多么愚蠢的错误。。。非常感谢你的帮助@IQV已经解决了我的问题。我刚刚在一个括号中放了一个“else”语句,比我应该做的要早。。。谢谢你们的快速回复!