Understanding-Tech
Understanding-Tech

Reputation: 57

Java Mouse - graphics String - GET mouse X and Y relative to a background image

I have been trying to make a simple tile based game and i am trying to get the mouse X and Y Co-ordinates.

I have successfully used this code to get X and Y co-ordinates

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

However, This returns the x and y co-ordinates relative to the JFrame that the mouse is in. What i am trying to do is make the x and y co-oridnates relative to the background image.

So here is some numbers and information:

Jframe: 1280x1024 px

Background image: 10000x10000 px

The background is scrolled when the player moves up,down,left,right of the jframe and i have overlayed a tile map on the background. i have created an array to store the values of each tile when the tiles are generated.

The array stores 3 values. Tile ID,X-co-ordinates,Y co-ordinates.

which is relative to the background image.

Example:

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

Background:10000x10000

TILE 45198 ------ Tile X location: 9880 ------ Tile Y location: 9976

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

So i need to get the mouse x and y in relation to the background image to find the correct tile with the mouse no matter where the jframe is.

in relation to the answer below linear interpolation. Here is the code to make it simpler

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

}

Thank you for any help in advance

Upvotes: 1

Views: 1346

Answers (2)

Fester
Fester

Reputation: 883

The way I've always solved problems like this is by getting the X & Y position of the background image and then adding that to the mouse position relative to the Jframe.

Upvotes: 2

trashgod
trashgod

Reputation: 205775

The top-level container may include irrelevant decorations. Instead, use linear interpolation relative to the enclosing panel, as shown here.

Upvotes: 3

Related Questions