Java square在我的swing应用程序中不旋转
我开发了一个小型Swing应用程序,其中我使用一个单独的类Java square在我的swing应用程序中不旋转,java,swing,timer,graphics2d,Java,Swing,Timer,Graphics2d,我开发了一个小型Swing应用程序,其中我使用一个单独的类组件在JFrame中添加了一个正方形。现在我想旋转这个正方形的中心,但我只看到一个静止的正方形,它根本不旋转 这是我的密码 public class Rotation extends JFrame { Rotation() { super("Animation of rotation about center"); setDefaultCloseOperation(JFrame.EXIT_ON_C
组件在JFrame
中添加了一个正方形。现在我想旋转这个正方形的中心,但我只看到一个静止的正方形,它根本不旋转
这是我的密码
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
变量?执行的公共无效操作(ActionEvent e){
public void actionPerformed(ActionEvent e) {
theta+= 10; // <------------I think prooblem was here..
if (theta==360){
theta=0;
}
repaint();
}
θ+=10;//代码中有很多错误:
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)旋转):
您可以覆盖组件的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.
g2.rotate(theta,125,125);
// 2.
super.paint(g);
谢谢StanislavL,我不小心忘记了那一行。但是现在有一个新问题。我在左上角得到的不是固定点的旋转,而是之前的方块也没有消失。@navien如果您的消息以“@StanislavL”开头然后StanislavL可以很快看到您的消息。不,它没有解决问题。正方形实际上也在左上角的四分之一圆中平移。问题是我需要做2个更改:1)g2.rotate(θ,125125);2)super.paint(g);是,尽管我们应该使用paintComponent()但是我引用了我上面的代码,它也工作得很好well@Naveen并不是所有可行的方法都是好的。使用这种开发方法,迟早会遇到很多性能问题。
// 1.
g2.rotate(theta,125,125);
// 2.
super.paint(g);