import java.net.*;
import javax.swing.*;
public class IconExample implements Runnable {
public void run() {
try {
URL url = new URL("http://www.jorgechaclan.com/duke/duke-thumbs.gif");
Icon icon = new ImageIcon(url);
JLabel label = new JLabel(icon);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(label);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new IconExample());
}
}
here does this look about right for another way?
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class GIFDemo extends MIDlet {
private boolean boolMotion=false;
private int iX=10,iY=60;
Display mDisplay;
Thread th;
public void destroyApp(boolean unconditional){}
public void pauseApp() {}
public void startApp() {
mDisplay = Display.getDisplay(this);
final MyCanvas can = new MyCanvas();
mDisplay.setCurrent(can);
}
}
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class MyCanvas extends Canvas implements Runnable {
Image img[]=new Image[3];
public MyCanvas() {
try {
img[0]=Image.createImage("/img1.png");
img[1]=Image.createImage("/img2.png");
img[2]=Image.createImage("/img3.png");
}catch(Exception e){}
Thread th=new Thread(this);
th.start();
}
//Display GIF image
public void paint(Graphics g) {
g.drawImage(img[imgIndex],0,0,g.TOP|g.LEFT);
}
//Handling keyEvents
protected void keyPressed(int keyCode) {
}
public void run() {
while(true) {
imgIndex++;
imgIndex%=3;
try {
Thread.sleep(500);
}catch(Exception e){}
}
}
}
> [code]import java.net.*;
> import javax.swing.*;
>
> public class IconExample implements Runnable {
>public void run() {
>try {
> URL url = new
> URL("http://www.jorgechaclan.com/duke/duke-thumbs.gif
> );
> Icon icon = new ImageIcon(url);
> JLabel label = new JLabel(icon);
>
> JFrame f = new JFrame();
> f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> f.getContentPane().add(label);
> f.pack();
> f.setLocationRelativeTo(null);
> f.setVisible(true);
>
> } catch (MalformedURLException e) {
>throw new RuntimeException(e);
>}
> }
>
>public static void main(String[] args) {
>SwingUtilities.invokeLater(new IconExample());
>}
> /code]
what do i do if the gif is saved on a flashdrive and not the Internet
1. You haven't stated if this is meant to use Swing or the old AWT. If Swing,
then don't use Canvas. You could use JPanel instead, but I would prefer JLabel myself, and just swap icons.
2. You have to be careful about multithreading:
http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html
To drive animation, you should use javax.swing.Timer, not your own thread.
3. Your GUI may not be updating -- I don't recall you writing if this code runs.
After changing you choice of image, you need to call repaint.
> what do i do if the gif is saved on a flashdrive and not the Internet
URLs are universal. This code works when the .class file and the image are in the same location:
URL url = this.getClass().getResource("file.gif");
If they are not in the same location, you can supply a relative path
,
or one that starts with /:
"images/file.gif" or "/images/file.gif", depending on your resource structuring.
If you need to supply an absolute path, I would think again. Most applications
should be zipped into jars and shouldn't use absolute paths for basic
resources like images.
> Why can't you use a local URL?
>
> URL url = new File("path to flash drive").toURL();
>
> Edit: too late again...
You beat me to it, anyway. But like I said, it it's a resource, it should probably
end up in a jar, so using this to permit the use of File is asking for trouble.
It gives me a **** load of error messages
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: java.net.MalformedURLException: unknown protocol: e
at IconExample.run(IconExample.java:19)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Caused by: java.net.MalformedURLException: unknown protocol: e
at java.net.URL.<init>(URL.java:574)
at java.net.URL.<init>(URL.java:464)
at java.net.URL.<init>(URL.java:413)
at IconExample.run(IconExample.java:7)
... 7 more
Message was edited by:
ichbinsterben
In reply #8, I mentioned that you can pass a path string to one of the ImageIcon constructors:
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
public class ImageExample implements Runnable, ActionListener {
private JLabel label;
public void run() {
label = new JLabel();
label.setPreferredSize(new Dimension(150,200));
JButton display = new JButton("Display");
display.addActionListener(this);
JPanel p = new JPanel(new BorderLayout());
p.add(label, BorderLayout.CENTER);
p.add(display, BorderLayout.SOUTH);
JFrame f = new JFrame("ImageExample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
label.setIcon(new ImageIcon("C:\\folder\\anotherfolder\\example.jpeg"));
}
public static void main(String[] args) {
EventQueue.invokeLater(new ImageExample());
}
}
> unknown protocol: e
You didn't really do what was suggested, did you? You tried to construct a URL like "new URL("e:/some/path")," didn't you? Well, what you wrote there in the quotes is a file path, not a URL. To convert a file path to a URL do what is shown above:File f = new File("e:/some/path");
URL u = f.toURL();
// or
URL u = f.toURI().toURL();