Java 鼠标运动侦听器仅在一个方向上

Java 鼠标运动侦听器仅在一个方向上,java,applet,awt,keylistener,mouselistener,Java,Applet,Awt,Keylistener,Mouselistener,我一直在用Java开发鼠标运动监听器,但无法完全解决这个问题,因为我希望对象朝着屏幕上鼠标指向的方向移动,但不幸的是,当鼠标位于小程序窗口内时,对象只朝一个方向移动。下面是我的代码 import java.awt.*; import java.awt.geom.*; import java.util.*; import java.applet.*; import java.awt.event.*; import javax.swing.*; public class MouseOver ext

我一直在用Java开发鼠标运动监听器,但无法完全解决这个问题,因为我希望对象朝着屏幕上鼠标指向的方向移动,但不幸的是,当鼠标位于小程序窗口内时,对象只朝一个方向移动。下面是我的代码

import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;

public class MouseOver extends Applet implements KeyListener, MouseListener,
      MouseMotionListener {
   private int[] Xpoints = { 0, -5, 5 };
   private int[] Ypoints = { -10, -2, -2 };
   private double xpos, ypos;
   private Polygon poly;
   int polyrot = 0;
   private int width; // !! added
   private int height; // !! added

   public void init() {
      poly = new Polygon(Xpoints, Ypoints, Xpoints.length);
      addKeyListener(this);
      addMouseListener(this);
      addMouseMotionListener(this);
   }

   public void paint(Graphics g) {
      Graphics2D g2d = (Graphics2D) g;
      AffineTransform id = new AffineTransform();
      width = getSize().width;
      height = getSize().height;
      g2d.setColor(Color.BLACK);
      g2d.fillRect(0, 0, width, height);
      g2d.setColor(Color.RED);
      g2d.draw(poly);
      g2d.translate(width / 2, height / 2);
      g2d.rotate(Math.toRadians(polyrot));
      g2d.scale(5, 5);
   }

   public void keyReleased(KeyEvent k) {
   }

   public void keyTyped(KeyEvent k) {
   }

   public void keyPressed(KeyEvent k) {
      switch (k.getKeyCode()) {
      case KeyEvent.VK_LEFT:
         if (polyrot < 0) {
            polyrot = 359;
            polyrot++;
         }
         repaint();
         break;
      case KeyEvent.VK_RIGHT:
         if (polyrot > 360) {
            polyrot = 0;
            polyrot--;
         }
         repaint();
         break;
      }
   }

   public void mouseEntered(MouseEvent m) {
   }

   public void mouseExited(MouseEvent m) {
   }

   public void mouseReleased(MouseEvent m) {
   }

   public void mouseClicked(MouseEvent m) {
   }

   public void mousePressed(MouseEvent m) {
      switch (m.getButton()) {
      case MouseEvent.BUTTON1:
         if (polyrot < 0) {
            polyrot = 359;
            polyrot--;
         }
         repaint();
         break;
      case MouseEvent.BUTTON2:
         if (polyrot > 360) {
            polyrot = 0;
            polyrot++;
         }
         repaint();
         break;
      }
   }

   public void mouseMoved(MouseEvent e) {
      xpos = getX();
      if (xpos < 0) {
         polyrot--;
      } else if (xpos > 0) {
         polyrot++;
      }
      repaint();
      // !! break; // Doesn't belong here
   }

   @Override
   public void mouseDragged(MouseEvent e) {
      // You forgot this method
   }
}
import java.awt.*;
导入java.awt.geom.*;
导入java.util.*;
导入java.applet.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类MouseOver扩展小程序实现KeyListener、MouseListener、,
MouseMotionListener{
私有int[]Xpoints={0,-5,5};
私有int[]Ypoints={-10,-2,-2};
私人双XPO、YPO;
私有多边形多边形;
int polyrot=0;
专用整数宽度;/!!已添加
专用整数高度;/!!已添加
公共void init(){
多边形=新多边形(X点、Y点、X点长度);
addKeyListener(此);
addMouseListener(这个);
addMouseMotionListener(此);
}
公共空间涂料(图g){
Graphics2D g2d=(Graphics2D)g;
AffineTransform id=新的AffineTransform();
宽度=getSize().width;
高度=getSize()。高度;
g2d.setColor(Color.BLACK);
g2d.fillRect(0,0,宽度,高度);
g2d.setColor(Color.RED);
g2d.draw(poly);
g2d.平移(宽度/2,高度/2);
旋转(数学托拉迪安(polyrot));
g2d.量表(5,5);
}
公共无效密钥已释放(密钥事件k){
}
public void keyTyped(KeyEvent k){
}
按下公共无效键(KeyEvent k){
开关(k.getKeyCode()){
case KeyEvent.VK_左:
if(polyrot<0){
polyrot=359;
polyrot++;
}
重新油漆();
打破
case KeyEvent.VK_RIGHT:
如果(polyrot>360){
polyrot=0;
多腐烂--;
}
重新油漆();
打破
}
}
公共无效鼠标事件(鼠标事件m){
}
公共无效mouseExited(MouseEvent m){
}
公共无效MouseEvent(MouseEvent m){
}
公共无效mouseClicked(MouseEvent m){
}
公共空间鼠标按下(MouseEvent m){
开关(m.getButton()){
case MouseEvent.BUTTON1:
if(polyrot<0){
polyrot=359;
多腐烂--;
}
重新油漆();
打破
case MouseEvent.BUTTON2:
如果(polyrot>360){
polyrot=0;
polyrot++;
}
重新油漆();
打破
}
}
public void mouseMoved(MouseEvent e){
xpos=getX();
如果(xpos<0){
多腐烂--;
}如果(xpos>0),则执行其他操作{
polyrot++;
}
重新油漆();
//!!break;//不属于这里
}
@凌驾
公共无效鼠标标记(鼠标事件e){
//你忘了这个方法
}
}
从这里开始:

