简体   繁体   English

Java Applet MouseListener无法正常工作

[英]Java Applet MouseListener not working

I thought I programmed it so that when I click on the 'Start' button that appears when it is not level 1 or higher, it would go to level 1. But nothing happens when I click it. 我以为我对它进行了编程,所以当我单击级别不高于1或更高级别时出现的“开始”按钮时,它将升至级别1。但是单击它时没有任何反应。

import java.applet.Applet;

 import java.awt.*; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.net.URL; import java.util.ArrayList; import java.util.Random; import java.awt.Font; public class Move extends Applet implements KeyListener, MouseListener { private Rectangle rect; private ArrayList<Integer> keysDown; Random randomGenerator = new Random(); int speed = 4; int level = 0; int xpos; int ypos; boolean startClicked; Image block; Image start; URL base; MediaTracker mt; int randomx = randomGenerator.nextInt(560); int randomy = randomGenerator.nextInt(360); public void init() { this.addKeyListener(this); this.addMouseListener(this); keysDown = new ArrayList<Integer>(); rect = new Rectangle(0, 0, 50, 50); ///////////////////////////////////you can use rect.getX(); mt = new MediaTracker(this); try { base = getDocumentBase(); } catch (Exception e) {} block = getImage(base, "block.gif"); start = getImage(base, "start_button.png"); mt.addImage(block, 1); try { mt.waitForAll(); } catch (InterruptedException e) {} } public void paint(Graphics g) { setSize(600, 400); Graphics2D g2 = (Graphics2D)g; if (level != 0) { g2.fill(rect); //g.drawImage(block, randomx, randomy, this); ###############################fhjvhfjvkjerhgvgf Font font = new Font("Arial", Font.BOLD, 18); g.setFont(font); String text = "Speed: " + speed; FontMetrics fm = g.getFontMetrics(); int x = (getWidth() - fm.stringWidth(text)) / 2; int y = (getHeight() - fm.getHeight()) + fm.getAscent(); g.drawString(text, x, y); } else { // start menu g.drawImage(start, 160, 160, this); } } @Override public void keyPressed(KeyEvent e) { if (!keysDown.contains(e.getKeyCode())) keysDown.add(new Integer(e.getKeyCode())); moveRect(); } @Override public void keyReleased(KeyEvent e) { keysDown.remove(new Integer(e.getKeyCode())); } public void moveRect() { if (level != 0) { int x = rect.x; int y = rect.y; if (keysDown.contains(KeyEvent.VK_UP)) { y -= speed; } if (keysDown.contains(KeyEvent.VK_DOWN)) { y += speed; } if (keysDown.contains(KeyEvent.VK_LEFT)) { x -= speed; } if (keysDown.contains(KeyEvent.VK_RIGHT)) { x += speed; } rect.setLocation(x, y); repaint(); if (x-32 > randomx-72 && y+64 > randomy && x-32 < randomx+72 && y-64 < randomy) { /// will be flag randomx = randomGenerator.nextInt(560); randomy = randomGenerator.nextInt(360); speed += 4; } } } @Override public void keyTyped(KeyEvent e) {} @Override public void mouseClicked(MouseEvent me) { if (level == 0) { xpos = me.getX(); ypos = me.getY(); if (xpos > 160 && ypos > 96 && xpos < 400 && ypos < 160) { level = 1; } } } @Override public void mouseEntered(MouseEvent me) { } @Override public void mouseExited(MouseEvent me) { } @Override public void mouseReleased(MouseEvent me) { } @Override public void mousePressed(MouseEvent me) { } } 

Thanks again for all the help! 再次感谢您提供的所有帮助! This will HOPEFULLY turn out to be a maze game... 这将是一场迷宫游戏。

PS: ignore the pointless comments. PS:忽略毫无意义的评论。

You are drawing at 你在画

g.drawImage(start, 160, 160, this);

But checking for 但是检查

        if (xpos > 160 && ypos > 96 && xpos < 400 && ypos < 160) {

Change that to 更改为

        if (xpos >= 160 && ypos > 159 && xpos < 400 && ypos < 190) {

Better yet make constants of these numbers or use an ImageIcon and add a listener to it. 最好还是使这些数字为常数,或者使用ImageIcon并为其添加一个侦听器。

   public static final int START_X_POS = 160;
   public static final int START_Y_POS = 160;

   public static final int START_WIDTH = 30;
   public static final int START_HEIGHT = 150;

//... // ...

        if (xpos >= (START_X_POS ) && ypos >= START_Y_POS  && xpos <= ( START_X_POS + START_WIDTH) && ypos <= (START_X_POS + START_HEIGHT )) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM