I've been working on this for an hour, with no success at all. I have this image which is 900x600, the same with as my window. I'm simply trying to set it as the background for my JPanel window.
I've tried that site with no success at all. It's been awhile since I've worked with Java, so this small project is a way for me to jump back into it. I've tried copying the code into the main code, but nothing happened when I ran. (However, it did compile without any issues). Looking at the code, and seeing it call the Super Paint Component, I then tried to put it in as a subclass, but that won't work either, so I reverted back into inserting all the code into the same file.
Here's my code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class Play extends JPanel implements ActionListener
{
private String display = "";
Frame playArea;
public Image backgroundImg;
public Play()
{
playArea = new Frame("Euchre");
try
{
backgroundImg = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource("background.gif"), "background.gif"));
}
catch (Exception e) { /*handled in paintComponent()*/ }
}
public void launch()
{
playArea.setLayout(new GridLayout (6, 1) );
playArea.setSize(900,600); //sets size WxH
playArea.setVisible(true); //lets see it
playArea.requestFocus();
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (g != null)
g.drawImage(backgroundImg, 0,0,this.getWidth(),this.getHeight(),this);
}
public void actionPerformed(ActionEvent event)
{
//do nothing
}
public static void main (String[] args)
{
Play go = new Play();
go.launch();
}
}
I've changed the PaintComponent method to this:
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (g != null)
System.out.println("Image is Valid");
else
System.out.println("Image is invalid");
}
Neither will print out, so for whatever reason, I believe the paintComponent method isn't being called. Any idea why?
Both files are in the same folder too, I've checked that (Play.java as well as background.gif)
Question
Hendrick
I've been working on this for an hour, with no success at all. I have this image which is 900x600, the same with as my window. I'm simply trying to set it as the background for my JPanel window.
http://faq.javaranch.com/java/BackgroundImageOnJPanel
I've tried that site with no success at all. It's been awhile since I've worked with Java, so this small project is a way for me to jump back into it. I've tried copying the code into the main code, but nothing happened when I ran. (However, it did compile without any issues). Looking at the code, and seeing it call the Super Paint Component, I then tried to put it in as a subclass, but that won't work either, so I reverted back into inserting all the code into the same file.
Here's my code:
import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.JOptionPane; public class Play extends JPanel implements ActionListener { private String display = ""; Frame playArea; public Image backgroundImg; public Play() { playArea = new Frame("Euchre"); try { backgroundImg = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource("background.gif"), "background.gif")); } catch (Exception e) { /*handled in paintComponent()*/ } } public void launch() { playArea.setLayout(new GridLayout (6, 1) ); playArea.setSize(900,600); //sets size WxH playArea.setVisible(true); //lets see it playArea.requestFocus(); } protected void paintComponent(Graphics g) { super.paintComponent(g); if (g != null) g.drawImage(backgroundImg, 0,0,this.getWidth(),this.getHeight(),this); } public void actionPerformed(ActionEvent event) { //do nothing } public static void main (String[] args) { Play go = new Play(); go.launch(); } }I've changed the PaintComponent method to this:
protected void paintComponent(Graphics g) { super.paintComponent(g); if (g != null) System.out.println("Image is Valid"); else System.out.println("Image is invalid"); }Neither will print out, so for whatever reason, I believe the paintComponent method isn't being called. Any idea why?
Both files are in the same folder too, I've checked that (Play.java as well as background.gif)
Link to comment
Share on other sites
6 answers to this question
Recommended Posts