java swing GUI for game | java game development-fundamentals

Hello, Guys, I'm gonna begin the java game development series. First of all,
I will create posts on basic topics such as double buffering,
transformation, etc. After completing it we'll be able to move forward to
make a game.

You can navigate the next and previous posts thru the button at the bottom
of the web page.

Or you can see the contents of this series here.

Clearing the basics will take a protracted time thus stick with me. I'll
keep updating my blog with regular game programming posts.

The first necessity to a game is a GUI window thus this post is on the way
to build GUI Window in java by dealing with the swing library.

Making a GUI window with swing's JFrame

Creating a GUI window is the very first thing that you just know. If you
don't know just continue otherwise go to the next post.

To make a GUI in java we are allowed to leverage java's SWING library.

What is Swing in java?

Swing is one in each of the libraries that comes with java JDK and could be
a Graphical program (GUI) toolkit that features a chic set of widgets.

It is a portion of Java Foundation Classes(JFC), that is an API for Java programs that offer a GUI. Swing includes packages that permit you to create
a complicated set of GUI elements for your Java applications and it's
platform-independent.

The swing library is made on the top of AWT (Abstract window toolkit), an
older, platform-dependent GUI toolkit

You can make use of Java GUI components like button, textbox, etc. from the
swing library and don't have to make every component from scratch.

Most of the elements in swing begin with 'J' like JText, JTextArea, JPanel,
JButton etc.

How to construct a GUI window with swing library

There's a class referred to as JFrame and is employed to make a GUI window in the swing. we can extend from this class or create an object of this class to generate a gui.

// don't forget to import JFrame
import javax.swing.JFrame; 

public class Main extends JFrame {
  public static void main(String[] args) {
    new Main();
  }
}

To make the window visible invoke the setVisible() within the constructor
and pass in a true as a parameter

// do not forget to import JFrame
import javax.swing.JFrame; 

private Main() {
    setVisible(true);
}

public class Main extends JFrame {
    public static void main(String[] args) {
        new Main();
    }
}

Now run the program, you will see a tiny window at the screen's top-left
corner.

java game programming tutorials

Still, It has issues that the program doesn't stop itself after closing the window. To solve that problem
invoke the setDefaultCloseOperation(EXIT_ON_CLOSE) method.

import javax.swing.JFrame; 



public class Main extends JFrame {

public Main() {
    // call it to stop the execution
    //after closing the window.
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    setTitle("java swing");

    // to set the size call setSize()
    // and to show gui invoke setVisible(true)
    setSize(800, 600);
    setVisible(true);
}


    public static void main(String[] args) {
        new Main();
    }
}
Share this Post

Leave a Reply

Your email address will not be published. Required fields are marked *