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