Results 1 to 3 of 3
Thread: Display Java Image
- 08-09-2013, 03:21 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 43
- Rep Power
- 0
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(); } }
- 08-09-2013, 03:58 AM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-09-2013, 06:10 AM #3
Similar Threads
-
how do i display a image using the GUI?
By vicu1 in forum New To JavaReplies: 3Last Post: 06-04-2012, 05:30 AM -
Display image
By asai in forum New To JavaReplies: 16Last Post: 04-03-2012, 02:15 PM -
image display
By asmitarnd in forum AWT / SwingReplies: 3Last Post: 04-07-2011, 12:17 PM -
Simple Java image stream display
By hoobajue in forum New To JavaReplies: 2Last Post: 08-17-2010, 02:18 PM -
How to display image ?
By Birkoff in forum AWT / SwingReplies: 7Last Post: 06-09-2008, 08:58 AM
Bookmarks