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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:23:17+00:00 2026-06-17T08:23:17+00:00

I have the code import java.awt.*; import javax.swing.*; public class MondrianPanel extends JPanel {

  • 0

I have the code

import java.awt.*;
import javax.swing.*;

public class MondrianPanel extends JPanel
{
    public MondrianPanel()
    {
        setPreferredSize(new Dimension(200, 600));
    }

    public void paintComponent(Graphics g) {
        for(int i = 0; i < 20; i++) {
            paint(g);
        }
    }

    public void paint(Graphics g)
    {
        Color c = new Color((int)Math.random()*255, (int)Math.random()*255, (int)Math.random()*255);
        g.setColor(c);
        g.fillRect((int)Math.random()*200, (int)Math.random()*600, (int)Math.random()*40, (int)Math.random()*40);
    }

}

What I’m trying to get it to do is draw a bunch of randomly colored rectangles at random places on the screen. However, when I run it, I just get a gray box. I was reading this question Drawing multiple lines with Java Swing and I saw that you should have a single paintComponent that calls paint a bunch of times and I tried adapting my code to this, but it still doesn’t work.

  • 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-17T08:23:18+00:00Added an answer on June 17, 2026 at 8:23 am

    The biggest problem here is that the (int) Math.random() * something is always 0. That’s because the cast is executed first and is 0 and then multiplied by something is still 0.

    It should be something like this: (int) (Math.random() * something).

    Then, you should rename paint(Graphics g) to draw(Graphics g), otherwise you are overwriting paint in a wrong way.

    The code below is working as you need:

    public class TestPanel extends JPanel {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            for (int i = 0; i < 20; i++) {
                draw(g);
            }
        }
    
        public void draw(Graphics g) {
            Color c = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
            g.setColor(c);
            g.fillRect((int) (Math.random() * 400), (int) (Math.random() * 300), (int) (Math.random() * 40), (int) (Math.random() * 40));
        }
    
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.getContentPane().add(new TestPanel(), BorderLayout.CENTER);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setSize(400, 300);
            f.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sample code : import java.awt.*; import javax.swing.*; import javax.swing.border.BevelBorder; public class
Please have a look the following code import javax.swing.*; import java.awt.event.*; import java.awt.*; public
I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
Please have a look at the following code import java.awt.*; import java.awt.event.*; import javax.swing.*;
Please have a look at the following code import java.awt.*; import java.awt.event.*; import javax.swing.*;
Please have a look at the following code import java.awt.*; import java.awt.event.*; import javax.swing.*;
Please have a look at the following code import java.awt.*; import java.awt.event.*; import javax.swing.*;
Please have a look at the following code import java.awt.event.*; import javax.swing.*; import java.awt.*;
Please have a look at the following code import java.awt.*; import javax.swing.JTable; import javax.swing.*;
Please have a look at the following code import java.awt.event.*; import javax.swing.*; import java.awt.*;

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.