gpt4 book ai didi

java - 将应用程序转换为 MVC 架构

转载 作者:太空宇宙 更新时间:2023-11-04 08:52:29 25 4
gpt4 key购买 nike

我正在练习编写 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);
}
}

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) {}
}

查看:

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;
}
}

任何关于如何克服这个问题的线索都会很棒。我的代码划分是否不正确?

最佳答案

这对我来说看起来不对。

模型不应扩展框架。框架是一个 View 的想法。模型中不应包含图形、绘画或除当前问题之外的任何内容。

我不会让 View 实现任何 Listener 接口(interface)。这是 Controller 的工作。监听器负责处理所有事件并更新模型以响应从 View 事件接收到的更改。

View 应该只是 Swing 或 AWT 类;没有听众。 Paint方法属于View。任何绘制的内容都应该在 View 中表达。

模型和 View 都不应该互相引用或 Controller ; Controller 拥有对模型和 View 的引用。无所不知的人知道整个问题。

模型只知道您正在解决的问题。

View 应该是完全可拆卸的。一个好的测试是能够删除 Swing UI 并编写一个纯文本驱动的 View 来替换它。如果您可以在不影响其他两个的情况下做到这一点,那么您就已经正确地分层了。

关于java - 将应用程序转换为 MVC 架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3033331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com