【问题标题】:Issue when rotating an object through the atan2 method通过 atan2 方法旋转对象时的问题
【发布时间】:2015-11-26 16:57:35
【问题描述】:

我试图让图像跟随我的光标,平稳地移动到它所在的位置并转向它。

当我靠近原点时,它完美无瑕:图像将完全翻转而不会出现问题,但是我离窗口原点越远,图像转动越少。当我在屏幕的另一边时,它不会翻转,而是旋转 5°-15°。

如果有人能指出问题所在,我会很高兴 =)

这是我当前的图像代码:

lblRover = new JLabel(sees) { // sees is an ImageIcon
        protected void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
            AffineTransform aT = g2.getTransform();
            Shape oldshape = g2.getClip();
            double x = getWidth()/2.0;
            double y = getHeight()/2.0;
            aT.rotate(Math.toRadians(degrees), x, y);
            g2.setTransform(aT);
            g2.setClip(oldshape);
            super.paintComponent(g);
        }
    };
    lblRover.setSize(179, 180);
    lblRover.setLocation(500, 300);
    JFrame.getFrames()[0].add(lblRover);

这是处理旋转及其运动的代码:

        NewJFrame.PInf = MouseInfo.getPointerInfo();
        /* The frame is contained in NewJFrame, PInf is a PointerInfo */
        p = (NewJFrame.PInf.getLocation()); // p is a point
        p.x-=NewJFrame.getWindows()[0].getLocationOnScreen().x;
        p.y-=NewJFrame.getWindows()[0].getLocationOnScreen().y;
        //i subtract the location of the window relative to the screen
        img = NewJFrame.lblRover.getLocation();
        img.x+=NewJFrame.lblRover.getWidth()/2;
        img.y+=NewJFrame.lblRover.getHeight()/2;
        // img will be the point with the center of my image

        sas=getAngle(p,img); // sas is a float variable
        NewJFrame.degrees=sas;
        //
        // From now on i move the image
        //
        diffx = p.x-img.x;
        diffy = p.y-img.y;
        diffx/=80; // 80 is an arbitrary number to smooth the effect
        diffy/=80; // high numbers will make the image not move at all
        Point var = new Point(NewJFrame.lblRover.getLocation());
        // I may have to use img here or subtract width and height /2
        var.x+=diffx;
        var.y+=diffy;
        NewJFrame.lblRover.setLocation(var);

        // A 5ms sleep to smooth the movement
        // I also refresh a debug label
        // Also i do refresh the frame, otherwise nothing happens

        NewJFrame.jLabel1.setText(""+NewJFrame.lblRover.getLocation().y 
        +"/"+ p.y+" "+NewJFrame.lblRover.getLocation().x +"/"+ p.x );

        NewJFrame.getFrames()[0].repaint();
        try {
            sleep(5);
        } catch (InterruptedException ex) {
            Logger.getLogger(thread.class.getName()).log(Level.SEVERE, null, ex);
        }

【问题讨论】:

    标签: java atan2


    【解决方案1】:

    好吧,经过多次尝试,我已经缩小了范围。

    现在,旋转代码放在最后,在重绘之前,它看起来像这样:

    sas=this.getAngle((float)diffy,(float)diffx);
    NewJFrame.degrees=sas;
    

    我还创建了这个使用两个浮点数而不是两个点的新方法:

    public float getAngle(Float x, Float y) {
    float angle = (float) Math.toDegrees(Math.atan2(x, y));
    //System.out.println(angle);
    return angle;
    

    我不知道为什么它现在可以工作,但以前没有,因为程序基本相同......但是嘿,它有效! =D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-04
      • 2014-12-18
      • 2022-12-18
      • 2013-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多