开发者

Problem with calling repaint() and paintComponent() method, using mvc design pattern

I'm creating a small game, you click buttons (up, down, left and right) to control a cat (represented by a rectangle) to chase a mouse (represented by another rectangle). Lame I know... anyway I'm using the mvc design pattern and I am having problems calling the repaint method from button listeners in the controller on the panel where the two rectangles are to be 'painted'. They paint successfully the first time but not any further time.

I've implemented the the paintComponent() method in two ways but both didn't work

  1. Create a separate class that extends JPanel and does the paintComponent business, creating a new object of that class in the view and using it to paint the rectangles.
  2. Creating a JPanel and writing the paintComponent stuff in the parenthesis of the new JPanel object.

and I've implemented the code to in the controller to repaint in two ways and both didn't work

  1. Call a method from the view that returns the jpanel that uses the paintComponent method and calling repaint on it.
  2. Creating a jpanel in the controller and assigning the panel from the view to it then calling repaint on that.

The code for the view and controller (which is long, sorry!) is below, it also includes the commented out stuff I couldn't get to work from the two methods to approaching the problem mentioned before...

View

/* * gameView.java */

package game;

import java.awt.; import java.awt.event.; import javax.swing.*;

public class gameView extends JFrame{

//components
private JButton up = new JButton("Up");
private JButton down = new JButton("Down");
private JButton left = new JButton("Left");
private JButton right = new JButton("Right");
//panels
private JPanel content = new JPanel();
//boardPanel leftPanel = new boardPanel();
private Stroke drawingStroke = new BasicStroke(1);

private JPanel leftPanel = new JPanel(){
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponents(g);
        myPaint(g);
        }
};
private JPanel rightPanel = new JPanel();
//model
private gameModel model;
//mouse and cat
private Rectangle cat;
private Rectangle mouse;

public void myPaint(Graphics g){

    Graphics2D g1 = (Graphics2D)g;
    Graphics2D g2 = (Graphics2D)g;

    g1.setStroke(drawingStroke);
    g1.draw(cat);
    g1.setPaint(Color.yellow);
    g1.fill(cat);

    g2.setStroke(drawingStroke);
    g2.draw(mouse);
    g2.setPaint(Color.red);
    g2.fill(mouse);
}

//constructor
public gameView(gameModel _model开发者_如何学编程){
    model = _model;
    //cat and mouse
    cat = new Rectangle(_model.getCatX(), _model.getCatY(), 10, 10);
    mouse = new Rectangle(_model.getMouseX(), _model.getMouseY(), 10, 10);
    //layout
    content.setSize(500, 400);
    content.setLayout(new GridLayout(0,2));

    leftPanel.setSize(200, 200);
    leftPanel.setBackground(Color.blue);

    rightPanel.setSize(100, 400);
    rightPanel.setLayout(new FlowLayout());
    rightPanel.add(new JLabel("Controls"));
    rightPanel.add(up);
    rightPanel.add(down);
    rightPanel.add(left);
    rightPanel.add(right);

    content.add(leftPanel);
    content.add(rightPanel);

    this.setSize(500, 400);
    this.setContentPane(content);
    this.setTitle("Cat & Mouse Game");
}
//returns the leftPanel to repaint in the controller
public JPanel getLeft(){
    return leftPanel;
}

//listeners for buttons
public void addUpListener(ActionListener moveUp){
    up.addActionListener(moveUp);
}
public void addDownListener(ActionListener moveDown){
    down.addActionListener(moveDown);
}
public void addLeftListener(ActionListener moveLeft){
    left.addActionListener(moveLeft);
}
public void addRightListener(ActionListener moveRight){
    right.addActionListener(moveRight);
}
public void addCloseListener(WindowListener close){
    this.addWindowListener(close);
}
//
public Rectangle getCat(){
    return cat;
}
public Rectangle getMouse(){
    return mouse;
}
//left side board panel
/*class boardPanel extends JPanel{
    Stroke drawingStroke = new BasicStroke(1);
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g1 = (Graphics2D)g;
        g1.setStroke(drawingStroke);
        g1.draw(cat);
        g1.setPaint(Color.yellow);
        g1.fill(cat);

        Graphics2D g2 = (Graphics2D)g;
        g2.setStroke(drawingStroke);
        g2.draw(mouse);
        g2.setPaint(Color.red);
        g2.fill(mouse);
    }
}
public JPanel getLeft(){
    return leftPanel;
}*/

}

Controller

/* * gameController.java */ package game;

import java.awt.event.; import javax.swing.;

public class gameController {

private gameModel model;
private gameView view;
private JPanel lp = new JPanel();

public gameController(gameModel _model, gameView _view){
    model = _model;
    view = _view;

    lp=_view.getLeft();

    //listeners
    view.addUpListener(new UpListener());
    view.addDownListener(new DownListener());
    view.addLeftListener(new LeftListener());
    view.addRightListener(new RightListener());
    view.addCloseListener(
        new WindowAdapter(){
              public void windowClosing(WindowEvent we){
                  System.exit(0);
                  }
        });
}
//DOWN
class DownListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        model.setCatY((model.getCatY()+10));
        //do a random move for the mouse
        //model.randomMove();
        //view.getLeft().repaint();
        lp.repaint();
        System.out.println("x="+model.getCatX()+"y="+model.getCatY());
    }
}
//LEFT
class LeftListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        model.setCatY((model.getCatY()-10));
        //do a random move for the mouse
        //model.randomMove();
        //view.getLeft().repaint();
        lp.repaint();
        System.out.println("x="+model.getCatX()+"y="+model.getCatY());
    }
}
//RIGHT
class RightListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        model.setCatY((model.getCatY()+10));
        //do a random move for the mouse
        //model.randomMove();
        //view.getLeft().repaint();
        lp.repaint();
        System.out.println("x="+model.getCatX()+"y="+model.getCatY());
    }
}

}


Looks like your button listeners update the model, but nothing actually updates the coordinates of the cat and mouse rectangles. They get their initial bounds from the model, but then are never updated.

The view should probably listen to the model to sync the cat and mouse's locations to the actual Rectangle objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