Tell me more ×
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 creating a Java 2D video game. I can load graphics just fine, but when it gets into double buffering I have issues. If i run this code, nothing is displayed except for the string("hello"); but the image is not displayed. now if i modified the code and exluded the Buffered Image the image would show just fine, so my question is how do i use a buffered image to draw sprites and also how do i do this a cross different classes?

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 {

    private BufferedImage backbuffer;
    private Graphics2D g2d;

    public GameView() {

        setBounds(0, 0, 500, 500);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

        Toolkit tk = Toolkit.getDefaultToolkit();
        Image img = tk.getImage(this.getClass().getResource("cage.png"));
        g2d.setColor(Color.red);
        //g2d.drawString("Hello",100,100);
        g2d.drawImage(img, 100, 100, this);
        repaint();
    }

    public static void main(String args[]) {
        new GameView();
    }

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

}
share|improve this question
1  
What problems are you having exactly? Describe them or give us a screenshot. What are you trying to do? What have you tried and why didn't it work? – Jonathan Hobbs Jun 20 '12 at 7:58
It's been 10 hours without clarification - voting to close as not a real question. – Byte56 Jun 20 '12 at 14:48
ok basically nothing is displayed. if i display it without the Buffered image using the g2d ibject it gets displayed just fine. even if i draw a rectangle using the double buffer it displays just fine. but if i try to draw an image nothing displays, and im sure the image is in the right directory otherwise the e.printstacktrace would complain about file not found. – kdavis8 Jun 20 '12 at 16:40
1  
Paint to buffer inside paint() function. Then paint buffer using Graphics parameter passed to paint() function. And don't use same variable name for back buffer's Graphics object and passed parameter. – Edin M. Jun 20 '12 at 17:15
@nekome you say "Paint to buffer inside paint() function." So what method do you suggest using to get that accomplished? e.g. g2d.draw(img,20,20,backbuffer);? also whats with the down votes? i altered the question to be more descriptive...up votes please..? – kdavis8 Jun 20 '12 at 17:56
show 2 more comments

2 Answers

I am not sure why but the problem was solved when i implemented runnable and drew to the g2d object within the run method.

Solved 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;

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

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

        Toolkit tk = Toolkit.getDefaultToolkit();
        img = tk.getImage(this.getClass().getResource("cage.png"));
        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);

    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        Thread t = Thread.currentThread();
        while (t == gameloop) {
            g2d.drawImage(img, 100, 100, this);
        }
    }

}
share|improve this answer

Look at your code, my guess is that your image is not displayed. I think you should be extending JPanel instead of JFrame. Add that panel into the frame. You will also need to override the preferred

share|improve this answer

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.