I was following a java tutorial on yt about a snake game using java swing gui and I managed to make the game window opening with the borders and background I wanted, but the arrays and the apple I created are not visible in the game window.
I tried to change colours to see if they are invisible somehow but they still do not appear. Can you guys suggest anything?? I tried to @Overrite the paintComponent method but still doesn't work. I'M using IntelliJ if that is important.
SnakeGame.java class
public class SnakeGame {
public static void main(String[] args) {
new GameFrame();
}
}
GameFrame.java class
import javax.swing.*;
public class GameFrame extends JFrame {
GameFrame(){
GamePanel panel = new GamePanel();
this.add(panel);
this.setTitle("Snake");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.pack();
this.setVisible(true);
this.setLocationRelativeTo(null);
}
}
GamePanel.java class
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.JPanel;
public class GamePanel extends JPanel implements ActionListener {
//screen width and height
static final int SCREEN_WIDTH = 600;
static final int SCREEN_HEIGHT = 600;
static final int UNIT_SIZE = 25;
//set game units in the panel
static final int GAME_UNITS = (SCREEN_WIDTH*SCREEN_HEIGHT)/UNIT_SIZE;
//create delay for timer
static final int DELAY = 75; //the higher the number of the delay, the slower the game will be
//the lower the number of the delay, the faster the game will be
//create arrays(x,y) to hold the coordinates of the snake
final int x[] = new int[GAME_UNITS];
final int y[] = new int[GAME_UNITS];
//set initial number of body parts for the snake
int bodyParts = 6;
int applesEaten;
int appleX;
int appleY;
char direction = 'R'; //snake begin by going to the right
boolean running = false;
Timer timer;
Random random;
GamePanel(){
random = new Random();
this.setPreferredSize(new Dimension(SCREEN_WIDTH, SCREEN_HEIGHT));
this.setBackground(Color.white);
this.setFocusable(true);
this.addKeyListener(new MyKeyAdapter());
startGame();
}
//Creating methods for the game
public void startGame(){
newApple();
running = true;
timer = new Timer(DELAY, this);
timer.start();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
}
public void draw(Graphics g){
//turn the background into a grid to help visualize
for(int i=0;i<SCREEN_HEIGHT/UNIT_SIZE;i++){
g.setColor(Color.GRAY);
g.drawLine(i*UNIT_SIZE, 0, i*UNIT_SIZE, SCREEN_HEIGHT);
g.drawLine(0, i*UNIT_SIZE, SCREEN_WIDTH, i*UNIT_SIZE);
}
g.setColor(Color.red);
g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);
for(int i = 0; i<bodyParts;i++){
if(i == 0){
g.setColor(Color.green);
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
else{
g.setColor(new Color(45,180,0));
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
}
}
public void newApple(){
appleX = random.nextInt((int)(SCREEN_WIDTH/UNIT_SIZE))*UNIT_SIZE;
appleY = random.nextInt((int)(SCREEN_HEIGHT/UNIT_SIZE))*UNIT_SIZE;
}