Hi Guys
Its me again, wasnt sure if i should start a new thread so i just did :)
I completly redid the code
Now im having a problem with the renderer again.
It called called in the timer of the controller class...
but it doesant get called?
Also my window wont close when i press x??
Also, my netbeans ide is saying Im leaking this in the contructer of the window class when i add the keylistener?
What does this mean??
My main class
package ballgame;
import java.awt.event.ActionEvent;
import javax.swing.Timer;
/**
*
* @author jonathan
*/
public class Controller {
public static Window window;
public static Model gameState;
public static Dimensions dimensions;
public static Controller mainControl;
public boolean run = true;
public Controller() {
run();
}
public final void run() {//this im going to totally redo to a proper game loop that wont take up cpu... maybe its just my old laptop... idk.
Timer timer = new Timer(50, (ActionEvent evt) -> {
while (run) {
gameState.Update();
Window.gamePainter.repaint();
}
});
timer.start();
}
public static void main(String[] args) {
dimensions = new Dimensions();//getting all the dimensions.
window = new Window();//creating a window
window.setVisible(true);
gameState = new Model();
mainControl = new Controller();
}
}
My window class
package ballgame;
import static ballgame.Controller.dimensions;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class Window extends JFrame implements KeyListener {
public static Paint_Render gamePainter;
public Window() {
gamePainter = new Paint_Render();
super.setTitle("Ball Game");
super.setSize(dimensions.getScreenWidth(), dimensions.getScreenHieght());
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
super.setLayout(null);
super.setResizable(false);
super.add(gamePainter);
super.addKeyListener(this);//find out why netbeans giving leaking this in contructer hint...
}
//key listener here
//listening to left and right arrow key input
//set the left and right key boolean in the Model class
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
Controller.gameState.setLeftArrow(true);
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
Controller.gameState.setRightArrow(true);
}
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
Controller.gameState.setLeftArrow(false);
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
Controller.gameState.setRightArrow(false);
}
}
@Override
public void keyTyped(KeyEvent e) {
}
}
My Model class
package ballgame;
import static ballgame.Controller.dimensions;
public final class Model {
private final int paddleSpeed = 5;
private final int ballSpeed = 5;
private boolean leftArrow;
private boolean rightArrow;
//these get set from the window class
public void setLeftArrow(boolean leftArrow) {
this.leftArrow = leftArrow;
}
public void setRightArrow(boolean rightArrow) {
this.rightArrow = rightArrow;
}
//called from the controller class
//updated the paddles x condinates
void Update() {
System.out.println("updating");
if (leftArrow == true) {
dimensions.removePaddleX(paddleSpeed);
} else if (rightArrow == true) {
dimensions.addPaddleX(paddleSpeed);
}
}
}
My Renderer/painting Class
package ballgame;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
/**
*
* @author jonathan
*/
public class Paint_Render extends JPanel {
void repaint(Graphics g) {
System.out.println("painting");
//painting the main window
g.setColor(Color.CYAN);
g.fillRect(0, 0, Controller.dimensions.getScreenWidth(), Controller.dimensions.getScreenHieght());
//painting the paddle
g.setColor(Color.black);
g.fillRect(Controller.dimensions.getPaddle().x, Controller.dimensions.getPaddle().y, Controller.dimensions.getPaddle().width, Controller.dimensions.getPaddle().height);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
repaint(g);
}
}
Any Help would be greatly appreciated.. tell me anything, tell me what im doing wrong and need to redo. straight to the point, im here to learn.