public void mouseMoved(MouseEvent e){
 xpos=getX();
 if(xpos<0){polyrot--;}
 else if(xpos>0){polyrot++;}
 repaint();
 break;
}
public void mouseMoved(MouseEvent e){
xpos=getX();
if(xpos0){polyrot++;}
重新油漆();
打破
}
似乎您只更新了XPO。您还应该更新变量ypos。 您可能需要这样做:

ypos=e.getY();
if (this.ypos<0){
 this.polyrot--;
}else if (this.ypos>0) {
 this.polyrot++;
}
this.repaint();
public void mouseMoved(MouseEvent e) {
    xpos = e.getX();
    if (xpos < getWidth() / 2) {
        polyrot--;
    } else {
        polyrot++;
    }
    repaint();
}
public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g.create();
    width = getSize().width;
    height = getSize().height;
    g2d.setColor(Color.BLACK);
    g2d.fillRect(0, 0, width, height);
    g2d.translate(width / 2, height / 2);
    g2d.rotate(Math.toRadians(polyrot));
    g2d.scale(5, 5);
    g2d.setColor(Color.RED);
    g2d.draw(poly);
    g2d.dispose();
}
ypos=e.getY();
if(this.ypos0){
这个.polyrot++;
}
这个。重新绘制();
从这里开始:

public void mouseMoved(MouseEvent e){
 xpos=getX();
 if(xpos<0){polyrot--;}
 else if(xpos>0){polyrot++;}
 repaint();
 break;
}
public void mouseMoved(MouseEvent e){
xpos=getX();
if(xpos0){polyrot++;}
重新油漆();
打破
}
似乎您只更新了XPO。您还应该更新变量ypos。 您可能需要这样做:

ypos=e.getY();
if (this.ypos<0){
 this.polyrot--;
}else if (this.ypos>0) {
 this.polyrot++;
}
this.repaint();
public void mouseMoved(MouseEvent e) {
    xpos = e.getX();
    if (xpos < getWidth() / 2) {
        polyrot--;
    } else {
        polyrot++;
    }
    repaint();
}
public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g.create();
    width = getSize().width;
    height = getSize().height;
    g2d.setColor(Color.BLACK);
    g2d.fillRect(0, 0, width, height);
    g2d.translate(width / 2, height / 2);
    g2d.rotate(Math.toRadians(polyrot));
    g2d.scale(5, 5);
    g2d.setColor(Color.RED);
    g2d.draw(poly);
    g2d.dispose();
}
ypos=e.getY();
if(this.ypos0){
这个.polyrot++;
}
这个。重新绘制();
从这里开始:

public void mouseMoved(MouseEvent e){
 xpos=getX();
 if(xpos<0){polyrot--;}
 else if(xpos>0){polyrot++;}
 repaint();
 break;
}
public void mouseMoved(MouseEvent e){
xpos=getX();
if(xpos0){polyrot++;}
重新油漆();
打破
}
似乎您只更新了XPO。您还应该更新变量ypos。 您可能需要这样做:

