Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    pwnagebeef is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default MouseListener problems

    Well i have this project where i have to make a java game for school in case your wondering.. I have so far 2 simple rectangles that are given random cords and h+w settings, im trying to get it so that when the person clicks the moving shape they get a point.. kinda like fruit ninja if you know what that is... Every time the user clicks one or the first time they click one atleast a jframe appears and it adds a point to the scoreboard. Im just trying to get the mouse events to work when the shape is clicked is basically what im confused about, i've done lots of googlen and i didn't find much and i've just been going in circles so i would love it if someone could help me.


    Java Code:
    package gameapplication;
    
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import javax.swing.*;
    import java.awt.event.ActionListener;
    import java.util.Random;
    import java.awt.*;
    import java.awt.Event.*;
    
    
    import javax.swing.Timer;
    
    
    
    
    
    @Sup
    pressWarnings("serial")
    public class gameapp1 extends JPanel implements MouseListener, ActionListener{
    	
    	Random generator = new Random();
    	int newNumber= generator.nextInt(8);
    	int newNumber1= generator.nextInt(100);
    	int newNumber2= generator.nextInt(100);
    	int newNumber3= generator.nextInt(100);
    	int newNumber4= generator.nextInt(500);
    	int n2= generator.nextInt(100);
    	int n3= generator.nextInt(100);
    	int n4= generator.nextInt(100);
    	int n1= generator.nextInt(500);
    	 
    	
    	
    	
    	Timer tm = new Timer(5,this);
    	int x =newNumber4, velX=newNumber, s1 = newNumber1, s2 = newNumber2 , e1 = newNumber3, x1 = n1;
        int together = x&velX&s1&s2&e1&x1;
        public void paintComponent(Graphics g){
        super.paintComponent(g);   
        boolean done;
        g.setColor(Color.RED);
        g.fillRect(e1, x, s1, s2);
        g.setColor(Color.RED);
        g.fillRect(n2, x, n3, n4);
        
    
        
        tm.start();
        
        }
        
        
        public void actionPerformed(ActionEvent e){
        	 if (x < 0 || x > 500)
        	    	velX = -velX;
        	    x = x + velX;
        	  
       
      
        
        repaint();
        }
        
        public int getX() {
    		return 0;
    	}
    	public int getY() {
    		return 0;
    	}
        public void mousePressed(MouseEvent e) {  
        	
        }
        public void mouseReleased(MouseEvent e) {}
        public void mouseClicked(MouseEvent e) { 
        	int letsdoit1 = getX();
        	int letsdoit2 = getY();
        	if (MouseEvent.MOUSE_CLICKED == together){
        		JFrame hello = new JFrame();
        		hello.setSize(300,200);
        		hello.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        		hello.setVisible(true);
        		hello.setResizable(true);
        		
        		
        	}
        }
        
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
    
    public static void main(String[] args) {
    	gameapp1 gm = new gameapp1();
    	JPanel pane = new JPanel();
    	JFrame jf = new JFrame();
    	jf.setSize(800,600);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setResizable(true);        
        jf.setVisible(true);
        
        jf.add(pane);
        jf.add(gm);
        
       
       
        	
        	
        }
       
        }
    Last edited by pwnagebeef; 05-07-2012 at 02:42 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: MouseListener problems


  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: MouseListener problems

    trying to get the mouse events to work when the shape is clicked
    Are the mouse listeners being called? What do you want the code to do in the listeners?
    What does "to work" mean?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    pwnagebeef is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default Re: MouseListener problems

    Quote Originally Posted by Norm View Post
    Are the mouse listeners being called? What do you want the code to do in the listeners?
    What does "to work" mean?
    i don't think they are.. i implemented them at the top class but im not sure how i could add them to the shapes themselves. I want the code in the listeners to tally up when the user clicks the shape. I think i may of confused you, when i meant " to work" i was trying to say, have the shapes be clickable.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: MouseListener problems

    Did you see post#2?

    Are the shapes components that can have listeners added to them?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    pwnagebeef is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default Re: MouseListener problems

    sorry im not really good at this but i believe they can? i mean i added made it paintComponent.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: MouseListener problems

    What Java class do the shapes extend? Have you tried calling its add listener method?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    pwnagebeef is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default Re: MouseListener problems

    the shapes are in the paintComponent class and they are in the main class of gameapp1 which is extended JPanel and implements Actionlistener and the MouseListner.
    i added the listener to my main class down where i made my jframe and added the main class to the jframe
    i still get no results though. Could there be something wrong in my mouseclicked area??

    Java Code:
    public static void main(String[] args) {
    	gameapp1 gm = new gameapp1();
    	JPanel pane = new JPanel();
    	JFrame jf = new JFrame();
    	jf.setSize(800,600);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setResizable(true);        
        jf.setVisible(true);
        jf.setTitle("Game");
        jf.add(pane);
        jf.addMouseListener(gm);
        jf.add(gm);
    MouseClicked area
    Java Code:
       public void mouseClicked(MouseEvent e) { 
        	  int letsdoit1 = getX();
              int letsdoit2 = getY();
              if (MouseEvent.MOUSE_CLICKED == together){
                  JFrame hello = new JFrame();
                  hello.setSize(300,200);
                  hello.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  hello.setVisible(true);
                  hello.setResizable(true);}
        	
    		
        		
        		
        	
        }

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: MouseListener problems

    Did you read post#2? There is a link to the tutorial you need to go read.

    If there is no class for a shape you can not add a listener to it.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    pwnagebeef is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default Re: MouseListener problems

    i skimmed over it but i didn't see anything about shapes? do you mean the one on mouse motion tut?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: MouseListener problems

    What Java class do your shapes extend?

    The tutorial explains how to use a mouse listener.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    pwnagebeef is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default Re: MouseListener problems

    what to do you mean? i know thats a noob question but i don't they extend anything because you can't extend the paintcomponent (Graphics g) method. I looked at their thing but they don't have any shapes, just a blank area that you can click on.

    Java Code:
        public void paintComponent(Graphics g) {
        super.paintComponent(g);   
        g.setColor(Color.RED);
        g.fillRect(e1, x, s1, s2);
        g.setColor(Color.RED);
        g.fillRect(n2, x, n3, n4);    
        
        tm.start();
        
        }

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: MouseListener problems

    You can only add listeners to components/classes.
    paint methods are used by classes to draw shapes, etc.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    pwnagebeef is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default Re: MouseListener problems

    mannn... so will my animation be thrown off when i have to rewrite my shapes code?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: MouseListener problems

    You should step back for a bit and do some design work before writing any code.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    pwnagebeef is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default Re: MouseListener problems

    what do you mean startover?

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: MouseListener problems

    I don't know how far back you need to go. You need to understand how a mouse listener works before going on.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    pwnagebeef is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default Re: MouseListener problems

    ok. This thing is do this coming up friday so :p im trying to find a quick way to do all this. Do you know any good tuts on the shape class thing?

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: MouseListener problems

    I don't know of any tuts on a shape class.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    pwnagebeef is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

Page 1 of 2 12 LastLast

Similar Threads

  1. Help with mouselistener
    By kastraki2000 in forum Java Applets
    Replies: 2
    Last Post: 04-30-2011, 04:56 PM
  2. can't get x and y from mouselistener
    By j2me64 in forum Java 2D
    Replies: 3
    Last Post: 04-24-2010, 05:57 PM
  3. MouseListener & GUI
    By Suurisa in forum New To Java
    Replies: 2
    Last Post: 10-27-2009, 01:52 AM
  4. i need help for MouseListener
    By sfaxianovic in forum New To Java
    Replies: 2
    Last Post: 08-21-2008, 04:30 AM
  5. MouseListener
    By Aswq in forum New To Java
    Replies: 12
    Last Post: 07-18-2008, 09:10 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
  •