Results 1 to 4 of 4
Thread: Picture in a JFrame problem
- 01-11-2008, 11:27 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 34
- Rep Power
- 0
Picture in a JFrame problem
I have inserted a picture in a JFrame. The problem is that the picture isn't appearing. Can someone pls tell me whats missing in my code? Thanks a lot.
This is my code:
Thanks a lot.Java Code:import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; import javax.swing.*; import java.util.Calendar; import java.awt.image.*; import javax.imageio.*; public class GQ extends JFrame implements ActionListener { private static final int FRAME_WIDTH = 1024; private static final int FRAME_HEIGHT = 768; private static final int FRAME_X_ORIGIN = 0; private static final int FRAME_Y_ORIGIN = 0; public static void main (String[] args) { GQ frame = new GQ(); frame.setVisible(true); } public GQ() { Container contentPane; contentPane = getContentPane(); contentPane.setBackground(Color.green); contentPane.setLayout(null); image = new JPanel(); contentPane.add(image); } class test4 extends JPanel { BufferedImage image; int w,h; public test4() { try { image = ImageIO.read(new File("geo2.jpg")); w = image.getWidth(); h = image.getHeight(); } catch (IOException ioe) { System.out.println(ioe); System.exit(0); } } public Dimension getPreferredSize() { return new Dimension(w,h); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image,0,0,this); } public void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); test4 t = new test4(); t.setLayout(new BorderLayout()); f.add(t); f.pack(); f.setVisible(true); } }Last edited by saytri; 01-12-2008 at 12:30 AM.
- 01-12-2008, 05:55 AM #2
Java Code:import java.awt.*; import java.awt.image.*; import java.io.*; import javax.swing.*; import javax.imageio.*; public class GQ3 extends JFrame { public GQ3() { setDefaultCloseOperation(EXIT_ON_CLOSE); Container contentPane = getContentPane(); contentPane.setBackground(Color.green); contentPane.setLayout(new BorderLayout()); Test4Rx imagePanel = new Test4Rx("images/hawk.jpg"); contentPane.add(imagePanel, "North"); contentPane.add(new Test4Rx("images/bison.jpg")); pack(); setLocationRelativeTo(null); setVisible(true); } public static void main (String[] args) { new GQ3(); } } class Test4Rx extends JComponent { BufferedImage image; int w,h; public Test4Rx(String path) { try { image = ImageIO.read(new File(path)); w = image.getWidth(); h = image.getHeight(); } catch (IOException ioe) { System.out.println(ioe.getMessage()); System.exit(0); } } public Dimension getPreferredSize() { return new Dimension(w,h); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image,0,0,this); } public void main(String[] args) { Test4Rx t = new Test4Rx("geo2.jpg"); JOptionPane.showMessageDialog(null, t, "", -1); } }
- 01-12-2008, 09:31 AM #3
Member
- Join Date
- Dec 2007
- Posts
- 34
- Rep Power
- 0
Thanks a lot. It worked now. :-)
- 01-12-2008, 09:44 AM #4
Member
- Join Date
- Dec 2007
- Posts
- 34
- Rep Power
- 0
Similar Threads
-
how to import a picture into an applet
By cecily in forum Java AppletsReplies: 2Last Post: 01-15-2010, 08:51 PM -
JFrame problem
By vassil_zorev in forum AWT / SwingReplies: 1Last Post: 01-25-2008, 02:53 AM -
JFrame problem
By saytri in forum New To JavaReplies: 6Last Post: 01-11-2008, 05:12 PM -
Print a picture file
By oli001 in forum New To JavaReplies: 0Last Post: 11-26-2007, 01:40 PM -
Help with JFrame
By Albert in forum AWT / SwingReplies: 2Last Post: 07-04-2007, 04:44 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks