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 4332136
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:12:48+00:00 2026-05-21T10:12:48+00:00

I’m creating a small game, you click buttons (up, down, left and right) to

  • 0

I’m creating a small game, you click buttons (up, down, left and right) to control a cat (represented by a rectangle) to chase a mouse (represented by another rectangle). Lame I know… anyway I’m using the mvc design pattern and I am having problems calling the repaint method from button listeners in the controller on the panel where the two rectangles are to be ‘painted’. They paint successfully the first time but not any further time.

I’ve implemented the the paintComponent() method in two ways but both didn’t work

  1. Create a separate class that extends JPanel and does the paintComponent business, creating a new object of that class in the view and using it to paint the rectangles.
  2. Creating a JPanel and writing the paintComponent stuff in the parenthesis of the new JPanel object.

and I’ve implemented the code to in the controller to repaint in two ways and both didn’t work

  1. Call a method from the view that returns the jpanel that uses the paintComponent method and calling repaint on it.
  2. Creating a jpanel in the controller and assigning the panel from the view to it then calling repaint on that.

The code for the view and controller (which is long, sorry!) is below, it also includes the commented out stuff I couldn’t get to work from the two methods to approaching the problem mentioned before…

View

/*
* gameView.java
*/

package game;

import java.awt.;
import java.awt.event.
;
import javax.swing.*;

public class gameView extends JFrame{

//components
private JButton up = new JButton("Up");
private JButton down = new JButton("Down");
private JButton left = new JButton("Left");
private JButton right = new JButton("Right");
//panels
private JPanel content = new JPanel();
//boardPanel leftPanel = new boardPanel();
private Stroke drawingStroke = new BasicStroke(1);

private JPanel leftPanel = new JPanel(){
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponents(g);
        myPaint(g);
        }
};
private JPanel rightPanel = new JPanel();
//model
private gameModel model;
//mouse and cat
private Rectangle cat;
private Rectangle mouse;

public void myPaint(Graphics g){

    Graphics2D g1 = (Graphics2D)g;
    Graphics2D g2 = (Graphics2D)g;

    g1.setStroke(drawingStroke);
    g1.draw(cat);
    g1.setPaint(Color.yellow);
    g1.fill(cat);

    g2.setStroke(drawingStroke);
    g2.draw(mouse);
    g2.setPaint(Color.red);
    g2.fill(mouse);
}

//constructor
public gameView(gameModel _model){
    model = _model;
    //cat and mouse
    cat = new Rectangle(_model.getCatX(), _model.getCatY(), 10, 10);
    mouse = new Rectangle(_model.getMouseX(), _model.getMouseY(), 10, 10);
    //layout
    content.setSize(500, 400);
    content.setLayout(new GridLayout(0,2));

    leftPanel.setSize(200, 200);
    leftPanel.setBackground(Color.blue);

    rightPanel.setSize(100, 400);
    rightPanel.setLayout(new FlowLayout());
    rightPanel.add(new JLabel("Controls"));
    rightPanel.add(up);
    rightPanel.add(down);
    rightPanel.add(left);
    rightPanel.add(right);

    content.add(leftPanel);
    content.add(rightPanel);

    this.setSize(500, 400);
    this.setContentPane(content);
    this.setTitle("Cat & Mouse Game");
}
//returns the leftPanel to repaint in the controller
public JPanel getLeft(){
    return leftPanel;
}

//listeners for buttons
public void addUpListener(ActionListener moveUp){
    up.addActionListener(moveUp);
}
public void addDownListener(ActionListener moveDown){
    down.addActionListener(moveDown);
}
public void addLeftListener(ActionListener moveLeft){
    left.addActionListener(moveLeft);
}
public void addRightListener(ActionListener moveRight){
    right.addActionListener(moveRight);
}
public void addCloseListener(WindowListener close){
    this.addWindowListener(close);
}
//
public Rectangle getCat(){
    return cat;
}
public Rectangle getMouse(){
    return mouse;
}
//left side board panel
/*class boardPanel extends JPanel{
    Stroke drawingStroke = new BasicStroke(1);
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g1 = (Graphics2D)g;
        g1.setStroke(drawingStroke);
        g1.draw(cat);
        g1.setPaint(Color.yellow);
        g1.fill(cat);

        Graphics2D g2 = (Graphics2D)g;
        g2.setStroke(drawingStroke);
        g2.draw(mouse);
        g2.setPaint(Color.red);
        g2.fill(mouse);
    }
}
public JPanel getLeft(){
    return leftPanel;
}*/

}

Controller

/*
* gameController.java
*/
package game;

import java.awt.event.;
import javax.swing.
;

public class gameController {

private gameModel model;
private gameView view;
private JPanel lp = new JPanel();

public gameController(gameModel _model, gameView _view){
    model = _model;
    view = _view;

    lp=_view.getLeft();

    //listeners
    view.addUpListener(new UpListener());
    view.addDownListener(new DownListener());
    view.addLeftListener(new LeftListener());
    view.addRightListener(new RightListener());
    view.addCloseListener(
        new WindowAdapter(){
              public void windowClosing(WindowEvent we){
                  System.exit(0);
                  }
        });
}
//DOWN
class DownListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        model.setCatY((model.getCatY()+10));
        //do a random move for the mouse
        //model.randomMove();
        //view.getLeft().repaint();
        lp.repaint();
        System.out.println("x="+model.getCatX()+"y="+model.getCatY());
    }
}
//LEFT
class LeftListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        model.setCatY((model.getCatY()-10));
        //do a random move for the mouse
        //model.randomMove();
        //view.getLeft().repaint();
        lp.repaint();
        System.out.println("x="+model.getCatX()+"y="+model.getCatY());
    }
}
//RIGHT
class RightListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        model.setCatY((model.getCatY()+10));
        //do a random move for the mouse
        //model.randomMove();
        //view.getLeft().repaint();
        lp.repaint();
        System.out.println("x="+model.getCatX()+"y="+model.getCatY());
    }
}

}

  • 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-21T10:12:49+00:00Added an answer on May 21, 2026 at 10:12 am

    Looks like your button listeners update the model, but nothing actually updates the coordinates of the cat and mouse rectangles. They get their initial bounds from the model, but then are never updated.

    The view should probably listen to the model to sync the cat and mouse’s locations to the actual Rectangle objects.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into

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.