1

I'm using a BufferedImage for smooth graphics (i.e. with no flicker). However, the square that is drawn in the middle is extremely flickery, and is actually invisible sometimes. I don't know what is wrong, this method has worked fine for my other games.

Here is my code (Square is type defined by me):

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import java.applet.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

public class DodgeArrows extends Applet implements KeyListener,Runnable{
    BufferedImage backbuffer;
    Thread gameloop;
    Square player;
    Random r = new Random();
    Graphics2D g2d;
    AffineTransform trans = new AffineTransform();
    public void init()
    {
        backbuffer = new BufferedImage(640,480,BufferedImage.TYPE_INT_RGB);
        g2d = backbuffer.createGraphics();
        player = new Square();
        player.setX(320);
        player.setY(480);
    }
    public void start()
    {
        gameloop = new Thread(this);
        gameloop.start();
    }
    public void run()
    {
        Thread t = Thread.currentThread();
        while(t==gameloop)
        {
            try
            {
                Thread.sleep(20);
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
            repaint();
        }
    }
    public void stop()
    {
        gameloop = null;
    }
    public void update(Graphics g)
    {
        g2d = (Graphics2D)g;
        g2d.setColor(Color.BLACK);
        g2d.fillRect(0,0,getSize().width,getSize().height);
        g2d.setColor(player.getColor());
        g2d.fill(player.getShape());
        paint(g);
    }
    public void keyTyped(KeyEvent e){}
    public void keyPressed(KeyEvent e)
    {
        //irrelevant keylistener code 
    }
    public void keyReleased(KeyEvent e){}

    public void paint(Graphics g)
    {
        g.drawImage(backbuffer,0,0,this);
    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
imulsion
  • 8,820
  • 20
  • 54
  • 84
  • 1
    1) Why code an applet? If it is due due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Jun 28 '13 at 18:15
  • @AndrewThompson I like applets. I am an enthusiast, not a pro. – imulsion Jun 28 '13 at 18:18
  • Well there you go.. I figured there had to be *someone* in the world that enjoys the pain. ;) – Andrew Thompson Jun 28 '13 at 18:19
  • @AndrewThompson pain? I find little pain in coding applets – imulsion Jun 28 '13 at 18:22
  • The pain mostly comes in deploying them, if you pay any attention to the users who complain about how it does not work in this or that version of their favorite browser. – Andrew Thompson Jun 28 '13 at 18:27
  • @AndrewThompson that is true, however i make them just for fun in appletviewer – imulsion Jun 28 '13 at 18:33

1 Answers1

2

Remove the line g2d = (Graphics2D)g; from your update(...) method and all your problems will go away.

You already created a backbuffer in the line g2d = backbuffer.createGraphics();. When you assign g to g2d you are making them both refer to the same Graphics object and are throwing away your buffer. You are back to drawing directly to the screen.

MaxAlexander
  • 460
  • 3
  • 10