I am currently working on a map creation program for the game being created, and therefore I am trying to change individual fragments on the map with the mouse. I successfully implemented the ability to click on a tile and change its value (from a 4-dimensional tile to a 2-sided tile), but I had problems working with mouseDragged. According to the Java docs, I have implemented the correct implementation of the mouseDragged interface in my MouseEventHandler, but when I drag the mouse and move the mouse around none of these events, I drop the println text that I have, Now. If anyone could shed light on why this is not working for me, I would love it. Thanks!
Question: if someone has better methods for creating such a map, or any (presumably something better than what I did), other methods for detecting a single mouse. While on the tile, I would like to hear It. This is only my first time working with such things, so I am completely inexperienced and would like some advice.
As for the code itself:
File 1: OneQuestMapgen.java
package OneQuestMapgen; import java.awt.Graphics; import java.util.ArrayList; import javax.swing.*; import tiles.Tile; import tiles.TileSet; public class OneQuestMapgen extends JApplet{ public static OneQuestMapgen instance; ArrayList<ArrayList<Tile>> map = new ArrayList<ArrayList<Tile>>(); TileSet tileSet = new TileSet(); public void init(){ this.setSize(950,600); } public void start(){ this.addMouseListener(new MouseEventListener(this)); int tileSize = tileSet.get("grasslands")[1].getHeight(); for (int i = 0; i < getHeight(); i += tileSize) { ArrayList<Tile> temp = new ArrayList<Tile>(); for (int j = 0; j < getWidth(); j += tileSize) { temp.add(new Tile(j, i, tileSize, tileSet.get("grasslands"))); } map.add(temp); } } public void paint(Graphics g){ for (int i = 0; i < map.size(); i++){ for(int j = 0; j < map.get(i).size(); j++){ map.get(i).get(j).render(g); } } } public void stop(){ } public void destroy(){ } public ArrayList<ArrayList<Tile>> getMap(){ return map; } }
File 2: MouseEventListener.java
package OneQuestMapgen; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.util.ArrayList; import tiles.Tile; public class MouseEventListener implements MouseListener, MouseMotionListener { OneQuestMapgen instance; ArrayList<ArrayList<Tile>> map; public MouseEventListener(OneQuestMapgen instance) { this.instance = instance; } @Override public void mouseClicked(MouseEvent e) { System.out.println(e.getX() + " , " + e.getY()); map = instance.getMap(); for (int i = 0; i < map.size(); i++) { for (int j = 0; j < map.get(i).size(); j++) if (map.get(i).get(j).bounds(e)) { map.get(i).get(j).onClicked(instance.getGraphics()); } } } @Override public void mouseEntered(MouseEvent e) {
File 3: Tile.java
package tiles; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; @SuppressWarnings("serial") public class Tile extends Square { BufferedImage[] tiles; int tileValue = 0; public Tile (int x, int y, int size, BufferedImage[] tileSet){ super(x,y,size); tiles = tileSet; } public void render(Graphics g) { g.drawImage(tiles[tileValue], getX(), getY(), getDimension(), getDimension(), null); } public void onClicked(Graphics g) { tileValue++; System.out.println(tileValue); g.drawImage(tiles[tileValue], getX(), getY(), getDimension(), getDimension(), null); } public boolean bounds(MouseEvent e){ if (e.getX() <= getX() + getDimension() && e.getX() >= getX() && e.getY() <= getY() + getDimension() && e.getY() >= getY()) return true; return false; } }
File 4: TileSet.java
package tiles; import java.awt.image.BufferedImage; import java.net.URL; import java.util.concurrent.ConcurrentHashMap; import javax.imageio.ImageIO; public final class TileSet { ConcurrentHashMap<String, BufferedImage[]> container = new ConcurrentHashMap<String, BufferedImage[]>(11); BufferedImage[] grasslands = new BufferedImage[11]; public TileSet(){ boolean toAdd = true; try { grasslands[0] = ImageIO.read(new URL("http://i675.photobucket.com/albums/vv118/va023/OneQuest/GrassBlank_zpse99de845.png")); grasslands[1] = ImageIO.read(new URL("http://i675.photobucket.com/albums/vv118/va023/OneQuest/4WayTile_zps49ebbeea.png")); grasslands[2] = ImageIO.read(new URL("http://i675.photobucket.com/albums/vv118/va023/OneQuest/LineHorizontal_zpsc7bd45d5.png")); grasslands[3] = ImageIO.read(new URL("http://i675.photobucket.com/albums/vv118/va023/OneQuest/LineVertical_zps9719b63f.png")); grasslands[4] = ImageIO.read(new URL("http://i675.photobucket.com/albums/vv118/va023/OneQuest/TShapeTop_zpsa4b2aaa1.png")) } catch (Exception e) { System.out.println("Unable to load resources for Grasslands"); } for (int i = 0; i < grasslands.length; i++) if (grasslands[i].getWidth() != grasslands[i].getHeight()) { System.out.println("Error, tiles are not square. Grasslands has not been added."); toAdd = false; } if (toAdd) { container.put("grasslands".toLowerCase(), grasslands); } } public BufferedImage[] get(String s) { return container.get(s.toLowerCase()); } }
File 5: Square.java
package tiles; import javax.swing.JLabel; public abstract class Square extends JLabel{ private static final long serialVersionUID = 1L; private int x; private int y; private int dimension; public Square(int x, int y, int dimension){ this.x = x; this.y = y; this.dimension = dimension; } public int getX(){ return x; } public int getY(){ return y; } public int getDimension(){ return dimension; } }
Note. If you want to pull everything from github, I added a link to my github repository for this project below:
URL: https://github.com/vagrawal1/OneQuestMapgen
HTTPS: https://github.com/vagrawal1/OneQuestMapgen.git
SSH: git @ github.com: vagrawal1 / OneQuestMapgen.git
Git Read only: git: //github.com/vagrawal1/OneQuestMapgen.git
Thanks again for your help and thank you for taking the time to read this.