944,117 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 50896
  • Java RSS
Aug 25th, 2005
0

thx but how do i display an image in java

Expand Post »
plz tell me the type of program(application etc.)
and the source, hopefully not difficult to understand
from C:/Magix
also can add //description
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MastaPho is offline Offline
3 posts
since Aug 2005
Aug 26th, 2005
0

Re: thx but how do i display an image in java

My preferred way: Have an external class that extends either Canvas or JPanel and use the paint method there.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Aug 30th, 2005
0

Re: thx but how do i display an image in java

Here is a quick & simple example of showing an image in java. The most import code is in the paint method. In this case, we are redefining the way a usual panel draws itself. The class ImagePanel does just this.

The other class just uses this panel in a frame and asks the user for an image to show. Hope this helps.

Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import javax.imageio.*;
  4. import java.io.*;
  5.  
  6. //panel used to draw image on
  7. public class ImagePanel extends JPanel
  8. {
  9. //path of image
  10. private String path;
  11.  
  12. //image object
  13. private Image img;
  14.  
  15. public ImagePanel(String path) throws IOException
  16. {
  17. //save path
  18. this.path = path;
  19.  
  20. //load image
  21. img = ImageIO.read(new File(path));
  22.  
  23. }
  24.  
  25. //override paint method of panel
  26. public void paint(Graphics g)
  27. {
  28. //draw the image
  29. if( img != null)
  30. g.drawImage(img,0,0, this);
  31. }
  32.  
  33. }
  34.  
  35. //example of using image panel
  36. class ImageFrame
  37. {
  38. public static void main(String[] args) throws IOException
  39. {
  40.  
  41. try
  42. {
  43.  
  44. //create frame
  45. JFrame f = new JFrame();
  46.  
  47. //ask for image file
  48. JFileChooser chooser = new JFileChooser();
  49. chooser.showOpenDialog(f);
  50.  
  51. //create panel with selected file
  52. ImagePanel panel = new ImagePanel( chooser.getSelectedFile().getPath() );
  53.  
  54. //add panel to pane
  55. f.getContentPane().add(panel);
  56.  
  57.  
  58. //show frame
  59. f.setBounds(0,0,800,800);
  60. f.setVisible(true);
  61. }
  62. catch(Exception e)
  63. {
  64. System.out.println ( "Please verify that you selected a valid image file");
  65. }
  66. }
  67. }
For more help, www.NeedProgrammingHelp.com
NPH
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
NPH is offline Offline
55 posts
since May 2005
Aug 30th, 2005
0

Re: thx but how do i display an image in java

is there a way to get the image without asking for user input?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MastaPho is offline Offline
3 posts
since Aug 2005
Aug 31st, 2005
0

Re: thx but how do i display an image in java

You don't have to ask the user for input. Just pass the name as a string. Here is how I do it:

Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.applet.*;
  4. import java.net.MalformedURLException;
  5. import java.net.URL;
  6.  
  7. public class ImageApplet extends Applet
  8. {
  9. public static final int IDEAL_WIDTH = 320;
  10. public static final int IDEAL_HEIGHT = 240;
  11.  
  12. private Image loadImage(String name)
  13. {
  14. Image result = null;
  15. MediaTracker tracker = new MediaTracker(this);
  16.  
  17. Toolkit toolkit = Toolkit.getDefaultToolkit();
  18.  
  19. result = toolkit.getImage(name);
  20. tracker.addImage(result, 0);
  21. try
  22. {
  23. tracker.waitForAll();
  24. } catch (InterruptedException e)
  25. {
  26. return null;
  27. }
  28.  
  29. return result;
  30. }
  31.  
  32. public void paint(Graphics g)
  33. {
  34. Image sea = loadImage("sea.jpg");
  35. g.drawImage(sea, 0, 0, null);
  36. }
  37.  
  38. public static void main(String args[])
  39. {
  40. Applet applet = new ImageApplet();
  41. Frame frame = new Frame();
  42. frame.addWindowListener(new WindowAdapter()
  43. {
  44. public void windowClosing(WindowEvent e)
  45. {
  46. System.exit(0);
  47. }
  48. });
  49.  
  50. frame.add(applet);
  51. frame.setSize(IDEAL_WIDTH, IDEAL_HEIGHT);
  52. frame.show();
  53. }
  54. }

Here is the full article the above code is from:

http://www.heatonresearch.com/articles/20/page5.html
Reputation Points: 12
Solved Threads: 0
Junior Poster in Training
JeffHeaton is offline Offline
58 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Please Help!!!
Next Thread in Java Forum Timeline: difference b/w equals and ==





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC