java - 方形不在我的 Swing 应用程序中旋转

标签 java swing timer graphics2d

我开发了一个小 Swing 应用程序,其中我使用单独的类 component 在我的 JFrame 中添加了一个 Square。现在我想在中心旋转这个 Square,但我只看到一个根本没有旋转的静态 Square。

这是我的代码...

public class Rotation extends JFrame {

    Rotation() {
        super("Animation of rotation about center");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400,400);

        add(new component());

        setVisible(true);
    }

    public static void main(String args[]){
        SwingUtilities.invokeLater(new Runnable(){public void run(){new Rotation();}});
    }
}

class component extends JPanel implements ActionListener {
    Timer timer;
    Rectangle.Double r=new Rectangle.Double(100,100,50,50);
    int theta=0;

    component() {
        timer=new Timer(10,this);
        timer.start();
    }

    public void actionPerformed(ActionEvent e) {
        if (theta==360){
            theta=0;
            theta++;
        }
        repaint();
    }

    public void paint(Graphics g){
        Graphics2D g2=(Graphics2D)g;
        g2.setColor(Color.GRAY);
        g2.rotate(theta);
        g2.fill(r);
    }
}

有人可以帮我确定并解决问题吗。

最佳答案

你的代码有很多错误:

  1. theta 应该是 double 的,并且应该以弧度而不是度数表示。所以让它翻倍:

    private double theta = 0;
    
  2. 您不要更改计时器操作中的 theta 值 - 在 actionPerformed 中执行此操作:

    public void actionPerformed ( ActionEvent e )
    {
        theta += Math.PI / 18;
        if ( theta >= Math.PI * 2 )
        {
            theta = 0;
        }
        repaint ();
    }
    
  3. 您没有指定图形上下文应围绕其旋转的点。执行此操作(否则您的正方形将围绕坐标 (0;0) 的开头旋转):

    public void paintComponent ( Graphics g )
    {
        super.paintComponent ( g );
    
        Graphics2D g2 = ( Graphics2D ) g;
        g2.setRenderingHint ( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
        g2.rotate ( theta, 125, 125 );
        g2.setColor ( Color.GRAY );
        g2.fill ( r );
    }
    
  4. 您覆盖组件的 paint 方法而不是 paintComponent总是改用 paintComponent,因为它针对重绘和其他 Swing 内容进行了优化,我真的不想在这里谈论它,因为它是一个很大的题外话。

    <
  5. 您使用 JPanel 作为基础组件来绘制简单的形状 - 请改用 JComponent,因为您实际上并不需要 JPanel 的任何功能(实际上也没有)。

查看最终工作示例:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @see http://stackoverflow.com/a/13051142/909085
 */

public class RotationTest extends JFrame
{
    public RotationTest ()
    {
        super ( "Animation of rotation about center" );
        setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
        setSize ( 400, 400 );

        add ( new MyComponent () );

        setVisible ( true );
    }

    public static void main ( String args[] )
    {
        SwingUtilities.invokeLater ( new Runnable ()
        {
            public void run ()
            {
                new RotationTest ();
            }
        } );
    }

    private class MyComponent extends JComponent implements ActionListener
    {
        private Timer timer;
        private Rectangle.Double r = new Rectangle.Double ( 100, 100, 50, 50 );
        private double theta = 0;

        public MyComponent ()
        {
            super ();
            timer = new Timer ( 1000 / 24, this );
            timer.start ();
        }

        public void actionPerformed ( ActionEvent e )
        {
            theta += Math.PI / 18;
            if ( theta >= Math.PI * 2 )
            {
                theta = 0;
            }
            repaint ();
        }

        public void paintComponent ( Graphics g )
        {
            super.paintComponent ( g );

            Graphics2D g2 = ( Graphics2D ) g;
            g2.setRenderingHint ( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
            g2.rotate ( theta, 125, 125 );
            g2.setColor ( Color.GRAY );
            g2.fill ( r );
        }
    }
}

正如您所看到的,我还在 paint 方法中添加了一个渲染提示,以使方 block 移动平滑,并重构了您的一些代码。

关于java - 方形不在我的 Swing 应用程序中旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13050860/

相关文章:

java - Maven 不使用 pom 中的 Surefire 插件构建项目

java jtable 用新值替换旧值

java - 复合 JTree 节点允许事件传递到下面的对象

iphone - 检测一个int是否连续重复

java - neo4j 事务锁

java - 将回调作为参数传递给 Android 中的抽象 setOnClickListener

java - JAXB 解码器下 sun.misc.URLClassPath.getLoader 中的高锁争用

java - 如何将半透明 JWindow 上的鼠标事件传递到下面的 JFrame?

C# Windows 服务 - 多个计时器

android - Chronometer 节省 fragment 更改时间