我开发了一个小的 Swing 应用程序,其中我使用单独的类component
在JFrame
中添加了一个 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);
}
}
有人可以帮我识别并解决问题吗?
你的代码中有很多错误:
-
theta
应为双精度,并且应以弧度而不是度表示。所以让它加倍:private double theta = 0;
-
您不会在计时器操作中更改
theta
值 - 在actionPerformed
中执行此操作:public void actionPerformed ( ActionEvent e ) { theta += Math.PI / 18; if ( theta >= Math.PI * 2 ) { theta = 0; } repaint (); }
-
您不指定图形上下文应围绕哪个点旋转。这样做(否则你的正方形将围绕坐标的开头旋转(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 ); }
-
您可以覆盖组件的
paint
方法,而不是paintComponent
。始终使用paintComponent
,因为它针对重绘和其他 Swing 内容进行了优化,我真的不想在这里谈论,因为这是一个很大的题外话。 -
你使用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 方法中添加了一个渲染提示,以使方形移动平滑,并重构了一些代码。
theta
变量在哪里更改?
public void actionPerformed(ActionEvent e) {
theta+= 10; // <------------I think prooblem was here..
if (theta==360){
theta=0;
}
repaint();
}
问题是我需要进行 2 项更改:
// 1.
g2.rotate(theta,125,125);
// 2.
super.paint(g);