Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-12-2008, 01:27 AM
Member
 
Join Date: Dec 2007
Posts: 34
saytri is on a distinguished road
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:

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); } }
Thanks a lot.

Last edited by saytri : 01-12-2008 at 02:30 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-12-2008, 07:55 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-12-2008, 11:31 AM
Member
 
Join Date: Dec 2007
Posts: 34
saytri is on a distinguished road
Thanks a lot. It worked now. :-)
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-12-2008, 11:44 AM
Member
 
Join Date: Dec 2007
Posts: 34
saytri is on a distinguished road
Can someone pls tell me if there is a way of how to read a textfile from a JTextArea?


Thanks a lot.

Last edited by saytri : 01-13-2008 at 03:04 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JFrame problem vassil_zorev AWT / Swing 1 01-25-2008 04:53 AM
JFrame problem saytri New To Java 6 01-11-2008 07:12 PM
Print a picture file oli001 New To Java 0 11-26-2007 03:40 PM
how to import a picture into an applet cecily Java Applets 1 08-03-2007 11:51 PM
Help with JFrame Albert AWT / Swing 2 07-04-2007 06:44 AM


All times are GMT +3. The time now is 02:25 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org