| | |
Image creation
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2009
Posts: 8
Reputation:
Solved Threads: 0
I'm learning GUI, can anyone show me how to display an image on a panel. So far I have this:
I tried to create an image like this:
Image image1 = new Image("red.jpg");
but it says its abstract.
If possible, try to show me how I can create an image, set it to "red.jpg", and display my image on the panel.
java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import javax.imageio.*; import javax.swing.*; import java.applet.*; public class JlabelDemo { public static void main(String[] args) { // create an image icon, put the icon on a button, then create a panel and a frame ImageIcon imageIcon1 = new ImageIcon("red.jpg"); JButton button1 = new JButton("Press me!", imageIcon1); JPanel panel1 = new JPanel(); JFrame frame1 = new JFrame("Image Display"); // add button1 to panel1, now add panel1 to frame1 panel1.add(button1); frame1.add(panel1); frame1.setSize(400, 300); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.setVisible(true); } }
Image image1 = new Image("red.jpg");
but it says its abstract.
If possible, try to show me how I can create an image, set it to "red.jpg", and display my image on the panel.
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
Image is abstract. Try using BufferedImage. So you don't want the image on the JButton, right? You want to draw it on the JPanel, right? If so, you can add this to the end of your main statement (note, don't add the JButton and don't resize the frame since the image will not repaint after the resizing). Here's the updated program. Not sure if you wanted to keep the JButton and whether it's supposed to have the image on the button or what:
Note that line 23 is commented out and lines 30 onward are new.
JAVA Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.*; import javax.swing.*; import java.applet.*; public class jLabelDemo { public static void main(String[] args) { // create an image icon, put the icon on a button, then create a panel and a frame ImageIcon imageIcon1 = new ImageIcon("red.jpg"); JButton button1 = new JButton("Press me!", imageIcon1); JPanel panel1 = new JPanel(); JFrame frame1 = new JFrame("Image Display"); // add button1 to panel1, now add panel1 to frame1 // panel1.add(button1); frame1.add(panel1); frame1.setSize(400, 300); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.setVisible(true); File file = new File ("red.jpg"); BufferedImage bi = null; try { bi = ImageIO.read(file); } catch (IOException ex) { System.out.println ("Couldn't open file."); System.exit (1); } panel1.getGraphics().drawImage(bi, 0, 0, null); } }
Note that line 23 is commented out and lines 30 onward are new.
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
Hmm. It wasn't doing that before, but yes, it is doing it now. That happens to me a lot and I end up experimenting around. I'm not 100% sure why it happens sometimes, but not others. Anyway, I tried making a class that extends JPanel and drawing it on an object of that class and it seems to not disappear now. I don't think that's necessary, but it works. This one has a paintComponent function, so you can resize it now.
JAVA Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import javax.imageio.*; import javax.swing.*; public class jLabelDemo { public static void main(String[] args) { // create an image icon, put the icon on a button, then create a panel and a frame ImageIcon imageIcon1 = new ImageIcon("red.jpg"); JButton button1 = new JButton("Press me!", imageIcon1); button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println ("I just got pressed"); } }); JFrame frame1 = new JFrame("Image Display"); frame1.setSize(400, 300); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyPanel panel1 = new MyPanel ("red.jpg"); frame1.add(panel1); frame1.setVisible(true); } } class MyPanel extends JPanel { BufferedImage bi = null; public MyPanel (String filename) { File file = new File (filename); try { bi = ImageIO.read(file); } catch (IOException ex) { System.out.println ("Couldn't open file."); System.exit (1); } } @Override public void paintComponent (Graphics g) { g.drawImage(bi, 0, 0, null); } }
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
•
•
•
•
Most likely the graphics context in the earlier version that you obtained from the getGraphics() call and drew on got updated by a repaint operation - so no more image. Overriding paintComponent() as you did in the second example is the proper way to do it.
Thanks. Good to know.
![]() |
Similar Threads
- Image Creation (ColdFusion)
- using disk instead of memory for image creation using intptr (VB.NET)
- How can I hyperlink an image ? (PHP)
- Advice on image creation (PHP)
- Auto replace hotlinked Image Javascript? (JavaScript / DHTML / AJAX)
- Monitor (Monitors, Displays and Video Cards)
- blur (Graphics and Multimedia)
- Image Not Showing (C)
Other Threads in the Java Forum
- Previous Thread: Reading data to a file
- Next Thread: Searching for a number in the string
Views: 516 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool linux list loop map method methods mobile netbeans newbie number object oracle os pong print problem producer program programming project projectideas read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






