Question

I have a problem. I'm creating this maze game using java for the very first time, and I have lots and lots of graphics(g) drawings/objects drawn through their own class.

What I want to get is the X and Y coordinate out of one of them.

public static int seems too hard to use in this case, without created an equal number of integers as there are drawings.

So, I need something to use instead of getY():

public void keyPressed(KeyEvent e){
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_LEFT) {
        for (int i=0; i<=42; i++) {
            if(cx != rektangel[i].getY())  // I tried "getY()" with fail(kind of dumb since it's used for mouselistener?

            cirkel[0].move(-10, 0);
            this.cx += -10;
            repaint();


    }
}

Information you might need to know in this code:

  • rektangel(the name) = rectangle in english.
  • cirkel is one object/drawing ==> I simply used a static int cx & cy for this guy to see it's coordinates.
  • Where it says i<42; ==> shows that it's ALOT of drawn objects. (static int won't be optimal)

=============ANSWERED======ANSWERED=======ANSWERED=======ANSWERED========== Sorry, but for some reason I can't comment your answers. The result is ridiculously laughable. I'm currently staying up late to finish my code for a homework I have. Thanks for your solution, and MadProgrammers comment. Here's my rectangle-class:

public class Rektangel {
   private int width,height;
   private int x,y; <---this row private
   Color c;
   public static int karta = 1;
   public static int rx = 0;
   public static int ry = 0;
}

The easiest thing in the world.. I had tried using rectangle[i].x in the previous code, but didn't work. Know why? Because I wrote private instead of public x & y. Sorry for the inconvenience and once again, Thanks!

Was it helpful?

Solution

You should maintain the position and size of each object within itself and provide a getX, getY and getWidth and getHeight method for each, this way, they become self managing, knowing where they are and how big they should be...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top