Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Proper positioning of Listeners.  (Read 1285 times)
0 Members and 1 Guest are viewing this topic.
Offline syrinth

Senior Newbie





« Posted 2012-12-18 03:48:56 »

Hey everyone, I'm sorry to bother you but this is a subject that's been bothering me a lot. I learned in University about Model View Control architecture, and we always had listener classes as separate classes. Every time I see listeners anywhere else though, they are part of the class that they are attached to.

In my game, I have several separate screens all with different buttons, features and controls. Currently I have it so that each Screen is it's own object and there is one listener to deal with everything. I was thinking though that maybe I should have one listener in each Screen class that is specifically programmed to handle that one screen's reactions.

Thoughts?
Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 133
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2012-12-18 03:54:54 »

You use as many listeners as you have to, e.g. one per button(ActionListener) or one per game (KeyListener, there's only one keyboard).
Since you have different buttons and stuff on each screen you obviously want different listeners, otherwise you can't distinguish between buttons. However, for something like mouse/keylistener I would suggest only using it once.
Offline noblemaster

JGO Ninja


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #2 - Posted 2012-12-18 09:58:26 »

Any code samples to post that explain it more in detail what you mean?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline syrinth

Senior Newbie





« Reply #3 - Posted 2012-12-28 00:25:11 »

and wow that was a pathetically protracted time period for my response. Apologies.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
public class DMToolGame extends JPanel implements ActionListener{
   
   /**
    *
    */

   private static final long serialVersionUID = 3711700171154644204L;  
   private Screen mCurrentScreen;
   private MouseListener mListener;
   private KeyListener mKeyListener;
   private double mCurrentFrame, mLastFrame;
   private Timer mTimer;

   public DMToolGame(int width, int height){
      //Screen setup
     setFocusable(true);
      setDoubleBuffered(true);
     
      init();
   }
   
   public void init(){
      mCurrentScreen = new TitleScreen();
      mListener = new MouseListener(this);
     
      mLastFrame = 0;
      mCurrentFrame = 0;
     
      mTimer = new Timer(5, this);
      mTimer.start();
     
      addMouseListener(mListener);
     
      addKeyListener(mKeyListener);
   }


This class does the painting to the canvas and is itself the window. However, all of the objects that you can interact with are stored on various Screens, a character select screen, a battle Screen etc. This class just represents the window, stores which Screen we are currently on and tells it to draw itself.

Currently I have one single MouseListener called mListener as explained

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
public class MouseListener extends MouseAdapter{
   private DMToolGame mTool;
   private Screen mCurrentScreen;
   
   public MouseListener(DMToolGame tool){
      mTool = tool;
      mCurrentScreen = mTool.getCurrentScreen();
   }
     
   public void mousePressed(MouseEvent e){
      Point p = e.getPoint();
         
      ScreenItem intersectObject = mCurrentScreen.objectIntersects(p);
      if(intersectObject != null){
         if(intersectObject.getClass() == Button.class){
            if(intersectObject.getName().equals("Battle")){
               mTool.setCurrentScreen(new BattleScreen());
               mCurrentScreen = mTool.getCurrentScreen();
            }
            else if(intersectObject.getName().equals("Load Campaign")){
               mTool.setCurrentScreen(new CampaignScreen());
               mCurrentScreen = mTool.getCurrentScreen();
            }
            else if(intersectObject.getName().equals("Next Turn")){
               ((BattleScreen)mCurrentScreen).NextTurn();
            }
            else if(intersectObject.getName().equals("Add Character")){
               ((BattleScreen)mCurrentScreen).AddCharacterDialog();
            }
         }
      }
   }
}


Since I'm making this listener generic and do all the work for the various screens, I'm requiring it to do various abstract hurdles to figure out what to do.

I've been trying to make Screen extend JPanel, and then add custom MouseListeners to each of the subScreens *BattleScreen, CampaignScreen etc, but they don't seem to trigger...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
public class TitleScreen extends Screen{
   /**
    *
    */

   private static final long serialVersionUID = 4838620713526543945L;
   private Image mBackground;
   private Button mLoadButton;

   public TitleScreen(){
      mBackground = managers.ImageManager.getInstance().getImage("TitleScreen.png");
      mLoadButton = new Button("Load Campaign", 100, 100);
     
      addMouseListener(new MouseListener());
   }
   
   public void draw(Graphics2D g2d, DMToolGame window) {
      g2d.drawImage(mBackground, 0, 0, window);
      mLoadButton.draw(g2d,  window);
   }
   
   public ScreenItem objectIntersects(Point p) {
      if(mLoadButton.contains(p)){
         return mLoadButton;
      }
      return null;
   }
   
   public class MouseListener extends MouseAdapter{
      public MouseListener(){
         System.out.println("Mcreated!");
      }
      @Override
      public void mouseClicked(MouseEvent e){
            System.out.println("Mouse was clicked");
        }
      @Override
        public void mouseEntered(MouseEvent arg0) {}
      @Override
        public void mouseExited(MouseEvent arg0) {}
      @Override
        public void mousePressed(MouseEvent arg0) {}
      @Override
        public void mouseReleased(MouseEvent arg0) {}
   }
}


It should be noted that I'm not using JButtons or any other Java Objects except for the Canvas so I cant just add listeners to the buttons in question.
Offline noblemaster

JGO Ninja


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #4 - Posted 2012-12-28 18:36:03 »

Yes, you can just add listeners to the JPanel (you don't need a JButton) and it should work fine. Anyhow, generally, your code looks it should work, but maybe there is another panel overlapping that's why mouse clicks aren't registering?

Quote
Currently I have it so that each Screen is it's own object and there is one listener to deal with everything. I was thinking though that maybe I should have one listener in each Screen class that is specifically programmed to handle that one screen's reactions.
I would suggest add one listener to each JPanel/JComponent you want to receive mouse events from. Very hard to maintain if you try to squeeze everything into 1 listener.

Quote
I learned in University about Model View Control architecture, and we always had listener classes as separate classes.
If you consider listeners in the "Control" part that's correct. In my opinion listeners are part of the "View" but not the "Control". The listener calls the "Control" element, e.g. your ((BattleScreen)mCurrentScreen).NextTurn().

Academically speaking, considering the listener a control element is probably correct. I generally found the following works a lot easier for me:
  • Model (model of your world): your game elements, e.g. World, Bullet, Entities, Player
  • Control (to update the model): your game update loop, your screens, loading/saving games etc.
  • View (inputs+outputs): GUI, OpenGL, Listeners, Mouse, Joystick, Sound

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (64 views)
2014-09-21 02:42:18

BurntPizza (34 views)
2014-09-21 01:30:30

moogie (42 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!