简体   繁体   English

使用MVC设计模式调用repaint()和paintComponent()方法的问题

[英]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'. me脚,我知道...无论如何,我正在使用mvc设计模式,并且在从面板上的控制器(其中要绘制两个矩形)的按钮侦听器调用repaint方法时遇到问题。 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 我已经以两种方式实现了paintComponent()方法,但两种方法均无效

  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. 创建一个扩展JPanel并从事paintComponent业务的单独的类,在视图中创建该类的新对象,并使用它绘制矩形。
  2. Creating a JPanel and writing the paintComponent stuff in the parenthesis of the new JPanel object. 创建一个JPanel并在新JPanel对象的括号中编写paintComponent内容。

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. 从视图中调用一个方法,该方法返回使用paintComponent方法的jpanel并对其调用repaint。
  2. Creating a jpanel in the controller and assigning the panel from the view to it then calling repaint on that. 在控制器中创建一个jpanel并从视图中为其分配面板,然后在该面板上调用repaint。

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 */ / * * gameView.java * /

package game; 包装游戏;

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

public class gameView extends JFrame{ 公共类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;
}*/

} }

Controller 控制者

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

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

public class gameController { 公共类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. 看起来您的按钮侦听器会更新模型,但实际上并没有任何内容更新cat和mouse矩形的坐标。 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. 视图可能应该侦听模型,以将猫和老鼠的位置同步到实际的Rectangle对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM