Loading a picture from URL

I'm trying to load a picture from an url. However, the class is not an applet. so I can't use: (apparently)

try{

base =new URL ("http://somewhereonthenet/" );

}

catch (MalformedURLException e)

{

System.out.println ("ouch that hurts");

}

images[m] = getImage(base,"orange.png");

So how the heck can I load a picture from an url into a picture object? My applet has a total of 10 classes so do I really can't make the helper class an applet right?

I don't know if I made everyone confused but this is what I'm trying to do in simplest terms:

I have a project with ... 'Main.java' (extends applet) then I have a class 'Loadpictures.java'. In 'Main.java' I instantiate 'Loadpictures.java' with this

publicvoid init()

{

Loadpictures images =new Loadpictures(url);

}

And finally within the "Loadpictures.java" class I need to load the picture up via "images[m] = getImage(base, "orange.png");" as I stated earlier. But I can't because 'Loadpictures.java' isn't an applet.

Wow, what do I do? Do I really need to create these pictures within Main.java?

Message was edited by:

Java_Rabbit

[1665 byte] By [Java_Rabbita] at [2007-11-26 15:10:57]
# 1
You can use ImageIO.read(url) or Toolkit.getDefaultToolkit().getImage(url)
Rodney_McKaya at 2007-7-8 9:01:49 > top of Java-index,Security,Cryptography...
# 2

Image files are in the images folder which is in the current folder.

Images from links at the bottom of [url=http://java.sun.com/docs/books/tutorial/uiswing/components/examples/index.html]Examples Index[/url].

// <applet code="AppletImages" width="400" height="400"></applet>

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.*;

import java.net.*;

import javax.imageio.ImageIO;

import javax.swing.*;

public class AppletImages extends JApplet {

public void init() {

Image[] images = loadImages();

setLayout(new GridLayout(0,2));

for(int j = 0; j < images.length; j++)

add(new JLabel(new ImageIcon(images[j]), JLabel.CENTER));

}

private Image[] loadImages() {

String[] ids = { "-t", "-c", "-cg--", "h-", "-c-h-" };

Image[] images = new Image[ids.length];

int j = 0;

try {

// Use applet methods.

images[j] = getImage(getCodeBase(), "images/geek" + ids[j] + ".gif");

loadImage(images[j]);

j++;

images[j] = getImage(new URL("file:c:/jexp/images/geek" +

ids[j] + ".gif"));

loadImage(images[j]);

// Use Toolkit method.

j++;

// Class loader does the lookup with this Class method.

// Therefore, image file must be on class path.

URL url = getClass().getResource("images/geek" + ids[j] + ".gif");

images[j] = Toolkit.getDefaultToolkit().createImage(url);

loadImage(images[j]);

// Use ImageIcon method.

j++;

url = getClass().getResource("images/geek" + ids[j] + ".gif");

ImageIcon icon = new ImageIcon(url);

int status = icon.getImageLoadStatus();

if(status != MediaTracker.COMPLETE)

System.out.println("ImageIcon load error: " + status);

images[j] = icon.getImage();

// Use ImageIO method.

j++;

url = getClass().getResource("images/geek" + ids[j] + ".gif");

images[j] = ImageIO.read(url);

} catch(MalformedURLException mue) {

System.out.println("url error for " + ids[j] +

": " + mue.getMessage());

} catch(IOException ioe) {

System.out.println("read error for " + ids[j] +

": " + ioe.getMessage());

}

return images;

}

private void loadImage(Image image) {

MediaTracker mt = new MediaTracker(this);

mt.addImage(image, 0);

try {

mt.waitForID(0);

} catch(InterruptedException e) {

System.out.println("load error status: " + mt.statusAll(false));

}

if(mt.isErrorAny())

System.out.println("load error: " + mt.statusAll(false));

}

}

crwooda at 2007-7-8 9:01:49 > top of Java-index,Security,Cryptography...