Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am trying to implement a draw map method that will draw the tiled image across the surface of the component. I'm having issue with this code. The double buffering does not seem to be working, because the sprite flickers like crazy;

my source code:

package myPackage;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

    public class GameView extends JFrame implements Runnable {

    public BufferedImage backbuffer;
    public Graphics2D g2d;
    public Image img;
    Thread gameloop;
    Scene scene;

    public GameView() {
        super("Game View");
        setSize(600, 600);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        backbuffer = new BufferedImage(getWidth(), getHeight(),
                BufferedImage.TYPE_INT_RGB);
        g2d = backbuffer.createGraphics();

        Toolkit tk = Toolkit.getDefaultToolkit();
        img = tk.getImage(this.getClass().getResource("cage.png"));
        scene = new Scene(g2d, this);

        gameloop = new Thread(this);
        gameloop.start();

    }

    public static void main(String args[]) {

        new GameView();
    }

    public void paint(Graphics g) {

        g.drawImage(backbuffer, 0, 0, this);
            repaint();

    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        Thread t = Thread.currentThread();
        while (t == gameloop) {

            scene.getScene("dirtmap");
            g2d.drawImage(img, 80, 80,this![enter image description here][1]);
        }
    }

    private void drawScene(String string) {
        // TODO Auto-generated method stub

        // g2d.setColor(Color.white);
        // g2d.fillRect(0, 0, getWidth(), getHeight());

        scene.getScene(string);

    }

}

package myPackage;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;

public class Scene {
    Graphics g2d;
    Component c;
    boolean loaded = false;

    public Scene(Graphics2D gr, Component co) {
        g2d = gr;
        c = co;
    }

    public void getScene(String mapName) {

        Toolkit tk = Toolkit.getDefaultToolkit();
        Image tile = tk.getImage(this.getClass().getResource("dirt.png"));

        // g2d.setColor(Color.red);
        for (int y = 0; y <= 18; y++) {
            for (int x = 0; x <= 18; x += 1) {
                g2d.drawImage(tile, x * 32, y * 32, c);
            }
        }

        loaded = true;
    }
}
share|improve this question
2  
Please remove all the code that is not related to the issue you're experiencing. Tell us what you've tried and why it didn't work. And try describing your problem better or including a screenshot. Edit your question with these changes. Thanks. –  Byte56 Jun 20 '12 at 20:56
    
try to copy and paste the code then compile it, just make sure you create a png file named dirt and anothre png file name cage. you will see the sprite flicker ontop of the dirt background.i thought double buffereing was supposed to fix that –  kdavis8 Jun 21 '12 at 4:15

1 Answer 1

You are interacting with a Swing UI (the component you pass to your scene's constructor) outside the EDT thread - this might be the reason you are having issues. You could try to run the code in the EDT:

@Override
public void run() {
    Thread t = Thread.currentThread();
    while (t == gameloop) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                scene.getScene("dirtmap");
                g2d.drawImage(img, 80, 80,this![enter image description here][1]);
            }
        });
    }
}
share|improve this answer
    
that code somehow stops the flickering but introduces a new problem, it freezes the UI thread making it impossible to exit the program,resize the frame and other such things –  kdavis8 Jun 22 '12 at 22:42
    
That would make sense because you keep sending new drawImage instructions to the UI in an infinite loop so it is fairly easy to imgaine that it can't deal with them all. To be honest your design looks weird. You might want to post a new question (possibly on stackoverflow as this is not really game related but more linked to how AWT / Swing work) to get more answers. –  assylias Jun 22 '12 at 22:59
    
good point. as i said I'm trying to write a 2d game based off eBooks but when you try and branch off into your own projects you get lost in the seas of meticulously error prone syntax. –  kdavis8 Jun 23 '12 at 6:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.