Forum Stats

  • 3,851,480 Users
  • 2,263,986 Discussions
  • 7,904,731 Comments

Discussions

JTable - animation

Aykut
Aykut Member Posts: 53
edited Apr 3, 2015 10:20AM in Java Programming

Hi all,

I have a JTable with 6 rows and 6 colums, i.e. 36 cells.

I have 1 image-icons..

I want an animation of the image, it should just be shown in the JTable as described below.

In my following explananation: Cell[row;column]

State 1

Cell[0;0] = 1st image

Cell[remain] = empty

PAUSE

State 2

Cell[0;0] = empty

Cell[0;1] = 1st image

Cell[remain] = empty


PAUSE


State 3

Cell[0;0] = empty

Cell[0;1] = empty

Cell[0;2] = 1st image

Cell[remain] = empty


PAUSE


and so on,



Once the image hits last column, it jumps 2n row 1st cell so the animation is like a snake move of the image.


I currently got stuck  loading the image 1 after another, so "transition from state_i  to state_i+1"

I got confused using Thread.


How can I have pause between those states (I described above).


I use the Threads somehow wrong.


Any example loading images in Cells (JTable) with pause in between?

Thanks Aykut



Tagged:

Answers

  • morgalr
    morgalr Member Posts: 457
    edited Mar 26, 2015 5:35PM

    code?

    My crystal ball says: you should be loading the images only 1 time, not every time you want to use them.

  • morgalr
    morgalr Member Posts: 457
    edited Mar 26, 2015 8:58PM

    Forget the thread, load the images once, then start a timer to space the transitions.

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Mar 27, 2015 3:08AM

    As @mogalr wrote load the image once and use a timer to step through the phases of the animation.

    Create a TableCellRender that shows that image (or not) depending on the current cells value.

    Create your own TableModel as extension of the DefaultTableModel. The method getValueAt() in the TableModel sould decode the current step and return a value (eg. a Boolean) that can be processed by the TableCellRenderer.

    The timer moves the animation to the next step and calls tableDataChanged() on the DefaultTableModel to force an update of the Table.

    bye

    TPD

  • Aykut
    Aykut Member Posts: 53
    edited Mar 29, 2015 11:44AM

    Following is my current challange

    - Table has 6x6 cells.

    - Table is empty,

    On cell [0;0] i.e. very first cell, I want to show 10 images one after another with delay of 1 seconds.

    I use this code:

    for (int i = 0; i < imagePath_array.length; i++) {

      ImageIcon icon2 = new ImageIcon(imagePath_array[i]);

      final Object[] obj = { null, icon2, null, true };

      try {

           Thread.sleep(1000);

      } catch (InterruptedException e1) {

           e1.printStackTrace();

      }

      ((MyTableModel) table.getModel()).setValueAt(obj, 0, 0);

    }

    My Problem:

    It does not show 1 after another image in cell 0;0.

    It shows after a while just only the last image on cell 0;0.

    How can I animate?

    Thx,
    Aykut

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Apr 2, 2015 5:46PM

    As I wrote earlier: you Need 3 Objecs:

    1. A CellRenderer, which holds the Image to draw an knows the model value that represents the current image loacation.
    2. A model class, that is storing the walues shown by the JTable (and ist CellRenderer)
    3. A Timer class, that manipulates the Model so that the Image is shown at a different cell.

    In your code I see all three of the Tasks done in one place. It loos like it is the timer implementation but since this is no SSCCE (http://sscce.org/)  I can only guess.

    It shows after a while just only the last image on cell 0;0.
    

    Line 12 of your code is

    ((MyTableModel) table.getModel()).setValueAt(obj, 0, 0);
    

    Do that numbers in your code look familiar to you?

    Do you know that those numbers are used for?

    bye

    TPD

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Apr 3, 2015 10:20AM

    here is how I'd implement the Stepper:

    import static org.junit.Assert.*;
    import org.junit.Test;
    public class TableAnimationTest {
    public class PhaseStepper {
      private final int columnCount;
      private final int rowCount;
      private int step;
      public PhaseStepper(int columnCount, int rowCount) {
       this.columnCount = columnCount;
       this.rowCount = rowCount;
      }
      public boolean isActive(int column, int row) {
       return (step % columnCount) == column
         && (step / columnCount) == row;
      }
      public void step() {
       step=++step%(columnCount*rowCount);
      }
    } @Test
    public void testPhaseStepper$isActive_fieldSize2x2_whenInitial_cell0x0isActive() {
      int columnCount = 2;
      int rowCount = 2;
      PhaseStepper phaseStepper = new PhaseStepper(columnCount, rowCount);
      boolean isActive = phaseStepper.isActive(0, 0);   assertTrue("active field is 0-0", isActive);
      assertOneActiveFieldOnly(columnCount, rowCount, phaseStepper);
    } @Test
    public void testPhaseStepper$isActive_fieldSize3x2_whenSteppedOnce_cell1x0isActive() {
      int columnCount = 3;
      int rowCount = 2;
      PhaseStepper phaseStepper = new PhaseStepper(columnCount, rowCount);   phaseStepper.step();
      boolean isActive = phaseStepper.isActive(1, 0);   assertTrue("active field is 1-0", isActive);
      assertOneActiveFieldOnly(columnCount, rowCount, phaseStepper);
    } @Test
    public void testPhaseStepper$isActive_fieldSize3x2_whenSteppedTwice_cell2x0isActive() {
      int columnCount = 3;
      int rowCount = 2;
      PhaseStepper phaseStepper = new PhaseStepper(columnCount, rowCount);   phaseStepper.step();
      phaseStepper.step();
      boolean isActive = phaseStepper.isActive(2, 0);   assertTrue("active field is 2-0", isActive);
      assertOneActiveFieldOnly(columnCount, rowCount, phaseStepper);
    } @Test
    public void testPhaseStepper$isActive_fieldSize3x2_whenSteppedTripple_cell0x1isActive() {
      int columnCount = 3;
      int rowCount = 2;
      PhaseStepper phaseStepper = new PhaseStepper(columnCount, rowCount);   phaseStepper.step();
      phaseStepper.step();
      phaseStepper.step();
      boolean isActive = phaseStepper.isActive(0, 1);   assertTrue("active field is 0-1", isActive);
      assertOneActiveFieldOnly(columnCount, rowCount, phaseStepper);
    }

    @Test
    public void testPhaseStepper$isActive_fieldSize3x2_whenSteppedBeyondLastCell_cell0x0isActive() {
      int columnCount = 3;
      int rowCount = 2;
      PhaseStepper phaseStepper = new PhaseStepper(columnCount, rowCount);
      for (int i = 0; i < columnCount; i++) {
       for (int j = 0; j < rowCount; j++) {
        phaseStepper.step();
       }
      }
      boolean isActive = phaseStepper.isActive(0, 0);
     
      assertTrue("active field is 0-0", isActive);
      assertOneActiveFieldOnly(columnCount, rowCount, phaseStepper);
    } private void assertOneActiveFieldOnly(int columnCount, int rowCount, PhaseStepper phaseStepper) {
      int countActiveFields = 0;
      for (int i = 0; i < columnCount; i++) {
       for (int j = 0; j < rowCount; j++) {
        countActiveFields += phaseStepper.isActive(i, j) ? 1 : 0;
       }
      }
      assertEquals("only one active field", 1, countActiveFields);
    }
    }

    bye

    TPD

This discussion has been closed.