Java鼠标 - 图形字符串 - 相对于背景图像获取鼠标X和Y.

时间:2013-12-29 17:14:28

标签: java swing jframe mouse

我一直试图制作一个简单的基于平铺的游戏,我试图获得鼠标X和Y坐标。

我已成功使用此代码获取X和Y坐标

   Point b;
   PointerInfo a;
   int x = 0;
   int y = 0;
   a = MouseInfo.getPointerInfo();
    b = a.getLocation();
    x = (int) b.getX();
    y = (int) b.getY();

    g.drawString("Mouse X: " + x, 5, 30);
    g.drawString("Mouse Y: " + y, 5, 40);

但是,这将返回相对于鼠标所在的JFrame的x和y坐标。我要做的是使x和y相对于背景图像进行协调。

所以这里有一些数字和信息:

Jframe:1280x1024 px

背景图片:10000x10000像素

当玩家向上,向下,向左,向右移动jframe并且我在背景上叠加了一个瓦片地图时,滚动背景。我创建了一个数组,用于在生成切片时存储每个切片的值。

该数组存储3个值。平铺ID,X坐标,Y坐标。

相对于背景图片。

示例:

/////////////////////////////////////////////// ///////////////////////////////

背景:10000x10000

TILE 45198 ------ Tile X位置:9880 ------ Tile Y位置:9976

/////////////////////////////////////////////// ////////////////////////////////

所以我需要获得与背景图像相关的鼠标x和y,无论jframe在哪里,都可以用鼠标找到正确的图块。

与线性插值下面的答案有关。这是使代码更简单的代码

frame.java

/*  NOTES
 * 
 *  10000/10000 pixel map
 *  
 *  232 tiles vertical
 *  192 tiles horizontal
 * 
 *  tile Width: 43
 *  tile Height: 52
 */
import javax.swing.JFrame;

public class Frame {
public static void main(String[] args) {
    JFrame frame = new JFrame("Isometric Game");
    frame.add(new Board());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1280,1024);
    frame.setVisible(true);

}

}

BOARD.java

public class Board extends JPanel implements ActionListener {

Character player;
Image background;
Image tile;
Timer time;
Point b;
PointerInfo a;
int check = 0;
int x = 0;
int y = 0;
int maxtiles = 100002;
int createdtiles = 0;
int ctiles = 0;
int tilex;
int tiley;
int tileid;
public static int[][] multix; // Stores the x and y Values for each tile
public Board()
{   
    player = new Character();
    addKeyListener(new AL());
    addMouseListener(new ALMOUSE());
    setFocusable(true);
    ImageIcon i = new ImageIcon("src/images/background.jpg"); //the location of the still image
    background = i.getImage();
    ImageIcon t = new ImageIcon("src/images/tile.png"); //the location of the still image
    tile= t.getImage();
    time = new Timer(5,this);
    time.start();
    a = MouseInfo.getPointerInfo();
    b = a.getLocation();


}

public void actionPerformed(ActionEvent e) {
    player.move();
    repaint();

}

public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    a = MouseInfo.getPointerInfo();
    b = a.getLocation();
    x = (int) b.getX();
    y = (int) b.getY();

    g2d.drawImage(background, 1200-player.nx2, 900-player.ny2, null); //Draw Background - players positions
    GetTiles(g2d);

    g2d.drawImage(player.getImage(),10, 10,null);
    tilex = background.getWidth(null) - background.getWidth(null);
    tiley = background.getHeight(null) - background.getHeight(null);

    g2d.drawImage(tile,x, y,null);
    g.drawString("Background X : " + tilex, 5, 10);
    g.drawString("Background Y : " + tiley, 5,20);
    g.drawString("Mouse X: " + x, 5, 30);
    g.drawString("Mouse Y: " + y, 5, 40);
    g.drawString("Created Tiles:" + ctiles, 5, 50);

    int collideget  = 0;
    Rectangle mouse = new Rectangle (x,y,50,50);

    for (int i = 0 ; i < multix.length; i++){

        Rectangle tilecollide = new Rectangle(multix[i][0],multix[i][1],52,43);

        if (mouse.intersects(tilecollide)){             
            collideget = 1;
        }

        if (collideget == 1){
            System.out.println(" MOUSE COLLIDED WITH TILEID:" + multix[i]);
            collideget = 0;
        }
    }
    }

private class AL extends KeyAdapter{
    public void keyReleased(KeyEvent e){
        player.keyReleased(e);
    }

    public void keyPressed(KeyEvent e){
        player.keyPressed(e);
    }
}

private class ALMOUSE extends MouseAdapter{
    public void mouseEntered(MouseEvent e){
        player.mouseEntered(e);
    }

    public void mouseExited(MouseEvent e){
        player.mouseExited(e);
    }
}

public void GetTiles(Graphics2D g2d){


    multix = new int[maxtiles][2];  // Stores the x and y Values for each tile
    tileid = 0;

    tilex = background.getWidth(null) - background.getWidth(null);
    tiley = background.getHeight(null) - background.getHeight(null);


        for (int h = 0; h <= 100000 ; h++) // Create the row of tiles
        {


            if (tiley > 9976){

                h = 100001;
            }
            else if (tilex > 0 && tilex <= 9984){


                multix[tileid][0] = tilex; // Store X value for tile ID
                multix[tileid][1] = tiley; // Store Y value for tile ID


                g2d.drawImage(tile,tilex - player.nx2, tiley  - player.ny2,null); // Draw Tile

                tilex += tile.getWidth(null); //get next tile

                createdtiles += 1; //ADD TILE
            }

            else if (tilex > 9984){

                tilex = background.getWidth(null) - background.getWidth(null); // Reset the tile back to the original x position
                tiley += tile.getHeight(null);  //Add the next tile height

                multix[tileid][0] = tilex; // Store X value for tile ID
                multix[tileid][1] = tiley; // Store Y value for tile ID

                g2d.drawImage(tile,tilex - player.nx2, tiley - player.ny2,null); // Draw Tile


                createdtiles += 1; //ADD TILE
            }

            else if (tilex == 0){

                multix[tileid][0] = tilex; // Store X value for tile ID
                multix[tileid][1] = tiley; // Store Y value for tile ID

                g2d.drawImage(tile,tilex - player.nx2, tiley  - player.ny2,null); // Draw Tile

                tilex += tile.getWidth(null); //Get next tile

                createdtiles += 1; //ADD TILE
            }
            tileid++;
        }


    if (check == 0){
        System.out.println(multix.length);
        for (int m = 0; m <= createdtiles; m++)
        {
            System.out.println( "TILE " + m + "   ------  Tile X location: " + multix[m][0]  + "   ------  Tile Y location: " + multix[m][1] );
        }
        check = 1;
        System.out.println("MAX # OF TILES:" + maxtiles);
        System.out.println("CREATED # OF TILES:" + createdtiles);
        ctiles = createdtiles;

    }

}

public int[][] getArray(){
    return multix;
}

}

感谢您提前提供任何帮助

2 个答案:

答案 0 :(得分:3)

top-level container可能包含不相关的装饰品。相反,使用linear interpolation相对于封闭的面板,如图here所示。

答案 1 :(得分:2)

我总是解决这样的问题的方法是让X&amp; Y背景图像的位置,然后将其添加到相对于Jframe的鼠标位置。