我正在尝试使用MVC在JAVA中制作俄罗斯方块。现在,我写了一切,一切正常。我只留下一个问题;改变观点。例如,当我获得200分时,视图必须改变。我想在我的游戏板中将方块更改为圆圈。但我不能让它发挥作用。我可以用一些方法来做,但我想用不同的类来管理它。我有一个abstractView类,真正的视图扩展了这个抽象类。如何从1切换到2?这是否发生在控制器中?或者我是否必须在模型中编写一些代码以及进行更改?
AbstractView
package Tetris;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.Timer;
public abstract class abstractView extends JFrame implements BoardObserver
{
ControllerInterface controller;
ModelInterface model;
JButton goDown;
JButton goLeft;
JButton goRight;
JButton turn;
JButton pause;
JButton restart;
JLabel scoreLabel;
JLabel scoreText;
JLabel levelLabel;
JLabel levelText;
JLabel pauseLabel;
Timer timer;
public abstractView(final ControllerInterface controller, ModelInterface model)
{
this.model=model;
this.controller=controller;
createView();
this.model.registerObserver(this);
}
public void createView()
{
this.setTitle("Tetris Jasper Nuytten");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setMinimumSize(new Dimension(400,500));
this.setPreferredSize(new Dimension(400,500));
this.setResizable(false);
setLayout(null);
goRight=new JButton("»");
goRight.setBounds(330, 370, 45, 20);
goLeft=new JButton("«");
goLeft.setBounds(235, 370, 45, 20);
turn = new JButton("Turn");
turn.setBounds(270,330,70,30);
this.turn.setVisible(true);
goDown= new JButton("Down");
goDown.setBounds(270,400,70,30);
pause=new JButton("|>");
pause.setBounds(230,240,50,40);
restart=new JButton("Restart");
restart.setBounds(300,240,80,40);
restart.setEnabled(false);
scoreLabel = new JLabel();
scoreLabel.setText("SCORE :");
scoreLabel.setBounds(230,90,70,40);
scoreText = new JLabel();
scoreText.setText(Integer.toString(this.controller.getScore()));
scoreText.setBounds(290,90,70,40);
levelLabel = new JLabel();
levelLabel.setText("LEVEL :");
levelLabel.setBounds(230,120,70,40);
levelText = new JLabel();
levelText.setText(Integer.toString(this.controller.getLevel()));
levelText.setBounds(290,120,70,40);
pauseLabel = new JLabel();
pauseLabel.setText("PAUSE");
pauseLabel.setBounds(260,180,70,40);
pauseLabel.setFont(new Font(pauseLabel.getFont().getName(),Font.PLAIN,20));
//this.scoreText.setSize();
this.add(goLeft);
this.add(goRight);
this.add(goDown);
this.add(turn);
this.add(restart);
this.add(pause);
this.add(scoreText);
this.add(scoreLabel);
this.add(levelText);
this.add(levelLabel);
this.add(pauseLabel);
// The actions for when a button has been pressed.
goDown.addActionListener(new java.awt.event.ActionListener()
{
@Override
public void actionPerformed(java.awt.event.ActionEvent evt)
{
goDownButtonClicked(evt);
}
}
);
goLeft.addActionListener(new java.awt.event.ActionListener()
{
@Override
public void actionPerformed(java.awt.event.ActionEvent evt)
{
goLeftButtonClicked(evt);
}
}
);
goRight.addActionListener(new java.awt.event.ActionListener()
{
@Override
public void actionPerformed(java.awt.event.ActionEvent evt)
{
goRightButtonClicked(evt);
}
}
);
pause.addActionListener(new java.awt.event.ActionListener()
{
@Override
public void actionPerformed(java.awt.event.ActionEvent evt)
{
pauseButtonClicked(evt);
}
}
);
restart.addActionListener(new java.awt.event.ActionListener()
{
@Override
public void actionPerformed(java.awt.event.ActionEvent evt)
{
restartButtonClicked(evt);
}
}
);
turn.addActionListener(new java.awt.event.ActionListener()
{
@Override
public void actionPerformed(java.awt.event.ActionEvent evt)
{
turnButtonClicked(evt);
}
}
);
// Activate Keys on keyboard.
this.addKeyListener(new java.awt.event.KeyAdapter()
{
@Override
public void keyPressed(java.awt.event.KeyEvent evt)
{
KeyReleased(evt);
}
});
this.setFocusable(true); //To make the keys active
this.setVisible(true);
}
private void goDownButtonClicked(java.awt.event.ActionEvent evt)
{
this.controller.goDown();
}
private void pauseButtonClicked(java.awt.event.ActionEvent evt)
{
this.controller.pause();
this.requestFocus();
repaintz(this.getGraphics());
}
private void turnButtonClicked(java.awt.event.ActionEvent evt)
{
this.controller.turn();
}
private void goLeftButtonClicked(java.awt.event.ActionEvent evt)
{
this.controller.goLeft();
}
private void goRightButtonClicked(java.awt.event.ActionEvent evt)
{
this.controller.goRight();
}
private void restartButtonClicked(java.awt.event.ActionEvent evt)
{
if(this.controller.getGameState())
this.controller.restart();
}
@Override
public void paint(Graphics g)
{
super.paint(g);
for (int i=0;i<this.controller.getBoard().length;i++)
{
for(int j = 0; j<this.controller.getBoard()[0].length;j++)
{
switch(this.controller.getBoard()[i][j])
{
case 1:
{
g.setColor(Color.PINK);
};
break;
case 2:
{
g.setColor(Color.cyan);
};
break;
case 3:
{
g.setColor(Color.BLUE);
};
break;
case 4:
{
g.setColor(Color.RED);
};
break;
case 5:
{
g.setColor(Color.YELLOW);
};
break;
case 6:
{
g.setColor(Color.orange);
};
break;
case 7:
{
g.setColor(Color.GREEN);
};
break;
case 0:
{
g.setColor(Color.LIGHT_GRAY);
};
break;
}
g.drawRect((j*20)+20,i*20+40,20,20);
g.fillRect((j*20)+20,i*20+40,20,20);
}
}
// Show the next tetris shape.
for (int rowMini=0;rowMini<4;rowMini++)
{
for (int columnMini=0;columnMini<3;columnMini++)
{
if(this.controller.getFuturePiece()[rowMini][columnMini]>0)
{
g.setColor(Color.BLACK);
}
else
{
g.setColor(Color.white);
}
g.drawRect((columnMini*20)+232,rowMini*20+40,20,20);
g.fillRect((columnMini*20)+232,rowMini*20+40,20,20);
}
}
}
// Methoden voor verschillende views
public abstract void repaintz(Graphics g);
//public abstract void repainty(Graphics g);
//public abstract void repaintx(Graphics g);
public void drawFutureShape(Graphics g)
{
for (int rowMini=0;rowMini<4;rowMini++)
{
for (int columnMini=0;columnMini<3;columnMini++)
{
if(this.controller.getFuturePiece()[rowMini][columnMini]>0)
{
g.setColor(Color.BLACK);
}
else
{
g.setColor(Color.WHITE);
}
g.drawRect((columnMini*20)+232,rowMini*20+40,20,20);
g.fillRect((columnMini*20)+232,rowMini*20+40,20,20);
}
}
}
public void calculateLogics()
{
if(!this.controller.getGameState())
{
this.restart.setEnabled(false);
scoreText.setText(Integer.toString(this.controller.getScore()));
levelText.setText(Integer.toString(this.controller.getLevel()));
}
else
{
this.restart.setEnabled(true);
this.scoreText.setText("GAME OVER");
}
if(this.controller.getPauseState())
{
this.pause.setText("||");
this.pauseLabel.setEnabled(false);
}
else
{
this.pause.setText("|>");
this.pauseLabel.setEnabled(true);
}
}
@Override
public void updateBoard()
{
repaintz(this.getGraphics());
this.requestFocus();
}
// Code to link a key with a method.
public void KeyReleased(java.awt.event.KeyEvent e)
{
if(e.getKeyChar()=='p')
{
this.controller.pause();
}
switch(e.getKeyCode())
{
case KeyEvent.VK_UP :this.controller.turn();break;
case KeyEvent.VK_DOWN :this.controller.goDown();break;
case KeyEvent.VK_LEFT :this.controller.goLeft();break;
case KeyEvent.VK_RIGHT :this.controller.goRight();break;
case KeyEvent.VK_SPACE :this.controller.dropDown();break;
}
}
}
VIEW1
package Tetris;
import java.awt.*;
public class TetrisView extends abstractView
{
public TetrisView(ControllerInterface controller, ModelInterface model) {
super(controller, model);
}
@Override
public void repaintz(Graphics g)
{
calculateLogics();
for (int i=0;i<this.controller.getBoard().length;i++)
{
for(int j = 0; j<this.controller.getBoard()[0].length;j++)
{
switch(this.controller.getBoard()[i][j])
{
case 1:
{
g.setColor(Color.PINK);
g.fillRect((j*20)+20,i*20+40,20,20);
g.setColor(Color.BLACK);
g.drawRect((j*20)+20,i*20+40,20,20);
};
break;
case 2:
{
g.setColor(Color.cyan);
g.fillRect((j*20)+20,i*20+40,20,20);
g.setColor(Color.BLACK);
g.drawRect((j*20)+20,i*20+40,20,20);
};
break;
case 3:
{
g.setColor(Color.BLUE);
g.fillRect((j*20)+20,i*20+40,20,20);
g.setColor(Color.BLACK);
g.drawRect((j*20)+20,i*20+40,20,20);
};
break;
case 4:
{
g.setColor(Color.RED);
g.fillRect((j*20)+20,i*20+40,20,20);
g.setColor(Color.BLACK);
g.drawRect((j*20)+20,i*20+40,20,20);
};
break;
case 5:
{
g.setColor(Color.YELLOW);
g.fillRect((j*20)+20,i*20+40,20,20);
g.setColor(Color.BLACK);
g.drawRect((j*20)+20,i*20+40,20,20);
};
break;
case 6:
{
g.setColor(Color.orange);
g.fillRect((j*20)+20,i*20+40,20,20);
g.setColor(Color.BLACK);
g.drawRect((j*20)+20,i*20+40,20,20);
};
break;
case 7:
{
g.setColor(Color.GREEN);
g.fillRect((j*20)+20,i*20+40,20,20);
g.setColor(Color.BLACK);
g.drawRect((j*20)+20,i*20+40,20,20);
};
break;
case 0:
{
g.setColor(Color.LIGHT_GRAY);
g.fillRect((j*20)+20,i*20+40,20,20);
};
break;
}
}
}
drawFutureShape(this.getGraphics());
}
}
视图2
package Tetris;
import java.awt.*;
public class TetrisView2 extends abstractView
{
public TetrisView2(ControllerInterface controller, ModelInterface model) {
super(controller, model);
}
@Override
public void repaintz(Graphics g)
{
calculateLogics();
for (int i=0;i<this.controller.getBoard().length;i++)
{
for(int j = 0; j<this.controller.getBoard()[0].length;j++)
{
switch(this.controller.getBoard()[i][j])
{
case 1:
{
g.setColor(Color.PINK);
};
break;
case 2:
{
g.setColor(Color.cyan);
};
break;
case 3:
{
g.setColor(Color.BLUE);
};
break;
case 4:
{
g.setColor(Color.RED);
};
break;
case 5:
{
g.setColor(Color.YELLOW);
};
break;
case 6:
{
g.setColor(Color.orange);
};
break;
case 7:
{
g.setColor(Color.GREEN);
};
break;
case 0:
{
g.setColor(Color.LIGHT_GRAY);
g.drawRect((j*20)+20,i*20+40,20,20);
g.fillRect((j*20)+20,i*20+40,20,20);
};
break;
}
g.drawRoundRect((j*20)+20,i*20+40,20,20,20,20);
g.fillRoundRect((j*20)+20,i*20+40,20,20,20,20);
}
}
drawFutureShape(this.getGraphics());
}
}