Basics
In this part of the Java 2D games tutorial, we will write about some basics needed to create games. We will create a skeleton of a game. Paint a donut and display a picture.About
This is Java 2D games tutorial. It is aimed at beginners. This tutorial will teach you basics of programming 2D games in Java programming language and Swing GUI toolkit. All images used in this tutorial can be downloaded here.Skeleton
We will show the skeleton of each of our Java 2D games.Board.java
package skeleton;The Board is a panel, where the game takes place.
import javax.swing.JPanel;
public class Board extends JPanel {
public Board() {
}
}
Skeleton.java
package skeleton;This is the entry point of the game. Here we have the main method.
import javax.swing.JFrame;
public class Skeleton extends JFrame {
public Skeleton() {
add(new Board());
setTitle("Skeleton");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 280);
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);
}
public static void main(String[] args) {
new Skeleton();
}
}
add(new Board());Here we put the Board to the center of the
JFrame
component. setDefaultCloseOperation(EXIT_ON_CLOSE);This will close the application when we click on the close button. It is not the default behaviour.
setSize(300, 280);This line sets the size for our window.
setLocationRelativeTo(null);We center the window.
setVisible(true);Show the window on the screen.
setResizable(false);Make the window unresizable.
Figure: Skeleton
Donut
The objects on the Board are either images or are drawn with the painting tools provided by the Java 2D API. In the next example, we draw a Donut. We use the painting API.Board.java
package donut;The painting is done inside the
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
public class Board extends JPanel{
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
RenderingHints rh =
new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHints(rh);
Dimension size = getSize();
double w = size.getWidth();
double h = size.getHeight();
Ellipse2D e = new Ellipse2D.Double(0, 0, 80, 130);
g2.setStroke(new BasicStroke(1));
g2.setColor(Color.gray);
for (double deg = 0; deg < 360; deg += 5) {
AffineTransform at =
AffineTransform.getTranslateInstance(w / 2, h / 2);
at.rotate(Math.toRadians(deg));
g2.draw(at.createTransformedShape(e));
}
}
}
paint()
method. Graphics2D g2 = (Graphics2D) g;The
Graphics2D
object provides a sophisticated control over painting. RenderingHints rh =The rendering hints are used to make the drawing smooth.
new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHints(rh);
Dimension size = getSize();We get the height and the width of the window.
double w = size.getWidth();
double h = size.getHeight();
Ellipse2D e = new Ellipse2D.Double(0, 0, 80, 130);Here we create the ellipse.
g2.setStroke(new BasicStroke(1));
g2.setColor(Color.gray);
for (double deg = 0; deg < 360; deg += 5) {Here the ellispse is rotated 72 times to create a "donut".
AffineTransform at =
AffineTransform.getTranslateInstance(w / 2, h / 2);
at.rotate(Math.toRadians(deg));
g2.draw(at.createTransformedShape(e));
}
Donut.java
package donut;This is the main class.
import javax.swing.JFrame;
public class Donut extends JFrame {
public Donut() {
add(new Board());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(360, 310);
setLocationRelativeTo(null);
setTitle("Donut");
setVisible(true);
}
public static void main(String[] args) {
new Donut();
}
}
Figure: Donut
Image
When we create computer games, we often work with images. In the next example we load an image and display it on the Board.Board.java
package bardejov;We display an image of a town on the Board. The image is drawn inside the
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Board extends JPanel {
Image bardejov;
public Board() {
ImageIcon ii = new ImageIcon(this.getClass().getResource("bardejov.jpg"));
bardejov = ii.getImage();
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(bardejov, 10, 10, null);
}
}
paint()
method. ImageIcon ii = new ImageIcon(this.getClass().getResource("bardejov.jpg"));We create an
ImageIcon
. bardejov = ii.getImage();We get an
Image
out of the ImageIcon
. g2d.drawImage(bardejov, 10, 10, null);We draw the image on the window.
Image.java
package bardejov;This is the main class of the example.
import javax.swing.JFrame;
public class Image extends JFrame {
public Image() {
add(new Board());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(280, 240);
setLocationRelativeTo(null);
setTitle("Bardejov");
setVisible(true);
}
public static void main(String[] args) {
new Image();
}
}
Figure: Image
In this chapter, we have covered some basics of Java game programming.
Maybe have an Anchor to the next Chapter?
ReplyDelete