0

I am trying to create a brick breaker game as a personal project. I using an MVC design with paint() and Graphics for the GUI.

When I call repaint(), the paint method is not being invoked. Below is some of the code I have so far.

This is the controller:

public class Controller {
private Model model;
public View view;

public Controller(Model model, View view){
    this.model = model;
    this.view = view;
    initView();

    int i = 2;
    while(i>0){
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                doCycle();
            }});
        i--;
    }
}



public void initController(){
}

public void initView(){
    model.createGamePieces();
    //view.initGamePieces(model.getPaddle());
    view.createFrame();

}

public void doCycle(){
    view.updateView(model);
}

}


And this is the View Class

public class View extends JFrame{
private JFrame frame;
private JPanel panel;
private Graphics g;
private JLabel label;
private Paddle paddle;
private JPanel paddlePanel;
private static Model model;
//private JLabel testLabel;

public View(){
    model  = new Model();
}

public void createFrame(){
    frame = new JFrame();

    frame.setSize(500,700);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Breakout Ball");
    frame.getContentPane().setBackground(Color.BLACK);
    frame.setVisible(true);
    frame.setVisible(true);

}


public void updateView(Model modelArg){
    Graphics g;
    model = modelArg;
    System.out.println("Hi there");
    System.out.println(SwingUtilities.isEventDispatchThread());

    revalidate();
    
    /*
            repaint() BELOW NOT BEING INVOKED
     */
    repaint();
}

@Override
public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2 = (Graphics2D) g;
    System.out.println("Hello");
    draw(g2);

}


public static void draw(Graphics2D g2){
    draw(g2, model.getPaddle());
}

public static void draw(Graphics2D g2, Paddle paddle){
    Rectangle paddleImage = new Rectangle(paddle.getX(),paddle.getY(), paddle.getWidth(),paddle.getHeight());
    g2.draw(paddleImage);
}

}

If anyone has any ideas for why repaint() does not invoke the paint() method in my View class, please let me know!

Thank you!

Jon

camickr
  • 321,443
  • 19
  • 166
  • 288
Jon Cohen
  • 1
  • 1
  • 1
    You shouldn't use the paint method. You should use the paintComponent method of a JPanel. See the Oracle tutorial, [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) to see how to paint with a JPanel. – Gilbert Le Blanc Oct 04 '21 at 15:55
  • 1
    You are doing too many things incorrectly to explain here. I recommend you check out [The Java Tutorials](http://docs.oracle.com/javase/tutorial/index.html) on how to properly paint. For one, don't paint in the JFrame. Paint in a JPanel that is in the JFrame and override `paintComponent(Graphics g);` – WJS Oct 04 '21 at 15:56
  • 1
    *the paint method is not being invoked* - because you have two frames. Your class extends JFrame and you overrride the paint() method of that class and that frame is never used. Then in your class you create a new JFrame. That is the frame that is visible. You need to start over. Read the tutorial link provided above. Download the working demo code and make changes to the working code. Then if it stops working you know the problem is something your changed. – camickr Oct 04 '21 at 16:01
  • Thank you all for the help! It is working properly now. – Jon Cohen Oct 04 '21 at 17:31

0 Answers0