簡體   English   中英

使用MVC設計模式調用repaint()和paintComponent()方法的問題

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

我正在創建一個小型游戲,您單擊按鈕(上,下,左和右)來控制一只貓(由一個矩形表示)追逐鼠標(由另一個矩形表示)。 me腳,我知道...無論如何,我正在使用mvc設計模式,並且在從面板上的控制器(其中要繪制兩個矩形)的按鈕偵聽器調用repaint方法時遇到問題。 他們第一次成功塗漆,但再也沒有成功。

我已經以兩種方式實現了paintComponent()方法,但兩種方法均無效

  1. 創建一個擴展JPanel並從事paintComponent業務的單獨的類,在視圖中創建該類的新對象,並使用它繪制矩形。
  2. 創建一個JPanel並在新JPanel對象的括號中編寫paintComponent內容。

並且我已經在控制器中實現了要重繪的代碼,有兩種方式,但兩種方式均無效

  1. 從視圖中調用一個方法,該方法返回使用paintComponent方法的jpanel並對其調用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。 ;

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

}

看起來您的按鈕偵聽器會更新模型,但實際上並沒有任何內容更新cat和mouse矩形的坐標。 他們從模型中獲得了初始界限,但從未更新過。

視圖可能應該偵聽模型,以將貓和老鼠的位置同步到實際的Rectangle對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM