Results 1 to 3 of 3
  1. #1
    jumpman8947 is offline Member
    Join Date
    Feb 2013
    Posts
    38
    Rep Power
    0

    Default Display Java Image

    Simple question here I basically want to display an image within the frame from the click of a button. My problem is getting that image to display. Here is the code Thanks...
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MyImage extends JFrame
    {
    	private JButton open;
    	private JPanel mainPanel;
    
    	public MyImage()
    	{
    		setTitle("Open Image");
    		setLocationRelativeTo(null);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setVisible(true);
    		setSize(700,700);
    
    		open = new JButton("open");
    		open.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				ImageIcon zz = new ImageIcon("barkley.jpg");
    				//mainPanel.add(zz);
    				setVisible(true);
    				pack();
    			}
    
    		});
    		mainPanel = new JPanel();
    		getContentPane().add(mainPanel);
    		mainPanel.setLayout(null);
    		mainPanel.add(open);
    		mainPanel.add(zz);
    		open.setBounds(0,0,100,100);
    	}
    		public static void main(String[] args)
    		{
    			MyImage mi = new MyImage();
    		}
    
    	}

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,414
    Rep Power
    5

    Default Re: Display Java Image

    Don't extend JFrame, but do extend JPanel. Then create an instance of the JFrame iand add the panel to it. Then override paintComponent() and use an appropriate method of the Graphics class (see Graphics JDK API for more info). Please check out the Oracle tutorials in my signature. Click on the link and then click on the Really Big Index and check out working with images.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Display Java Image

    Or don't reinvent the wheel and do use a JLabel with an ImageIcon.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. how do i display a image using the GUI?
    By vicu1 in forum New To Java
    Replies: 3
    Last Post: 06-04-2012, 04:30 AM
  2. Display image
    By asai in forum New To Java
    Replies: 16
    Last Post: 04-03-2012, 01:15 PM
  3. image display
    By asmitarnd in forum AWT / Swing
    Replies: 3
    Last Post: 04-07-2011, 11:17 AM
  4. Simple Java image stream display
    By hoobajue in forum New To Java
    Replies: 2
    Last Post: 08-17-2010, 01:18 PM
  5. How to display image ?
    By Birkoff in forum AWT / Swing
    Replies: 7
    Last Post: 06-09-2008, 07:58 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
  •