Game Programming - Episode 4 - Window

The Cherno · Intermediate ·🧒 Coding for Kids ·13y ago

Key Takeaways

The video demonstrates game programming in Java, focusing on creating a window using a JFrame object and extending the Canvas class to process input events and draw on the screen. It covers topics such as object-oriented programming, game development, and window management, utilizing tools like Java, JFrame, Canvas, and Eclipse.

Full Transcript

hey what's up guys My Name Is AO welcome to episode four of game programming okay so last time we took a look at uh actually creating like a game Loop um with our our running variable and a while loop in our run uh method um and this time you know today on today's episode we're going to take a look at actually making a window so what I want to do is actually get something up and running so that you guys can um canest this code and uh see if it works and uh check if you've got errors obviously you know we don't because there's like 30 lines of code here and not much can actually go go wrong but um nevertheless we will we will make that window so first thing I'm want to do is make um a variable uh rather an object we're going to make a jframe object so private jframe jframe and what a jframe is and we'll have to import that a jframe is basically like a window okay um it's like frame you could use a a normal frame that's absolutely fine but um we're going to use a J frame just because well I'd recommend you use a J frame um now uh one other thing we actually have to do is we have to make sure that our game class extends canvas now what um let's import canvas as well what canvas is is essentially what a canvas is is it's almost like think of it as a canvas like honestly you're painting and you've got this canvas it's it's like a it it basically basically it represents a blank rectangular area of the screen onto which you know our application can draw or um or also to process input events so when we press something on the keyboard um as long as we we've got a canvas you know we can process those events we can actually draw things to that canvas it's like a blank rectangle on the screen that we can actually manipulate in any way we want okay now by extending canvas I'm saying that game is a subass of of canvas in other words game almost inherits everything that canvas has um again there are obviously complications to that but um Loosely you know what we're saying is that game is now a subass of canvas so with that in mind we're going to actually create a Constructor for our game class now this is important um for those of you who don't understand uh a Constructor we're going to do by simply typing public game and then um our curly bracket so you can see that there's no return type right um I didn't actually explain this last time because I thought it was fairly simple but you'll notice that we're actually typing you know the visibility and then void right all of this is void now what void means is that the return type is void we're returning void we're returning nothing what we're doing is we're just executing this code and then that's it the method is done we're not actually returning anything we could have a method where we return something so in other words when we call this method it actually returns data to us this return returns void this returns nothing it just completes what's in the curly brackets and that's it now that is all you need to know for now if you're unfamiliar with Java that's it we're just going to be using void methods until things get more complicated so for now all you need to know is that void returns nothing if if a method is void it just executes everything that's inside the method and that's it it's done now here we don't have a return type because this is a Constructor so what we've got instead is just public and then the name of our class which game followed by the two curly brackets uh what do I call these two round brackets two parentheses I don't know two um normal brackets I guess and then and then we've got our um curly brackets so a Constructor basically runs where whenever a new instance of this object is created so whenever we type something along the lines of you know for example again I'm just typing code to prove to you guys I'm just I'm not going to I'm going to erase this in a second so don't type this um if I do something like new game um essentially what's going to happen is apart from creating a new instance of this game it's actually going to execute everything that's in this game Constructor um so in other words by going you know thread equals new thread same thing if thread has a Constructor which of course it does cuz we're taking parameters um then when we call that it will actually execute all the code that is in the Constructor now this is very useful I'm not going to go into the usefulness of Constructors of course they're useful but one of the reasons they're useful is because this code will only be run once that is when you create the object that's very important so whatever code we create here will only be ex executed once and that is when we create this object now game will only be able to will only be created once as it should be because we only want to create one game class not two or multiple just one so when we first start our game this is what's going to happen now a few things here have to happen first thing I want to do though is actually Define a sze for our game so we've got three variables here that deal with the resolution now I'm going to actually put them to use and apply these variables to a method or to a um a function to actually you know get it to set our window to that size so all I'm going to do is type Dimension size all right so Dimension is a class it essentially holds a dimension or a resolution and I'm going to set that equal I've just named it size cuz it's going to be the size of our window size of our frame equals new new dimension and then for the parameters we're just going to put width time scale comma that's the first parameter and then second parameter is height time scale so what we've got here um is let's first of all import Dimension now we've got it in our um import list Java now knows what class we're referring to um so width is going to be the width times the scale cuz remember we're making um everything three times bigger than it is here um you'll you'll see the effect of this in the future I find it really hard to sort of explain to you guys exactly what it means so in the future you'll actually see it in action I can actually provide you with examples as to what's going on with the scale and everything but the window is going to be 900 pixels wide right with time scale um and the height of the frame is going to be that right but we've basically we've just created a dimension um object called size with essentially with these parameters so now we actually have to apply it and the way we do that is we simp simply type set preferred size and then the uh parameter is size so we're setting the preferred size of our window to this okay that's what we're doing now um for all you guys who are unfamiliar with this you might be like well set set preferred size that's referring to a method right where is this method well um essentially where this method is is it's in canvis so you'll notice that um the oh actually if you just if you just hold it um if you just hold your mouse over it'll tell you but essentially because we're extending canvas that means that we've got direct access to its methods that are either protected or public um so in other words this method is located in canvas or actually you'll find that it's located in component so component canvas extends component someone it's kind kind of complicated all you need to know is that if we are extending canvas we can call the set preferred size method and it will actually set the preferred size of our canvas to to be um the size of this all right so that's um that's sort of like the first part of this uh episode I'm going to wrap this up today though but tomorrow we'll um we'll definitely draw our our window and you know what scrap that I feel like I haven't done anything like we've literally no we're going to keep going so what I want to do now is actually make a main method so public static void main um and then string and I'll explain this in a minute for those of you who are unfamiliar for those of you who know exactly what what this means just chill out and you know take a break I mean just do something else while I explain this cuz some of you guys are clueless so public static void main uh and then string and then augs um this is essentially Java's main method so all you need to know about this is when we when we run this program it's going to find this method and it's going to execute it here so in other words this is where our program starts this is the entry point this is what happens when we run our program everything that is in here this is how our program starts this is how our game starts whatever this is the start this is the beginning this is the Genesis this is the Inception whatever so um first thing I want to do is actually create a um an object out of our current game now you'll notice that this method is static that means that in in other words what that essentially means is that it has no relation with the rest of our um with the rest of our class so we can't simply unless we also make um variables static we can't directly um access them so the first thing we need to do is actually create an object out of this game class so I'm going to do that as I would with any other object simply type game game equals new game so now we've got an object of game with with its own instance right this thing that we can actually use um for the remainder of you know our programming in this method um what I'm going to do now is actually also for jframe right I'm actually going to set frame equal to new jframe so when we run this Constructor um it's going to it's going to give a value it's going to actually set frame equal to new jframe um which creates a new instance of our jframe so what we can do now is because you know I guess our frame object is outside of our static method we can refer to it through game right CU this is remember this is game this is our game object game dotframe do whatever you know we want to do so first thing we want to do is actually set resizable to false so we don't want our window to be able to be resized sized that causes tons of graphical errors let me tell you and make sure that this is the very first thing that gets applied to frame okay so your first thing for frame should be set resizable false if you plan on doing that and we do um next thing we'll do is just probably create a title so uh set title we'll just call it rain so you just put a string in here right I've just typed rain in quotation marks that will be the title of our window so in other words this window is titled you know Java blah blah blah blah um and uh our window will be called rain cuz that's the name of our game um the next thing I'll do is actually um I'll add right our game component so I'll add a component to the frame and it will be game now the reason this works first of all is this add method what it does is you know essentially it just adds a component into our frame so it fills the window with something now what we're adding into it is an instance of this game object so an instance of this class and the reason we can actually add this class to um to our frame is because our class extends canvas so it's a subtype of canvas it is a subass of canvas so in other words it is a component and we're adding a component to our window uh next next we'll actually pack the frame uh what pack does is it sets the size of the frame according to the components so we've set the size of our component to be 900 pixels by you know whatever that is time 3 so we've set a particular size a particular resolution to our component to our game pack is going to basically size up this Frame to be the same size as that component um gameframe do set default close operation to jframe do exit on close what this is going to do is when we actually hit the um the close button in the top right corner or you know if you're on Mac in the top left corner um if you hit that button it's going to jframe dox on close so in other words it's going to terminate the application when you hit that button it's going to completely close it's going to close the process it's going to end the application when you uh hit the x button when you hit the exit button so that's really important because what can happen is if you simply close um if you don't have this code and you simply close your program by hitting the x button what's going to happen is it's going to keep on running it's just not going to display the window you've closed the window but you haven't actually closed the program so what you want to do is actually close the program you want to delete that thread you want to remove that uh sorry not thread you want to delete that process you want to actually remove that process that is currently um you know that's currently running because uh we want to close our application when we hit the x button um now the next thing we need to do is set a location so we want to set the location relative 2 and we can actually just put null as the parameter and what that's going to do is Center our window into the screen so instead of being in the top left corner our our frame our window will actually be right in the center of the screen um and finally gameframe do said visible to True very important make sure you don't forget this last line of code because what this is going to do is actually show our frame it's going to show our window we want to show our window we don't want it to to remain hidden so make sure it's actually showing by setting visible to True last thing we actually have to do is start our game so we've we've created a window we've done everything but remember in order for our game to function we actually need to call this start method so we can do that simply by typing game so we're referring to again the game class and uh we'll type game.art what what that's going to do is start our game now one more thing before we actually launch this um it is a serializable class because it extends canvas um so make sure you just add the default serial version ID okay it um you don't need to understand a whole lot about it just add it you know you the warning will go away it's fine it's optional it's up to you but um it is a Convention of java so I suggest you follow it um yeah so in other words let's just displ something in our while running Loop right now so that we can test if this works so we've go system.out.print line um what what we can do in here is actually print a line of text or numbers or anything really um and it will actually print to the console so let's just print something like running so what what this will print hope y over and over again until because it's in a while running Loop until we actually terminate the application um it's going to print running over and over again uh when we hit the debug button so there's a few ways of actually running the programming Eclipse there's the debug button and then there's the Run button now um use the debug button there's honestly not much difference between the two but debug actually lets you um I guess make changes and save your code live and if it's in a loop you know if it's constantly checking that code over and over again like it is in this case it will update so let's just hit the uh the debug button we'll see what happens all right so there we go you can see that we get a nice window that is the resolution of our game it's 169 aspect ratio it's got the title of rain and you can see over here in the console view where um we're constantly as you can see it's constantly displaying um this text called Running um so so yeah there's our window next episode I don't know what we'll do next episode next episode we're basically going to just actually fill it with Graphics so that we can um you know make the background black and actually start uh start painting I guess stuff onto our canvas so we'll start rendering stuff onto the screen so um yeah I hope you enjoyed this this tutorial if you did please um please hit the like button and uh I'll see you guys see you guys next time I'll see you guys tomorrow bye [Music]

