d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Minesweeper Game
Prev12
Add Reply New Topic New Poll
Member
Posts: 2,388
Joined: Nov 26 2006
Gold: 23.40
Jun 12 2012 03:36pm
Okay so I have the choice of difficulty in a separate class, in the Menu class. The rows, columns, and bombs are sent into the GUI class through parameters, and in the GUI class implements MouseListener.

When I override the mouseClicked method of MouseListener how can I send in the variables rows, columns, and bombs? It gives me an error if I send more than just the parameter of (MouseEvent event).

The error is the GUI is not abstract and does not override abstract method mouseClicked. So can I not send in more than the default parameter?

This is what I'm trying to do...
public void mouseClicked(MouseEvent event, int rows, int columns)

This post was edited by MachoMonkey89 on Jun 12 2012 03:36pm
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Jun 12 2012 03:53pm
you can't. that's not how it works.

the way UI code typically works is that you keep track of the difficulty (or rows/columns/bombs) in some form of a shared state (a class variable in the main handler or something), and on the mouse click event handler, you set those variables and call a reload() method on the entire UI (the reload method obviously wipes clean the UI and creates a new one based on the current values of the aforementioned shared state).

if this isn't how your UI code is currently designed... well, it ought to be.

This post was edited by irimi on Jun 12 2012 03:54pm
Member
Posts: 2,388
Joined: Nov 26 2006
Gold: 23.40
Jun 12 2012 04:58pm
Quote (irimi @ Jun 12 2012 05:53pm)
you can't.  that's not how it works.

the way UI code typically works is that you keep track of the difficulty (or rows/columns/bombs) in some form of a shared state (a class variable in the main handler or something), and on the mouse click event handler, you set those variables and call a reload() method on the entire UI (the reload method obviously wipes clean the UI and creates a new one based on the current values of the aforementioned shared state).

if this isn't how your UI code is currently designed... well, it ought to be.


okay thanks.

Now I had my grid working until I wanted to display the bombs remaining at the top.
I created a new panel and set the bombs remaining to a label then added the label to the panel by doing this

Code
bombPanel.add(Jbombs, BorderLayout.NORTH;


and now it displays the bomb count but only one button of the grid, right underneath it. Do I have to change something with the frame to accommodate another panel?

e/ Would I have to follow a pattern and set the grid to CENTER or something?

e2/ And I have no idea how to do what you said. This is my first real project if you haven't already noticed.

This post was edited by MachoMonkey89 on Jun 12 2012 05:08pm
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Jun 12 2012 09:54pm
this is a question specific to JFrame and how to use it, so it's not something I know off the top of my head. Google it or read the documentation/API
Member
Posts: 2,388
Joined: Nov 26 2006
Gold: 23.40
Jun 12 2012 10:06pm
Quote (irimi @ Jun 12 2012 11:54pm)
this is a question specific to JFrame and how to use it, so it's not something I know off the top of my head. Google it or read the documentation/API


Code
       JButton easy = new JButton("EASY");
       JButton medium = new JButton("MEDIUM");
       JButton hard = new JButton("HARD");      
       
       
       easy.addMouseListener(this);
       medium.addMouseListener(this);
       hard.addMouseListener(this);

public void mouseClicked(MouseEvent e)
   {        
       if (e.getSource() == easy)
       {
           rows = 10;
           columns = 10;
           bombs = 10;
           frameX = 250;
           frameY = 250;
       }
       else if (e.getSource() == medium)
       {
           rows = 16;
           columns = 16;
           bombs = 40;
           frameX = 500;
           frameY = 500;
       }
       else if (e.getSource() == hard)
       {
           rows = 16;
           columns = 30;
           bombs = 99;
           frameX = 700;
           frameY = 500;
       }


So I changed it all around and now it can't find the variable "easy". Which is the first button. Where I instantiate the buttons and add the MouseListeners, that is all in the class's constructor. MouseListener is implemented and JFrame is extended.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 13 2012 12:04am
Declare it outside of your method. Your scope is too narrow.
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll