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. 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!
您应该保持每个对象在内部的位置和大小,并为每个对象提供一个getX
, getY
, getWidth
和getHeight
方法,这样它们就可以自我管理,知道它们在哪里以及应该有多大。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.