Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 850735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:18:44+00:00 2026-05-15T07:18:44+00:00

I’m practicing writing MVC applications. I have a Mastermind game, that I would like

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:18:45+00:00Added an answer on May 15, 2026 at 7:18 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.