Spinning a circle in J2ME using a Canvas.

Posted by JohnQPublic on Stack Overflow See other posts from Stack Overflow or by JohnQPublic
Published on 2010-05-19T22:17:01Z Indexed on 2010/05/19 22:20 UTC
Read the original article Hit count: 232

Filed under:
|
|

Hello all! I have a problem where I need to make a multi-colored wheel spin using a Canvas in J2ME. What I need to do is have the user increase the speed of the spin or slow the spin of the wheel. I have it mostly worked out (I think) but can't think of a way for the wheel to spin without causing my cellphone to crash. Here is what I have so far, it's close but not exactly what I need.

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();
    }
}

I have tried using a redraw() method that adds the spinSpeed to each of the wedge values while(spinSpeed>0) and calls the repaint() method after the addition, but it causes a crash and lockup (I assume due to an infinite loop). Does anyone have any tips or ideas how I could automate the spin so you do not have the press the button every time you want it to spin?

(P.S - I have been lurking for a while, but this is my first post. If it's too general or asking for too much info (sorry if it is) and I either remove it or fix it. Thank you!)

© Stack Overflow or respective owner

Related posts about j2me

Related posts about homework