Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9055049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:50:56+00:00 2026-06-16T13:50:56+00:00

This is probably an elementary question. However, I have completed reading the 9th Chapter

  • 0

This is probably an elementary question. However, I have completed reading the 9th Chapter of Java Programming for the Absolute Beginner and have approached the Challenges section. I cannot quite get the progam to show a gradient from dark to light.

The question asks:

“Create a Canvas that paints a gradient that’s dark on one side and slowly gets lighter
as it moves to the other side.”

I have looked at the Java Challenge on Creating a Canvas page (which I asked about before) and got a better understanding of creating a color gradient canvas, however, I am still having great difficulty.

I think that my main problem is how to get the gray colors in between as the program shows a completely black square or when run with just the first for loop, a completely white square. I THINK THAT MY FOR LOOPS ARE PROBLEMATIC AND DO NOT CORRECTLY IDENTIFY THE VARIABLES.

An answer to this question can potentially aid many new Java programmers in understanding Graphics and Canvas.

I do not know JFrame, Swing, Points, JPanels, BufferedImage, or GradientPaint.

Thank you very much for your time and cooperation reagrding this matter.

HERE IS THE CODE:

import java.awt.*;

public class RandomColorSquare extends GUIFrame {
    Canvas slight;
    public final static int MIN = 0,
        MAX = 225;

    public RandomColorSquare(int r, int g, int b) {
        super("Random Color Square");
        r = r >= MIN && r <= MAX ? r : MIN;
        g = g >= MIN && g <= MAX ? g : MIN;
        b = r >= MIN && b <= MAX ? b : MIN;
        slight = new Canvas();
        slight.setBackground(new Color(r,g, b));
        slight.setSize(200, 150);
        add(slight, BorderLayout.CENTER);

        for(r=0; r<225; r++) {
            Color c = slight.getBackground().brighter();
            slight.setBackground(c);
        }

        for (g=0; g<225; g++) {
            Color d = slight.getBackground().darker();
            slight.setBackground(d);
        }
        for (b=0; b<225; b++) {
             Color e = slight.getBackground().darker();
             slight.setBackground(e);
        }

        pack();
        setVisible(true);
    }

    public static void main(String args[]) {
        if (args.length != 3) {
            new RandomColorSquare(0, 0, 0);
        }
        else {
            new RandomColorSquare(Integer.parseInt(args[0]),                                     Integer.parseInt(args[1]),
                   Integer.parseInt(args[2]));
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:50:57+00:00Added an answer on June 16, 2026 at 1:50 pm

    First of all, as I mentioned in the comment, when you setBackground you are changing the background of the Canvas, not adding to it. So whatever you set it to last is what you see. In your case, that is the darkest form of blue, which is just black. If you comment out the last two loops, you get a white background (the brightest red).

    However. Using only what you know this is the best I can come up with. It involves creating your own custom Canvas, which I called CustomCanvas. In there, you can override the paint method and keep drawing progressively lighter filled rectangles across the screen. Here’s a small sample to help you understand what I mean:

    class CustomeCanvas extends Canvas{
        public void paint(Graphics g){
            Color background = new Color(30,30,120); //Set this accordingly
            for(int i=0;i<getWidth();i++){
                g.setColor(background);
                background = getBrighter(background);
                g.fillRect(i,0, 1, getHeight());
            }
        }
    
        private Color getBrighter(Color c) {
            int r = c.getRed();
            int g = c.getGreen();
            int b = c.getBlue();
    
            if(r< MAX) r+=1;
            if(g< MAX) g+=1;
            if(b< MAX) b+=1;
    
            return new Color(r,g,b);
        }
    }
    

    Which produces this background:

    enter image description here

    I still recommend reading about GradientPaint which makes this process a lot easier and nicer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably an elementary question. However, I have completed reading the 9th Chapter
This is probably an elementary question. However, I have completed reading the 5th Chapter
I'm fairly new to OO programming specifically with Java. I have a question pertaining
This probably is a dummy question but I cannot find a clear indication. I
Im sorry for this probably dumm question, but I want to simply open modals
This probably sounds like a really dumb question, but here goes....Web Services, what the
This probably is a very very basic question but i can't seem to find
I know this probably is a fairly complicated question but... heres my case: I
I know this is probably elementary to unix people, but I haven't found a
This is probably very elementary, but I'm still learning. I've imported a record from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.