我正在为我正在创建的游戏制作地图生成程序,所以我试图能够使用我的鼠标修改地图上的各个图块。我已经成功实现了点击图块并更改其值(从4路图块到2行图块)的功能,但是我无法使用mouseDragged工作。根据Java文档,我已经将mouseDragged接口正确实现到我的MouseEventHandler中,但是当我拖动鼠标并移动我的鼠标时,这些事件都没有跳过并扔掉我给他们扔的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 repo添加了一个链接:
网址: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
再次感谢您的帮助,感谢您花时间阅读本文。
答案 0 :(得分:9)
您需要添加MouseMotionListener才能拖动工作。您只需添加一个MouseListener。
即,
public void start() {
MouseEventListener myMouseEventListener = new MouseEventListener(this);
addMouseListener(myMouseEventListener);
addMouseMotionListener(myMouseEventListener);
//this.addMouseListener(new MouseEventListener(this));
另外根据我的评论,Square类的getX()和getY()方法覆盖了JLabel的相同方法,如果你不小心这些并正确使用它们就会完全陷入困境组件在GUI中的位置。我自己,我竭尽全力避免压倒这些方法。如果覆盖不是你的意图,请考虑更改他们的名字。
此外,我倾向于mousePressed(...)
优于mouseClicked(...)
方法,因为即使您在按下鼠标后移动鼠标,前者也会对媒体做出回应。
编辑:另一个错误!您几乎永远不会通过调用组件上的getGraphics()
来获取您的Graphics实例,因为返回的Graphics对象是渐渐消失的,很快就会变为null。相反,要么绘制一个显示在JComponent或JPanel paintComponent(...)
中的BufferedImage,要么只绘制paintComponent(...)
本身。
编辑2:为什么覆盖paint(...)
?这对我来说似乎有点危险,特别是因为你没有调用超级方法,特别是因为没有必要做这种事情。