我目前正在为我正在创建的游戏开发地图生成程序,因此我试图能够使用鼠标修改地图上的各个图块。我已经成功实现了单击图块并更改其值的功能(从 4 路图块到 2 路图块),但是我无法让 mouseDragged 工作。根据Java文档,我已经在我的MouseEventHandler中正确实现了mouseDragged接口,但是当我拖动鼠标并在这些事件中移动鼠标时,我不会绊倒并抛出我让他们正确抛出的println文本现在。如果有人能解释为什么这对我不起作用,我会喜欢的。谢谢!
一个附带的问题:如果有人有任何更好的方法来尝试生成这样的地图,或者任何(可能比我所做的更好)检测单个鼠标按下瓷砖的其他方法,我很想听听。这只是我第一次处理这种事情,所以我很缺乏经验,希望得到一些建议。
至于代码本身:
文件 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;
}
}
文件 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) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent arg0) {
System.out.println("Mouse Dragged: " + arg0.getX() + " , " + arg0.getY());
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("Mouse Moved: " + arg0.getX() + " , " + arg0.getY());
}
}
文件 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;
}
}
文件 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());
}
}
文件 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;
}
}
注意:如果您希望能够将其全部从 github 中提取出来,我在下面为该项目添加了指向我的 github 存储库的链接:
网址:https ://github.com/vagrawal1/OneQuestMapgen
HTTPS:https ://github.com/vagrawal1/OneQuestMapgen.git
SSH:git@github.com:vagrawal1/OneQuestMapgen.git
Git 只读:git://github.com/vagrawal1/OneQuestMapgen.git
再次感谢您的帮助,并感谢您花时间阅读本文。