Note that there are some explanatory texts on larger screens.

plurals
  1. POTile disappearing in previous position after being "set"
    text
    copied!<p>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.</p> <p>FIXED, I modified my TileList.java code (set function) to this: Alright, I fixed it: I simply changed the set(Tile tile) function!</p> <pre><code>public void set(Tile tile) { for(int i = 0; i &lt; this.tileList.length; i++) { int x = this.tileList[i].getX(); int y = this.tileList[i].getY(); if((x == tile.getX()) &amp;&amp; (y == tile.getY())) { System.out.println("Changing tile: (" + x + "," + y + ")" + " with (" + tile.getX() + "," + tile.getY() + ")"); this.tileList[i].setImage(tile.getImage()); } } } </code></pre> <p>Image showing error: <a href="http://i.imgur.com/eosPt.png" rel="nofollow">http://i.imgur.com/eosPt.png</a></p> <p>map.java:</p> <pre><code>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 &lt; this.tileSet.size; i++) { Tile tile = this.tileSet.get(i); gfx.drawImage(tile.getImage(), tile.getX(), tile.getY(), null); } for(int i = 0; i &lt;= 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 &lt; size/2; x++) { for(int y = 0; y &lt; 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 &lt; 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 &gt;= minX) &amp;&amp; (x &lt; maxX) &amp;&amp; (y &gt;= minY) &amp;&amp; (y &lt; 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) {} } </code></pre> <p>Tile.java</p> <pre><code>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; } } </code></pre> <p>TileList.java package org.naive.util;</p> <pre><code>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 &lt; this.tileList.length; i++) { int x = this.tileList[i].getX(); int y = this.tileList[i].getY(); if((x == tile.getX()) &amp;&amp; (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]; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload