simple animation question (I hope)

J

John Doe

Hi. Lets say I have 3 images (image1.jpg, image2,jpg and image3.jpg).
What is the simplest way to show image1.jpg in a table cell, then 1
second later - image2.jpg, then 1 second later - image3.jpg?

I'm trying to create a time counter showing elapsed time, or maybe time
left in a graphical way, say as a bar that moves, or maybe a little dot
that moves each second. I would like to show that animation in a table
cell. Something like that.
 
K

Knute Johnson

John said:
Hi. Lets say I have 3 images (image1.jpg, image2,jpg and image3.jpg).
What is the simplest way to show image1.jpg in a table cell, then 1
second later - image2.jpg, then 1 second later - image3.jpg?

I'm trying to create a time counter showing elapsed time, or maybe time
left in a graphical way, say as a bar that moves, or maybe a little dot
that moves each second. I would like to show that animation in a table
cell. Something like that.

import java.awt.*;
import java.awt.event.*;

public class TimePanel extends Panel implements Runnable {
int msec;
double percent;

public TimePanel(int msec) {
super();

this.msec = msec;

new Thread(this).start();
}

public void run() {
long stop = System.currentTimeMillis() + msec;
while (stop > System.currentTimeMillis()) {
try {
Thread.sleep(10);
} catch (InterruptedException ie) { }
percent = 1.0 - (stop - System.currentTimeMillis()) /
(double)msec;
repaint();
}
percent = 0.0;
repaint();
new Thread(this).start();
}

public void update(Graphics g) {
paint(g);
}

public void paint(Graphics g) {
if (percent == 0.0) {
g.setColor(Color.WHITE);
g.fillRect(0,0,getWidth(),getHeight());
}

g.setColor(Color.BLUE);
int width = (int)(getWidth() * percent);
g.fillRect(0,0,width,getHeight());

String str = Integer.toString(msec) + " msec";
FontMetrics fm = g.getFontMetrics();
int strWidth = fm.stringWidth(str);
g.setColor(Color.WHITE);
g.drawString(str,(getWidth() - strWidth)/2,
(getHeight() + fm.getHeight() / 2) / 2);
}

public static void main(String[] args) {
Frame f = new Frame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
TimePanel tp = new TimePanel(10000);
f.add(tp);
f.setSize(120,80);
f.setVisible(true);
}
}
 
H

hiwa

John said:
Hi. Lets say I have 3 images (image1.jpg, image2,jpg and image3.jpg).
What is the simplest way to show image1.jpg in a table cell, then 1
second later - image2.jpg, then 1 second later - image3.jpg?

I'm trying to create a time counter showing elapsed time, or maybe time
left in a graphical way, say as a bar that moves, or maybe a little dot
that moves each second. I would like to show that animation in a table
cell. Something like that.
/*
With an immense help from guru camickr as shown on:
http://forum.java.sun.com/thread.jspa?messageID=4472998
*/
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;

public class AnimeTableCell{
JFrame frame;
Container con;
JScrollPane scp;
JTable table;
AnimePanel anp, ant;
Image[] images;

public AnimeTableCell(){
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con = frame.getContentPane();

images = new Image[4];
images[0] = new ImageIcon("images/a.png").getImage();
images[1] = new ImageIcon("images/b.png").getImage();
images[2] = new ImageIcon("images/c.png").getImage();
images[3] = new ImageIcon("images/d.png").getImage();

table = new JTable(10, 6){
public Component prepareRenderer
(TableCellRenderer renderer, int row, int column){
Component c = super.prepareRenderer(renderer, row, column);
if (row == 3 && column == 2){
if (ant != null){
c = ant;
}
}
return c;
}
};
scp = new JScrollPane(table);
con.add(scp, BorderLayout.CENTER);
ant = new AnimePanel(images, 500, true); //for cell renderer,
repaint table

anp = new AnimePanel(images, 500, false); //this doesn't repaint
table
con.add(anp, BorderLayout.SOUTH);

frame.setSize(400, 400);
frame.setVisible(true);
}

class AnimePanel extends JPanel implements ActionListener{
Image[] imgs;
int delay;
boolean trepaint;
int range;
int idx;
Timer timer;
AbstractTableModel atb;

public AnimePanel(Image[] ima, int de, boolean r){
setPreferredSize
(new Dimension(ima[0].getWidth(this), ima[0].getHeight(this)));
imgs = ima;
delay = de;
trepaint = r;

range = ima.length - 1;
idx = 0;
atb = (AbstractTableModel)(table.getModel());
timer = new Timer(delay, this);
timer.start();
}

public void actionPerformed(ActionEvent e){
++idx;
if (idx > range){
idx = 0;
}
repaint();
if (table != null && trepaint){
atb.fireTableCellUpdated(3, 2);
}
}

public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(imgs[idx], 0, 0, this);
}
}

public static void main(String[] args){
new AnimeTableCell();
}
}
 
H

hiwa

Andrew said:
John Doe wrote:

"..and now for something completely different"
(to the fine answers, already presented)


<http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JProgressBar.html>

Andrew T.
As you might already know it well, rendering JProgressBar as a table
cell is not a simple task to write. Cell renderer component is not a
real component that would do real animation of incremental bar
painting. It's a static snapshot, or a rubber-stamp-sort-of.
 
A

Andrew Thompson

(re. table cells)
...
As you might already know it well, rendering JProgressBar as a table
cell is not a simple task to write.

Actually - I was unaware of it. Thanks for the tip.

...were you aware that 'best trimming' practices would
suggest that 'sigs.'* or signatures should be trimmed from
replies? ;-)

Andrew T. <- *'sig.'
 
S

Simon Brooke

John Doe said:
Hi. Lets say I have 3 images (image1.jpg, image2,jpg and image3.jpg).
What is the simplest way to show image1.jpg in a table cell, then 1
second later - image2.jpg, then 1 second later - image3.jpg?

I'm trying to create a time counter showing elapsed time, or maybe time
left in a graphical way, say as a bar that moves, or maybe a little dot
that moves each second. I would like to show that animation in a table
cell. Something like that.

This question has nothing whatever to do with Java; you would be better
asking it in a JavaScript group.

Java and JavaScript are completely different languages. The confusion of
names arises out of an incredibly stupid marketing decision.
 
D

Daniel Pitts

Simon said:
This question has nothing whatever to do with Java; you would be better
asking it in a JavaScript group.

Java and JavaScript are completely different languages. The confusion of
names arises out of an incredibly stupid marketing decision.

I have to ask, what makes you think he specifically means an HTML
table? Although it would have been better for him to be more specific,
I think from the context everyone assumed he meant a JTable.

Indeed, Java and JavaScript are different, but I think his quesion was
appropriate.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,270
Messages
2,571,102
Members
48,773
Latest member
Kaybee

Latest Threads

Top