GIF's

Can java use gifs that have animation on it- a moving picture?
[69 byte] By [ichbinsterbena] at [2007-11-27 3:43:19]
# 1

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());

}

}

DrLaszloJamfa at 2007-7-12 8:46:58 > top of Java-index,Java Essentials,New To Java...
# 2

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){}

}

}

}

ichbinsterbena at 2007-7-12 8:46:58 > top of Java-index,Java Essentials,New To Java...
# 3

> [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

ichbinsterbena at 2007-7-12 8:46:58 > top of Java-index,Java Essentials,New To Java...
# 4

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.

DrLaszloJamfa at 2007-7-12 8:46:58 > top of Java-index,Java Essentials,New To Java...
# 5
Your code works better than mine, however i don't want the URL, i need it for the flash. Right now im using AWT cuz that's what ive been working with the past couple of months.
ichbinsterbena at 2007-7-12 8:46:58 > top of Java-index,Java Essentials,New To Java...
# 6

> 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.

DrLaszloJamfa at 2007-7-12 8:46:58 > top of Java-index,Java Essentials,New To Java...
# 7
Why can't you use a local URL?URL url = new File("path to flash drive").toURL();Edit: too late again...
jsalonena at 2007-7-12 8:46:58 > top of Java-index,Java Essentials,New To Java...
# 8
If you absolutely have to, you can convert a File to a URL:File file = ...URL url = file.toURI().toURL();ImageIcon also has a constructor that takes a file path String.
DrLaszloJamfa at 2007-7-12 8:46:58 > top of Java-index,Java Essentials,New To Java...
# 9

> 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.

DrLaszloJamfa at 2007-7-12 8:46:58 > top of Java-index,Java Essentials,New To Java...
# 10

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

ichbinsterbena at 2007-7-12 8:46:58 > top of Java-index,Java Essentials,New To Java...
# 11
?
DrLaszloJamfa at 2007-7-12 8:46:58 > top of Java-index,Java Essentials,New To Java...
# 12
what do you mean "?"
ichbinsterbena at 2007-7-12 8:46:58 > top of Java-index,Java Essentials,New To Java...
# 13
> what do you mean "?"I saw your post when it was just one line long! Give me a minute to stare at that error...
DrLaszloJamfa at 2007-7-12 8:46:58 > top of Java-index,Java Essentials,New To Java...
# 14
You are calling new URL("...") on IconExample.java, line 7 with a malformed string.It doesn't look like a url, the kind of string that you could enter into your browser's address bar.
DrLaszloJamfa at 2007-7-12 8:46:58 > top of Java-index,Java Essentials,New To Java...
# 15
how do i not use a URL
ichbinsterbena at 2007-7-21 20:53:37 > top of Java-index,Java Essentials,New To Java...
# 16

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());

}

}

DrLaszloJamfa at 2007-7-21 20:53:37 > top of Java-index,Java Essentials,New To Java...
# 17

> 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();

jsalonena at 2007-7-21 20:53:37 > top of Java-index,Java Essentials,New To Java...
# 18
URL u = file.toURL(); FYI: File's toURL was deprecated in 1.6
DrLaszloJamfa at 2007-7-21 20:53:37 > top of Java-index,Java Essentials,New To Java...
# 19
> FYI: File's toURL was deprecated in 1.6Wow, it was about time!
jsalonena at 2007-7-21 20:53:37 > top of Java-index,Java Essentials,New To Java...