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 6093791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:36:06+00:00 2026-05-23T12:36:06+00:00

I’m creating a map editor for a little game project that I’m doing. Considering

  • 0

I’m creating a map editor for a little game project that I’m doing. Considering that the map editor isn’t going to be intense, I simply used java 2d, which I hate. Anyways, here is my map.java, tile.java, and my TileList.java code.

FIXED, I modified my TileList.java code (set function) to this: Alright, I fixed it: I simply changed the set(Tile tile) function!

public void set(Tile tile) {
    for(int i = 0; i < this.tileList.length; i++) {
        int x = this.tileList[i].getX();
        int y = this.tileList[i].getY();
        if((x == tile.getX()) && (y == tile.getY())) {
            System.out.println("Changing tile: (" + x + "," + y + ")" + " with (" + tile.getX() + "," + tile.getY() + ")");
            this.tileList[i].setImage(tile.getImage());
        }
    }
}

Image showing error: https://i.stack.imgur.com/XjYE9.png

map.java:

package org.naive.gui.impl;

import org.naive.util.TileList;

import javax.swing.JPanel;
import java.util.HashMap;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Rectangle;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * Copyright 2011 Fellixombc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
public class Map extends JPanel implements MouseListener {

    private final int tileSize;
    private final int mapSize;
    private final int size;
    private TileList tileSet;
    private Tile currentTile = null;

    /* Creates the Map, e.g, Panel
     * @param int Desired size (in tiles) of the map
     */
    public Map(int size, int tileSize) {
        this.tileSize = tileSize / 2;
        this.size = size;
        this.mapSize = (this.tileSize)*(size/2);
        this.tileSet = new TileList(size/2 * size/2);
        properties();
    }

    /* Initlize the properties for the JPanel
     */
    public void properties() {
        this.setPreferredSize(new Dimension(mapSize, mapSize));
        this.addMouseListener(this);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D gfx = (Graphics2D) g;
        for(int i = 0; i < this.tileSet.size; i++) {
            Tile tile = this.tileSet.get(i);
            gfx.drawImage(tile.getImage(), tile.getX(), tile.getY(), null);
        }
        for(int i = 0; i <= size/2; i++) {
            gfx.setColor(Color.BLACK);
            gfx.drawLine(i * this.tileSize, 0, i * this.tileSize, this.tileSize * this.size/2);
            gfx.drawLine(0, i * this.tileSize, this.tileSize * this.size/2, i * this.tileSize);
        }
    }

    public void populate() {
        int i = 0;
        for(int x = 0; x < size/2; x++) {
            for(int y = 0; y < size/2; y++) {
                Tile tile = new Tile("grass.png");
                tile.setPos(x * this.tileSize, y * this.tileSize);
                this.tileSet.setAtIndex(i, tile);
                i++;
            }
        }
    }

    /* Sets a new tile
     * @param tile The *new* tile to be set
     */
    public void setTile(Tile tile) {
        if(this.currentTile != null) {
            tile.setPos(this.currentTile.getX(), this.currentTile.getY());
            this.tileSet.set(tile);
            this.currentTile = tile;
        }
        this.repaint();
    }

    /* Gets the tile closest* to the mouse click
     * @param int The x-axis location of the mouse click
     * @param2 int The y-axis location of the mouse click
     */
    public void getTile(int x, int y) {
        for(int i = 0; i < this.tileSet.size; i++) {
            Tile tile = this.tileSet.get(i);
            int minX = tile.getX();
            int minY = tile.getY();
            int maxX = minX + this.tileSize;
            int maxY = minY + this.tileSize;

            if((x >= minX) && (x < maxX) && (y >= minY) && (y < maxY)) {
                this.currentTile = tile;
                System.out.println("Tile at: " + "(" + this.currentTile.getX() + "," + this.currentTile.getY() + ")");
            }
        }
    }


    public void setTileSet(TileList tileSet) {
        this.tileSet = tileSet;
    }

    /* Gets the TileList, e.g, the tiles of the 'map'
     * @return hashmap Returns the list of tiles
     */
    public TileList getTileSet() {
        return this.tileSet;
    }

    public int getMapSize() {
        return this.size;
    }

    public int getTileSize() {
        return this.tileSize * 2;
    }


    /* Gets where the mouse clicked on the canvas
     * @param mouseevent Where the mouse event occurred
     */
    public void mouseClicked(MouseEvent e) {
        this.getTile(e.getX(), e.getY());
    }

    /* Useless..
     */
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
}

Tile.java

package org.naive.gui.impl;

import javax.swing.ImageIcon;

/**
 * Copyright 2011 Fellixombc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
public class Tile extends ImageIcon {

    private int x = 0;
    private int y = 0;
    private final String sprite;

    public Tile(String sprite) {
        super("data/sprite/" + sprite);
        this.sprite = sprite;
    }

    public String getSprite() {
        return this.sprite;
    }

    public void setPos(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return this.x;
    }

    public int getY() {
        return this.y;
    }

}

TileList.java
package org.naive.util;

import org.naive.gui.impl.Tile;

import java.util.Iterator;

/**
 * Copyright 2011 Fellixombc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
public class TileList {

    public int size;
    private Tile[] tileList;

    public TileList(int size) {
        this.size = size;
        this.tileList = new Tile[size];
    }

    public void setAtIndex(int index, Tile tile) {
        this.tileList[index] = tile;
    }

    public void set(Tile tile) {
        for(int i = 0; i < this.tileList.length; i++) {
            int x = this.tileList[i].getX();
            int y = this.tileList[i].getY();
            if((x == tile.getX()) && (y == tile.getY())) {
                System.out.println("Changing tile: (" + x + "," + y + ")" + " with (" + tile.getX() + "," + tile.getY() + ")");
                this.tileList[i] = tile;
            }
        }
    }

    public Tile get(int index) {
        return this.tileList[index];
    }
}
  • 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-05-23T12:36:06+00:00Added an answer on May 23, 2026 at 12:36 pm

    One thing that might help to make it a bit clearer what exactly is going on and also more efficient in your code is to eradicate the loop within this function:

    public void set(Tile tile) {
        for(int i = 0; i < this.tileList.length; i++) {
            int x = this.tileList[i].getX();
            int y = this.tileList[i].getY();
            if((x == tile.getX()) && (y == tile.getY())) {
                System.out.println("Changing tile: (" + x + "," + y + ")" + " with (" + tile.getX() + "," + tile.getY() + ")");
                this.tileList[i] = tile;
            }
        }
    }
    

    Now I would recommend doing something along the lines of this:

    public void set(Tile tile)
    {
         int tileIndex = tile.getX() + (tile.getY() * 16); // 16 is the grid Y segments, this assumes your grid is square
         if(tileIndex >= 0 && tileIndex < (16*16)) // some validation 0 to max size
         {
             System.out.println("Changing tile: (" + tile.getX() + "," + tile.getY() + ")");
             this.tileList[i] = tile;
         }
    }
    

    It now should be more efficient and maybe you can spot the error 🙂 I would also probably replace the getTile function to do the same, when you know the x,y position you do not need to loop to find it.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.