Image creation

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2009
Posts: 8
Reputation: DotA is an unknown quantity at this point 
Solved Threads: 0
DotA DotA is offline Offline
Newbie Poster

Image creation

 
0
  #1
Apr 26th, 2009
I'm learning GUI, can anyone show me how to display an image on a panel. So far I have this:
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.image.*;
  4. import java.io.*;
  5. import javax.imageio.*;
  6. import javax.swing.*;
  7. import java.applet.*;
  8.  
  9.  
  10. public class JlabelDemo
  11. {
  12. public static void main(String[] args)
  13. {
  14. // create an image icon, put the icon on a button, then create a panel and a frame
  15. ImageIcon imageIcon1 = new ImageIcon("red.jpg");
  16. JButton button1 = new JButton("Press me!", imageIcon1);
  17. JPanel panel1 = new JPanel();
  18. JFrame frame1 = new JFrame("Image Display");
  19.  
  20. // add button1 to panel1, now add panel1 to frame1
  21. panel1.add(button1);
  22. frame1.add(panel1);
  23.  
  24. frame1.setSize(400, 300);
  25. frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  26. frame1.setVisible(true);
  27. }
  28. }
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Image creation

 
0
  #2
Apr 26th, 2009
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:

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.image.*;
  4. import java.io.*;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7. import javax.imageio.*;
  8. import javax.swing.*;
  9. import java.applet.*;
  10.  
  11.  
  12. public class jLabelDemo
  13. {
  14. public static void main(String[] args)
  15. {
  16. // create an image icon, put the icon on a button, then create a panel and a frame
  17. ImageIcon imageIcon1 = new ImageIcon("red.jpg");
  18. JButton button1 = new JButton("Press me!", imageIcon1);
  19. JPanel panel1 = new JPanel();
  20. JFrame frame1 = new JFrame("Image Display");
  21.  
  22. // add button1 to panel1, now add panel1 to frame1
  23. // panel1.add(button1);
  24. frame1.add(panel1);
  25.  
  26. frame1.setSize(400, 300);
  27. frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28. frame1.setVisible(true);
  29.  
  30. File file = new File ("red.jpg");
  31. BufferedImage bi = null;
  32. try
  33. {
  34. bi = ImageIO.read(file);
  35. }
  36. catch (IOException ex)
  37. {
  38. System.out.println ("Couldn't open file.");
  39. System.exit (1);
  40. }
  41.  
  42. panel1.getGraphics().drawImage(bi, 0, 0, null);
  43. }
  44. }

Note that line 23 is commented out and lines 30 onward are new.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 8
Reputation: DotA is an unknown quantity at this point 
Solved Threads: 0
DotA DotA is offline Offline
Newbie Poster

Re: Image creation

 
0
  #3
Apr 26th, 2009
The code you suggested works. However, the image displays for a split second and disappears, any suggestions on how I can make it stay, or would I need to keep calling redraw?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Image creation

 
0
  #4
Apr 26th, 2009
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.

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.image.*;
  4. import java.io.*;
  5. import javax.imageio.*;
  6. import javax.swing.*;
  7.  
  8.  
  9. public class jLabelDemo
  10. {
  11. public static void main(String[] args)
  12. {
  13. // create an image icon, put the icon on a button, then create a panel and a frame
  14. ImageIcon imageIcon1 = new ImageIcon("red.jpg");
  15. JButton button1 = new JButton("Press me!", imageIcon1);
  16. button1.addActionListener(new ActionListener()
  17. {
  18. public void actionPerformed(ActionEvent e)
  19. {
  20. System.out.println ("I just got pressed");
  21. }
  22. });
  23.  
  24. JFrame frame1 = new JFrame("Image Display");
  25. frame1.setSize(400, 300);
  26. frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27.  
  28. MyPanel panel1 = new MyPanel ("red.jpg");
  29. frame1.add(panel1);
  30. frame1.setVisible(true);
  31. }
  32. }
  33.  
  34.  
  35. class MyPanel extends JPanel
  36. {
  37. BufferedImage bi = null;
  38.  
  39. public MyPanel (String filename)
  40. {
  41. File file = new File (filename);
  42. try
  43. {
  44. bi = ImageIO.read(file);
  45. }
  46. catch (IOException ex)
  47. {
  48. System.out.println ("Couldn't open file.");
  49. System.exit (1);
  50. }
  51. }
  52.  
  53.  
  54. @Override
  55. public void paintComponent (Graphics g)
  56. {
  57. g.drawImage(bi, 0, 0, null);
  58. }
  59. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 8
Reputation: DotA is an unknown quantity at this point 
Solved Threads: 0
DotA DotA is offline Offline
Newbie Poster

Re: Image creation

 
0
  #5
Apr 26th, 2009
Thank you very much, it works great now, it's very neatly done and simple for me to understand.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Image creation

 
0
  #6
Apr 27th, 2009
Originally Posted by DotA View Post
Thank you very much, it works great now, it's very neatly done and simple for me to understand.
You are welcome. Perhaps someone else knows why the earlier version quickly disappears sometimes, but not others?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Image creation

 
0
  #7
Apr 28th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Image creation

 
0
  #8
Apr 29th, 2009
Originally Posted by Ezzaral View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 516 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC