Repository
https://github.com/dmlloyd/openjdk
What Will I Learn?
- You will learn how to create simple game on netbeans.
- You will learn how to create two program on one package.
- you will learn how to use picture on netbeans program.
Requirements
- you must understand basic of java programming.
- you must know how to implement GUI on program.
- you must know how set size and location GUI.
Difficulty
- Intermediate
Tutorial Contents
okay, on this occasion I will make a simple game on netbeans, game that I mean is TicTacToe game. in the process of making this game we will create two programs that are in one package. okay we just go to the process.
- The first step you should do is create a new project
- then you create two classes in the same package.
for the first class you name it with * game.java * and below is the code you should write in this class. you can download it on this gist:full code link
okay, I'll explain a few important parts of the above program.
these are some of the classes we use in the process of making this game that is for the process of making a panel, frame, and grid.
import java.awt.Component;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.GridLayout;
public class Game extends JFrame {
this is the declaration of the class nameJPanel p = new JPanel();
this is the declaration of the object panel we created, and we initialize it with the letter P.XOButton buttons[] = new XOButton[9];
and this is for the creation of buttons that will display the X and O symbols.- and this is for the process of determining the frame size and the location of the frame.
setSize(400, 400);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this serves to set the process of opening and closing windows.p.setLayout(new GridLayout(3,3));
and this serves to set the layout and lines that will divide the layout into 3X3.- and this is the looping process that will determine the contents of the boxes.
for (int i=0; i<9; i++) {
buttons[i] = new XOButton();
Component add = p.add(buttons[i]);
- okay, I think for the first class is enough explanation, then we will create a second class.
- create a new class with the name Button and type the program below.
full code link - okay, I'll explain a bit about the second program.
- these are some of the classes we use in this program.
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
enable button functionimport javax.swing.ImageIcon;
this class we use because we use images in this program.import java.awt.event.ActionListener;
This serves to activate the action when we click.- this is the process of declaration of the class name that there is an action listener that will activate the button when the click process is done it will display the appropriate image.
public class XOButton extends JButton implements ActionListener{
ImageIcon x,o;
byte value =0;
- and this is the process I mean and will display the image according to the click made.
x = new ImageIcon(this.getClass().getResource("/GameMain/xx.png"));
o = new ImageIcon(this.getClass().getResource("/GameMain/oo.png"));
this.addActionListener(this);
- and this is the part that will set the action where if we double click will display the image X and if we do click once will display image O.
- okay, the program is done we make, then we will run it.
- right click on the first program and select run file
- it will look like this.
- if the image appears as above means the program that we make a success.
- thanks for following my tutorial.
Curriculum
- Java Tutorial : How To Create java JFrame/GUI To Play Music On NetBeans
- Java Tutorial: How To Select And Send All File In One Folder Using "for" Looping Method On NetBeans
- HOW TO CREATE ACTION TO AUTO SELECT FILE USING "if" CONDITIONAL ON JAVA
- Java Tutorial: How To Create a Program To Send And Receive Files On NetBeans
I thank you for your contribution. Here are my thoughts;
Tutorials should teach the user something unique, shouldn't show ubiquitous content-creation and replicate concepts which can be found over internet with great ease. Therefore, in the voting phase, your tutorial might not be considered.
There are lots of punctuation and structure mistakes in your paragraph as well as grammar mistakes. I suggest you to double check your post on spelling, grammar, structure, and punctuation. Having these mistakes makes your post hard to read and understand. A tutorial must be formal and easily understood.
Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.
Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]