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

  • SEARCH
  • Home
  • 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 8895681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:50:46+00:00 2026-06-14T23:50:46+00:00

I’ve got my code functioning and repeatedly changing the colors of the squares in

  • 0

I’ve got my code functioning and repeatedly changing the colors of the squares in my code, but all the squares are the same colors when the change.

Main program:

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.util.Random;
import javax.swing.Timer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Canvas;
import javax.swing.JPanel;

public class RandomColoredBoxes extends JPanel
{
private Timer timer;
private final static int SLEEP = 110;

public RandomColoredBoxes()
{       
    setBackground(Color.BLACK);
    setVisible(true);

    ActionListener paintCaller = new ActionListener(){
        public void actionPerformed(ActionEvent event)
        {
            repaint();  
        }
    };
    timer = new Timer(SLEEP, paintCaller);
    timer.start();
}   

public void paintComponent( Graphics window )
{
    super.paintComponent(window);

    window.setColor(Color.RED);
    window.setFont(new Font("TAHOMA",Font.BOLD,12));
    window.drawString("Graphics Lab Lab11k ", 20, 40);
    window.drawString("Drawing boxes with nested loops ", 20, 80);

    drawBoxes(window);
}

public void drawBoxes(Graphics window)
{
    int colorValue1 = (int)(Math.random() * 256);
    int colorValue2 = (int)(Math.random() * 256);
    int colorValue3 = (int)(Math.random() * 256);

    Color random = new Color(colorValue1, colorValue2, colorValue3);
    window.setColor(random);
    //for loop to to across the x - getWidth() might be useful
    for(int x = 30; x <= getWidth()- 30; x+=15){
        for(int y = 100; y <= getHeight() - 30; y+= 15){
            window.fillRect(x, y, 8, 8);
        }
    }
        //for loop to go down the y - getHeight() might be useful

            //draw random colored boxes
}
}

And my graphics runner class:

import javax.swing.JFrame;

public class GraphicsRunnerRandom extends JFrame
{
private static final int WIDTH = 800;
private static final int HEIGHT = 600;

public GraphicsRunnerRandom()
{
    super("Graphics Runner");
    setSize(WIDTH,HEIGHT);

    getContentPane().add(new RandomColoredBoxes());                 

    //getContentPane().add(new RandomColoredBoxes());

    setVisible(true);
}

public static void main( String args[] )
{
    GraphicsRunnerRandom run = new GraphicsRunnerRandom();
}
}

other than the squares changing all-together, everything else is functioning as it should

  • 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-14T23:50:48+00:00Added an answer on June 14, 2026 at 11:50 pm

    Instead of this:

    int colorValue1 = (int)(Math.random() * 256);
    int colorValue2 = (int)(Math.random() * 256);
    int colorValue3 = (int)(Math.random() * 256);
    
    Color random = new Color(colorValue1, colorValue2, colorValue3);
    window.setColor(random);
    //for loop to to across the x - getWidth() might be useful
    for(int x = 30; x <= getWidth()- 30; x+=15){
        for(int y = 100; y <= getHeight() - 30; y+= 15){
            window.fillRect(x, y, 8, 8);
        }
    }
    

    Try this:

    //for loop to to across the x - getWidth() might be useful
    for(int x = 30; x <= getWidth()- 30; x+=15){
        for(int y = 100; y <= getHeight() - 30; y+= 15){
            int colorValue1 = (int)(Math.random() * 256);
            int colorValue2 = (int)(Math.random() * 256);
            int colorValue3 = (int)(Math.random() * 256);
    
            Color random = new Color(colorValue1, colorValue2, colorValue3);
            window.setColor(random);
    
            window.fillRect(x, y, 8, 8);
        }
    }
    

    The basic difference is creating the new color (and calling window.setColor as well) from inside the loop- that way each time the loop starts, before drawing in the box, a new random color will be used.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.