[英]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.