user1007883
user1007883

Reputation: 69

Graphics flashing

When I run the program everything works fine, it all works and stuff but the graphics constantly flash. I'm new to doing stuff like this and I have no idea how to fix this.

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;


public class MovementTest extends Frame implements Runnable, MouseMotionListener, KeyListener {

    private static Thread thread;
    private int mouseX, mouseY;
    private boolean movingRight;
    private boolean movingLeft;
    private boolean movingUp;
    private boolean movingDown;

    private Rectangle player = new Rectangle(50, 50, 100, 100);
    private Rectangle wallOne = new Rectangle(400, 50, 100, 100);

    public MovementTest() {
        setSize(800, 600);
        setVisible(true);
        addKeyListener(this);
        addMouseMotionListener(this);
    }

    public static void main(String[] args) {
        thread = new Thread(new MovementTest());
        thread.start();
    }

    @Override
    public void run() {
        try {
            while(true) {
                if(player.intersects(wallOne)) {
                    thread.stop();
                    break;
                }
                if(movingRight) {
                    player.x++;
                } else if(movingLeft) {
                    player.x--;
                }
                if(movingUp) {
                    player.y--;
                } else if(movingDown) {
                    player.y++;
                }
                paint(getGraphics());
                Thread.sleep(5);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_D)
            movingRight = true;
        if (key == KeyEvent.VK_A)
            movingLeft = true;
        if (key == KeyEvent.VK_W)
            movingUp = true;
        if (key == KeyEvent.VK_S)
            movingDown = true;
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_D)
            movingRight = false;
        if (key == KeyEvent.VK_A)
            movingLeft = false;
        if (key == KeyEvent.VK_W)
            movingUp = false;
        if (key == KeyEvent.VK_S)
            movingDown = false;
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    public void paint(Graphics g) {
        g.drawString("x: " + mouseX + " y: " + mouseY, 5, 35);
        g.drawRect(player.x, player.y, 100, 100);
        g.drawRect(wallOne.x, wallOne.y, 100, 100);
        repaint();
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        mouseX = e.getX();
        mouseY = e.getY();
    }
}

Upvotes: 1

Views: 2873

Answers (2)

Fault
Fault

Reputation: 1249

You definitely do want to look into double buffering. Right now what you are doing is drawing everything to the screen directly, and the user sees this. What double buffering does is it first draws everything on an off-screen buffer (image), and once it's calculated all that, and the image is ready, you draw the off-screen buffer on to your visible buffer (thus the user only sees the final frame rather than the in-between frames).

It's a similar concept as to what OpenGL or Direct3D applications most commonly do. They have two buffers. Everything is drawn to the off-screen buffer, and once that's done, the buffers are swapped, and the one that's now off screen is cleared, after which a new image is once again drawn on it.

And as glowcoder also pointed out, you have a really high fps. Try to synchronize it with your monitor's refresh rate to avoid wasting resources drawing more often than is necessary.

Upvotes: 4

corsiKa
corsiKa

Reputation: 82589

Take repaint out of your paint method. You reallly don't want it in there. That's what's causing your problem.

After that, add this - it helps keep things organized in the long run.

public void update() {
    repaint();
}

Then, add in repaint methods where you move things. You'll also want to add a clear to your paint method. This will actually let your rectangles move. Update: I notice you already have something to redraw your stuff. Excellent. It's really fast though (200 fps!!) so you'll definitely want to look into double buffering.

Then later on we can talk about double buffering :) But you can Google for that or something.

Upvotes: 1

Related Questions