ypos=e.getY();
if (this.ypos<0){
 this.polyrot--;
}else if (this.ypos>0) {
 this.polyrot++;
}
this.repaint();
public void mouseMoved(MouseEvent e) {
    xpos = e.getX();
    if (xpos < getWidth() / 2) {
        polyrot--;
    } else {
        polyrot++;
    }
    repaint();
}
public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g.create();
    width = getSize().width;
    height = getSize().height;
    g2d.setColor(Color.BLACK);
    g2d.fillRect(0, 0, width, height);
    g2d.translate(width / 2, height / 2);
    g2d.rotate(Math.toRadians(polyrot));
    g2d.scale(5, 5);
    g2d.setColor(Color.RED);
    g2d.draw(poly);
    g2d.dispose();
}
ypos=e.getY();
if(this.ypos0){
这个.polyrot++;
}
这个。重新绘制();
从这里开始:

public void mouseMoved(MouseEvent e){
 xpos=getX();
 if(xpos<0){polyrot--;}
 else if(xpos>0){polyrot++;}
 repaint();
 break;
}
public void mouseMoved(MouseEvent e){
xpos=getX();
if(xpos0){polyrot++;}
重新油漆();
打破
}
似乎您只更新了XPO。您还应该更新变量ypos。 您可能需要这样做:

ypos=e.getY();
if (this.ypos<0){
 this.polyrot--;
}else if (this.ypos>0) {
 this.polyrot++;
}
this.repaint();
public void mouseMoved(MouseEvent e) {
    xpos = e.getX();
    if (xpos < getWidth() / 2) {
        polyrot--;
    } else {
        polyrot++;
    }
    repaint();
}
public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g.create();
    width = getSize().width;
    height = getSize().height;
    g2d.setColor(Color.BLACK);
    g2d.fillRect(0, 0, width, height);
    g2d.translate(width / 2, height / 2);
    g2d.rotate(Math.toRadians(polyrot));
    g2d.scale(5, 5);
    g2d.setColor(Color.RED);
    g2d.draw(poly);
    g2d.dispose();
}
ypos=e.getY();
if(this.ypos0){
这个.polyrot++;
}
这个。重新绘制();

您的问题在于这一行:

public void mouseMoved(MouseEvent e){
 xpos=getX(); // ******
 if(xpos<0){polyrot--;}
 else if(xpos>0){polyrot++;}
 repaint();
 break;
}
请不要忽视我对你问题的评论。请记住,我们是在业余时间提供帮助的志愿者。请不要让帮助你变得更加困难



我试图编辑您的代码,使其能够编译,现在已缩进。考虑创建一个Swing应用程序,而不是AWT应用程序,因为Swing应用程序更加灵活、强大和健壮。p> 您的问题在于这一行:

public void mouseMoved(MouseEvent e){
 xpos=getX(); // ******
 if(xpos<0){polyrot--;}
 else if(xpos>0){polyrot++;}
 repaint();
 break;
}
请不要忽视我对你问题的评论。请记住,我们是在业余时间提供帮助的志愿者。请不要让帮助你变得更加困难



我试图编辑您的代码,使其能够编译,现在已缩进。考虑创建一个Swing应用程序,而不是AWT应用程序,因为Swing应用程序更加灵活、强大和健壮。p> 您的问题在于这一行:

public void mouseMoved(MouseEvent e){
 xpos=getX(); // ******
 if(xpos<0){polyrot--;}
 else if(xpos>0){polyrot++;}
 repaint();
 break;
}
请不要忽视我对你问题的评论。请记住,我们是在业余时间提供帮助的志愿者。请不要让帮助你变得更加困难



我试图编辑您的代码,使其能够编译,现在已缩进。考虑创建一个Swing应用程序,而不是AWT应用程序,因为Swing应用程序更加灵活、强大和健壮。p> 您的问题在于这一行:

public void mouseMoved(MouseEvent e){
 xpos=getX(); // ******
 if(xpos<0){polyrot--;}
 else if(xpos>0){polyrot++;}
 repaint();
 break;
}
请不要忽视我对你问题的评论。请记住,我们是在业余时间提供帮助的志愿者。请不要让帮助你变得更加困难


我试图编辑您的代码,使其能够编译,现在已缩进。考虑创建一个Swing应用程序,而不是AWT应用程序,因为Swing应用程序更为FL。