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

Java鼠标-图形字符串-获取相对于背景图像的鼠标X和Y,java,swing,jframe,mouse,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和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像素

背景图像:10000x1000像素

当播放器在jframe上下左右移动时,背景会滚动,我已经在背景上覆盖了一个平铺贴图。我已经创建了一个数组来存储生成瓷砖时每个瓷砖的值

该数组存储3个值。瓷砖ID、X坐标、Y坐标

它是相对于背景图像的

示例:

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

背景:10000x1000

瓷砖45198------瓷砖X位置:9880------瓷砖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);

}
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;
}
}

BOARD.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);

}
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;
}
公共类板扩展JPanel实现ActionListener{
角色扮演者;
图像背景;
图像块;
定时器时间;
b点;
指针信息a;
整数检查=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;//存储每个磁贴的x和y值
公共委员会()
{   
player=新角色();
addKeyListener(新AL());
addMouseListener(新ALMOUSE());
设置聚焦(真);
ImageIcon i=newImageIcon(“src/images/background.jpg”);//静止图像的位置
background=i.getImage();
ImageIcon t=newImageIcon(“src/images/tile.png”);//静止图像的位置
tile=t.getImage();
时间=新计时器(5,此);
time.start();
a=MouseInfo.getPointerInfo();
b=a.getLocation();
}
已执行的公共无效操作(操作事件e){
player.move();
重新油漆();
}
公共空间涂料(图g){
超级油漆(g);
Graphics2D g2d=(Graphics2D)g;
a=MouseInfo.getPointerInfo();
b=a.getLocation();
x=(int)b.getX();
y=(int)b.getY();
g2d.drawImage(背景,1200-player.nx2,900-player.ny2,null);//绘制背景-玩家位置
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(平铺,x,y,空);
g、 抽绳(“背景X:+tilex,5,10);
g、 抽绳(背景Y:+tiley,5,20);
g、 抽绳(“鼠标X:+X,5,30”);
g、 抽绳(“鼠标Y:+Y,5,40”);
g、 抽绳(“创建的瓷砖:+ctiles,5,50”);
int=0;
矩形鼠标=新矩形(x,y,50,50);
对于(int i=0;i0&&tilex 9984){
tilex=background.getWidth(null)-background.getWidth(null);//将磁贴重置回原始x位置
tiley+=tile.getHeight(null);//添加下一个tile高度
multix[tileid][0]=tilex;//为tile ID存储X值
multix[tileid][1]=tiley;//为tile ID存储Y值
g2d.drawImage(tile,tilex-player.nx2,tiley-player.ny2,null);//绘制tile
CreatedFiles+=1;//添加磁贴
}
else if(tilex==0){
multix[tileid][0]=tilex;//为tile ID存储X值
multix[tileid][1]=tiley;//为tile ID存储Y值
g2d.drawImage(tile,tilex-player.nx2,tiley-player.ny2,null);//绘制tile
tilex+=tile.getWidth(null);//获取下一个tile
CreatedFiles+=1;//添加磁贴
}
tileid++;
}
如果(检查==0){
System.out.println(倍数长度);

对于(int m=0;m可能包括不相关的装饰。取而代之的是相对于封闭面板使用,如图所示。

可能包括不相关的装饰。取而代之的是相对于封闭面板使用,如图所示。

我一直以来解决此类问题的方法是获取背景图像的X&Y位置,然后添加t到相对于Jframe的鼠标位置。

我解决类似问题的方法是b