将应用程序转换为 MVC 架构
我正在练习编写 MVC 应用程序。我有一个 Mastermind 游戏,我想将其重写为 MVC 应用程序。我已将我的代码分成几个部分,但我没有得到工作游戏,而是得到空框架和“public void Paint( Graphics g )”中的错误。在我看来,错误来自于使用 null 参数调用此方法。但如何克服这个问题呢? MVC 对于 swing 来说非常简单,但是 awt 及其绘制方法要复杂得多。
工作应用程序代码:
http://paste.pocoo.org/show/224982/
应用程序划分为 MVC :
主要:
public class Main {
public static void main(String[] args){
Model model = new Model();
View view = new View("Mastermind", 400, 590, model);
Controller controller = new Controller(model, view);
view.setVisible(true);
}
}
控制器:
import java.awt.*;
import java.awt.event.*;
public class Controller implements MouseListener, ActionListener {
private Model model;
private View view;
public Controller(Model m, View v){
model = m;
view = v;
view.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
} });
view.addMouseListener(this);
}
public void actionPerformed( ActionEvent e ) {
if(e.getSource() == view.checkAnswer){
if(model.isRowFull){
model.check();
}
}
}
public void mousePressed(MouseEvent e) {
Point mouse = new Point();
mouse = e.getPoint();
if (model.isPlaying){
if (mouse.x > 350) {
int button = 1 + (int)((mouse.y - 32) / 50);
if ((button >= 1) && (button <= 5)){
model.fillHole(button);
}
}
}
}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
视图:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class View extends Frame implements ActionListener {
Model model;
JButton checkAnswer;
private JPanel button;
static final int HIT_X[] = {270,290,310,290,310}, HIT_Y[] = {506,496,496,516,516};
public View(String name, int w, int h, Model m){
model = m;
setTitle( name );
setSize( w,h );
setResizable( false );
this.setLayout(new BorderLayout());
button = new JPanel();
button.setSize( new Dimension(400, 100));
button.setVisible(true);
checkAnswer = new JButton("Check");
checkAnswer.addActionListener(this);
checkAnswer.setSize( new Dimension(200, 30));
button.add( checkAnswer );
this.add( button, BorderLayout.SOUTH);
button.setVisible(true);
for ( int i=0; i < model.SCORE; i++ ){
for ( int j = 0; j < model.LINE; j++ ){
model.pins[i][j] = new Pin(20,0);
model.pins[i][j].setPosition(j*50+30,510-i*50);
model.pins[i+model.SCORE][j] = new Pin(8,0);
model.pins[i+model.SCORE][j].setPosition(HIT_X[j],HIT_Y[j]-i*50);
}
}
for ( int i=0; i < model.LINE; i++ ){
model.pins[model.OPTIONS][i] = new Pin( 20, i+2 );
model.pins[model.OPTIONS][i].setPosition( 370,i * 50 + 56);
}
model.combination();
model.paint(null);
}
public void actionPerformed( ActionEvent e ) {
}
}
模型:
import java.awt.*;
public class Model extends Frame{
static final int
LINE = 5,
SCORE = 10, OPTIONS = 20;
Pin pins[][] = new Pin[21][LINE];
int combination[] = new int[LINE];
int curPin = 0;
int turn = 1;
int repaintPin;
boolean isUpdate = true, isPlaying = true, isRowFull = false;
public Model(){
}
void fillHole(int color) {
pins[turn-1][curPin].setColor(color+1);
repaintPins( turn );
curPin = (curPin+1) % LINE;
if (curPin == 0){
isRowFull = true;
}
}
public void paint( Graphics g ) {
g.setColor( new Color(238, 238, 238));
g.fillRect( 0,0,400,590);
for ( int i=0; i < pins.length; i++ ) {
pins[i][0].paint(g);
pins[i][1].paint(g);
pins[i][2].paint(g);
pins[i][3].paint(g);
pins[i][4].paint(g);
}
}
public void update( Graphics g ) {
if ( isUpdate ) {
paint(g);
}
else {
isUpdate = true;
pins[repaintPin-1][0].paint(g);
pins[repaintPin-1][1].paint(g);
pins[repaintPin-1][2].paint(g);
pins[repaintPin-1][3].paint(g);
pins[repaintPin-1][4].paint(g);
}
}
void repaintPins( int pin ) {
repaintPin = pin;
isUpdate = false;
repaint();
}
void check() {
int junkPegs[] = new int[LINE], junkCode[] = new int[LINE];
int pegCount = 0, pico = 0;
for ( int i = 0; i < LINE; i++ ) {
junkPegs[i] = pins[turn-1][i].getColor();
junkCode[i] = combination[i];
}
for ( int i = 0; i < LINE; i++ ){
if (junkPegs[i]==junkCode[i]) {
pins[turn+SCORE][pegCount].setColor(1);
pegCount++;
pico++;
junkPegs[i] = 98;
junkCode[i] = 99;
}
}
for ( int i = 0; i < LINE; i++ ){
for ( int j = 0; j < LINE; j++ )
if (junkPegs[i]==junkCode[j]) {
pins[turn+SCORE][pegCount].setColor(2);
pegCount++;
junkPegs[i] = 98;
junkCode[j] = 99;
j = LINE;
}
}
repaintPins( turn+SCORE );
if ( pico == LINE ){
isPlaying = false;
}
else if ( turn >= 10 ){
isPlaying = false;
}
else{
curPin = 0;
isRowFull = false;
turn++;
}
}
void combination() {
for ( int i = 0; i < LINE; i++ ){
combination[i] = 1 + (int)(Math.random()*5);
System.out.print(i+",");
}
}
}
class Pin{
private int color, X, Y, radius;
private static final Color COLORS[] = {
Color.black,
Color.white,
Color.red,
Color.yellow,
Color.green,
Color.blue,
new Color(7, 254, 250)};
public Pin(){
X = 0; Y = 0; radius = 0; color = 0;
}
public Pin( int r,int c ){
X = 0; Y = 0; radius = r; color = c;
}
public void paint( Graphics g ){
int x = X-radius;
int y = Y-radius;
if (color > 0){
g.setColor( COLORS[color]);
g.fillOval( x,y,2*radius,2*radius );
}
else{
g.setColor( new Color(238, 238, 238) );
g.drawOval( x,y,2*radius-1,2*radius-1 );
}
g.setColor( Color.black );
g.drawOval( x,y,2*radius,2*radius );
}
public void setPosition( int x,int y ){
this.X = x ;
this.Y = y ;
}
public void setColor( int c ){
color = c;
}
public int getColor() {
return color;
}
}
有关如何克服此问题的任何线索都很棒。我的代码划分是否不当?
I'm practicing writing MVC applications. I have a Mastermind game, that I would like to rewrite as MVC app. I have divided my code to parts, but instead of working game I'm getting empty Frame and an error in "public void paint( Graphics g )". Error comes from calling this method in my view with null argument. But how to overcome this ? MVC was quite simple with swing but awt and it's paint methods are much more complicated.
Code of working app :
http://paste.pocoo.org/show/224982/
App divided to MVC :
Main :
public class Main {
public static void main(String[] args){
Model model = new Model();
View view = new View("Mastermind", 400, 590, model);
Controller controller = new Controller(model, view);
view.setVisible(true);
}
}
Controller :
import java.awt.*;
import java.awt.event.*;
public class Controller implements MouseListener, ActionListener {
private Model model;
private View view;
public Controller(Model m, View v){
model = m;
view = v;
view.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
} });
view.addMouseListener(this);
}
public void actionPerformed( ActionEvent e ) {
if(e.getSource() == view.checkAnswer){
if(model.isRowFull){
model.check();
}
}
}
public void mousePressed(MouseEvent e) {
Point mouse = new Point();
mouse = e.getPoint();
if (model.isPlaying){
if (mouse.x > 350) {
int button = 1 + (int)((mouse.y - 32) / 50);
if ((button >= 1) && (button <= 5)){
model.fillHole(button);
}
}
}
}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
View :
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class View extends Frame implements ActionListener {
Model model;
JButton checkAnswer;
private JPanel button;
static final int HIT_X[] = {270,290,310,290,310}, HIT_Y[] = {506,496,496,516,516};
public View(String name, int w, int h, Model m){
model = m;
setTitle( name );
setSize( w,h );
setResizable( false );
this.setLayout(new BorderLayout());
button = new JPanel();
button.setSize( new Dimension(400, 100));
button.setVisible(true);
checkAnswer = new JButton("Check");
checkAnswer.addActionListener(this);
checkAnswer.setSize( new Dimension(200, 30));
button.add( checkAnswer );
this.add( button, BorderLayout.SOUTH);
button.setVisible(true);
for ( int i=0; i < model.SCORE; i++ ){
for ( int j = 0; j < model.LINE; j++ ){
model.pins[i][j] = new Pin(20,0);
model.pins[i][j].setPosition(j*50+30,510-i*50);
model.pins[i+model.SCORE][j] = new Pin(8,0);
model.pins[i+model.SCORE][j].setPosition(HIT_X[j],HIT_Y[j]-i*50);
}
}
for ( int i=0; i < model.LINE; i++ ){
model.pins[model.OPTIONS][i] = new Pin( 20, i+2 );
model.pins[model.OPTIONS][i].setPosition( 370,i * 50 + 56);
}
model.combination();
model.paint(null);
}
public void actionPerformed( ActionEvent e ) {
}
}
Model:
import java.awt.*;
public class Model extends Frame{
static final int
LINE = 5,
SCORE = 10, OPTIONS = 20;
Pin pins[][] = new Pin[21][LINE];
int combination[] = new int[LINE];
int curPin = 0;
int turn = 1;
int repaintPin;
boolean isUpdate = true, isPlaying = true, isRowFull = false;
public Model(){
}
void fillHole(int color) {
pins[turn-1][curPin].setColor(color+1);
repaintPins( turn );
curPin = (curPin+1) % LINE;
if (curPin == 0){
isRowFull = true;
}
}
public void paint( Graphics g ) {
g.setColor( new Color(238, 238, 238));
g.fillRect( 0,0,400,590);
for ( int i=0; i < pins.length; i++ ) {
pins[i][0].paint(g);
pins[i][1].paint(g);
pins[i][2].paint(g);
pins[i][3].paint(g);
pins[i][4].paint(g);
}
}
public void update( Graphics g ) {
if ( isUpdate ) {
paint(g);
}
else {
isUpdate = true;
pins[repaintPin-1][0].paint(g);
pins[repaintPin-1][1].paint(g);
pins[repaintPin-1][2].paint(g);
pins[repaintPin-1][3].paint(g);
pins[repaintPin-1][4].paint(g);
}
}
void repaintPins( int pin ) {
repaintPin = pin;
isUpdate = false;
repaint();
}
void check() {
int junkPegs[] = new int[LINE], junkCode[] = new int[LINE];
int pegCount = 0, pico = 0;
for ( int i = 0; i < LINE; i++ ) {
junkPegs[i] = pins[turn-1][i].getColor();
junkCode[i] = combination[i];
}
for ( int i = 0; i < LINE; i++ ){
if (junkPegs[i]==junkCode[i]) {
pins[turn+SCORE][pegCount].setColor(1);
pegCount++;
pico++;
junkPegs[i] = 98;
junkCode[i] = 99;
}
}
for ( int i = 0; i < LINE; i++ ){
for ( int j = 0; j < LINE; j++ )
if (junkPegs[i]==junkCode[j]) {
pins[turn+SCORE][pegCount].setColor(2);
pegCount++;
junkPegs[i] = 98;
junkCode[j] = 99;
j = LINE;
}
}
repaintPins( turn+SCORE );
if ( pico == LINE ){
isPlaying = false;
}
else if ( turn >= 10 ){
isPlaying = false;
}
else{
curPin = 0;
isRowFull = false;
turn++;
}
}
void combination() {
for ( int i = 0; i < LINE; i++ ){
combination[i] = 1 + (int)(Math.random()*5);
System.out.print(i+",");
}
}
}
class Pin{
private int color, X, Y, radius;
private static final Color COLORS[] = {
Color.black,
Color.white,
Color.red,
Color.yellow,
Color.green,
Color.blue,
new Color(7, 254, 250)};
public Pin(){
X = 0; Y = 0; radius = 0; color = 0;
}
public Pin( int r,int c ){
X = 0; Y = 0; radius = r; color = c;
}
public void paint( Graphics g ){
int x = X-radius;
int y = Y-radius;
if (color > 0){
g.setColor( COLORS[color]);
g.fillOval( x,y,2*radius,2*radius );
}
else{
g.setColor( new Color(238, 238, 238) );
g.drawOval( x,y,2*radius-1,2*radius-1 );
}
g.setColor( Color.black );
g.drawOval( x,y,2*radius,2*radius );
}
public void setPosition( int x,int y ){
this.X = x ;
this.Y = y ;
}
public void setColor( int c ){
color = c;
}
public int getColor() {
return color;
}
}
Any clues on how to overcome this would be great. Have I divided my code improperly ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对我来说看起来不太合适。
模型不应扩展框架。框架是一个视图的想法。模型中不应包含图形、绘画或除当前问题之外的任何内容。
我不会让 View 实现任何 Listener 接口。这是控制器的工作。监听器负责处理所有事件并更新模型以响应从视图事件接收到的更改。
View 应该只是 Swing 或 AWT 类;没有听众。 Paint方法属于View。任何绘制的内容都应该在视图中表达。
模型和视图都不应该互相引用或控制器;控制器拥有对模型和视图的引用。这是知道整个问题的全知者。
模型只知道您正在解决的问题。
视图应该是完全可拆卸的。一个好的测试是能够删除 Swing UI 并编写一个纯文本驱动的 View 来替换它。如果你能做到这一点而不影响其他两个,那么你就已经正确地分层了。
This doesn't look right to me.
Model should not extend Frame. Frame is a View idea. Model should not have graphics, or painting, or anything besides the problem at hand inside it.
I wouldn't have View implement any of the Listener interfaces. That's the Controller's job. It's the Listener that handles all events and updates the Model to respond to the changes it receives from the View event.
View should be just Swing or AWT classes; no Listeners. Paint methods belong in View. Anything that's painted should be expressed in View.
Neither Model nor View should have references to each other or Controller; the Controller owns references to both Model and View. It's the omniscient one that knows the whole problem.
Model only knows about the problem you're solving.
View should be completely detachable. A good test is to be able to remove the Swing UI and write a purely text-driven View to replace it. If you can do it without affecting the other two, you've layered things properly.