0

I am writing a Shikaku-Game in Java. In that Shikaku-Game you have to draw rectangles around the shown JLabels (JLabels use an image of the class ViewIcon, that implements the interface icon). Everytime when you draw a new rectangle that intersects an existing one or goes completely over an existing one, the existing rectangle should be deleted and the new one must not be painted. Unfortunatelly now I have the following method that is used in the MouseReleased-Method of my Mouseadapter. Existing rectangles are collected in the ArrayList rectangles. The "not so good looking" if-clause checks all the possibilities how a new rectangle can intersect an existing one.

I put already rectangles.size() in the method to check if the ArrayList clears it's elements, but it doesn't. The amount just raises, but it should normally show as size the amount of rectangles that are in that moment in the game.

Sometimes existing rectangles do disappeare, sometimes just partly, sometimes I can't draw a rectangle on a place where a rectangle alreasy disappeared... I can't understand yet what when happens.

Another information that you should know is what the method drawRectangle(boolean b) does: It takes coordPressed as the top left corner and coordReleased as the buttom right corner (if in the game a rectangle is drawn by the user e.g. from buttom left to top right the method standardizes at first "from top left to buttom right"). After that it finds out (by using the Coordinte-methods getX() and getY()) which JLabel/ViewIcons are the edge ones in that rectangle and puts in these ViewIcons e.g. topLeftLinePainted on true or false, depending on if it should be shown (creation of a rectangle) or hidden (deletion of a rectangle). Creation works perfect, as long as it doesn't intersect another rectangle.

private ArrayList<PaternRectangle> rectangles = new ArrayList();
private Coordinate coordPressed;
private Coordinate coordReleased;   

public void drawOrDeleteRectangle() {

        Iterator<PaternRectangle> itrPatRect = rectangles.iterator();

        boolean rectIntersectionDetected = false;
        int itrPatRectIndexCounter = 0;
        Coordinate coordPressedBackUp = coordPressed;
        Coordinate coordReleasedBackUp = coordReleased;

        while (itrPatRect.hasNext()) {

            PaternRectangle tempRect = itrPatRect.next();

            if (coordReleased.getX() <= tempRect.getCoordButtomRight().getX()
                && coordReleased.getY() <= tempRect.getCoordButtomRight().getY()
                && coordReleased.getX() >= tempRect.getCoordTopLeft().getX()
                && coordReleased.getY() >= tempRect.getCoordTopLeft().getY()
            ||     coordPressed.getX() <= tempRect.getCoordButtomRight().getX()
                && coordPressed.getY() <= tempRect.getCoordButtomRight().getY()
                && coordPressed.getX() >= tempRect.getCoordTopLeft().getX()
                && coordPressed.getY() >= tempRect.getCoordTopLeft().getY()
            ||     coordPressed.getX() <= tempRect.getCoordTopLeft().getX()
                && coordPressed.getY() <= tempRect.getCoordTopLeft().getY()
                && coordReleased.getX() >= tempRect.getCoordButtomRight().getX()
                && coordReleased.getY() >= tempRect.getCoordButtomRight().getY()
            ||     coordPressed.getX() >= tempRect.getCoordButtomRight().getX()
                && coordPressed.getY() <= tempRect.getCoordTopLeft().getY()
                && coordReleased.getX() <= tempRect.getCoordTopLeft().getX()
                && coordReleased.getY() >= tempRect.getCoordButtomRight().getY()            
            ||     coordPressed.getX() >= tempRect.getCoordButtomRight().getX()
                && coordPressed.getY() >= tempRect.getCoordButtomRight().getY()
                && coordReleased.getX() <= tempRect.getCoordTopLeft().getX()
                && coordReleased.getY() <= tempRect.getCoordTopLeft().getY()
            ||     coordPressed.getX() <= tempRect.getCoordTopLeft().getX()
                && coordPressed.getY() >= tempRect.getCoordButtomRight().getY()
                && coordReleased.getX() >= tempRect.getCoordButtomRight().getX()
                && coordReleased.getY() <= tempRect.getCoordTopLeft().getY()) {

                coordPressed = tempRect.getCoordTopLeft();
                coordReleased = tempRect.getCoordButtomRight();
                drawRectangle(false);
                rectIntersectionDetected = true;
                rectangles.remove(itrPatRectIndexCounter);
                break;
            }

            itrPatRectIndexCounter++;   
        }

        if (!rectIntersectionDetected) {

            coordPressed = coordPressedBackUp;
            coordReleased = coordReleasedBackUp;
            drawRectangle(true);
            PaternRectangle newRectangle = new PaternRectangle(coordPressed, coordReleased);
            rectangles.add(newRectangle);
            System.out.println("Test");
        }
    }

I hope that you can help me in deleting the rectangles on a correctly manner, so that they just disappear when they are intersected by a new one...

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Kawa
  • 11
  • 2
  • Have you tried your code instead with `itrPatRect .remove();`? – SubOptimal Apr 10 '15 at 06:09
  • 1
    For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Apr 10 '15 at 06:09
  • 3
    *"The "not so good looking" if-clause checks all the possibilities how a new rectangle can intersect an existing one."* Form a `Shape` of each rectangle and it becomes easy. See [Collision detection with complex shapes](http://stackoverflow.com/a/14575043/418556) for a working example. – Andrew Thompson Apr 10 '15 at 06:11
  • That `if` clause check looks really messy. Maybe that's your problem ? – Jonas Czech Apr 10 '15 at 06:25
  • When dealing with `ArrayList`s and messing up it's contents, I usually go with the `for` loop approach starting from `i = arraylist.size()`. Try that instead of the `iterator`. Then just do `arrayList.remove(i);` when needed – jmcg Apr 10 '15 at 07:02
  • @AndrewThompson I dont think that this would work easily,the ViewIcons are arrangend in its Jpanel in a Gridlayout, so I would have to put one JPanel over the other, I didnt find a way yet how to do this.. – Kawa Apr 10 '15 at 07:04
  • @SubOptimal not yet, but I want to remove an element from rectangles, so maybe you mean rectangles.remove(tempRect) ? – Kawa Apr 10 '15 at 07:21
  • @Kawa If I right understand what you try to do. You want to remove the current processed element from `rectangles`. As you are traversing the list by it's iterator it makes more sense to me to call `remove()` on the iterator, which would remove the current processed element. Instead of manually compute the current element index of the list. – SubOptimal Apr 10 '15 at 07:24
  • *"I dont think that this would work easily,"* It worked pretty easily in the example I linked, but.. *"the ViewIcons are arrangend in its Jpanel in a Gridlayout, so I would have to put one JPanel over the other, I didnt find a way yet how to do this.."* ..that just reads like nonsense to me. **Post an MCVE.** – Andrew Thompson Apr 10 '15 at 07:25
  • @SubOptimal and this removes it automatically from the Arraylist? If yes than this is a very good idea.. I'm still trying to save the index manually by the counter. Unfortunatelly I don't have my Laptop with me now but I'll try it later – Kawa Apr 10 '15 at 07:28
  • @AndrewThompson okay, sorry for that code. I'll post one when I have my code with me – Kawa Apr 10 '15 at 07:30

1 Answers1

0

Sorry buddies for the circumstances... I was creating an additional rectangle in my drawRectangle-Method and was adding this to my rectangles. I deleted that both lines and now it works fine...

Kawa
  • 11
  • 2