Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7762771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:29:35+00:00 2026-06-01T14:29:35+00:00

I am trying to do mouse picking and the tile I click on changes

  • 0

I am trying to do mouse picking and the tile I click on changes to whatever the opposite tile is ie. grass to dirt, but every grass tile has the same “ID” so every grass tile on the screen changes to dirt. How can I go about generating these tiles in a better way? I want it to be randomly generated and not drawn from an array map of like 000001100.

Block class

public class Block {

public enum BlockType {
    Dirt,
    Grass,
    Selection
}

BlockType Type;
Vector2f Position;
Image texture;
boolean breakable;

public Block(BlockType Type, Vector2f Position, Image texture, boolean breakable) {
    this.Type = Type;
    this.Position = Position;
    this.texture = texture;
    this.breakable = breakable;
}

    public BlockType getType() { 
        return Type; 
    }
    public void setType(BlockType value) {
        Type = value;
    }

    public Vector2f getPosition() { 
        return Position; 
    }
    public void setPosition(Vector2f value) { 
        Position = value; 
    }

    public Image gettexture() { 
        return texture; 
    }
    public void settexture(Image value) { 
        texture = value; 
    }

    public boolean getbreakable() { 
        return breakable; 
    }
    public void setbreakable(boolean value) { 
        breakable = value; 
    }
}

Tile Generation Class

public class TileGen {

Block block;
public Block[] tiles = new Block[2];
public int width, height;
public int[][] index;
boolean selected;
int mouseX, mouseY;
int tileX, tileY;

Image dirt, grass, selection;
SpriteSheet tileSheet;

public void init() throws SlickException {
    tileSheet = new SpriteSheet("assets/tiles/tileSheet.png", 64, 64);

    grass = tileSheet.getSprite(0,0);
    dirt = tileSheet.getSprite(1,0);
    selection = tileSheet.getSprite(2,0);

    tiles[0] = new Block(BlockType.Grass, new Vector2f(tileX,tileY), grass, true);
    tiles[1] = new Block(BlockType.Dirt, new Vector2f(tileX,tileY), dirt, true);

    width = 50;
    height = 50;

    index = new int[width][height];

    Random rand = new Random();
    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < height; y++)
        {
            index[x][y] = rand.nextInt(2);
        }
    }
}

public void update(GameContainer gc) {
    Input input = gc.getInput();
    mouseX = input.getMouseX();
    mouseY = input.getMouseY();
    tileX = mouseX / width;
    tileY = mouseY / height;

    if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
        selected = true;
    }
    else{
        selected = false;
    }
    System.out.println(tiles[index[tileX][tileY]]);
}

public void render() {
    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < height; y++)
        {
            tiles[index[x][y]].texture.draw(x * 64, y *64);

            if(IsMouseInsideTile(x, y))
                selection.draw(x * 64, y * 64);
            if(selected && tiles[index[x][y]].breakable) {
                if(tiles[index[tileX][tileY]].Type == BlockType.Grass)
                    tiles[index[tileX][tileY]].texture = dirt;
            }
        }
    }
}

public boolean IsMouseInsideTile(int x, int y)
{
    return (mouseX >= x * 64 && mouseX <= (x + 1) * 64 &&
            mouseY >= y * 64 && mouseY <= (y + 1) * 64);
}

I am using slick2d library. I’m not sure if ID is the right word, but I hope you can understand what I am trying to ask.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:29:37+00:00Added an answer on June 1, 2026 at 2:29 pm

    It looks like your existing structure is fine, but it doesn’t look like you understand what it does. The int[][] index array holds a grid of tile types, corresponding to x and y coordinates. To change the tile type at a particular coordinate, all you need to do is set the index array to the type you want.

    Specifically, in your render function, you would have something like:

    if(IsMouseInsideTile(x, y) && selected && tiles[index[x][y]].breakable)
        if(tiles[index[tileX][tileY]].Type == BlockType.Grass)
            tiles[index[tileX][tileY]].texture = dirt;
    

    I’d try to figure out exactly what the code you have is doing before modifying it further.

    Note: why is this in the render function anyways? You should probably have it in its own function or at least inside your update function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use pyhooks to detect mouse clicks anywhere on screen. The problem
I'm trying to move an object on the click of a mouse while the
I'm trying to get the mouse move event from external applet that has been
I'm trying to be able to have a mouse click on current mouse position.
I'm trying to simulate mouse click at window with JNA. public class App {
I'm trying to display mouse coordinates (math coordinates) in my JPanel , but i
I'm trying to get a mouse click event in Qt, this mouse click must
I was trying to create a mouse clicked event of a JmenuItem but it
I'm trying to handle mouse clicks in an application, but the logic is becoming
I'm trying to catch a mouse-click even on a Table (which should cause a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.