Original Description

Want me to teach you Java and Game Programming privately? ► http://www.thecherno.com/tutoring Welcome to Game Programming, a series in which we take an in depth look at how to make a game from scratch, in Java. BEST laptop for programming! ► http://geni.us/pakTES My FAVOURITE keyboard for programming! ► http://geni.us/zNhB FAVOURITE monitors for programming! ► http://geni.us/Ig6KBq ---------------------------------------------------------------------------------------------------------------- Website: http://www.thecherno.com Twitter: http://www.twitter.com/thecherno Facebook: http://www.facebook.com/thecherno ---------------------------------------------------------------------------------------------------------------- Outro music is by Approaching Nirvana: http://www.youtube.com/approachingnirvana
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from The Cherno · The Cherno · 45 of 60

1 3D Game Programming - Episode 1 - Window
3D Game Programming - Episode 1 - Window
The Cherno
2 3D Game Programming - Episode 2 - Game Loop
3D Game Programming - Episode 2 - Game Loop
The Cherno
3 3D Game Programming - Episode 3 - Arrays
3D Game Programming - Episode 3 - Arrays
The Cherno
4 3D Game Programming - Episode 4 - Drawing Pixels!
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
5 3D Game Programming - Episode 4.5 - How Rendering Works
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
6 3D Game Programming - Episode 5 - Playing with Pixels!
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
7 3D Game Programming - Episode 6 - Performance Boosting
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
8 3D Game Programming - Episode 7 - FPS Counter
3D Game Programming - Episode 7 - FPS Counter
The Cherno
9 3D Game Programming - Episode 8 - Alpha Support and More
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
10 3D Game Programming - Episode 9 - Beginning 3D
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
11 3D Game Programming - Episode 10 - Floors and Animation
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
12 3D Game Programming - Episode 11 - Rotation
3D Game Programming - Episode 11 - Rotation
The Cherno
13 3D Game Programming - Episode 12 - User Input
3D Game Programming - Episode 12 - User Input
The Cherno
14 3D Game Programming - Episode 13 - Render Distance Limiter!
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
15 3D Game Programming - Episode 14 - Basic Mouse Movement
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
16 3D Game Programming - Episode 15 - Textures + More!
3D Game Programming - Episode 15 - Textures + More!
The Cherno
17 3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
18 3D Game Programming - Episode 16.5 - Exporting Runnable Jars
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
19 3D Game Programming - Episode 17 - Small Adjustments + Birthday!
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
20 3D Game Programming - Episode 17.5 - Creating an Applet
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
21 3D Game Programming - Episode 18 - The Beginning of Walls
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
22 3D Game Programming - Episode 18.1 - A Few More Things
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
23 Episode 18.5 - Creating an EXE File in Java
Episode 18.5 - Creating an EXE File in Java
The Cherno
24 3D Game Programming - Episode 19 - Rendering Walls
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
25 3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
26 3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
27 3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
28 3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
29 3D Game Programming - Episode 24 - Making Our Launcher Work
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
30 3D Game Programming - Episode 25 - Writing and Reading Files
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
31 3D Game Programming - Episode 26 - Custom Resolutions
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
32 3D Game Programming - Episode 27 - Decorating the Launcher
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
33 3D Game Programming - Episode 28 - Continuing our Custom Launcher!
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
34 3D Game Programming - Episode 29 - Launching The Game
3D Game Programming - Episode 29 - Launching The Game
The Cherno
35 3D Game Programming - Episode 30 - Colour Processing In-Depth
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
36 3D Game Programming - Episode 31 - Sprites!
3D Game Programming - Episode 31 - Sprites!
The Cherno
37 3D Game Programming - Episode 32 - Sprite Mapping
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
38 3D Game Programming - Episode 33 - High Resolution Rendering
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
39 3D Game Programming - Episode 34 - Entities
3D Game Programming - Episode 34 - Entities
The Cherno
40 Genesis - My Game for Ludum Dare 24
Genesis - My Game for Ludum Dare 24
The Cherno
41 Vlog + Ludum Dare Results
Vlog + Ludum Dare Results
The Cherno
42 Game Programming - Episode 1 - Resolution
Game Programming - Episode 1 - Resolution
The Cherno
43 Game Programming - Episode 2 - Threads
Game Programming - Episode 2 - Threads
The Cherno
44 Game Programming - Episode 3 - Game Loop
Game Programming - Episode 3 - Game Loop
The Cherno
Game Programming - Episode 4 - Window
Game Programming - Episode 4 - Window
The Cherno
46 Episode 5 - Buffer Strategy
Episode 5 - Buffer Strategy
The Cherno
47 Game Programming - Episode 6 - Graphics Initialized
Game Programming - Episode 6 - Graphics Initialized
The Cherno
48 Game Programming - Episode 7 - Buffered Image and Rasters
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
49 Game Programming - Episode 8 - The Screen Class
Game Programming - Episode 8 - The Screen Class
The Cherno
50 Game Programming - Episode 9 - Rendering Pixels
Game Programming - Episode 9 - Rendering Pixels
The Cherno
51 Game Programming - Episode 10 - Clearing the Screen
Game Programming - Episode 10 - Clearing the Screen
The Cherno
52 Game Programming - Episode 11 - "Out of Bounds, Baby!"
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
53 Game Programming - Episode 12 - Negative Bounds
Game Programming - Episode 12 - Negative Bounds
The Cherno
54 Game Programming - Episode 13 - Timer
Game Programming - Episode 13 - Timer
The Cherno
55 Game Programming - Episode 14 - FPS Counter
Game Programming - Episode 14 - FPS Counter
The Cherno
56 Episode 15 - Tiles
Episode 15 - Tiles
The Cherno
57 Game Programming - Episode 16 - The Map
Game Programming - Episode 16 - The Map
The Cherno
58 The Walls 2 - Minecraft PvP Survival Map
The Walls 2 - Minecraft PvP Survival Map
The Cherno
59 Game Programming - Episode 17 - Key Input
Game Programming - Episode 17 - Key Input
The Cherno
60 Game Programming - Episode 18 - Controlling The Map
Game Programming - Episode 18 - Controlling The Map
The Cherno

This video teaches game programming in Java, covering the creation of a window using a JFrame object and extending the Canvas class. It provides a comprehensive understanding of object-oriented programming, game development, and window management, making it essential for intermediate learners. By following the steps outlined in the video, viewers can create their own game window and start building their game from scratch.

Key Takeaways
  1. Make a variable (object) to represent a window (JFrame)
  2. Extend Canvas to process input events and draw on the screen
  3. Create a Constructor for the game class to initialize the object
  4. Define a size for the game by creating a Dimension object
  5. Set the preferred size of the window using the setPreferredSize method
  6. Create a main method in the game class
  7. Create an object of the game class
  8. Set the JFrame object resizable to false
  9. Set the title of the window to 'rain'
  10. Add the game component to the frame
💡 The video highlights the importance of understanding object-oriented programming and game development concepts to create a game window in Java. It also demonstrates how to use the JFrame and Canvas classes to create a window and process input events.

Related AI Lessons

Up next
Man Builds a Backyard Tiny House and Turns It into a Home Office | Start to Finish by @Elseweyr
AKLA GELEN
Watch →