1

I have a script that cycles (almost like a slideshow) through a Vector object (flipBook), using a Thread (animationThread), and adds them to a JPanel. However, the added image is only 2x2 pixels large. I've verified the images are 50x50, but they don't appear to be properly showing.

Here's some of the code going on behind the Thread instance. I'm not entirely sure which code would be beneficial for finding the source for.

public void startThread() {
    if (flipWidth != 0 && flipHeight != 0) {
        System.out.println("[ AnimationAsset ] " + "We're starting the thread");
        Runnable r = new Runnable() {
            @Override
            public void run() {
                runWork();
            }           
        };
        animationThread = new Thread(r, "AnimationThread");
        animationThread.start();
        going = true;
    }
}
private void runWork() {
    try {
        while (going) {
            repaint();
            flipIndex = (flipIndex + 1) % numFlips;
            System.out.println("[ AnimationAsset ] flipIndex: " + flipIndex);
            Thread.sleep(1000);
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        System.out.println("[ AnimationAsset ] " + "Interrupted");
    }
}
public void paint(Graphics g) {
    update(g);
}
public void update(Graphics g) {
    System.out.println("[ AnimationAsset ] " + flipIndex);
    ((Graphics2D) g).drawImage(flipBook.get(flipIndex), null, 5, 5);

}
Firstmate
  • 25
  • 1
  • 4

2 Answers2

3

and adds them to a JPanel

This is not a Swing code. This is AWT code.

You would never override the update() and paint() methods this way when using Swing. Get rid of this code and start over.

To do this in Swing I would use a JLabel with an Icon and add the label to the frame.

Then, to do animation in SWing your should use a Swing Timer.

When the timer fires you simply use the setIcon(...) method of the label to replace the old icon with your new icon.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • +1 for `javax.swing.Timer` and `Icon` in `JLabel`. Here's a related [example](http://stackoverflow.com/questions/3078178/jpanel-in-puzzle-game-not-updating/3078354#3078354). – trashgod Aug 07 '11 at 05:07
  • Hey this worked perfectly! Thank you a lot, it's *a lot* cleaner than how I was doing it before (which was also more confusing). Again, thanks. – Firstmate Aug 07 '11 at 19:38
1

Emm you have a strange drawing code as

((Graphics2D) g).drawImage(flipBook.get(flipIndex), null, 5, 5);

You can see the docs here... to use Graphic2D drawImage() method right

the most common way of image drawing is to paint image right on JComponent, as a rule, the JLabel. Here is a component example

public class MyLabel extends JLabel
{

private Image image;

public MyLabel(Image image)
{

this.image=image;

}

public void paintComponent(Graphics g)
{

  g.drawImage(this.image,x,y,width,height,null);

}

}

so here you can use the component as a common swing object

public class MyPanel extends JPanel
{
 public MyPanel()
{
  Image image=null;
 try{
  image=ImageIO.read(new File("image.png"));
  }
 catch (IOException e) {
 }

  this.add(new MyLabel(image));

}

}

Good luck

user592704
  • 3,674
  • 11
  • 70
  • 107
  • I don't quite understand, I just wanted to draw a BufferedImage. That's all. – Firstmate Aug 07 '11 at 02:31
  • Then use a JLabel with an Icon and you don't need any custom painting code. The code you posted should NOT be used with Swing! – camickr Aug 07 '11 at 02:54
  • @Camickr, can you point me to an article describing the distinctions between the two at an API level? As someone who is just beginning java graphics programming, I'm finding the distinction between the two somewhat confusing. – Firstmate Aug 07 '11 at 03:09
  • 1
    As I have stated twice before there is no need to do custom painting. You can use standard Swing components for this requirement. So I suggest you learn Swing first. When you are ready for custom painting then read the Swing tutorial. I already gave you a link to the tutorial in my above answer. If you look at the table of contents you will find a section on `Performing Custom Painting` right at the bottom of the list. – camickr Aug 07 '11 at 03:47
  • You can use Swing components as a canvas for Graphics painting. The most common painting component is either JLabel or JPanel. I edited my answer watch it please – user592704 Aug 07 '11 at 05:26