1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to show images in simple java program?

Discussion in 'Programming' started by Achiever, Dec 31, 2015.

  1. #1
    Hello,

    i have some problems regrarding,

    I want to show, image icons in output. so is that needed to take hosting for perticular image from www. or. is that possible on localhost?

    reply me some solutions..
     
    Achiever, Dec 31, 2015 IP
  2. elisagrace

    elisagrace Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #2
    Hey,here's the code to show image in java program:
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    import javax.swing.*;

    /**
    * A Java class to demonstrate how to load an image from disk with the
    * ImageIO class. Also shows how to display the image by creating an
    * ImageIcon, placing that icon an a JLabel, and placing that label on
    * a JFrame.
    *
    * @author alvin alexander, devdaily.com
    */
    public class ImageDemo
    {
    public static void main(String[] args) throws Exception
    {
    new ImageDemo(args[0]);
    }

    public ImageDemo(final String filename) throws Exception
    {
    SwingUtilities.invokeLater(new Runnable()
    {
    public void run()
    {
    JFrame editorFrame = new JFrame("Image Demo");
    editorFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    BufferedImage image = null;
    try
    {
    image = ImageIO.read(new File(filename));
    }
    catch (Exception e)
    {
    e.printStackTrace();
    System.exit(1);
    }
    ImageIcon imageIcon = new ImageIcon(image);
    JLabel jLabel = new JLabel();
    jLabel.setIcon(imageIcon);
    editorFrame.getContentPane().add(jLabel, BorderLayout.CENTER);

    editorFrame.pack();
    editorFrame.setLocationRelativeTo(null);
    editorFrame.setVisible(true);
    }
    });
    }
    }
     
    elisagrace, Jan 12, 2016 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    Sorry, but you're in the wrong area. Java is to JavaScript as Ham is to Hamburger! Completely unrelated language that only share the first four lettters as a marketing stunt by back-room white-collar criminal ***** from nearly twenty years ago.
     
    deathshadow, Jan 12, 2016 IP