thx but how do i display an image in java

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

Join Date: Aug 2005
Posts: 3
Reputation: MastaPho is an unknown quantity at this point 
Solved Threads: 0
MastaPho MastaPho is offline Offline
Newbie Poster

thx but how do i display an image in java

 
0
  #1
Aug 25th, 2005
plz tell me the type of program(application etc.)
and the source, hopefully not difficult to understand
from C:/Magix
also can add //description
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

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

 
0
  #2
Aug 26th, 2005
My preferred way: Have an external class that extends either Canvas or JPanel and use the paint method there.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 55
Reputation: NPH is an unknown quantity at this point 
Solved Threads: 1
NPH NPH is offline Offline
Junior Poster in Training

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

 
0
  #3
Aug 30th, 2005
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 3
Reputation: MastaPho is an unknown quantity at this point 
Solved Threads: 0
MastaPho MastaPho is offline Offline
Newbie Poster

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

 
0
  #4
Aug 30th, 2005
is there a way to get the image without asking for user input?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 58
Reputation: JeffHeaton is an unknown quantity at this point 
Solved Threads: 0
JeffHeaton's Avatar
JeffHeaton JeffHeaton is offline Offline
Junior Poster in Training

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

 
0
  #5
Aug 31st, 2005
You don't have to ask the user for input. Just pass the name as a string. Here is how I do it:

  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
Jeff Heaton
Check out my Java Neural Networks
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC