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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:26:25+00:00 2026-05-31T20:26:25+00:00

Basic summary: Working on a game that generates a 15×15 grid for the player

  • 0

Basic summary: Working on a game that generates a 15×15 grid for the player to move around in. Each cell in the grid as an image that is randomly generated with shades of brown so each cell looks different. Only problem is I’m trying to create a buffered image for each cell that holds the random series of colors. My problem in the Dirt class is it won’t call the paintComponent method. Here is the Dirt class. (passed in a rectangle of the cell in constructor)

package game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

public class Dirt extends JPanel{
    private Rectangle rect;
    private BufferedImage image;
    private int pixelSize;

    public Dirt(Rectangle x){
        rect = x;
        image = new BufferedImage(rect.width, rect.width, BufferedImage.TYPE_INT_RGB);
        pixelSize = rect.width/15;
        setVisible(true);
        validate();
        this.repaint();
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = image.createGraphics();
        Rectangle[][] rects = new Rectangle[15][15];
        Color[] colors = {new Color(160,82,45),new Color(139,69,19),new Color(165,42,42)};
        java.util.Random randomGenerator = new java.util.Random();
        for(int i = 0; i < 15; i++){
            for(int j = 0; j < 15; j++)
                rects[i][j] = new Rectangle(pixelSize*i,pixelSize*j,pixelSize,pixelSize);
        }
        for(int i = 0; i < 15; i++){
            for(int j = 0; j < 15; j++){
                System.out.println(i + " " + j);
                g2d.setColor(colors[randomGenerator.nextInt(3)]);
                g2d.fillRect(rects[i][j].x, rects[i][j].y, rects[i][j].width, rects[i][j].height);
            }
        }
        g2d.dispose();
    }

    public Image getImage(){return image;}
    public Rectangle getRect(){return rect;}
}
  • 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-31T20:26:26+00:00Added an answer on May 31, 2026 at 8:26 pm

    Add the following to the end of your paintComponent method:

    g.drawImage(image, 0, 0, this);
    

    Like @Carl Manaster says in his comment, you’ve drawn to the buffered image, but you’re not then drawing the buffered image to the Graphics object passed into the paintComponent method.

    EDIT: I added the following to your class to test, and included my code line above in the paintComponent method and it works for me:

    public static void main(String args[]) {
        JFrame f = new JFrame();
    
        f.addWindowListener(new WindowAdapter() {
    
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
    
        });
    
        f.getContentPane().setLayout(null);
    
        Dirt d = new Dirt(new Rectangle(40, 40));
        d.setBounds(20, 20, 64, 64);
        f.getContentPane().add(d);
    
        f.setSize(300, 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've got a WCF service that is working for basic queries. I started with
Summary: I am working on a website that connects to Facebook through the Graph
Basic doubt...If QTP generates vbscript code as we record actions, can't we directly write
I noticed that Office 2010 comes with Visual Basic for Applications 7.0. However I
I am currently working on creating an import-based pipeline for my indie game using
I have a very basic class that is a list of sub-classes, plus some
Summary I have written an Excel wrapper in .NET using Visual Basic and Visual
I have a pretty basic form that shows quiz questions along with choices: <%=
I'm building a notification framework and for that I'm serializing and deserializing a basic
Basic summary of question is: How do I best optimize my memory allocation to

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.