鼠标移动侦听器只在一个方向 [英] mouse motion listener only in one direction

查看:199
本文介绍了鼠标移动侦听器只在一个方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在Java中的鼠标移动监听程序无法整理出来完全是因为我希望对象驶向何方曾经在屏幕上的鼠标指向,但unforunately当鼠标小程序窗口内的方向移动时,对象只移动朝单一方向。这里是我的code以下。

 进口java.awt中的*。
导入的java.awt.geom *。
进口的java.util。*;
导入的java.applet。*;
java.awt.event中导入*。
进口的javax.swing *。公共类鼠标悬停扩展的Applet实现的KeyListener,MouseListener的,
      {的MouseMotionListener
   私人INT [] Xpoints = {0,-5,5};
   私人诠释[] = ypoints中{-10,-2,-2};
   私人双XPOS,ypos;
   私人多边形聚;
   INT polyrot = 0;
   私人诠释宽度; //!添加
   私人诠释高度; //!添加   公共无效的init(){
      聚=新的多边形(Xpoints,ypoints中,Xpoints.length);
      addKeyListener(本);
      addMouseListener将(本);
      addMouseMotionListener(本);
   }   公共无效漆(图形G){
      Graphics2D的G2D =(Graphics2D的)克;
      的AffineTransform ID =新AffineTransform();
      。宽度=的getSize()宽度;
      。HEIGHT =的getSize()高度;
      g2d.setColor(Color.BLACK);
      g2d.fillRect(0,0,宽度,高度);
      g2d.setColor(Color.RED);
      g2d.draw(聚);
      g2d.translate(宽度/ 2,高度/ 2);
      g2d.rotate(Math.toRadians(polyrot));
      g2d.scale(5,5);
   }   公共无效调用keyReleased(KeyEvent的K){
   }   公共无效的keyTyped(KeyEvent的K){
   }   公共无效键pressed(KeyEvent的K){
      开关(k.getKey code()){
      案例KeyEvent.VK_LEFT:
         如果(polyrot℃,){
            polyrot = 359;
            polyrot ++;
         }
         重绘();
         打破;
      案例KeyEvent.VK_RIGHT:
         如果(polyrot> 360){
            polyrot = 0;
            polyrot--;
         }
         重绘();
         打破;
      }
   }   公共无效的mouseEntered(的MouseEvent米){
   }   公共无效的mouseExited(的MouseEvent米){
   }   公共无效的mouseReleased(的MouseEvent米){
   }   公共无效的mouseClicked(的MouseEvent米){
   }   公共无效鼠标pressed(的MouseEvent米){
      开关(m.getButton()){
      案例MouseEvent.BUTTON1:
         如果(polyrot℃,){
            polyrot = 359;
            polyrot--;
         }
         重绘();
         打破;
      案例MouseEvent.BUTTON2:
         如果(polyrot> 360){
            polyrot = 0;
            polyrot ++;
         }
         重绘();
         打破;
      }
   }   公共无效的mouseMoved(的MouseEvent E){
      XPOS =的getX();
      如果(XPOS℃,){
         polyrot--;
      }否则如果(XPOS大于0){
         polyrot ++;
      }
      重绘();
      //!打破; //不属于这里
   }   @覆盖
   公共无效的mouseDragged(的MouseEvent E){
      //你忘了这个方法
   }
}


解决方案

您的问题是这一行:

 公共无效的mouseMoved(的MouseEvent E){
 XPOS =的getX(); // ******
 如果(XPOS℃,){polyrot--;}
 否则如果(XPOS大于0){polyrot ++;}
 重绘();
 打破;
}

这将返回该applet的的鼠标光标的x位置。您需要使用您的MouseEvent对象,电子,而是让鼠标的位置。将其更改为:

  XPOS = e.getX();

请不要忽视我对你的问题作出评论。请记住,我们是志愿者谁在我们的自由时间帮助。请不要让任何难度比它必须帮助你。


我试过,这样它编译编辑您的code,现在是缩进。考虑创建一个Swing应用程序,而不是一个AWT应用程序,因为摇摆应用更加灵活,强大和稳固。

i have been working on mouse motion listener in Java couldn't sort it out completely because i want the object to move towards the direction where ever on the screen the mouse is pointed at but unforunately when the mouse is inside the applet window, the object moves only towards a single direction. Here is my code below..

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
   }
}

解决方案

Your problem is with this line:

public void mouseMoved(MouseEvent e){
 xpos=getX(); // ******
 if(xpos<0){polyrot--;}
 else if(xpos>0){polyrot++;}
 repaint();
 break;
}

That returns the x position of the applet not the mouse cursor. You need to use your MouseEvent object, e and instead get the mouse's position. Change it to:

xpos = e.getX();

Please don't ignore the comment that I made to your question. Please remember that we're volunteers who help on our free time. Please don't make it any more difficult than it has to be to help you.


I've tried to edit your code so that it compiles, and now is indented. Consider creating a Swing application, not an AWT application, since Swing apps are more flexible, powerful and robust.

这篇关于鼠标移动侦听器只在一个方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