重绘方法不是重绘视图

时间:2019-01-01 01:58:54

标签: java swing

所以这是我的PlayView

public class PlayView extends JPanel {  

TagMan tagMan;

public PlayView() {
    this.setFocusable(true);
    setupPane();
    tagMan = new TagMan();
}

public void setupPane() {
    this.setPreferredSize( new Dimension(1100,800) );
    Color colorBlueBack = Color.decode("#000332");
    this.setBackground(colorBlueBack);
}

//paint tagman
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g.setColor(Color.WHITE);
    g2.fill(new Ellipse2D.Double(tagMan.getXPosition(), tagMan.getYPosition(), tagMan.getSize(), tagMan.getSize()));
}

}

这是我的控制器类, 公共类GameController扩展了KeyAdapter {

// Controllers
private MainController mainController;

// Views
private GameView gameView;
private PlayView playView;

// Models
private Game game;

public GameController(GameView gameView, PlayView playView, MainController mainController) {
    this.gameView = gameView;
    this.playView = playView;
    this.mainController = mainController;

    game = new Game();

    playView.addKeyListener(this);
}

@Override
public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
        game.getTagMan().moveRight();
        System.out.println("RIGHT: " + game.getTagMan().getXPosition());
    }

    if (e.getKeyCode() == KeyEvent.VK_UP) {
        game.getTagMan().moveUp();
        System.out.println("UP: " + game.getTagMan().getYPosition());
    }

    if (e.getKeyCode() == KeyEvent.VK_DOWN) {
        game.getTagMan().moveDown();
        System.out.println("DOWN: " + game.getTagMan().getYPosition());
    }
    playView.repaint();

}

@Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

因此,我尝试在每次按下按钮时重新绘制对象。但是它不会动..?我想念什么。该操作将更改tagMan值,并在系统输出中正确打印。我现在已经尝试了4个小时,但是找不到正确的解决方案。

0 个答案:

没有答案