Solved

changing colors in an applet

Posted on 2004-10-05
6
193 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySqlDump not dumping triggers 1 30
how to see all occupied ports on windows 10 laptop 15 61
Chrome and Firefox Java 5 27
Java: anonymous class 4 17
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

786 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