gpt4 book ai didi

java - MouseListener没有移动

转载 作者:行者123 更新时间:2023-12-01 23:16:34 25 4
gpt4 key购买 nike

我一直在学习如何在 Java 中使用角色扮演游戏,并找到了一个很棒的教程。然而,本教程展示了如何使用关键监听器移动 Sprite ,我想做的是通过获取屏幕上的鼠标位置来移动 Sprite ,并在没有任何关键监听器的情况下自行移动 Sprite 。这是我到目前为止的代码,感谢您的帮助! (哦,问题是 Sprite 没有移动,忘了说……是因为 move 方法中的 while 循环吗?)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Image;
import java.awt.event.*;

import javax.swing.ImageIcon;

public class SpaceCraft {

private String craft = "craft.png";

private int dx;
private int dy;
private int x;
private int y;
private Image image;

public SpaceCraft() {
ImageIcon spaceCraft = new ImageIcon(this.getClass().getResource(craft));
image = spaceCraft.getImage();
x = 40;
y = 60;
}

public void move() {
while ((x==Board.mouseX)==false) {
if (x < Board.mouseX){
x += 1;
} else if (x > Board.mouseX){
x -= 1;
}
}
while ((y==Board.mouseY)==false) {
if (y < Board.mouseY){
y += 1;
} else if (y > Board.mouseY){
y -= 1;
}
}

}

public int getX() {
return x;
}

public int getY() {
return y;
}

public Image getImage() {
return image;
}



/* public void keyPressed(KeyEvent e) {

int key = e.getKeyCode();

if (key == KeyEvent.VK_LEFT){
dx = -1;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 1;
}

if (key == KeyEvent.VK_UP) {
dy = -1;
}

if (key == KeyEvent.VK_DOWN) {
dy = 1;
}

}*/

/* public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();

if (key == KeyEvent.VK_LEFT) {
dx = 0;
}

if (key == KeyEvent.VK_RIGHT) {
dx = 0;
}

if (key == KeyEvent.VK_UP) {
dy = 0;
}

if (key == KeyEvent.VK_DOWN) {
dy = 0;
}
}*/


}


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.*;

import javax.swing.*;

import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener {

public static int mouseX;
public static int mouseY;
private Timer timer;
private SpaceCraft craft;

public Board() {
//addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.GREEN);
setDoubleBuffered(true);

craft = new SpaceCraft();

timer = new Timer(5, this);
timer.start();
}

public void paint(Graphics g) {
super.paint(g);

Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(), this);

Toolkit.getDefaultToolkit().sync();
g.dispose();
}

public void actionPerformed(ActionEvent e) {
craft.move();
repaint();
}
public void mouseReleased(MouseEvent e) {
mouseX = e.getXOnScreen();
mouseY = e.getYOnScreen();
craft.move();
repaint();
}

/*private class TAdapter extends KeyAdapter {

public void keyReleased(KeyEvent e) {
craft.keyReleased(e);
}

public void keyPressed(KeyEvent e) {
craft.keyPressed(e);
}
}*/



}

import javax.swing.*;
import java.applet.*;
import java.awt.Dimension;
import java.net.*;

public class RType extends Applet {
Board b = new Board();

public void init() {
b.setPreferredSize(new Dimension(400, 400));
add(b);

}

}

最佳答案

您必须将 MouseListener 添加到您的面板 Board

改变

public class Board extends JPanel implements ActionListener {

public class Board extends JPanel implements ActionListener,MouseListener  {

在构造函数中添加

addMouseListener(this);

关于java - MouseListener没有移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21121278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com