Solved

changing colors in an applet

Posted on 2004-10-05
6
196 Views
Last Modified: 2010-03-31
hi in this program i need to  Change the colors of the ovals, maintaining the "gradient" effect of the original applet please help me am desperate hope you can solve my problem by the way am a student and if you can tell me step by step i will appriciete it .



import java.applet.*;
import java.awt.*;

//========================== HW04 CLASS ===========================

public class HW04 extends Applet
{
   
    public void paint(Graphics g)
    {
        // Draw the black background.
        g.setColor(Color.blue);
        g.fillRect(0, 0, 500, 350);
       
       
       
       
        // Draw ten nested "holes."
        drawHole(0, g);
        drawHole(1, g);
        drawHole(2, g);
        drawHole(3, g);
        drawHole(4, g);
        drawHole(5, g);
        drawHole(6, g);
        drawHole(7, g);
        drawHole(8, g);
        drawHole(9, g);
        drawHole(10,g);
        drawHole(11,g);
        drawHole(12,g);
        drawHole(13,g);
        drawHole(14,g);
        drawHole(15,g);
       
         
        // Draw the text, in white with a blue drop shadow.
        g.setColor(Color.black);
        g.setFont(new Font("Dialog", Font.BOLD, 35));
        g.drawString("Gooooooooooya", 22, 300);
       
        g.setColor(Color.yellow);
        g.drawString("Gooooooooooya", 20, 298);
       
       
    }
   
   
    private void drawHole(int n, Graphics g)
    {
        int X0 = 50,        // x-anchor
            Y0 = 50,        // y-anchor
            W0 = 350,        // initial width
            H0 = 200,        // initial height
            X_INC = 10,        // x-anchor increment
            Y_INC = 5,        // y-anchor increment
            RG_INC = 5,    // red-green increment
            BLUE_INC = 14;    // blue increment
               
        g.setColor(new Color(n * RG_INC, n * RG_INC, (n + 1) * BLUE_INC));
        g.fillOval(X0 + n * X_INC, Y0 + n * Y_INC,
                    W0 - n * X_INC, H0 - n * 2 * Y_INC);
    }
}



0
Comment
Question by:mijangos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12233280
>     g.setColor(new Color(n * RG_INC, n * RG_INC, (n + 1) * BLUE_INC));

you need to change that line to change the colour.
what colour do you want it to be?
0
 

Author Comment

by:mijangos
ID: 12233305
green or one that matches the background  please and canyou tell me the steps to do it  thanks
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 12233550
for more green try:

       g.setColor(new Color(0, n * RG_INC, 0);
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:mijangos
ID: 12233738
thanks alot it works just fine one more question i was doing this since last weekend my assignmet was to add more ovals and change the color of this code the one i just paste but it took me some time will you be kind if you can explain me the way i newbie like me will understand i want to know how to add more ovals and change the colors of this ovals cause to be honest when i add the ovals i just guess the numbers that i've changed in the variables and finally got it but it was a guess am not sure how to do it i will paste the very original source code and can you explain how to add two or more ovals to it and how to change the color cause you gave me the answer which i appriciete thats what i asked but can you tell me the steps so i can have this in my files for future programs thanks for my first question you answered .

this is the original code :

import java.applet.*;
import java.awt.*;


/**
 * This applet draws a scene consisting of ten nested blue ovals
 * on a black background, along with a text message.
 */
public class Wormhole extends Applet
{
    /**
     * Do all the drawing.
     */
    public void paint(Graphics g)
    {
        // Draw the black background.
        g.setColor(Color.black);
        g.fillRect(0, 0, 500, 350);
       
        // Draw ten nested "holes."
        drawHole(0, g);
        drawHole(1, g);
        drawHole(2, g);
        drawHole(3, g);
        drawHole(4, g);
        drawHole(5, g);
        drawHole(6, g);
        drawHole(7, g);
        drawHole(8, g);
        drawHole(9, g);
         
        // Draw the text, in white with a blue drop shadow.
        g.setColor(Color.blue);
        g.setFont(new Font("TimesRoman", Font.BOLD, 36));
        g.drawString("On to Cydonia!", 22, 300);
       
        g.setColor(Color.white);
        g.drawString("On to Cydonia!", 20, 298);
    }
   
    /**
     * Using the supplied Graphics object, draw the n-th hole.
     * n should be between 0 (darkest, largest) and 9 (lightest,
     * smallest).
     */
    private void drawHole(int n, Graphics g)
    {
        int X0 = 50,        // x-anchor
            Y0 = 50,        // y-anchor
            W0 = 350,        // initial width
            H0 = 200,        // initial height
            X_INC = 30,        // x-anchor increment
            Y_INC = 10,        // y-anchor increment
            RG_INC = 10,    // red-green increment
            BLUE_INC = 25;    // blue increment
           
        g.setColor(new Color(n * RG_INC, n * RG_INC, (n + 1) * BLUE_INC));
        g.fillOval(X0 + n * X_INC, Y0 + n * Y_INC,
                    W0 - n * X_INC, H0 - n * 2 * Y_INC);
    }
}



0
 
LVL 92

Expert Comment

by:objects
ID: 12233921
to paint another oval it looks like you need to add another call to drawHole()

        // Draw ten nested "holes."
        drawHole(0, g);
        drawHole(1, g);
        drawHole(2, g);
        drawHole(3, g);
        drawHole(4, g);
        drawHole(5, g);
        drawHole(6, g);
        drawHole(7, g);
        drawHole(8, g);
        drawHole(9, g);

to add another try adding:

drawHole(10, g);

colour is specified using the red, green and blue components (0-255) in the following caLL:

    g.setColor(new Color(n * RG_INC, n * RG_INC, (n + 1) * BLUE_INC));

red = n * RG_INC
blue = n * RG_INC
green = (n + 1) * BLUE_INC
0
 

Author Comment

by:mijangos
ID: 12234109
yes i add the (10,g); but in order for that oval to appear you need to modify the variables which i cant
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question