Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Applets

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Applets

    Hey, I've been set the following question as part of my programming class:
    Write an applet that draws a series of concentric circles, alternating between red and blue in colour, the largest of which fits exactly within the applet drawing area. Use loops as appropriate. The number of circles should be specified as a parameter to the applet; if unspecified or invalid, it should default to 2.
    You may need to use the following methods of the Graphics class: setColor(Color c): set the drawing colour drawOval(x,y,xdia,ydia): draws an oval

    I'm able to draw the concentric circles with no problem but the changing of the colour for every second one is the part that is causing me problems....any help welcome
    Santiagomez


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,592
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Applets

    changing of the colour for every second one is the part that is causing me problems
    You need some logic to select a color for every circle. Many ways to do this.
    Have a method that returns a color and call that for the next color to use.
    Now use some brain power and think how the method can return a different color every time it is called.
    How about an array of colors that it steps thru with an index.

  3. #3
    Member
    Join Date
    Jun 2010
    Posts
    48
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Re: Applets

    Here is one way. Changes the green color from the RGB. This way it goes from nearly white to violet and vice versa. Just the timer is missing which would repaint the oval after particular delay.
    RGBgreen = 2;
    direction = 1;
    ---------------------------------------------------
    RGBgreen += direction;
    if(RGBgreen <= 1 || RGBgreen >= 254)
                direction = -direction;
    Color theColor = new Color(192, RGBgreen, 192);
    g.setColor(theColor);
    g.drawOval(x, y, width, height);
    EDIT: sorry if helped you too much. I gave you the answer.
    Last edited by Asido; July 31st, 2010 at 06:05 PM.

Similar Threads

  1. Help needed with applets
    By Brt93yoda in forum What's Wrong With My Code?
    Replies: 16
    Last Post: July 12th, 2010, 07:36 PM
  2. Audioclip play makes applets slow
    By Marcus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 19th, 2010, 12:20 PM
  3. Learn applets in java
    By mohsendeveloper in forum Java Applets
    Replies: 2
    Last Post: June 25th, 2009, 02:50 PM