2012-10-24 31 views
3

我已經開發了一個小的Swing應用程序,其中我使用單獨的類component將廣場添加到了我的JFrame中。現在我想在它的中心旋轉這個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); 
    } 
} 

可能有人請幫我找出並解決問題。

回答

-2

的問題是,我需要做出改變2:

// 1. 
g2.rotate(theta,125,125); 

// 2. 
super.paint(g); 
+0

2是** **錯了,請參閱@MikleGarin的回答 – kleopatra

+0

雅的子彈4雖然我們應該使用的paintComponent(),但我引用w.r.t我上面的代碼,它也完美的作品的作品是好的良好 –

+0

@Naveen不是萬能的。遲早,通過這種開發方法,您將會遇到很多性能問題。 –

3

theta變量在哪裏變化?

+0

感謝StanislavL,我不小心忘記了線。 但現在有一個新問題。 而不是在一個固定點旋轉我得到左上角的圖紙,前面的方塊也沒有消失。 –

+0

@Naveen如果您的信息以「@StanislavL」開頭,那麼StanislavL可以快速查看您的信息。 – chrome

1
public void actionPerformed(ActionEvent e) { 
    theta+= 10; // <------------I think prooblem was here.. 
    if (theta==360){ 
     theta=0; 
    } 
    repaint(); 
} 
+0

沒有,它沒有解決問題。廣場實際上也翻譯在左上角的四分之一圈。 –

+1

問題是,我需要做出2個變化: 1)g2.rotate(theta,125,125); 2)超級。漆(克); –

5

有很多在你的代碼錯誤:

  1. theta應雙倍,應在弧度,不度呈現。因此,使它雙:

    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的beggining旋轉; 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的東西優化,我不想在這裏談論,因爲它是一個大的offtopic。

  5. 您使用JPanel作爲繪製簡單形狀的基本組件 - 因爲您並不需要任何JPanel的功能(實際上沒有任何),所以改爲使用JComponent。

看到最後的工作示例:

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方法,使方形運動平穩,重構你的一些代碼。