Setting up the JFrame

vanZeben · 566K views · 2012 · 33:16

Open on YouTube

Game programming & enginesIndie game developmentPython, Ruby & scripting languagestutorial

Why Stack Overflow recommends it

Stack Overflow users link it as the tutorial many beginners follow when starting a 2D Java game, and they cite it when asking about errors in the JFrame, Canvas, BufferStrategy and tick/frame loop code it walks through.

Recommended by 2 people across 2 different months, 2013 to 2020.

What it is

The first episode of vanZeben's Java 2D game series, a 33-minute tutorial on building a game in pure Java with no engine. It sets up a JFrame and Canvas, a render loop with BufferStrategy, and a fixed-tick game loop, based on Notch's Ludum Dare entry Minicraft.

What people said

Recommends yanman1234 · 2013-03 · Stack Overflow · on “Add resource folder to export”

I think this may help explain what I'm trying to do link [this video] The applicable part is from 2:30 to 3:25. I of course tried following what he said but it failed to work.

Recommends Kyranstar · 2014-04 · Stack Overflow · on “Java 2d RPG game with multiplayer support”

This video [this video] is how my good friend learned java development. Obviously you're not going to find any tutorial that follow your exact needs, but this one is semi-close. I believe it goes into networking later in the series. This video [LINK] is also very good, but it is farther away from your specifications. Another, better thing you can do is follow simpler tutorials [LINK] until you know what you are doing, then change the source around to fit your specifications. Generally if you do not know any java at all, starting with a game like this is not a very good idea. But if you are very motivated, it can be done. By the way, this broad type of question is not very well suited for a Q & A

Cites Kaperly · 2013-04 · Stack Overflow · on “Errors displaying buffered image”

I have recently been following a tutorial on how to make a 2D game in java. I know all the concepts and I am using the video as a guide line so that I can get everything setup and then start pumping all of my ideas and content into the skeleton. I was following this( [this video] ) tutorial when I ran into a problem. About 28 minutes into the video I type this code: public void render() { BufferStrategy bs = getBufferStrategy(); if(bs == null) { createBufferStrategy(3); return; } Graphics g = bs.getDrawGraphics(); g.setColor(Color.BLACK); g.fillRect(0, 0, getWidth(), getHeight()); g.dispose(); bs.show(); } This was all fine until I tried to run it. He runs it in the video and it works just fine,

Cites KenigOri · 2013-05 · Stack Overflow · on “Error in JFrame”

I saw this video [this video] [ 10:00 ] to start create a 2d game in Java and I tried using the code he used in the video and I get error and I don't know how to fix him. package me.Kenig.game2d; import java.awt.Canvas; import java.awt.Dimension; import javax.swing.JFrame; public class Game extends Canvas implements Runnable { private static final long serialVersionUID = 1L; public static final int WIDTH = 160; public static final int HEIGHT = WIDTH/12*9; public static final int SCALE = 3; public static final String NAME = "Game"; private JFrame frame; // error public Game(){ setMinimumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE)); setMaximumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE)); setPre

Cites Austin Ellis · 2016-11 · Stack Overflow · on “Why is my java code not limiting these numbers?”

Currently having an issue with the amount of ticks that are being produced by my code. I am currently following along a video game guide and the amount of ticks that are used to update the logic is exponentially higher than the frames when they should both be resting upon 60. This is the YouTube video [this video] any advice would be greatly appreciated. Thank you in advanced! package game; import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Dimension; import javax.swing.JFrame; public class game extends Canvas implements Runnable { private static final long serialVersionUID = 1L; public static final int WIDTH = 160; public static final int HEIGHT = WIDTH / 12 * 9; public static

Cites mindoverflow · 2018-07 · Stack Overflow · on “2D Game graphics in native Java: Canvas vs. JComponent”

Apparently there are multiple ways of drawing game graphics in Java. All of them seem reasonable, yet I can't find out which way is the "best" way in terms of speed/efficiency: Using the Canvas class (I): Game is an instance of Canvas or contains one Manipulate an int array representing the colors of each pixel This [this video] is an example for this method. Using the canvas class (II): Game is an instance of Canvas or contains one Graphics/G2D object is used to draw onto the canvas Rest is similar to the first approach Using a JLabel's paintComponent() method (How I'd do it): Game is an instance of JLabel (or any other suitable class) or contans one repaint() is called every frame Graphics/G2D

Cites Gem Seeker · 2020-04 · Stack Overflow · on “Smoothing out Android Game Loop”

This might be a little too late but, here is how I implemented my game loop. With this, it can help you track your game's ticks(or updates) and frames per second. long lastTime = System.nanoTime(); double nsPerTick = 1_000_000_000D/60D; int frames = 0; int ticks = 0; long lastTimer = System.currentTimeMillis(); double delta = 0; while (mRunning) { Canvas canvas = null; long now = System.nanoTime(); delta += (now-lastTime)/nsPerTick; lastTime = now; boolean shouldRender = false; while (delta >= 1) { ticks++; gameView.update(); delta -= 1; shouldRender = true; } if (shouldRender) { frames++; canvas = surfaceHolder.lockCanvas(); gameView.render(canvas); surfaceHolder.unlockCanvasAndPost(canvas)