I want to create a test project to improve my Graphics in Java. What my goal is, is to do a simple Tic Tac Toe game wich the player can input using Numpad where they want their next move.
What I am doing now is I am drawing the Background to the frame / layout for Tic Tac Toe. When I try to draw the X or the O on top of the background/layout it won't show up. I also tried to paint the X and O next to the background but it just won't work.
Should I update it and if so how do I update it or is it just not possible. I just can't figure it out.
I have 4 separate classes for this.
The first class is Frame. Second Background. Third InputHandler. Fourth Turn.
Here is the code:
Frame
package Game;
import java.awt.GridLayout;
import javax.swing.JFrame;
public class Frame extends JFrame {
private Background bg;
private Turn trn;
private InputHandler in;
public Frame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setResizable(false);
setTitle("Hi");
init();
}
public void init() {
setLocationRelativeTo(null);
setLayout(new GridLayout(1, 1, 0, 0));
bg = new Background();
add(bg);
trn = new Turn();
add(trn);
setVisible(true);
in = new InputHandler();
addKeyListener(in);
}
public static void main(String[] args) {
new Frame();
}
}
Background
package Game;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class Background extends JPanel {
private BufferedImage image;
public Background() {
try {
image = ImageIO.read(getClass().getResourceAsStream("/tiles/tile_TicTacToeGrid.png"));
} catch (Exception e) {
System.out.println("File not found.\n\n" + e);
}
repaint();
}
public void paint(Graphics g) {
// Drawing to screen
g.drawImage(image, 0, 0, null);
}
}
InputHandler
package Game;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
public class InputHandler implements KeyListener {
private Turn turn;
public InputHandler() {
turn = new Turn();
}
@Override
public void keyTyped(KeyEvent e) {
//
}
@Override
public void keyPressed(KeyEvent e) {
int k = e.getKeyCode();
if (k == KeyEvent.VK_NUMPAD1) {
turn.play();
}
if (k == KeyEvent.VK_NUMPAD2) {
turn.play();
}
if (k == KeyEvent.VK_NUMPAD3) {
turn.play();
}
if (k == KeyEvent.VK_NUMPAD4) {
turn.play();
}
if (k == KeyEvent.VK_NUMPAD5) {
turn.play();
}
if (k == KeyEvent.VK_NUMPAD6) {
turn.play();
}
if (k == KeyEvent.VK_NUMPAD7) {
turn.play();
}
if (k == KeyEvent.VK_NUMPAD8) {
turn.play();
}
if (k == KeyEvent.VK_NUMPAD9) {
turn.play();
}
}
@Override
public void keyReleased(KeyEvent e) {
//
}
}
Turn
package Game;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import tiles.Draw;
public class Turn extends JPanel{
public char nextTurn = 'O';
public BufferedImage image;
private Draw draw;
public Turn() {
}
public void pTurn() {
if (nextTurn == 'O') {
nextTurn = 'X';
} else if (nextTurn == 'X') {
nextTurn = 'O';
}
}
public void play() {
try {
image = ImageIO.read(getClass().getResourceAsStream("/tiles/tile_X.png"));
System.out.println("X Found");
pTurn();
repaint();
} catch (Exception e) {
System.out.println("File not found\n" + e);
}
}
public void paint(Graphics2D g) {
g.drawImage(image, 200, 200, null);
}
}