I would like to repaint my screen. As of now all it does is show the first screen with a dot where the head is supposed to be. This is fine, however I've written in my code that I want to move the head down 10 pixels every second. I'm printing at what point the head is supposed to be at, and in the command prompt it shows that the y value is indeed increasing. However on my screen the head is not moving.
I have tried using the revalidate method, trying to extend the canvas class instead of jframe, I have tried using different classes just for the paint method, i have tried replacing the paint method with the paintComponent method. And as you can probably tell I have a subpar understanding of anything related to painting in java. I have tried reading into these superclasses but they are too complicated for me to understand. I have also tried running without the sleep declaration. This didn't matter.
Main class: This class contains the main method to start running the snake game.
import java.util.concurrent.TimeUnit;
public class Main{
public static void main(String[] args) throws InterruptedException {
Main programma = new Main();
programma.rungame();
}
void rungame() throws InterruptedException {
AllGUIElements gui = new AllGUIElements();
gui.gui();
while (true) {
TimeUnit.SECONDS.sleep(1);
gui.setGameScreen();
}
}
}
AllGUIElements class: This class makes a new frame, containing a new panel. This panel is being painted using paintComponent. setGameScreen updates the position of the head and is supposed to repaint the screen.
import javax.swing.*;
import java.awt.*;
public class AllGUIElements extends JPanel {
private JFrame frame;
private JPanel panel;
private int screen;
Snake hoofd = new Head(new Point(30,30),3,null);
void gui() throws InterruptedException {
frame = new JFrame("Snake Game");
panel = new AllGUIElements();
panel.setBackground(Color.GRAY);
panel.setSize(1000,500);
frame.setSize(1000,500);
frame.add(panel);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void setGameScreen() {
repaint();
if (hoofd.getDirection() == 1) {
hoofd.setPosition(new Point(hoofd.getPosition().x, hoofd.getPosition().y-10));
}
if (hoofd.getDirection() == 2) {
hoofd.setPosition(new Point(hoofd.getPosition().x+10, hoofd.getPosition().y));
}
if (hoofd.getDirection() == 3) {
hoofd.setPosition(new Point(hoofd.getPosition().x, hoofd.getPosition().y+10));
}
if (hoofd.getDirection() == 4) {
hoofd.setPosition(new Point(hoofd.getPosition().x-10, hoofd.getPosition().y));
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.DARK_GRAY);
g.fillRect(0, 0, 1000, 10);
g.fillRect(0, 0, 10, 500);
g.fillRect(990, 0, 10, 500);
g.fillRect(0, 490, 1000, 10);
g.setColor(Color.GREEN);
g.fillRect(hoofd.getPosition().x, hoofd.getPosition().y, 10, 10);
}
}
Screenobject class: Parent class of Snake and Food (which is not posted since it's not necessary) which returns the position of the head, bodypart and food.
import java.awt.*;
public class Screenobject{
Point pos;
Screenobject(Point pos){
this.pos = pos;
}
void setPosition(Point pos){
this.pos = pos;
}
Point getPosition() {
return pos;
}
}
Snake class: Child of Screenobject and parent of Head and Body. This class sets the direction of the objects.
import java.awt.*;
public class Snake extends Screenobject{
int direction;
//directions:
//1 = up
//2 = right
//3 = down
//4 = left
Snake nextpart;
Snake(Point pos, int direction, Snake nextpart){
super(pos);
this.direction = direction;
this.nextpart = nextpart;
}
int getDirection() {
return direction;
}
}
Head class: Child class of snake and is supposed to be the first object of a linked list for the snake.
import java.awt.*;
public class Head extends Snake{
Head(Point pos, int direction, Snake nextpart){
super(pos, direction, nextpart);
}
}
Body class: Child class of Snake and grows for every food eaten.
import java.awt.*;
public class Body extends Snake{
Body(Point pos, int direction, Snake nextpart){
super(pos, direction, nextpart);
}
}