This has got to be the oldest gripe in the book, but a friend of mine using the Java2D api has been experiencing flicker problems using the following code. As far as I can tell it uses double-buffering correctly, and I've seen the same problems myself when I run it. I've thrown every trick in the book I know at it, even going so far as "bufferInt = ((DataBufferInt)(buffer.getRaster().getDataBuffer())).getData();", to no avail. Any clues as to the underlying problem would be appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*;
public class ImageFrame extends JFrame implements ActionListener{ private BufferedImage frameImg; private Graphics2D frameImgG; private Image plainsI, warriorI; private int xloc; private Timer xlocTimer; public ImageFrame() { super("Image testing"); } public void run() { plainsI = Toolkit.getDefaultToolkit().getImage("Plains01.jpg"); warriorI = Toolkit.getDefaultToolkit().getImage("Warrior.png"); frameImg = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB); frameImgG = frameImg.createGraphics(); xloc = 150; xlocTimer = new Timer(200, this); xlocTimer.start(); setSize(800, 600); setVisible(true); } public void paint(Graphics g) { super.paint(g); frameImgG.drawImage(plainsI, 0, 0, 800, 600, null); frameImgG.drawImage(warriorI, xloc, 400, null); g.drawImage(frameImg, 0, 0, null); } public void actionPerformed(ActionEvent a) { repaint(); xloc+=10; if (xloc > 500) { xlocTimer.stop(); } } public static void main(String[] args) { ImageFrame application = new ImageFrame(); application.run(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |