Results 1 to 6 of 6
- 05-26-2010, 06:43 AM #1
Member
- Join Date
- May 2010
- Posts
- 5
- Rep Power
- 0
- 05-26-2010, 07:07 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
If you need more help post your SSCCE.
- 05-26-2010, 11:08 PM #3
Member
- Join Date
- May 2010
- Posts
- 5
- Rep Power
- 0
Java Code:import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.applet.*; import java.net.URL; import java.awt.Component; public class China extends JFrame { Image img; public static void main(String[] args) { new China(); } public China() { // Window and Picture Setup this.setSize(415,335); this.setTitle("China Restaraunt"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new PicturePanel(); Toolkit kit = Toolkit.getDefaultToolkit(); img = kit.getImage("C:\\Users\\Andy\\Pictures\\Mountain.jpg"); img = img.getScaledInstance(400, -1, Image.SCALE_SMOOTH); this.repaint(); JLabel label1 = new JLabel("Test label"); panel.add(label1); this.add(panel); this.setVisible(true); } private class PicturePanel extends JPanel { public void paint(Graphics g) { g.drawImage(img,0,0,this); } } }
If you run this code you can see that I have a picture set as the background but the label i made won't show up.
Moderator Edit: Code tags added to aid readabilityLast edited by Fubarable; 05-26-2010 at 11:13 PM. Reason: Moderator Edit: Code tags added to aid readability
-
Note changes in code. Also note that code has not been compiled nor tested.
Java Code:public class China extends JFrame { Image img; public static void main(String[] args) { new China(); } public China() { // Window and Picture Setup this.setPreferredSize(new Dimension(415,335)); // **** note change this.setTitle("China Restaraunt"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new PicturePanel(); Toolkit kit = Toolkit.getDefaultToolkit(); img = kit.getImage("C:\\Users\\Andy\\Pictures\\Mountain.jpg"); img = img.getScaledInstance(400, -1, Image.SCALE_SMOOTH); // this.repaint(); // **** no need for this JLabel label1 = new JLabel("Test label"); panel.add(label1); add(panel); pack(); // **** added setVisible(true); } private class PicturePanel extends JPanel { // **** this method should be paintComponent, not paint public void paintComponent(Graphics g) { // **** don't forget to call the super method first super.paintComponent(g) g.drawImage(img,0,0,this); } } }
- 05-26-2010, 11:57 PM #5
Member
- Join Date
- May 2010
- Posts
- 5
- Rep Power
- 0
Thanks so much. It worked exactly how i wanted it to, but if you could explain some things so that I understand the changes better that would be great. For one, why did you use the setPreferredSize method instead of just setSize? Also, how is the paintComponent method ever called if it's not used in the China class? I thought that when i used this.paint(), it was calling the paint method but with it gone I don't see how this works.
- 05-27-2010, 01:42 AM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Swing tutorial
Read the section from the Swing tutorial on "Using Layout Managers" to understand how layout managers work.why did you use the setPreferredSize method instead of just setSize?
Read the section from the Swing tutorial on "Custom Painting" for an explanation and examples (especially the part on "A Closer Look at the Paint Mechanism)., how is the paintComponent method ever called
Download a keep a reference to the tutorial.
Similar Threads
-
uploaded image wont display if i change filename
By schenker in forum Java ServletReplies: 12Last Post: 06-11-2010, 11:13 AM -
Image in JLabel
By ishere11 in forum AWT / SwingReplies: 2Last Post: 04-29-2010, 08:02 AM -
Background image
By leiferouis in forum New To JavaReplies: 9Last Post: 03-08-2009, 05:49 PM -
Image as background
By Java.child in forum AWT / SwingReplies: 2Last Post: 10-02-2008, 11:37 PM -
Background image in java
By toby in forum AWT / SwingReplies: 1Last Post: 07-29-2007, 07:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks