Thread: Static Mouse [Cool way of using Mouse]

Results 1 to 5 of 5
  1. #1 Static Mouse [Cool way of using Mouse] 
    Java Game Developer.
    Numeric's Avatar
    Join Date
    Apr 2014
    Posts
    45
    Thanks given
    1
    Thanks received
    9
    Rep Power
    5
    This is a good way to keep your MouseListener methods clean. Its also a good start to something new depending on how you use it. Hope you guys find it useful.

    How To Use:
    Code:
    public void yourloop() {
       while (yourcondition) {
          if (Mouse.isClicked()) {
              // put your code here.
              Mouse.reset();
          }
       }
    }
    or

    Code:
    if (Mouse.isMouseClicked(MouseActions.DRAG)) {
           // put your code here.
           Mouse.reset();
    }
    Declare this somewhere:
    Code:
    private final Mouse mouse = new Mouse();
    Than add this to your GUI code:
    Code:
    addMouseListener(mouse);
    addMouseMotionListener(mouse);

    Create a class called "Mouse.java":

    Code:
    package com.graham.engine;
    
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    
    /**
     * 
     * @author GrahamJr
     * 
     */
    public class Mouse implements MouseListener, MouseMotionListener {
    
    	// private final Game game;
    	public static int MOUSE_ACTION = -1;
    	public static int ABS_X = -1;
    	public static int ABS_Y = -1;
    
    	/**
    	 * public Mouse(final Game _game) { game = _game; }
    	 **/
    
    	public static enum MouseActions {
    		CLICK(1), DRAG(2), RIGHT_CLICK(3);
    		private final int actionIndex;
    
    		private MouseActions(final int _actionIndex) {
    			actionIndex = _actionIndex;
    		}
    
    		public int getActionIndex() {
    			return actionIndex;
    		}
    
    	}
    
    	public static boolean isMouseClicked(final MouseActions CLICK_TYPE) {
    		return CLICK_TYPE.getActionIndex() == MOUSE_ACTION;
    	}
    
    	public static boolean isClicked() {
    		return MOUSE_ACTION > 0;
    	}
    
    	@Override
    	public void mouseDragged(final MouseEvent arg0) {
    		ABS_X = arg0.getX();
    		ABS_Y = arg0.getY();
    		MOUSE_ACTION = MouseActions.DRAG.getActionIndex();
    	}
    
    	@Override
    	public void mouseMoved(final MouseEvent arg0) {
    		ABS_X = arg0.getX();
    		ABS_Y = arg0.getY();
    	}
    
    	@Override
    	public void mouseClicked(final MouseEvent arg0) {
    		ABS_X = arg0.getX();
    		ABS_Y = arg0.getY();
    		MOUSE_ACTION = MouseActions.CLICK.getActionIndex();
    	}
    
    	@Override
    	public void mouseEntered(MouseEvent arg0) {
    
    	}
    
    	@Override
    	public void mouseExited(MouseEvent arg0) {
    
    	}
    
    	@Override
    	public void mousePressed(MouseEvent arg0) {
    
    	}
    
    	@Override
    	public void mouseReleased(MouseEvent arg0) {
    
    	}
    
    	public static void reset() {
    		MOUSE_ACTION = -1;
    	}
    
    }
    Hoped this helped you please leave any comments if you have any suggestions etc...
    Reply With Quote  
     

  2. #2  
    Registered Member
    Join Date
    May 2014
    Posts
    85
    Thanks given
    30
    Thanks received
    14
    Rep Power
    20
    MouseAdapter works as both a MouseListener and MouseMotionListener. Due to the fact that MouseAdapter doesn't force you to implement methods you might not use (due to the fact that its a class, not an interface). Not everyone digs it though, so good job I use this type of structure for key events aswell.

    Code:
    class Keyboard {
         public static boolean[] keys = new boolean[200]; //as many keys as you're tracking
         public static KeyAdapter listener = new KeyAdapter() {
              public void keyPressed(KeyEvent e) {
                   keys[e.getKeyCode()] = true;
              }
    
              public void keyReleased(KeyEvent e) {
                   keys[e.getKeyCode()] = false;
              }
         };
    }
    This way, you can now check if the key is pressed at any time

    Code:
    while(yourcondition) {
         if(Keyboard.keys[KeyEvent.VK_UP])
              //move player up
    }
    Reply With Quote  
     

  3. #3  
    Registered Member
    Join Date
    May 2014
    Posts
    85
    Thanks given
    30
    Thanks received
    14
    Rep Power
    20
    Quote Originally Posted by GrahamSr View Post
    I'd hate you pop your bubbles but both your conventions aren't that great. You would capitalize the field name entirely if it were a static final. And I don't see the point in creating an enum for the mouse like that.

    Mouse

    Code:
    import java.awt.Component;
    import java.awt.Rectangle;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseWheelEvent;
    
    public class Mouse extends MouseAdapter {
    
    	private static final Mouse instance = new Mouse();
    	private static Component component;
    
    	public static int x, y;
    	public static int pressX, pressY, pressedButton;
    	public static int dragX, dragY, draggedButton;
    	public static int wheelRotation;
    
    	public static void listenTo(Component c) {
    		if (component != c) {
    			c.removeMouseListener(instance);
    			c.removeMouseMotionListener(instance);
    			c.removeMouseWheelListener(instance);
    		}
    
    		c.addMouseListener(instance);
    		c.addMouseMotionListener(instance);
    		c.addMouseWheelListener(instance);
    		component = c;
    	}
    
    	public static boolean isWithin(int x0, int y0, int x1, int y1) {
    		return x > x0 && x < x1 && y > y0 && y < y1;
    	}
    
    	public static boolean isWithin(Rectangle r) {
    		return r.contains(x, y);
    	}
    
    	/**
    	 * Called after a game loop so clicks/rotations aren't carried on to the next frame.
    	 */
    	public static void reset() {
    		pressedButton = 0;
    		wheelRotation = 0;
    	}
    
    	@Override
    	public void mouseWheelMoved(MouseWheelEvent e) {
    		wheelRotation = e.getWheelRotation();
    	}
    
    	@Override
    	public void mouseDragged(MouseEvent e) {
    		dragX = e.getX();
    		dragY = e.getY();
    	}
    
    	@Override
    	public void mouseMoved(MouseEvent e) {
    		x = e.getX();
    		y = e.getY();
    	}
    
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		x = e.getX();
    		y = e.getY();
    	}
    
    	@Override
    	public void mousePressed(MouseEvent e) {
    		pressX = e.getX();
    		pressY = e.getY();
    		pressedButton = e.isMetaDown() ? 2 : 1;
    		draggedButton = pressedButton;
    	}
    
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		draggedButton = 0;
    	}
    
    }
    I gotta agree; only constants should be fully capitalized. I also gotta point out, both of you half-assed the singleton pattern. No offense to your code, GrahamSr, but if you're gonna get at GrahamJr for conventions, then I can't just let you go free. You didn't declare a private constructor. You also never null-check when adding listener to components through "listenTo" (possibility of NPE). Although, you are right about enums. No use for them here; useless overhead. Your way is more efficient though, seeing how not only is it more flexible or more controlled, but you didn't leave any methods blank (like GrahamJr did), and you acutally handle mouse wheel events correctly.
    Reply With Quote  
     

  4. #4  
    Registered Member SuperMario's Avatar
    Join Date
    May 2012
    Posts
    329
    Thanks given
    30
    Thanks received
    26
    Rep Power
    5
    Only capitalize if it doesn't change, it's constant. if that's the case declare it final and capitalize.

    EDIT : Shit, wasn't aware of the gravedig.
    Reply With Quote  
     

  5. #5  
    Registered Member Killer 99's Avatar
    Join Date
    Dec 2007
    Posts
    1,480
    Thanks given
    171
    Thanks received
    503
    Rep Power
    414
    Why would you turn something that was previously an asynchronous system into a synchronous one? This can easily have race conditions that would cause mouse events to be lost.
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. [PI] Simple way of using tiaras.
    By phl0w in forum Snippets
    Replies: 12
    Last Post: 01-21-2012, 12:55 AM
  2. Proper way of using System.gc();?
    By Zahhak in forum Help
    Replies: 5
    Last Post: 02-20-2010, 03:02 AM
  3. Add-On to: "Cool way of getting skillcapes"
    By Scu11 in forum Tutorials
    Replies: 36
    Last Post: 10-18-2008, 08:14 PM
  4. Cool way of getting Skill Capes....
    By Zachyboo in forum Tutorials
    Replies: 43
    Last Post: 01-20-2008, 11:30 PM
  5. Othey way of using ckey! "Random book"
    By WolvesSoulZ in forum Tutorials
    Replies: 13
    Last Post: 12-13-2007, 06:29 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •