方形在我的摇摆应用中不旋转

Ins*_*der 3 java swing timer graphics2d

我开发了一个小的Swing应用程序,我在其中JFrame使用一个单独的类添加了一个Square component.现在我想在它的中心旋转这个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);
    }
}
Run Code Online (Sandbox Code Playgroud)

请有人帮我识别并解决问题.

Mik*_*rin 5

您的代码中存在很多错误:

  1. theta应该是双倍的,应该以弧度表示,而不是度数.所以加倍:

    private double theta = 0;
    
    Run Code Online (Sandbox Code Playgroud)
  2. 您不会theta在计时器操作中更改值 - 请执行以下操作actionPerformed:

    public void actionPerformed ( ActionEvent e )
    {
        theta += Math.PI / 18;
        if ( theta >= Math.PI * 2 )
        {
            theta = 0;
        }
        repaint ();
    }
    
    Run Code Online (Sandbox Code Playgroud)
  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 );
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. You override component's paint method instead of paintComponent. Always use paintComponent instead as it is optimized for repaints and other Swing stuff i don't really want to talk about here because its a big offtopic.

  5. You use JPanel as a base component to paint a simple shape - use JComponent instead since you don't really need any JPanel's features (and there aren't any actually).

See the final working example:

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 );
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

As you can see i also added a rendering hint into paint method to make square movement smooth and refactored some of your code.