我有一个问题。我第一次使用java创建这个迷宫游戏,
我有很多很多graphics(g)
通过自己的类绘制的绘图/对象。
我想要得到的是X
和Y
协调其中之一。
public static int
在这种情况下,如果没有创建与绘图相同数量的整数,似乎很难使用。
所以,我需要用一些东西来代替 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();
}
}
在此代码中您可能需要了解的信息:
-
rektangel(the name)
= 英语中的矩形。 -
cirkel
是一个对象/绘图==>我只是使用了static int
cx
&cy
让这个人看到它的坐标。 - 上面写着
i<42;
==>表明它有很多绘制的对象。 (static int
不是最佳选择)
============已解答======已解答=======已解答=======已解答========== 抱歉,但由于某种原因我无法评论您的答案。结果真是可笑至极。我目前正在熬夜完成我的作业代码。感谢您的解决方案和 MadProgrammers 的评论。这是我的矩形类:
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;
}
世界上最简单的事情..我尝试过使用 rectangle[i].x
在之前的代码中,但没有工作。知道为什么?因为我写的是 private 而不是 public x & y。很抱歉给您带来不便,再次感谢!
最佳答案
您应该维护每个对象本身的位置和大小,并提供 getX
、getY
和 getWidth
和 getHeight
每个方法,这样,他们就可以 self 管理,知道他们在哪里以及他们应该有多大......
关于java - 从图形对象获取 X 和 Y 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19308237/