Easiest way to display an image?

Hey everyone,

What is the easiest way to display an image using java graphics?

Thanks!

[106 byte] By [cmpolisa] at [2007-11-27 10:45:23]
# 1

What type of image?

Where are you going to be displaying it?

What type of application?

Oh wait, I've got a question. What is the best way to design a JFrame in java?

Navy_Codera at 2007-7-28 20:12:39 > top of Java-index,Java Essentials,New To Java...
# 2

JLabel + ImageIcon:

import java.net.*;

import javax.swing.*;

public class ImageExample implements Runnable {

private URL url;

public ImageExample(URL url) {

this.url = url;

}

public void run() { //create gui

JFrame f = new JFrame();

f.getContentPane().add(new JLabel(new ImageIcon(url)));

f.pack();

f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

f.setLocationRelativeTo(null);

f.setVisible(true);

}

public static void main(String[] args) throws MalformedURLException {

URL url = new URL("http://blogs.sun.com/jag/resource/JagHeadshot-small.jpg");

SwingUtilities.invokeLater(new ImageExample(url));

}

}

BigDaddyLoveHandlesa at 2007-7-28 20:12:39 > top of Java-index,Java Essentials,New To Java...
# 3

What type of image? .gif

Where are you going to be displaying it? on a java applet

Is there a way of doing this without using jframe and swing. I have a basic java applet setup. I want to use g.drawimage(), but I dont know how to setup the image class and files, where do i put the files if I want them to load locally? Thanks again!

cmpolisa at 2007-7-28 20:12:39 > top of Java-index,Java Essentials,New To Java...
# 4

Sorry, I don't do applets.

BigDaddyLoveHandlesa at 2007-7-28 20:12:39 > top of Java-index,Java Essentials,New To Java...
# 5

> What type of image? .gif

> Where are you going to be displaying it? on a java

> applet

>

> Is there a way of doing this without using jframe and

> swing. I have a basic java applet setup. I want to

> use g.drawimage(), but I dont know how to setup the

> image class and files, where do i put the files if I

> want them to load locally? Thanks again!

I'm not sure if an applet can access local files without changing the permissions.. but since i only had a semester of java applets 3 years ago it could be changed..

deAppela at 2007-7-28 20:12:39 > top of Java-index,Java Essentials,New To Java...
# 6

> > Is there a way of doing this without using jframe

> and

> > swing. I have a basic java applet setup. I want to

> > use g.drawimage(), but I dont know how to setup

> the

> > image class and files, where do i put the files if

> I

> > want them to load locally? Thanks again!

I would recommend using JApplet instead of Applet, if possible. (This would also allow you to use BDLH's suggestion.)

>

> I'm not sure if an applet can access local files

> without changing the permissions.. but since i only

> had a semester of java applets 3 years ago it could

> be changed..

An applet can access local files, iff you have signed the applet and the end user accepts your certificate.

Navy_Codera at 2007-7-28 20:12:39 > top of Java-index,Java Essentials,New To Java...
# 7

This doesn't require a signed applet.

import java.awt.*;

import java.applet.*;

public class ImgApplet extends Applet {

Image pic;

public void init()

{

pic = getImage(getCodeBase(), getParameter("pic"));

}

public void paint(Graphics g)

{

g.drawImage(pic, 10, 10, this);

}

}

Use this html to start the above applet:

<HTML>

<BODY>

<APPLET CODE="ImgApplet.class" width=500 height=500>

<PARAM NAME=pic VALUE="some.gif">

</APPLET>

</BODY>

</HTML>

Place all three files (class file, html file, and gif)

in the same directory.

ChuckBinga at 2007-7-28 20:12:39 > top of Java-index,Java Essentials,New To Java...
# 8

Is there a way to set the filename in the java file, and not mess with the applet file?

Thanks

cmpolisa at 2007-7-28 20:12:39 > top of Java-index,Java Essentials,New To Java...
# 9

> Is there a way to set the filename in the java file,

> and not mess with the applet file?

>

> Thanks

Sure, just simplify the last code sample.

BigDaddyLoveHandlesa at 2007-7-28 20:12:39 > top of Java-index,Java Essentials,New To Java...