Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help - Button Events - exiting out of while loops... HELP !
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
bioflare




PostPosted: Sat Jun 04, 2005 3:31 pm   Post subject: Help - Button Events - exiting out of while loops... HELP !

I'm writing a little code for a game, and im using a few while loops to control an incrementing number, the number slowly increases getting faster and faster. Now that i have that part done. I want to be able to start the with a button click and stop it at any point in time. But i am failing at doing this.
code:
//import java packages for applet, and event
import java.applet.*;
import java.awt.*;
import java.awt.event.*;


//class InputNamesFrame Frame
public class AnglePowerMeter extends Frame implements ActionListener
{


    private int click;
    //declare new borderlayout for the layout of the whole screen
    BorderLayout layout = new BorderLayout ();

    //declare panels for the borderlayout
    Panel center = new Panel ();
    Panel top = new Panel ();
    Panel left = new Panel ();
    Panel right = new Panel ();
    Panel bottom = new Panel ();
    Panel middle = new Panel ();

    //initialize labels to prompt users for input of names
    Label question = new Label ("Get Ready to choose your Power and Angle!", Label.CENTER);
    Label getAngle = new Label ("Angle", Label.LEFT);
    Label getPower = new Label ("Power", Label.LEFT);

    //new textfields for input of names
    TextField pAngle = new TextField (2);
    TextField pPower = new TextField (2);

    //buttons to continue and play, or reset all textfields, or cancel and go back to main menu
    Button startAngle = new Button ("Angle Start");

    Button startPower = new Button ("Power Start");
    Button cancel = new Button ("Cancel");

    //declare objects used in this object >.>
    //MeterEngine meter = new MeterEngine ();
    //double value of the angle to int value to string
    // String angle = Math.round (exp) + "";
    //constructor method
    AnglePowerMeter (String title)
    {

        super (title);

        resize (300, 150);

        setLayout (layout);

        top.setLayout (new FlowLayout ());
        top.add (question);
        add ("North", top);

        center.setLayout (new FlowLayout ());
        center.add (middle);
        add ("Center", center);

        middle.setLayout (new FlowLayout ());
        middle.add ("West", getAngle);
        getAngle.setBackground (Color.white);
        middle.add ("West", pAngle);
        pAngle.setEditable (false);
        middle.add ("East", getPower);
        getPower.setBackground (Color.white);
        middle.add ("East", pPower);
        pPower.setEditable (false);


        left.setLayout (new FlowLayout ());
        add ("West", left);

        right.setLayout (new FlowLayout ());
        add ("East", right);

        bottom.setLayout (new FlowLayout ());
        bottom.add (startAngle);
        //    startAngle.setMnemonic (KeyEvent.VK_A); //alt a shortcut
        startAngle.addActionListener (this);

        bottom.add (startPower);
        bottom.add (cancel);
        add ("South", bottom);

    }



    public void paint (Graphics g)
    {
        setBackground (Color.black);
    }


    public void actionPerformed (ActionEvent e)
    {
        Object source = e.getSource ();
        if (source == startAngle)
        {
            double c = 1.8, i = 1, max = 90, exp = 0;
            final double inc = 0.000355; //speed at while it increases
            boolean f = false, g = false;
           
            while (true)// #1
            {
                while (f == false) //increasing speed going up to 90
                {
                    exp = Math.pow (c, i);
                    i += inc;
                    pAngle.setText (Math.round (exp) + "");
                    if (exp > max)
                    {
                        f = true;
                        g = false;
                    } //end if

                } //end while
                while (g == false) //decreasing speed going down from 90
                {
                    exp = Math.pow (c, i);
                    i -= inc;
                    pAngle.setText (Math.round (exp) + "");
                    if (exp < 1)
                    {
                        g = true;
                        f = false;
                    } //end if
                } //end while
            } //end while
            //end while
            click++;
        }
        else if (source == startPower)
        {
            double c = 1.8, i = 1, exp = 0, max = 1000;
            final double inc = 0.000543; //speed at which it increases
            boolean f = false, g = false;
            while (true)//#2
            {
                while (f == false)
                {
                    exp = Math.pow (c, i);
                    pPower.setText (Math.round (exp) + "");

                    i += inc;
                    if (exp > max)
                    {
                        f = true;
                        g = false;
                    } //end if
                } //end while
                while (g == false)
                {
                    exp = Math.pow (c, i);
                    pPower.setText (Math.round (exp) + "");

                    i -= inc;
                    if (exp < 1)
                    {
                        g = true;
                        f = false;
                    } //end if
                } //end while
            } //end while
        }
        else if (source == cancel)
        {
            pAngle.setText ("");
            pPower.setText ("");
            dispose ();
        }

    }
}


At #1 and #2 are the increasing part and decreasing parts. I use the same type of loop for Increasing the angle and power. I want it so that when i click a button it starts, and when i click that button again it stops. Right now all it does is stay in the loop. Any suggestions?
Sponsor
Sponsor
Sponsor
sponsor
Aziz




PostPosted: Sat Jun 25, 2005 2:10 pm   Post subject: (No subject)

Java n00b here, but I've been reading my Ready to Program textbook my teacher let my borrow and if I'm not mistaken you could just use a

code:
break;


expression.
wtd




PostPosted: Sat Jun 25, 2005 3:54 pm   Post subject: (No subject)

Aziz wrote:
Java n00b here, but I've been reading my Ready to Program textbook my teacher let my borrow and if I'm not mistaken you could just use a

code:
break;


expression.


Yes.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: