Link to home
Start Free TrialLog in
Avatar of mijangos
mijangos

asked on

changing colors in an applet

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



Avatar of Mick Barry
Mick Barry
Flag of Australia image

>     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?
Avatar of mijangos
mijangos

ASKER

green or one that matches the background  please and canyou tell me the steps to do it  thanks
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);
    }
}



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
yes i add the (10,g); but in order for that oval to appear you need to modify the variables which i cant