我开发了一个小型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);
}
}
有人可以帮我识别并解决问题。
答案 0 :(得分:5)
您的代码中存在很多错误:
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方法中添加了一个渲染提示,使方形运动变得平滑,并重构了一些代码。
答案 1 :(得分:3)
theta
变量的位置在哪里?
答案 2 :(得分:1)
public void actionPerformed(ActionEvent e) {
theta+= 10; // <------------I think prooblem was here..
if (theta==360){
theta=0;
}
repaint();
}
答案 3 :(得分:-2)
问题是我需要进行2次更改:
// 1.
g2.rotate(theta,125,125);
// 2.
super.paint(g);