Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
3.12/5 (3 votes)
See more:
So i have a basic game loop. My source is KillerGameProgramming in Java and foriegnguymike on YouTutbe. So I understand what's the most proper and correct way to add Double buffer graphics in here. (Assuming I have A Main class with a JFrame)

Please consider the fact that I'm new to advanced programming(Meaning I can make a basic game without game loop, but need to use game loop). Also refer to chapters or things from the book Killer Game Programming in Java. I'm having trouble with the book.(Also excuse incorrect program spelling I had to quickly type it since my code is on my laptop):

Java
    public class GamePanel extends JPanel implements Runnable {
    
    private Boolean running = false;

    private Thread thread;

     public GamePanel() {
     setFocusable(true);
     requestFocus();
     start();

   }
        public void init() {}

     public void start() {}
     public void stop() {}

     public void run(){
     running = true;
     while(running) {
     render();
     paint();
     
      }
   }
          public void render() {} //////// WHAT WOULD I DO TO START WITH GRAPHICS? /////


   }

     public void paint() {
     repaint(); ///////// WHAT SHOULD BE CALLED HERE? ////////
}
Posted

1 solution

It just so happens that I'm also making my first game in Java so I'll try to help you out as much as I can. I haven't read that book yet though. Most of my knowledge is from playing around and reading on forums.

I don't understand your question though. Do you need help with the game loop or with the rendering? I'm guessing it's about the rendering looking at your code.

Since you're using a JFrame to draw your graphics on (as far as I know this is the best place to do it. I'm using the same.) I recommend overriding the "protected void paintComponent(Graphics g)" method. Place all your drawing code in there and in your game loop simply call "repaint();"

In the "paintComponent" method you can start drawing your graphics. If you're having trouble with this I sagest that you play around with some demo projects to get the hang of this.

Here is some code to import a png and draw it on the JPanel:

Java
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Driver extends JPanel
{
	JFrame frame;
	Image img;
	BufferedImage backBuffer;
	
	public static void main(String[] args)
	{
		Driver drive = new Driver();
		drive.initialize();
	}
	
	private void initialize()
	{
		frame = new JFrame();
		frame.setTitle("Main Menu");
		frame.setSize(816, 638);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocation(500, 0);
		frame.setLayout(new BorderLayout());
		frame.add(this);
		 	 	
		try
		{
			img = ImageIO.read(new File("pick.png"));
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	 	 	
		frame.setVisible(true);
		
		backBuffer = new BufferedImage( this.getSize().width, this.getSize().height, BufferedImage.TYPE_INT_RGB);
	}
	
	@Override
	protected void paintComponent(Graphics g)
	{
		Graphics bbg = backBuffer.getGraphics();
		
		int displayX = 10;		// Create the image 10px from Left
		int displayY = 10;		// Create the image 10px from Top
		int displayWidth = 20;	// Stretch/shrink the image to 20px in width (You can leave this out in the drawImage. I use it for zooming in my game)
		int displayHeight = 20;	// Stretch/shrink the image to 20px in height (You can leave this out in the drawImage. I use it for zooming in my game)
		
		bbg.drawImage(img, displayX, displayY, displayWidth, displayHeight, this);
		
		g.drawImage(backBuffer, 0, 0, this); 
	}
}


I hope this helps you.
Best of luck with your game. I hope I'll be able to play it some day :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900