Easiest way to display an image?
Hey everyone,
What is the easiest way to display an image using java graphics?
Thanks!
Hey everyone,
What is the easiest way to display an image using java graphics?
Thanks!
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?
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));
}
}
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!
> 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..
> > 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.
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.
> 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.