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.

ive changed this

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...     
    }

To This

public class Window extends JPanel implements KeyListener {

    public static Paint_Render gamePainter;

    public Window() {
        gamePainter = new Paint_Render();
        JFrame frame = new JFrame();
        frame.setTitle("Ball Game");
        frame.setSize(dimensions.getScreenWidth(), dimensions.getScreenHieght());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
        frame.setResizable(false);
        frame.add(gamePainter);
        frame.addKeyListener(this);//find out why netbeans giving leaking this in contructer hint...  
        frame.setVisible(true);
    }

But that dident change anything iether. I think i need a more concrete explanation on this. Ive been google and looking stuff up but nothing I seem to accually get...

Ive also tried Dispose on close

Leaking in constructor...

The language spec only guarantees that the new object will be in a complete and consistent state after the constructor has finished.
When you add the key listener you pass a reference to you current object, so it's immediately possible for another class to call methods in your object, even though its constructor has not finished.
It's one of those theoretical warnings that rarely cause problems in practice, but the simplest way to keep safe in a case like this one is to finish creating the window and make it visible before adding any listeners.

ps I greatly preferred the first version. It shouldn't be the responsibility of a JPanel to create its own parent frame like in v2.
pps you don’t need all those supers when calling methods inherited from a superclass

so by first version you mean the first version i posted in this thread correct??, and if i take away the supers... Netbeans gives me the warning Over ridable method call in constructer....
,but by taking away the supers... my program does close now.

Another wierd thing is if i set my window visible in the controller class after creating window... instead of setting visible in window class, it wont close... even without the supers??

my key listener isnt working either?

Also Im still having issues with why my code isnt rendering?

The "first version" is from your "I've changed this..." post - where you have one class for the game JPanel and another class (extends JFrame) for the main window, For me that's good design when each class represents one thing fully.

super. in constructors:
Netbeans is designed to support huge mission-critical projects and so warns of all kinds of code that, in theory, could cause bugs. Calling a method in a constructor, when the method could have been overridden and the override method assumed construction is complete can cause bugs. Will this happen in a one-person small project? Highly unlikely.
For now you can safely ignore that message - just keep your constructors small and simple, and be aware of what order things are being done in.

As for your other 3 issues - without seeiing the code there's nothing I can do.

The issue is now resolved james:). Thanks for all of this..... I will most likely be back for more:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.