Results 1 to 28 of 28

Thread: Java problem, totally confused!

Hybrid View

  1. #1
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    Re: Java problem, totally confused!

    i could get you started quickly before i go to work...
    you could do something like this, u just need to add a textfield and stuff.

    Code:
    import java.awt.*;
       import java.awt.event.*;
       import java.applet.*;
       
       public class class1
           extends Applet
           implements ActionListener
       {
         private Button btnColor;
         private String[] strColors =
            { "White", "Yellow", "Green", "Pink" };
         private Color[] clColors =
            { Color.white, Color.yellow,
              Color.green, Color.pink };
         private int index;
       
         public void init ()
         {
           setBackground (Color.lightGray);
           index = 0;
           btnColor = new Button (strColors [index]);
           btnColor.setBackground (clColors [index]);
           btnColor.addActionListener (this);
           add (btnColor);
         }
       
         public void actionPerformed (ActionEvent event)
         {
           if (++index >= strColors.length)
             index = 0;
           btnColor.setLabel (strColors [index]);
           btnColor.setBackground (clColors [index]);
         }
       }
    Last edited by TBeck; May 5th, 2006 at 02:33 PM.

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    18

    Re: Java problem, totally confused!

    Thankyou TBeck

    I'm starting to like you heh...

    I'll play around with your code and it'll come in handy knowing the colour commands, but... going to see what i can do about switching it around to produce circle cirumferences, i knew what to do at the time but i always go brain dead when i actually go to do it lol

    Pyra

  3. #3
    Addicted Member TBeck's Avatar
    Join Date
    Apr 2006
    Location
    Ontario, Canada
    Posts
    254

    Re: Java problem, totally confused!

    yea the program changes the colors but the code you need is a similar structure with the actions. Glad to help again

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    18

    Re: Java problem, totally confused!

    Ok... i have the program fully working now, BUT i want to make a few little changes to it to make it a bit fancier and more versatile...

    What i'd like to do is make the text entries labels so they will move with applet window resizing and everything will stay aligned. I've been playing about with labels and keep getting a blank screen, nothing appears until the window is resized, then it all starts multiplying and it just looks stupid heh.

    Is there a simple way to make my labels appear upon running the applet and NOT multiply?

    Secondly... how the hell do you tell the label where you want it to go!? They all come out in one single line lol

    Pyra

    Here is my fully working code:

    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.math.*;

    public class As3task1 extends Applet
    implements ActionListener
    {
    TextField radField;
    Double diameter;
    Double circumference;
    Double radius;


    public void paint(Graphics g)
    {
    g.drawString("Enter the radius of your circle in the",220,60);
    g.drawString("box below to find the circumference!",218,80);
    g.drawString("The circumference of this circle is: "+circumference+" centimetres",210,125);
    }

    public void init()
    {
    radField = new TextField(15);
    add(radField);
    add (new Label ("centimetres"));
    radField.addActionListener (this);
    }

    public void actionPerformed (ActionEvent event)
    {
    radius = Double.parseDouble(radField.getText());
    diameter = radius * 2;
    circumference = diameter * Math.PI;
    repaint();
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width