Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help with displaying images (gif, jpeg...) within a JFrame (or similar)

843806Jan 14 2008 — edited Jan 16 2008
Hi all,
i'm new to the forum and i'll be granted if you kind gentlemen/women could use some advices for my gui home application.
My purpose is to display a static image in a container (i'd prefer to use javax.swing objects, but if you think that java.awt is more suitable for my app, please feel free to tell me about...). I used a modified code from one of the Java Tutorial's examples, tht actually is an applet and displays images as it is using the paint() method. I just can't realize how to set the GraphicContext in a JFrame or a JPanel to contain the image to display, without using applets. Following part of the code I use (a JButton will fire JApplet start, JApplet is another class file called Apple.java):
class DMToolz extends JFrame {
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JButton jButton = null;
	private JComboBox jComboBox = null;
	private JMenuItem jMenuItem = null;
	private JMenuBar jJMenuBar = null;
	private JMenu jMenu = null;
	private JMenu jMenu1 = null;
	public int w=10, h=10;
	public String filename;
	/**
	 * @throws HeadlessException
	 */
	public DMToolz() throws HeadlessException {
		// TODO Auto-generated constructor stub
		super();
		initialize();
	}

	public DMToolz(String arg0) throws HeadlessException {
		super(arg0);
		// TODO Auto-generated constructor stub
		initialize();
	}

	/**
	 * This method initializes jButton
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(723, 505, 103, 38));
			jButton.setText("UrcaMado'");
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
/**************SCRIVERE QUI IL NOME DEL FILE CON L'IMMAGINE DA CARICARE*******************/
					filename = "reference table player2.gif";
			        java.net.URL imgURL = DMToolz.class.getResource("images/"+filename);
			        BufferedImage img = null;
			        try {
			        	img =  ImageIO.read(imgURL);
			            w = img.getWidth();
			            h = img.getHeight();
			            System.out.println("*DM* Immagine letta - W:"+w+" H:"+h);
			         } catch (Exception ex) {System.out.println("*DM* Immagine non letta");}
		         
			        JApplet a = new Apple();
			        a.setName(filename);
			        JFrame f = new JFrame ("UrcaBBestia!");
			        f.addWindowListener(new WindowAdapter() {
			            public void windowClosing(WindowEvent e) {System.exit(0);}
			        });
			        f.getContentPane().add(a);
			        f.setPreferredSize( new Dimension(w,h+30) );//30 � (circa) l'altezza della barra del titolo del frame
			       	f.pack();
					f.setVisible(true);			        

				}
			});
		}
		return jButton;
	}

public class Apple extends JApplet {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
    final static Color bg = Color.white;
    final static Color fg = Color.black;
    final static Color red = Color.red;
    final static Color white = Color.white;

    public void init() {
        //Initialize drawing colors
        setBackground(bg);
        setForeground(fg);
    }

    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
       
        try {
        	String filename = this.getName(); //uso il nome dell'applet come riferimento al nome del file da caricare
        	java.net.URL imgURL = DMToolz.class.getResource("images/"+filename);
        	BufferedImage img =  ImageIO.read(imgURL);
        	if (img.equals(null)) {System.out.println("IMG null!");System.exit(0);}
            System.out.println("IMG letta");
            int w = img.getWidth();
            int h = img.getHeight();
            resize(w,h);
            g2.drawImage(img, 
    				0,0,Color.LIGHT_GRAY,
    				null);// no ImageObserver
         } catch (Exception ex) {ex.printStackTrace();
        						System.out.println("Immagine non letta.");System.exit(0);}

        

    }

}//end class
Please never mind about some italian text, used as reminder....
Thanks in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 13 2008
Added on Jan 14 2008
2 comments
135 views