Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By frogman6102

Thread: Slick 2D Mouse Click and or original java mouse listener?

  1. #1
    frogman6102 is offline Member
    Join Date
    Nov 2014
    Posts
    2
    Rep Power
    0

    Angry Slick 2D Mouse Click and or original java mouse listener?

    Hello! I'm making a java game in Slick2D. Although, I want to make it so that when I click, it will place a image next to my cursor. I've tried the mousePressed method and it's not reconizing it? I've been trying for about two days now. If someone could provide a example of a Slick2D MousePressed event that would be great. I also was wondering if I tried a original java code mouse listener if it would work and that someone could provide a example. :)
    sajjad_islam likes this.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Slick 2D Mouse Click and or original java mouse listener?

    Why Slick2D? Its developer no longer maintains it for a long time now. A more up to date way of doing games using Java is to use LibGDX, and that has the added benefit of making your game deployable to different environments.

    If you use technology that is still actively developed and used, you'll have far more chance of finding places to ask for help and to find working examples. That being said, I highly recommend you sign up to java-gaming.org where nearly all Java game developers hang out. Including the person who originally developed Slick2D.

    Engines, Libraries and Tools - Java-Gaming.org
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    mice is offline Member
    Join Date
    Nov 2014
    Posts
    3
    Rep Power
    0

    Default Re: Slick 2D Mouse Click and or original java mouse listener?

    You can use this class and call to it from anywhere in your program.

    Just do anywhere in your program:
    Java Code:
    Input.getInput().getMousePos();
    to return the point your mouse is currently at.

    When you place your image you would do:

    Java Code:
    Point p = new Point();
    
    public void draw(Graphics2D g){
    
    if(Input.getInput().leftMouseButton){
    p.setLocation(Input.getInput().getMousePos());
    }
    
    g.drawImage(imageObject, p.x,p.y, null);
    
    }
    In my opinion, if you're going to use other game engines, just come up with your own as you go. I'm doing it just fine, if you need any help just sent me a pm with a link to your post that you need help, or ask in a pm directly.

    Java Code:
    import java.awt.Component;
    import java.awt.Point;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    
    
    public class Input implements MouseListener, MouseMotionListener{
    
    	public  boolean leftMouseButton;
    	public  boolean middleMouseButton;
    	public  boolean rightMouseButton;
    	public  boolean anyMouseButton;
    	public  Point mousePos;
    
    	private static Input input = new Input();
    
    	public static Input getInput(){
    		return input;
    	}
    
    	public void addListeners(Component e){
    		e.addMouseListener(this);
    		e.addMouseMotionListener(this);
    	}
    
    	public void removeListeners(Component e){
    		e.removeMouseListener(this);
    		e.removeMouseMotionListener(this);
    	}
    
    	public Point getMousePos(){
    		return mousePos;
    	}
    	
    	public int getMouseX(){
    		return mousePos.x;
    	}
    	
    	public int getMouseY(){
    		return mousePos.y;
    	}
    
    	@Override
    	public void mouseDragged(MouseEvent e){
    		mousePos = e.getPoint();
    	}
    	
    	@Override
    	public void mouseMoved(MouseEvent e){
    		mousePos = e.getPoint();
    	}
    	
    	@Override
    	public void mouseClicked(MouseEvent e){
    		
    	}
    	
    	@Override
    	public void mouseEntered(MouseEvent e){
    		mousePos = e.getPoint();
    	}
    	
    	@Override
    	public void mouseExited(MouseEvent e){
    		mousePos = e.getPoint();
    	}
    	
    	@Override
    	public void mousePressed(MouseEvent e){
    		
    		if(e.getButton() == 1){
    			leftMouseButton = true;
    		}
    		if(e.getButton() == 2){
    			middleMouseButton = true;
    		}
    		if(e.getButton() == 3){
    			rightMouseButton = true;
    		}
    	}
    	
    	@Override
    	public void mouseReleased(MouseEvent e){
    		if(!anyMouseButton){
    		anyMouseButton = false;
    		}
    		if(e.getButton() == 1){
    			leftMouseButton = false;
    		}
    		if(e.getButton() == 2){
    			middleMouseButton = false;
    		}
    		if(e.getButton() == 3){
    			rightMouseButton = false;
    		}
    	}
    
    }
    Last edited by mice; 11-04-2014 at 05:21 AM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Slick 2D Mouse Click and or original java mouse listener?

    Quote Originally Posted by mice View Post
    ... if you need any help just sent me a pm with a link to your post that you need help, or ask in a pm directly.
    That's totally against the spirit of a forum, which is to share problems and their possible solutions.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    mice is offline Member
    Join Date
    Nov 2014
    Posts
    3
    Rep Power
    0

    Default Re: Slick 2D Mouse Click and or original java mouse listener?

    Quote Originally Posted by DarrylBurke View Post
    That's totally against the spirit of a forum, which is to share problems and their possible solutions.

    db
    And there is a PM button for what.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Slick 2D Mouse Click and or original java mouse listener?

    Quote Originally Posted by mice View Post
    And there is a PM button for what.
    Perhaps you should just pretend it doesn't exist if you have to ask that.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    frogman6102 is offline Member
    Join Date
    Nov 2014
    Posts
    2
    Rep Power
    0

    Default Re: Slick 2D Mouse Click and or original java mouse listener?

    Quote Originally Posted by mice View Post
    You can use this class and call to it from anywhere in your program.

    Just do anywhere in your program:
    Java Code:
    Input.getInput().getMousePos();
    to return the point your mouse is currently at.

    When you place your image you would do:

    Java Code:
    Point p = new Point();
    
    public void draw(Graphics2D g){
    
    if(Input.getInput().leftMouseButton){
    p.setLocation(Input.getInput().getMousePos());
    }
    
    g.drawImage(imageObject, p.x,p.y, null);
    
    }
    In my opinion, if you're going to use other game engines, just come up with your own as you go. I'm doing it just fine, if you need any help just sent me a pm with a link to your post that you need help, or ask in a pm directly.

    Java Code:
    import java.awt.Component;
    import java.awt.Point;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    
    
    public class Input implements MouseListener, MouseMotionListener{
    
    	public  boolean leftMouseButton;
    	public  boolean middleMouseButton;
    	public  boolean rightMouseButton;
    	public  boolean anyMouseButton;
    	public  Point mousePos;
    
    	private static Input input = new Input();
    
    	public static Input getInput(){
    		return input;
    	}
    
    	public void addListeners(Component e){
    		e.addMouseListener(this);
    		e.addMouseMotionListener(this);
    	}
    
    	public void removeListeners(Component e){
    		e.removeMouseListener(this);
    		e.removeMouseMotionListener(this);
    	}
    
    	public Point getMousePos(){
    		return mousePos;
    	}
    	
    	public int getMouseX(){
    		return mousePos.x;
    	}
    	
    	public int getMouseY(){
    		return mousePos.y;
    	}
    
    	@Override
    	public void mouseDragged(MouseEvent e){
    		mousePos = e.getPoint();
    	}
    	
    	@Override
    	public void mouseMoved(MouseEvent e){
    		mousePos = e.getPoint();
    	}
    	
    	@Override
    	public void mouseClicked(MouseEvent e){
    		
    	}
    	
    	@Override
    	public void mouseEntered(MouseEvent e){
    		mousePos = e.getPoint();
    	}
    	
    	@Override
    	public void mouseExited(MouseEvent e){
    		mousePos = e.getPoint();
    	}
    	
    	@Override
    	public void mousePressed(MouseEvent e){
    		
    		if(e.getButton() == 1){
    			leftMouseButton = true;
    		}
    		if(e.getButton() == 2){
    			middleMouseButton = true;
    		}
    		if(e.getButton() == 3){
    			rightMouseButton = true;
    		}
    	}
    	
    	@Override
    	public void mouseReleased(MouseEvent e){
    		if(!anyMouseButton){
    		anyMouseButton = false;
    		}
    		if(e.getButton() == 1){
    			leftMouseButton = false;
    		}
    		if(e.getButton() == 2){
    			middleMouseButton = false;
    		}
    		if(e.getButton() == 3){
    			rightMouseButton = false;
    		}
    	}
    
    }
    It works. It draws the image, but I need the image to stay there. When I move my mouse the image moves along with my mouse. I want it to stay where I put it. I need a way for it to detect to stop placing the image everytime my mouse moves.

Similar Threads

  1. Mouse listener
    By chetoos in forum Advanced Java
    Replies: 3
    Last Post: 06-23-2011, 09:23 PM
  2. mouse click listener from textfield
    By ryanmk54 in forum AWT / Swing
    Replies: 2
    Last Post: 05-11-2011, 02:22 AM
  3. Need Help with Mouse Listener!!!!
    By Doggir in forum New To Java
    Replies: 11
    Last Post: 05-05-2011, 11:48 PM
  4. Mouse Listener for mouse floating over object?
    By Krooger in forum AWT / Swing
    Replies: 1
    Last Post: 11-18-2009, 04:34 AM
  5. Replies: 6
    Last Post: 03-17-2009, 02:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •