Currantly trying to make a simple game but i cant get the renderer to repaint. My problem is that i used to make everything in 1 class... much simpler.
But now im just not sure anymore.
This is what i have so far... it just wont repaint. (Note i have a thread for now that calls the renderer in my gamethread class... the last class posted. )
my main class...
package ballgame;
import javax.swing.JFrame;
/**
*
* @author jonathan
*/
public class BallGame {
public static Renderer gamePainter;
public static BallGame ballGame;
public static Dimensions dimensions;
public static GameThread gameThread;
private final int WIDTH;
private final int HIEGHT;
public BallGame() {
gamePainter = new Renderer();
WIDTH = dimensions.getWIDTH();
HIEGHT = dimensions.getHIEGHT();
JFrame jframe = new JFrame("Ball Game");
jframe.setResizable(false);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(WIDTH, HIEGHT);
jframe.setVisible(true);
jframe.add(gamePainter);
}
public static void main(String[] args) {
//initializing components
dimensions = new Dimensions();
ballGame = new BallGame();
gameThread = new GameThread();
}
}
Than here i have my Renderer class
package ballgame;
import java.awt.Graphics;
import javax.swing.JPanel;
/**
*
* @author jonathan
*/
public class Renderer extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
BallGame.gameThread.repaint(g);
}
}
And Finally here i have my GameThread class
kinda where i wanna paint everything... I also want to make a another class called ball class... what will make many balls...
starting with 1 and the user has to keep them from hitting the floor, any how... here is the class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ballgame;
import static ballgame.BallGame.dimensions;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import javax.swing.JPanel;
/**
*
* @author jonathan
*/
public final class GameThread extends JPanel {
private final int HIEGHT;
private final int WIDTH;
private final int paddleHieght;
private final int paddleWidth;
private Rectangle paddle;
public GameThread() {
WIDTH = dimensions.getWIDTH();
HIEGHT = dimensions.getHIEGHT();
paddleHieght = dimensions.getPaddleHIEGHT();
paddleWidth = dimensions.getPaddleWidth();
paddle = new Rectangle(WIDTH / 2 - paddleWidth / 2, HIEGHT - paddleHieght, paddleWidth, paddleHieght);
new ScheduledThreadPoolExecutor(1).
scheduleAtFixedRate(this::gameLoop,
0, 30, MILLISECONDS);
}
void gameLoop() {
updatePositions();
BallGame.gamePainter.repaint();
}
void updatePositions() {
//here i will be updating all the positions of the rectangles... and circles... based on user input
}
void repaint(Graphics g) {
System.out.println("in here");
g.setColor(Color.CYAN);
g.fillRect(0, 0, WIDTH, HIEGHT);
paintPaddle(g);
}
void paintPaddle(Graphics g) {
g.setColor(Color.black);
g.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
}
}
Any Help would be appreciated.
Thanks