Java 如何获取图像/标签的坐标?

Java 如何获取图像/标签的坐标?,java,image,coordinates,jlabel,Java,Image,Coordinates,Jlabel,我有一个游戏,里面有一个玩家,他是一个篮子和一个掉落的水果。主要的一点是抓住水果。我试着获取水果的坐标和位置,但我无法让它工作。 有什么帮助吗 这是我用来尝试获取坐标的代码,但由于.getMinX始终为零,而.getMinY。。我需要另一种获得协调或水果图像的方法 public static void checkCollision(BufferedImage player) { double fruitOneLocation = image.getMinX() &

我有一个游戏,里面有一个玩家,他是一个篮子和一个掉落的水果。主要的一点是抓住水果。我试着获取水果的坐标和位置,但我无法让它工作。 有什么帮助吗

这是我用来尝试获取坐标的代码,但由于.getMinX始终为零,而.getMinY。。我需要另一种获得协调或水果图像的方法

public static void checkCollision(BufferedImage player)
    {
        double fruitOneLocation = image.getMinX() & image.getMinY();
        double playerLocation = player.getMinX() & player.getMinY();

如果您正在编写游戏,您的玩家(和其他游戏对象)不能仅用图像对象表示。您应该为此编写自己的类

下面是一个例子:

import java.awt.*;
public class Player {
  //X- and Y-Coordinates
  public int x,y;

  //Player graphic
  Image img=Toolkit.getDefaultToolkit().getImage("path.to.your.image.file");

  public Player(){

  }
  //Rendering method
  //You should call this in your frame class where you override paint(Graphics g)
  //or paintComponent(Graphics g)
  //
  //like this: player.draw(g,this)
  public void draw(Graphics g,JPanel jp){
    //Draw the Image at the right position
    g.drawImage(img,x,y,jp);
  }

  //Get X coordinate
  public int getX(){
    return x;
  }

  //Get Y coordinate
  public int getY(){
    return y;
  }

  //Update method
  //You should call this in your game loop
  public void update(){
    //Handle button presses, collision testing or other stuff relating
    //the player object here
  }
}


如您所见,您可以直接访问和控制玩家的坐标。您应该为所有游戏对象编写这样的类。

我猜您必须绘制这些图像,并且代码中必须有逻辑来定义它们的绘制位置?