Results 1 to 4 of 4
Thread: Background image not loading?
- 11-20-2012, 06:26 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 30
- Rep Power
- 0
Background image not loading?
Hi guys so I'm having some trouble with the paint method, could anybody help?
So that is my drawing panel, and here is where it is giving me the error :Java Code:class MyDrawingPanel extends JPanel { public MyDrawingPanel(Template parentRef) { this.parentRef=parentRef; setPreferredSize(new Dimension(800,600)); setBackground(Color.WHITE); } public void paint(Graphics g) { super.paint(g); } }
No suitable method found for drawImage - that is the error.Java Code:class MyUIPanel extends JPanel { Template parentRef; JButton b1=new JButton("Click Me"); BufferedImage backgroundImage; public MyUIPanel(AssignmentTemplate parentRef) { this.parentRef=parentRef; setPreferredSize(new Dimension(300,600)); setBackground(Color.LIGHT_GRAY); setLayout(null); add(b1); b1.setBounds(20,20,100,30); } public void drawBackground(Graphics g) { try { backgroundImage = ImageIO.read(new File("C:\\Temp\\PEGBackground.png")); } catch (IOException ex) { System.out.println("Cannot find image!"); } g.[COLOR="#FF0000"]drawImage[/COLOR](backgroundImage); } }
Can anybody help me? Am I doing the paint method correct?
I have looked online and read about it but can't seem to do anymore, thank you
- 11-20-2012, 06:49 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Background image not loading?
Take a look at Graphics (Java Platform SE 6).
The draweImage method requires at least a x and y value and an ImageObserver (could be null)!
The sense of your overrided paint method is not clear to me - is something missing here? And in swing applications usually we override the paintComponent method instead of the paint method!
- 11-20-2012, 07:17 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 30
- Rep Power
- 0
Re: Background image not loading?
Thank you I will have a look at that and get back to you :)
- 11-20-2012, 09:17 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 30
- Rep Power
- 0
Re: Background image not loading?
I have read up on it, I am getting no errors but am confused as to why the image does not appear on the screen :
Java Code:class MyDrawingPanel extends JPanel { Template parentRef; private BufferedImage image; public MyDrawingPanel(Template parentRef) { this.parentRef=parentRef; setPreferredSize(new Dimension(800,600)); setBackground(Color.WHITE); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image, 0, 0, null); } }Java Code:class MyUIPanel extends JPanel { AssignmentTemplate parentRef; JButton b1=new JButton("Click Me"); private BufferedImage image; public MyUIPanel(AssignmentTemplate parentRef) { this.parentRef=parentRef; setPreferredSize(new Dimension(300,600)); setBackground(Color.LIGHT_GRAY); setLayout(null); add(b1); b1.setBounds(20,20,100,30); } public MyUIPanel(){ try{ image = ImageIO.read(new File("C:\\Temp\\PEGBackground.PNG")); } catch (IOException ex) { System.out.println("No such file"); } }
What I am trying to do, is from the class MyUIPanel, it will get my background image. How do I then get MyDrawingPanel class to draw that image onto the screen?
I have put g.drawImage image, which is what I called it in MyUIPanel. Do I need to call the paint component method in MyUIPanel ??
thank you
Similar Threads
-
Loading a image with Image and ImageIcon
By cel0x in forum AWT / SwingReplies: 3Last Post: 02-15-2012, 10:38 AM -
Loading an image and then using it as a background
By ResidentBiscuit in forum New To JavaReplies: 3Last Post: 11-30-2011, 04:04 PM -
Java Applet loading image; render as you get image?
By ea25 in forum New To JavaReplies: 12Last Post: 04-14-2011, 01:58 PM -
Loading an image into JDesktopPane as background
By Rose88 in forum Advanced JavaReplies: 13Last Post: 08-25-2009, 01:10 AM -
Image as background
By Java.child in forum AWT / SwingReplies: 2Last Post: 10-02-2008, 11:37 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks