2013-11-04 78 views
-1

我在網上找到了一張圖片(http://i.stack.imgur.com/y1oT4.png),我正在嘗試拍攝太陽和天空,並使它們圍繞屏幕中心旋轉,以便太陽及其光線似乎在旋轉。圍繞單個點旋轉一組Graphics對象?

我打算使用計時器來控制運動,但我無法弄清楚如何以任意角度旋轉。換句話說,我知道如何以90的增量旋轉(切換寬度和高度),但是我想要做的是將一組對象分組並圍繞一個點旋轉它們。

我環顧四周,發現了AffineTransform()方法,但我無法弄清楚這是否真的是我需要的或者如何使用它。

編輯:這是否解決我的問題? How to rotate Graphics in Java我會嘗試並更新。

編輯:它讓我更接近,但沒有解決它。它返回此運行時錯誤:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at FallScene.rotateBack(FallScene.java:77) 
    at SceneDriver$1TimerListener.actionPerformed(SceneDriver.java:66) 
    at javax.swing.Timer.fireActionPerformed(Timer.java:312) 
    at javax.swing.Timer$DoPostEvent.run(Timer.java:244) 
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) 
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705) 
    at java.awt.EventQueue.access$000(EventQueue.java:101) 
    at java.awt.EventQueue$3.run(EventQueue.java:666) 
    at java.awt.EventQueue$3.run(EventQueue.java:664) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo 
    main.java:76) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:675) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre 
    ad.java:211) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread. 
    java:128) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre 
    ad.java:117) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90) 
    Press any key to continue... 

在FallScene.rotateBack(FallScene.java:77)電話是:

bg.rotate(Math.toRadians(deg)); 

...都到:

public void paintComponent(Graphics g) 
{ 
    super.paintComponent(g); 

    // Get the size of the component window 
    int w = getWidth(); 
    int h = getHeight(); 

    // The Graphics2D object for the BACKGROUND 
    Graphics2D bg = (Graphics2D)g;   

    // Sun 
    Color solarYellow = new Color(255, 218, 0); 
    bg.setPaint(solarYellow); 
    Ellipse2D.Double sun = new Ellipse2D.Double((w/2) - 150, (h/2) - 150, 300, 300); 
    bg.fill(sun); bg.draw(sun); 
} 

回答

0

如果你仍然需要它,我認爲這個可操作和評論的代碼應該可以幫助你理解如何繪製它。

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.geom.Ellipse2D; 
import java.awt.geom.Line2D; 
import java.awt.geom.Path2D; 
import java.util.TimerTask; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 
import javax.swing.Timer; 

public class SunRotate extends JComponent 
{ 
    public static void main(String[] args) { 
     final SunRotate sunRotate = new SunRotate(45); 
     JFrame f = new JFrame(); 
     f.setContentPane(sunRotate); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.pack(); 
     f.setSize(new Dimension(800, 600)); 
     f.setVisible(true); 
     new java.util.Timer().scheduleAtFixedRate(new TimerTask() { 
      @Override 
      public void run() { 
       sunRotate.deltaAngle(.3f); 
       sunRotate.repaint(); 
      } 
     }, 16, 16); // every 16 milliseconds 
    } 


    private float angle; 

    public void deltaAngle(float delta) { 
     angle += delta; 
    } 

    public SunRotate(float angle) { 
     this.angle = angle; 
    } 

    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 

     int w = getWidth(); 
     int h = getHeight(); 

     // Recover Graphics2D 
     Graphics2D g2 = (Graphics2D) g; 

     // Move and rotate 
     g2.translate(w/2.0, h/2.0); 
     g2.rotate(Math.toRadians(angle)); 


     // draw around 0,0 
     Color solarYellow = new Color(255, 218, 0); 
     g2.setPaint(solarYellow); 
     Ellipse2D.Double sun = new Ellipse2D.Double(-150, -150, 300, 300); 
     g2.fill(sun); 
     { // draw some rays because the sun is round so we don't see the rotation 
      // make a ray (triangle) 
      Path2D ray = new Path2D.Float(); 
      ray.moveTo(0, 0); 
      ray.lineTo(1000, 50); 
      ray.lineTo(1000, -50); 
      ray.closePath(); 
      // draw N rays, rotating each time 
      int N = 20; 
      for (int i = 0; i < N; i++) { 
       g2.fill(ray); 
       g2.rotate(Math.PI * 2/N); 
      } 
     } 
    } 

} 
+0

這會作爲一個獨立文件運行嗎? – IndigoContinuum

+0

是的,它是一個完整的文件 –