Results 1 to 4 of 4
  1. #1
    saytri is offline Member
    Join Date
    Dec 2007
    Posts
    34
    Rep Power
    0

    Default 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:

    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);
    } 
             
              
        }
    Thanks a lot.
    Last edited by saytri; 01-12-2008 at 12:30 AM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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);
        } 
    }

  3. #3
    saytri is offline Member
    Join Date
    Dec 2007
    Posts
    34
    Rep Power
    0

    Default

    Thanks a lot. It worked now. :-)

  4. #4
    saytri is offline Member
    Join Date
    Dec 2007
    Posts
    34
    Rep Power
    0

    Default

    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 01:04 AM.

Similar Threads

  1. how to import a picture into an applet
    By cecily in forum Java Applets
    Replies: 2
    Last Post: 01-15-2010, 08:51 PM
  2. JFrame problem
    By vassil_zorev in forum AWT / Swing
    Replies: 1
    Last Post: 01-25-2008, 02:53 AM
  3. JFrame problem
    By saytri in forum New To Java
    Replies: 6
    Last Post: 01-11-2008, 05:12 PM
  4. Print a picture file
    By oli001 in forum New To Java
    Replies: 0
    Last Post: 11-26-2007, 01:40 PM
  5. Help with JFrame
    By Albert in forum AWT / Swing
    Replies: 2
    Last Post: 07-04-2007, 04:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •