In this tutorial I will show you how to create a screen for your program. In this tutorial I will show you the basic empty screen. Later tutorials will also be about how to fill them with certain things.
This is the code I will start with.
The first thing you need to do is make this file extend Canvas. To do that you will need this code.
public class Tutorial extends Canvas
When you add this code to your file you will get an error under Canvas. To fix that you will have to import the Canvas file. There are 3 ways of importing a file.
1. Hover your mouse over the error and click Import.
2. Press Ctrl, Shift, O.
3. Type it yourself.
The code you will get or have to write has to be below package and above the public class line. It will look like this.
When you do this you will get a yellow line under the class name. This is not an error, but a problem. You can run the application with this, but it shouldn’t be in there.
To fix this problem you have to hover your mouse over the class name and click the Add default serial version ID. This will add these lines of code to your file.
The first 3 lines are comments. There are 2 types of comments. This is the first one. The second one can be used by adding // to your code. Anything behind that or inside of the comments above will not be read as code and will simply be skipped. This is useful when you have some test code and you want to remove it from a release, but not lose the code. It can also be used to explain what certain things in your code do.
Right now you can simply remove these comments and the file should now look like this.
Now when you run it nothing will happen.
To make something happen you will have to add this line of code between the start of the class and the start of the main method.
static JFrame frame = newJFrame();
You will get an error under JFrame which can be fixed by importing the file like you did earlier with Canvas. It will add this line of code.
JFrame is the file you will be using to create the screen. frame is the variable name you give it and the = new JFrame() part means that you use that file when you refer to frame. The word static simply means you can use it inside of a static method like main.
Next up you will have to add 2 lines of code to the main method to make the screen show up. The first one looks like this.
setSize is the method you use to set the size of your screen. The first parameter in here is the width and the second one the height of your screen. frame is simply the file where this method is used from. This line of code will create a screen of 640 width and 480 height.
If you run it now you will still not see anything. That is because we haven’t told frame to show the window. To make it show up you will have to add this line of code.
Once again setVisible is the method we will use which is inside of the frame file. The parameter in here can be true or false. If it’s true it will show up and if it is false it will not.
The file should look like this.
Now when you run the application by hitting the green arrow on the top in Eclipse you will get a 640 by 480 white box.
In the next tutorial I will show you how to change some of the things in that screen.