这是我的代码到目前为止,我想要做的是每次移动鼠标时添加一个新的对象,但是系统在经过几个小时的思考后甚至都没有访问MouseEvent类,我仍然无法想象问题。请帮助!!
我的主要课程:
package testing;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.Random;
public class Wincall extends Canvas implements Runnable {
public static final int HEIGHT = 640, WIDTH = 1080;
private WinTest w;
private Handler handler;
private ME me = new ME(this);
public Wincall(){
handler = new Handler();
w = new WinTest(WIDTH, HEIGHT, "Test", this);
}
public synchronized void run(){
while(true){
long now = System.currentTimeMillis();
this.tick();
this.render();
long after = System.currentTimeMillis();
int tt = (int) (after-now);
if(tt>5)
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Time Taken in millisecs : " + tt);
}
}
public void tick(){
handler.tick();
}
public void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null)
{
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
//render
g.setColor(Color.BLACK);
g.fillRect(0 ,0 ,WIDTH, HEIGHT);
handler.render(g);
//render end
g.dispose();
bs.show();
}
public void addStuff(){
handler.addObject(new TestGO(me.getX(), me.getY(), 32, 32));
}
public static void main(String[] args){
new Wincall();
}
}
我的MouseEvent类:
package testing;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class ME implements MouseMotionListener{
private int mx = 0, my = 0;
private Wincall game;
public ME(Wincall game){
this.game = game;
}
public void mouseDragged(MouseEvent e){}
public void mouseMoved(MouseEvent e) {
game.addStuff();
mx = e.getX();
my = e.getY();
System.out.println(mx);
System.out.println(my);
}
public int getX(){
return mx;
}
public int getY(){
return my;
}
}
我的窗口类:
package testing;
import java.awt.Canvas;
import javax.swing.JFrame;
public class WinTest {
private static final long serialVersionUID = -369751247370351003L;
public WinTest(int h, int w, String title, Wincall game){
JFrame f = new JFrame(title);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(h, w);
f.add(game);
f.setVisible(true);
f.requestFocus();
f.setResizable(false);
f.setFocusable(true);
game.addMouseMotionListener(new ME());
game.run();
}
}
答案 0 :(得分:0)
我正在查看一些旧的源代码:
尝试
addMouseMotionListener(ME);
而不是:
f.addMouseMotionListener(ME);
答案 1 :(得分:0)
虽然不清楚你想要做什么。但是您需要将MouseMotionListener
添加到画布而不是JFrame
。由于您要向JFrame
添加画布,因此canvas
应该捕获MouseEvents。因此,您的Wintest
应该看起来像这样:
public class WinTest extends Canvas {
private static final long serialVersionUID = -369751247370351003L;
public WinTest(int h, int w, String title, Wincall game, ME me) {
JFrame f = new JFrame(title);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(h, w);
f.add(game);
f.setVisible(true);
f.requestFocus();
f.setResizable(false);
f.setFocusable(true);
// f.addMouseMotionListener(me);
game.addMouseMotionListener(me);
game.run();
}
}
更新:
Wincalll课程:
public class Wincall extends Canvas implements Runnable {
public static final int HEIGHT = 640, WIDTH = 1080;
private WinTest w;
// private Handler handler;
private ME me = new ME(this);
public Wincall() {
// handler = new Handler();
w = new WinTest(WIDTH, HEIGHT, "Test", this, me);
}
public synchronized void run() {
while (true) {
long now = System.currentTimeMillis();
this.tick();
this.render();
long after = System.currentTimeMillis();
int tt = (int) (after - now);
if (tt > 5)
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
// System.out.println("Time Taken in millisecs : " + tt);
}
}
public void tick() {
// handler.tick();
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
// render
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
// handler.render(g);
// render end
g.dispose();
bs.show();
}
public void addStuff() {
System.out.println("addStuff");
// handler.addObject(new TestGO(me.getX(), me.getY(), 32, 32));
}
public static void main(String[] args) {
new Wincall();
}
}