Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > simple animation question (I hope)

Reply
Thread Tools

simple animation question (I hope)

 
 
John Doe
Guest
Posts: n/a
 
      11-19-2006
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.

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      11-19-2006
John Doe wrote:
> 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);
}
}

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
 
hiwa
Guest
Posts: n/a
 
      11-19-2006
John Doe wrote:
> 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_CLOS E);
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();
}
}

 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      11-19-2006
John Doe wrote:

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

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


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

Andrew T.

 
Reply With Quote
 
hiwa
Guest
Posts: n/a
 
      11-19-2006
Andrew Thompson wrote:
> John Doe wrote:
>
> "..and now for something completely different"
> (to the fine answers, already presented)
>
> > 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, ..

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

 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      11-19-2006
hiwa wrote:
> Andrew Thompson wrote:
> > John Doe wrote:


(re. table cells)

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

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

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

 
Reply With Quote
 
Simon Brooke
Guest
Posts: n/a
 
      11-19-2006
in message <(E-Mail Removed). com>, John Doe
('(E-Mail Removed)') wrote:

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

--
http://www.velocityreviews.com/forums/(E-Mail Removed) (Simon Brooke) http://www.jasmine.org.uk/~simon/
;; If Python is executable pseudocode,
;; then Perl is executable line noise
-- seen on Slashdot.
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      11-19-2006

Simon Brooke wrote:
> in message <(E-Mail Removed). com>, John Doe
> ('(E-Mail Removed)') wrote:
>
> > 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.


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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make simple animation on web softwaredeveloper Javascript 1 09-27-2006 04:24 PM
error in simple animation Sudhin Java 2 07-19-2006 05:50 PM
[New to C] Creating a simple animation using C. bfowlkes@gmail.com C Programming 7 03-26-2006 12:19 AM
Help with simple animation in JApplet. John D. Java 5 11-02-2005 03:41 AM
Simple Animation Question Alberto.V Java 9 12-11-2003 07:10 AM



Advertisments