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 637829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:39:59+00:00 2026-05-13T20:39:59+00:00

How can I fill color on the grid boxes randomly? Rather than orderly as

  • 0

How can I fill color on the grid boxes randomly?

Rather than orderly as shown in picture here:

Grid http://www.freeimagehosting.net/uploads/4ed76557de.jpg

public class grid extends JPanel{
    Label one = new Label();
    Label two = new Label();
    Label three = new Label();
    Label four = new Label();

    public static void main(String[] args){
        JFrame jf=new JFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(new YAnswers());
        jf.pack();
        jf.setVisible(true);
    }

    grid (){
       int rows=10; int cols=10;
       setLayout(new GridLayout(rows,cols));
       add(one); one.setBackground(Color.red);
       add(two); two.setBackground(Color.orange);
       add(three); three.setBackground(Color.green);
       add(four); four.setBackground(Color.black);
       boxes[] bx=new boxes[rows*cols];

        for(int i=0;i<rows*cols;i++){
            System.out.println("i"+i);
            bx[i]=new boxes();
            if(i%2<1)
                bx[i].setColor(1);
            add(bx[i]);
        }
    } //end grid()
}
  • 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-05-13T20:39:59+00:00Added an answer on May 13, 2026 at 8:39 pm

    You can get a random color by using Math.random:

    new Color( (float)Math.random(), (float)Math.random(), (float)Math.random() );
    

    BTW: Class-names start with upper-case in Java, so use Grid instead of grid.


    EDIT

    The following code uses GridBagLayout to produce this result:

    alt text http://img214.imageshack.us/img214/5426/so2374295.png

    public Grid ()
    {
        final Color BACKGROUND = Color.LIGHT_GRAY;
        final Color[] colors = new Color[]
            {Color.BLACK, Color.BLACK, Color.BLUE, Color.BLUE};
    
        final int ROWS=10;
        final int COLS=10;
    
        setBackground(Color.BLACK);
        setLayout(new GridBagLayout());
    
        Label[][] label = new Label[ROWS][COLS];
    
        GridBagConstraints gc = new GridBagConstraints();
        gc.weightx = 1d;
        gc.weighty = 1d;
        gc.insets = new Insets(0, 0, 1, 1);
        gc.fill = GridBagConstraints.BOTH;
    
        // fill the whole panel with labels
        for( int r=0 ; r<ROWS ; r++) {
            for( int c=0 ; c<COLS ; c++) {
                Label l = new Label();
                l.setBackground(BACKGROUND);
                gc.gridx = r;
                gc.gridy = c;
                add(l, gc);
                label[r][c] = l;
            }
        }
    
        // now find random fields for the colors defined in BACKGROUND
        for(Color col : colors) {
            int r, c;
            do { // make sure to find unique fields
                r = (int)Math.floor(Math.random() * ROWS);
                c = (int)Math.floor(Math.random() * COLS);
            } while(!label[r][c].getBackground().equals(BACKGROUND));
            label[r][c].setBackground(col);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a better way I can fill out a spread sheet on a
I need to backup the original data from a table so I can fill
Can somebody point me to a resource that explains how to go about having
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can you cast a List<int> to List<string> somehow? I know I could loop through
can you recommend some good ASP.NET tutorials or a good book? Should I jump
Can a LINQ enabled app run on a machine that only has the .NET
Can anyone tell me how I can display a status message like 12 seconds
Can you tell me what is the difference between abstraction and information hiding in
Can I get a 'when to use' for these and others? <% %> <%#

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.