Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with calling repaint() and paintComponent() method, using mvc design pattern
    primarykey
    data
    text
    <p>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.</p> <p>I've implemented the the paintComponent() method in two ways but both didn't work</p> <ol> <li>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. </li> <li>Creating a JPanel and writing the paintComponent stuff in the parenthesis of the new JPanel object.</li> </ol> <p>and I've implemented the code to in the controller to repaint in two ways and both didn't work</p> <ol> <li>Call a method from the view that returns the jpanel that uses the paintComponent method and calling repaint on it.</li> <li>Creating a jpanel in the controller and assigning the panel from the view to it then calling repaint on that.</li> </ol> <p>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...</p> <pre><code>View </code></pre> <p>/* * gameView.java */</p> <p>package game;</p> <p>import java.awt.<em>; import java.awt.event.</em>; import javax.swing.*;</p> <p>public class gameView extends JFrame{</p> <pre><code>//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 &amp; 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; }*/ </code></pre> <p>}</p> <p>Controller</p> <p>/* * gameController.java */ package game;</p> <p>import java.awt.event.<em>; import javax.swing.</em>;</p> <p>public class gameController {</p> <pre><code>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()); } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload