-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);
}
這會作爲一個獨立文件運行嗎? – IndigoContinuum
是的,它是一個完整的文件 –