2015-04-12 135 views
0

我有一个游戏,其中包含一个球员是篮子和水果坠落的主要观点是抓住水果。我尝试获取水果的坐标和位置,但我无法使其工作。 有帮助吗?如何获取图像的坐标/ JLabel?

这是我用来尝试和获取坐标的代码,但这是行不通的,因为.getMinX将永远是零和.getMinY ..我需要另一种获得协调或水果图像的方式。

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

我认为你必须绘制这些图像,并且你的代码中必须有逻辑来定义它们的绘制位置。 – copeg

回答

0

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

下面是一个例子:

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 
    } 

}

正如你所看到的,你必须在玩家的坐标直接访问和控制。你应该为这个游戏的所有对象写一个这样的类。