Animation for table cell renderer
Code shown below is a complete failure. Nothing is displayed on the target cell let alone do animation. What's wrong with this code?
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
publicclass 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();
JTable table =new JTable(10, 6){
public Component preapareRenderer
(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 AnimePanelextends JPanelimplements ActionListener{
Image[] imgs;
int delay;
boolean trepaint;
int range;
int idx;
Timer timer;
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;
timer =new Timer(delay,this);
timer.start();
}
publicvoid actionPerformed(ActionEvent e){
++idx;
if (idx > range){
idx = 0;
}
repaint();
if (table !=null && trepaint){
table.repaint();
}
}
publicvoid paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(imgs[idx], 0, 0,this);
}
}
publicstaticvoid main(String[] args){
new AnimeTableCell();
}
}

