【发布时间】:2014-04-30 10:16:38
【问题描述】:
我是 Java 新手,需要一些帮助!我的教授希望我创建 4 种方法,最终目标是输出一个动画旋转方块,就像我在之前的代码中所做的那样:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GTest2 {
public static void main(String[] args) {
GraphicsPanel gp = new GraphicsPanel();
gp.delay(1000);
int x = gp.getWidth() / 2;
int y = gp.getHeight() / 2;
gp.setLocation(x, y);
for (int n=1; n <= 150; n++) {
gp.clear();
for (int angle=0; angle < 360; angle += 10) {
for (int i=0; i < 4; i++) {
gp.draw(100);
gp.turn(90);
}
gp.turn(10);
}
gp.render();
gp.delay(30);
gp.turn(2);
}
}
}
我的教授为我们提供了 GraphicsPanel 类,因此我们可以执行这些创建形状和动画的程序:http://pastebin.com/Ha1pdLrc
这是我目前所拥有的,但我似乎只能创建一个旋转正方形:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class G7 {
public static void main(String[] args) {
GraphicsPanel gp = new GraphicsPanel();
gp.delay(1000);
animateCircleOfPolygons(gp);
}
public static void animateCircleOfPolygons(GraphicsPanel gp){
for (int n = 1; n <= 150; n++) {
gp.clear();
drawCircleOfPolygons(gp);
gp.render();
gp.delay(30);
gp.turn(2);
}
}
public static void drawCircleOfPolygons(GraphicsPanel gp){
for (int angle = 0; angle < 360; angle += 10) {
drawPolygon(gp, 100, 4);
gp.turn(10);
}
}
public static void drawPolygon(GraphicsPanel gp, int sideLength, int sideCount){
for (int i = 0; i < sideCount; i++) {
gp.draw(sideLength);
gp.turn(360.0 / sideCount);
gp.delay(30);
}
}
}
我到处寻找无济于事的答案,谁能帮我理解我做错了什么。
【问题讨论】:
-
"最终目标是输出一个动画旋转正方形" -> "我似乎只能创建一个旋转正方形"。这听起来像是在做你想做的事,不......?
-
不完全是,我可以让一个正方形旋转多次,但我真正想要的是动画,我的意思是形状滚动的错觉。如果您测试第一组代码,您就会明白我的意思。
-
graphicspanel 在我上面提供的 pastebin 中,也感谢帮助! pastebin.com/Ha1pdLrc
标签: java animation user-interface methods awt