Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

paintComponent not being called

794258Mar 19 2010 — edited Mar 23 2010
I have an image I want to be painted as the backgroudn image of a JPanel, but the paintComponent method is not being called. Here's my class:
package com.cyledawson.tworeborn;

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

public class BackgroundImage extends JPanel {
	private static final long serialVersionUID = 1L;

	public BackgroundImage() {
		super();
		setPreferredSize(new Dimension(800, 600));
		repaint(); //Added to see if paintComponent is even being launched. It's not
	}

	public void paintComponent(Graphics g) {
		JOptionPane.showMessageDialog(null, "Rawr");
		g.drawImage(new ImageIcon("terra_client_image.png").getImage(), 0, 0, 800, 600, this);
		super.paintComponents(g);
	}
}
Two notes: 1) I added a JOptionPane to check is paintComponent is being launched, and the message pane doesn't appear, and 2) As I noted, I decided to add a repaint() to the constructor in hopes it would launch the paintComponent method, but it did not.

I'm sure there's just some small mistake I'm completely overlooking, but I can find it.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 20 2010
Added on Mar 19 2010
13 comments
2,045 views