Solved

changing colors in an applet

Posted on 2004-10-05
6
191 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
  • 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now