simple animation question (I hope)

Discussion in 'Java' started by John Doe, Nov 19, 2006.

  1. John Doe

    John Doe Guest

    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.
     
    John Doe, Nov 19, 2006
    #1
    1. Advertising

  2. 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/
     
    Knute Johnson, Nov 19, 2006
    #2
    1. Advertising

  3. John Doe

    hiwa Guest

    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_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();
    }
    }
     
    hiwa, Nov 19, 2006
    #3
  4. 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.
     
    Andrew Thompson, Nov 19, 2006
    #4
  5. John Doe

    hiwa Guest

    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.
     
    hiwa, Nov 19, 2006
    #5
  6. 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.'
     
    Andrew Thompson, Nov 19, 2006
    #6
  7. John Doe

    Simon Brooke Guest

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


    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.

    --
    (Simon Brooke) http://www.jasmine.org.uk/~simon/
    ;; If Python is executable pseudocode,
    ;; then Perl is executable line noise
    -- seen on Slashdot.
     
    Simon Brooke, Nov 19, 2006
    #7
  8. John Doe

    Daniel Pitts Guest

    Simon Brooke wrote:
    > in message <>, 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.

    >
    > 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.
     
    Daniel Pitts, Nov 19, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. me2u
    Replies:
    2
    Views:
    309
    Saravana [MVP]
    Oct 31, 2003
  2. Shane
    Replies:
    4
    Views:
    351
    Shane
    Jul 20, 2004
  3. Rob

    simple question (I hope!)

    Rob, Jan 5, 2007, in forum: HTML
    Replies:
    10
    Views:
    553
    dorayme
    Jan 8, 2007
  4. Bryan Glennon

    Simple attribute question (I hope)

    Bryan Glennon, Nov 12, 2003, in forum: ASP .Net Web Controls
    Replies:
    5
    Views:
    166
    Jacob Yang [MSFT]
    Nov 17, 2003
  5. Adrian

    Simple question I hope!

    Adrian, Sep 19, 2003, in forum: Javascript
    Replies:
    9
    Views:
    150
    Adrian
    Sep 21, 2003
Loading...

Share This Page