[英]Spinning a circle in J2ME using a Canvas
我有一個問題,需要在J2ME中使用Canvas進行多色車輪旋轉。 我需要做的是讓用戶增加旋轉速度或減慢車輪的旋轉速度。 我已經解決了大部分問題(我認為),但是想不出一種方法來使車輪旋轉而又不會導致手機崩潰。 到目前為止,這是我所擁有的,雖然很接近,但並不完全是我所需要的。
class MyCanvas extends Canvas{
//wedgeOne/Two/Three define where this particular section of circle begins to be drawn from
int wedgeOne;
int wedgeTwo;
int wedgeThree;
int spinSpeed;
MyCanvas(){
wedgeOne = 0;
wedgeTwo = 120;
wedgeThree = 240;
spinSpeed = 0;
}
//Using the paint method to
public void paint(Graphics g){
//Redraw the circle with the current wedge series.
g.setColor(255,0,0);
g.fillArc(getWidth()/2, getHeight()/2, 100, 100, wedgeOne, 120);
g.setColor(0,255,0);
g.fillArc(getWidth()/2, getHeight()/2, 100, 100, wedgeTwo, 120);
g.setColor(0,0,255);
g.fillArc(getWidth()/2, getHeight()/2, 100, 100, wedgeThree, 120);
}
protected void keyPressed(int keyCode){
switch (keyCode){
//When the 6 button is pressed, the wheel spins forward 5 degrees.
case KEY_NUM6:
wedgeOne += 5; wedgeTwo += 5; wedgeThree += 5;
repaint();
break;
//When the 4 button is pressed, the wheel spins backwards 5 degrees.
case KEY_NUM4:
wedgeOne -= 5; wedgeTwo -= 5; wedgeThree -= 5;
repaint();
}
}
我嘗試使用redraw()方法將spinSpeed添加到每個楔形值while(spinSpeed> 0)並在添加后調用repaint()方法,但它會導致崩潰和鎖定(我假設由於無限大環)。 有沒有人有任何提示或想法,我將如何使旋轉自動進行,這樣您就不必在每次旋轉時都按下按鈕?
(PS-我已經潛伏了一段時間,但這是我的第一篇文章。如果它太籠統或要求提供太多信息(對不起,請問我是刪除還是修復它。謝謝!)
我認為您應該創建一個線程,並在該線程的run方法中調用此paint / repaint方法。 如果您想讓按鍵運行線程而不是簡單地:
case KEY_NUM6: thread.start;
鑒於您還沒有回應,即使我的經驗不在J2ME中,我也想盡自己的力量。 我有一些Java2D的實踐,在J2ME上可能會實現,盡管有人可以告訴我是否有其他實踐!
如果您能夠使用JPanel,則可以設計一個類似於我以前使用過的類似游戲周期的設計。
這個想法:以給定的時間間隔,詢問必須繪制的每個實體(在本例中為楔形),更新它以找到新位置,並要求其繪制自身。 為了說明這一點,這是我以前使用過的GamePanel的精簡版-
public class GamePanel extends JPanel {
Timer loopTimer;
boolean changed = false;
ArrayList<Entity> entities = new ArrayList<Entity>();
int spinSpeed = 1;
int pollingMillis = 20;
public GamePanel () {
setBackground(Color.black);
setDoubleBuffered(true);
entities.add(new Wedge(0,0,0,90,Color.red)); // See below about these lines
entities.add(new Wedge(0,0,90,90,Color.blue));
entities.add(new Wedge(0,0,180,90,Color.yellow));
entities.add(new Wedge(0,0,270,90,Color.green));
loopTimer = new Timer();
loopTimer.scheduleAtFixedRate(new runLoop(), 0, pollingMillis);
}
public void addEntity(Entity e) {
entities.add(e);
}
@Override
public void paint (Graphics graph) {
super.paint(graph);
Graphics2D g = (Graphics2D) graph;
for (Entity e: entities) {
e.draw(g);
}
g.dispose();
}
private class runLoop extends TimerTask {
@Override
public void run() {
for (Entity e: entities) {
e.update();
}
if (changed) {
repaint();
changed = false;
}
}
}
}
這將創建一個基本循環,使您可以將項目渲染到JPanel中。 上面的代碼添加了4個“楔”,盡管您還需要更多代碼。 為此,您需要以下抽象類:
public abstract class Entity {
int x,y;
public Entity (int x, int y) {
this.x = x;
this.y = y;
}
public abstract void update();
public abstract void draw(Graphics2D g);
}
現在,您要渲染的實體擴展了該類,從而允許將它們添加到渲染周期中。 例如,楔子類-私有類Wedge擴展了Entity {int startAngle; int arcLength; 顏色顏色;
public Wedge (int x, int y, int start, int length, Color color) {
super(x,y);
startAngle = start;
arcLength = length;
this.color = color;
}
@Override
public void update() {
startAngle -= spinSpeed;
changed = true;
}
@Override
public void draw(Graphics2D g) {
g.setColor(color);
g.fillArc(x, y, 200, 200, startAngle, arcLength);
}
}
然后,這應允許您渲染旋轉的動畫圓! 我希望其中一些可以轉移到J2ME,並且我也希望您理解該代碼可能不是最佳的,如果您希望我擴展其中的任何內容,請發表評論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.