Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

changing colors in an applet

Posted on 2004-10-05
6
Medium Priority
?
200 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 2000 total points
ID: 12233550
for more green try:

       g.setColor(new Color(0, n * RG_INC, 0);
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

609 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