mbreen
mbreen

Reputation: 558

Java Panel Won't Paint

Basic summary: Working on a game that generates a 15x15 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;}
}

Upvotes: 1

Views: 772

Answers (1)

Chris White
Chris White

Reputation: 30089

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);
}

Upvotes: 2

Related Questions