| | |
thx but how do i display an image in java
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2005
Posts: 55
Reputation:
Solved Threads: 1
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.
For more help, www.NeedProgrammingHelp.com
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)
import javax.swing.*; import java.awt.*; import javax.imageio.*; import java.io.*; //panel used to draw image on public class ImagePanel extends JPanel { //path of image private String path; //image object private Image img; public ImagePanel(String path) throws IOException { //save path this.path = path; //load image img = ImageIO.read(new File(path)); } //override paint method of panel public void paint(Graphics g) { //draw the image if( img != null) g.drawImage(img,0,0, this); } } //example of using image panel class ImageFrame { public static void main(String[] args) throws IOException { try { //create frame JFrame f = new JFrame(); //ask for image file JFileChooser chooser = new JFileChooser(); chooser.showOpenDialog(f); //create panel with selected file ImagePanel panel = new ImagePanel( chooser.getSelectedFile().getPath() ); //add panel to pane f.getContentPane().add(panel); //show frame f.setBounds(0,0,800,800); f.setVisible(true); } catch(Exception e) { System.out.println ( "Please verify that you selected a valid image file"); } } }
You don't have to ask the user for input. Just pass the name as a string. Here is how I do it:
Here is the full article the above code is from:
http://www.heatonresearch.com/articles/20/page5.html
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import java.applet.*; import java.net.MalformedURLException; import java.net.URL; public class ImageApplet extends Applet { public static final int IDEAL_WIDTH = 320; public static final int IDEAL_HEIGHT = 240; private Image loadImage(String name) { Image result = null; MediaTracker tracker = new MediaTracker(this); Toolkit toolkit = Toolkit.getDefaultToolkit(); result = toolkit.getImage(name); tracker.addImage(result, 0); try { tracker.waitForAll(); } catch (InterruptedException e) { return null; } return result; } public void paint(Graphics g) { Image sea = loadImage("sea.jpg"); g.drawImage(sea, 0, 0, null); } public static void main(String args[]) { Applet applet = new ImageApplet(); Frame frame = new Frame(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); frame.add(applet); frame.setSize(IDEAL_WIDTH, IDEAL_HEIGHT); frame.show(); } }
Here is the full article the above code is from:
http://www.heatonresearch.com/articles/20/page5.html
![]() |
Similar Threads
- display image when button clicked (Java)
- Display image from a database get me ascii codes (ASP)
- Display image for background (Java)
- image via java (Java)
- display image if applet is "not inited" (Java)
Other Threads in the Java Forum
- Previous Thread: Please Help!!!
- Next Thread: difference b/w equals and ==
| Thread Tools | Search this Thread |
Tag cloud for Java
actionlistener android api apple applet application apps arguments array arrays automation balls binary bluetooth card chat class classes client code component consumer database draw eclipse ee error event exception file fractal free game gameprogramming gis givemetehcodez graphics gui helpwithhomework html ide image input integer j2me j2seprojects java javaprojects jmf jni jpanel julia jvm linux list loop machine map method methods migrate mobile myaggfun netbeans newbie nextline nls notdisplaying number oracle output print problem program programming project recursion recursive scanner screen security server set size sms socket sort spamblocker sql sqlite string sun swing terminal test threads time tree trolltech windows






