Flickering Image in Java?

Posted by Noah Cagle on Stack Overflow See other posts from Stack Overflow or by Noah Cagle
Published on 2013-10-29T03:38:00Z Indexed on 2013/10/29 3:53 UTC
Read the original article Hit count: 162

Filed under:
|
|
|

OK so I am coding a game right now to prepair for Ludum Dare SharkJam, and I am using a new method for programming, because the last method I had crashes my PC, so this one should work. Well it does work and all, better too, but the images that I put in it flicker. Here is the whole main class (where the images are drawn) package me.NoahCagle.watermaze;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

import me.NoahCagle.watermaze.entity.EntityShark;
import me.NoahCagle.watermaze.entity.Player;
import me.NoahCagle.watermaze.input.Keys;
import me.NoahCagle.watermaze.map.Map;

public class Game extends JFrame {
private static final long serialVersionUID = 1L;

Map map = new Map(0, 0);
Player player = new Player(50, 30);
static EntityShark shark = new EntityShark(400, 400);
public Image dbImage;

public Game() {
    setSize(800, 600);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
    setTitle("Water Maze");
    setResizable(false);
    setBackground(Color.blue);
    addKeyListener(new Keys());
}

public static void main(String[] args) {
    new Game();
    Thread s = new Thread(shark);
    s.start();
}

public void paint(Graphics g) {
    dbImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
    g.drawImage(dbImage, map.x, map.y, null);
    g.drawImage(player.player, player.x, player.y, this);
    g.drawImage(shark.shark, shark.x, shark.y, this);
    repaint();
}


}

What this code does for me is makes the Images work correctly, just flickering, alot. Can anyone help me with my issue? EDIT: I think it has something to do with where I call the repaint method in the paint method, so look there.

© Stack Overflow or respective owner

Related posts about java

Related posts about image