| | |
JPanel and putting a Background GIF pic...NOT WORKING
![]() |
I'm new to swing, and was curious if anyone could point me in the right direction here. Cannot figure out how to make this repaint crap work, or if I'm supposed to be using repaint(), or what, to make this image get drawn to the background of my window. Here is my code that I have so far. It is drawing nothing at this point:
i
And Then My JFrame class
An lastly i have my main method which just calls an instance of my JFrame class, and then setVisibile(true)
If Anyone could tell me why it wont write the image, and how to remedy it, I would much appreciate. Thanks
i
Java Syntax (Toggle Plain Text)
mport java.io.*; import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * * @author * @version September 8, 2005, 11:54 PM */ class MyPanel extends JPanel { String imageFilePath = "C:/MyDocuments/MemoryGame/woodTable.gif"; ImageIcon icon; Image img; public MyPanel() { icon = new ImageIcon(imageFilePath); img = icon.getImage(); } public void paint(Graphics g) { super.paint(g); g.drawImage(img, 300, 200, this); } }
And Then My JFrame class
Java Syntax (Toggle Plain Text)
import java.io.*; import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * * @author * @version September 8, 2005, 10:40 PM */ public class memGame extends JFrame { /** Creates a new instance of memGame */ public memGame() { super("Mario Memory Game"); Container win = getContentPane(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640,480); MyPanel mp = new MyPanel(); mp.repaint(); win.add(mp); JMenuBar menuBar = new JMenuBar(); JMenu gMenu = new JMenu("Game"); JMenu gMenu2 = new JMenu("Extra"); JMenuItem newG = new JMenuItem("New Game"); JMenuItem quit = new JMenuItem("Quit Game"); JMenuItem help = new JMenuItem("Help"); JMenuItem about = new JMenuItem("About"); gMenu.add(newG); gMenu.add(quit); gMenu2.add(help); gMenu2.add(about); menuBar.add(gMenu); menuBar.add(gMenu2); setJMenuBar(menuBar); } public void ActionPerformed(ActionEvent e) { } }
An lastly i have my main method which just calls an instance of my JFrame class, and then setVisibile(true)
If Anyone could tell me why it wont write the image, and how to remedy it, I would much appreciate. Thanks
•
•
Join Date: Jun 2005
Posts: 71
Reputation:
Solved Threads: 1
I may be wrong here but do you need to 'escape' the path deliminators, like this:
The '\\' is not a typo. The first '\' escapes the second.
Kate
Java Syntax (Toggle Plain Text)
String imageFilePath = "C:\\MyDocuments\\MemoryGame\\woodTable.gif";
Kate
Remove the call to super.paintComponent(whatever it was). I spent 2 weeks trying to fix this once and found out that for some reason, calling that made the background disappear.
Example: http://compuchat.freeownhost.com/sni...age_background
Example: http://compuchat.freeownhost.com/sni...age_background
•
•
Join Date: May 2005
Posts: 55
Reputation:
Solved Threads: 1
Your image is not being found. Probably the directory "C:/MyDocuments/MemoryGame/woodTable.gif"; doesn't exist.
If its in your documents folder then the correct directory would be:
C:/Documents and Settings/YOUR WINDOWS USER NAME HERE/My Documents/woodTable.gif.
For more help, www.NeedProgrammingHelp.com
If its in your documents folder then the correct directory would be:
C:/Documents and Settings/YOUR WINDOWS USER NAME HERE/My Documents/woodTable.gif.
For more help, www.NeedProgrammingHelp.com
Okay, so long time not replying. Here's the deal.
One, I'm using Netbeans, and the image is being found, because netBeans raises bitchy errors if the image isn't found. So I don't believe it is that.
Two, that removal of the super.paint(g) call did nothing. It turned the screen white, instead of gray, which i suppose is something, but not what I need.
Three, the delimeters in netbeans do not need to be escaped. I've done files like this before and it's found them all. Something is wrong with my method, or with how I'm calling up the image once its been loaded. Somethign is missing. If someone has any more suggestions, I would like to know, because this looks logical to me.
One, I'm using Netbeans, and the image is being found, because netBeans raises bitchy errors if the image isn't found. So I don't believe it is that.
Two, that removal of the super.paint(g) call did nothing. It turned the screen white, instead of gray, which i suppose is something, but not what I need.
Three, the delimeters in netbeans do not need to be escaped. I've done files like this before and it's found them all. Something is wrong with my method, or with how I'm calling up the image once its been loaded. Somethign is missing. If someone has any more suggestions, I would like to know, because this looks logical to me.
•
•
Join Date: Aug 2005
Posts: 216
Reputation:
Solved Threads: 8
My guess is the panel you're adding to the Frame isn't taking up the entire frame or the image isn't being found. The folloing code works fine on my machine.
Here is the frame using GridBagLayout.
Regards,
Nate
Java Syntax (Toggle Plain Text)
public class ImageJPanel extends JPanel { private Image aImage = null; public ImageJPanel() { init(); } private void init() { File file = new File("D:\\Temp\\met_art_07_000891.jpg"); System.out.println("file.exists(): " + file.exists()); ImageIcon imageIcon = new ImageIcon(file.getAbsolutePath()); aImage = imageIcon.getImage(); } public void paint(Graphics g) { super.paint(g); g.drawImage(aImage, 0, 0, this); } }
Here is the frame using GridBagLayout.
Java Syntax (Toggle Plain Text)
public class ExtendedJFrame extends JFrame { public ExtendedJFrame() { init(); } public ExtendedJFrame(String title) { super(title); init(); } private void init() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(640,480); GridBagLayout gbl = new GridBagLayout(); getContentPane().setLayout(gbl); GridBagConstraints gbc = new GridBagConstraints(); ImageJPanel panel = new ImageJPanel(); gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 1; gbc.weighty = 1; gbc.fill = GridBagConstraints.BOTH; getContentPane().add(panel, gbc); } }
Regards,
Nate
![]() |
Similar Threads
- Gif and transparency (Site Layout and Usability)
Other Threads in the Java Forum
- Previous Thread: How to replace repeated substring in other string?
- Next Thread: variable burger might not have been initialized
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






