0

我正在创建一个小游戏,您单击按钮(上、下、左、右)来控制一只猫(由一个矩形表示)追逐一只鼠标(由另一个矩形表示)。蹩脚的我知道......无论如何,我正在使用 mvc 设计模式,并且我在从面板上要“绘制”两个矩形的控制器中的按钮侦听器中调用 repaint 方法时遇到问题。他们第一次成功地绘画,但没有任何进一步的时间。

我已经以两种方式实现了 paintComponent() 方法,但都没有工作

  1. 创建一个扩展 JPanel 并执行paintComponent 业务的单独类,在视图中创建该类的新对象并使用它来绘制矩形。
  2. 创建一个 JPanel 并在新 JPanel 对象的括号中写入 paintComponent 内容。

我已经在控制器中实现了代码以两种方式重新绘制,但都没有工作

  1. 从返回 jpanel 的视图中调用一个方法,该 jpanel 使用 paintComponent 方法并在其上调用 repaint。
  2. 在控制器中创建一个 jpanel 并将面板从视图分配给它,然后在其上调用 repaint。

视图和控制器的代码(很长,对不起!)在下面,它还包括注释掉的东西,我无法通过两种方法来解决前面提到的问题......

View

/* * gameView.java */

打包游戏;

导入 java.awt。; 导入 java.awt.event。; 导入 javax.swing.*;

公共类 gameView 扩展 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;
}*/

}

控制器

/* * gameController.java */ 打包游戏;

导入 java.awt.event。; 导入 javax.swing。;

公共类游戏控制器{

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());
    }
}

}

4

1 回答 1

0

看起来您的按钮侦听器更新了模型,但实际上并没有更新猫和鼠标矩形的坐标。他们从模型中获得初始界限,但随后永远不会更新。

视图可能应该听模型以将猫和老鼠的位置同步到实际的 Rectangle 对象。

于 2011-04-15T16:37:24.947 回答