Results 1 to 10 of 10
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default How can I access this keylistener if it is within a method?

    I am trying to make a program where a ball moves up continuously when you press space, then begins to move down when you reach a certain point. My method for this is to have a timer, and have a method that does this: When you press space, add 10 y coords every second (using a timer), and if you reach 470 y, then begin to drop 10 y coords. I made a method to hold the keylistener, and am running that method inside the actionPerformed method which is within another class. However, since it is a method, I cannot add my keylistener to my frame in the main method. Does anyone know a fix?

    Thanks

    main

    error line 9
    Java Code:
    import javax.swing.*;
    public class Main {
    
    	public Main(){
    		JFrame frame = new JFrame("Jump");
    		Jump jump = new Jump();
    		frame.add(jump);
            frame.setSize(600,600);
            frame.addKeyListener(jump.k);
            MyListener listener = new MyListener(jump);
    		Timer timer = new Timer(1000,listener);
    		timer.start();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    	public static void main(String[] args) {
    	new Main();
    	}
    
    }
    mylistener
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    
    public class MyListener implements ActionListener {
    private Jump jump;
    public MyListener(Jump jump){
    	this.jump = jump;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
    	jump.JumpIt();
    }
    
    
    }
    Jump class
    Java Code:
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    import javax.swing.*;
    public class Jump extends JComponent  {
    	int xspeed=5;
    	int yspeed=10;
    	int figX = 15;
    	int figY = 510;
    	int diameter = 60;
    	
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		g.fillOval(figX,figY,diameter,diameter);
    		repaint();
    	}
    
    	public void JumpIt(){
        KeyListener k = new KeyListener(){
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {
       int k = e.getKeyCode();
       if(k == KeyEvent.VK_SPACE){
    	   figY-=yspeed;
    	   repaint();
    	   if(figY<400){
    		   figY+=yspeed;
    		   repaint();
    	   }
    	   if(figY==510){
    		   yspeed=0;
    		   repaint();
    	   }
    	   repaint();
       }
       if(k==KeyEvent.VK_D){
    	   figY=figY+10;
       }
    		   
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {		
    		
    	}
    	};
    	}
    	
    }
    Thanks guys

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: How can I access this keylistener if it is within a method?

    What are the names of the methods you are having problems with?
    A method in one class can call a method in another class if it has a reference to the class that the method is in.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: How can I access this keylistener if it is within a method?

    So, the problem I am having is that I am calling the method in the actionPerformed, but cannot add the keylistener to the frame since it is within the method and the method isnt called necessarily

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: How can I access this keylistener if it is within a method?

    What method are you trying to call? And where are you trying to call it from?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: How can I access this keylistener if it is within a method?

    Why are you creating the key listener inside a method which may not be called?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: How can I access this keylistener if it is within a method?

    Quote Originally Posted by jim829 View Post
    Why are you creating the key listener inside a method which may not be called?

    Regards,
    Jim
    Because I need to declare it as an anonymous class so I can set the Key events. If I declare it outside of the method and then initialize within, nothing happens.

  7. #7
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: How can I access this keylistener if it is within a method?

    I am trying to call JumpIt() in the main class. The method is located within the Jump class.

  8. #8
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: How can I access this keylistener if it is within a method?

    If it helps, I displayed the timer and noticed it wasn't working.

  9. #9
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: How can I access this keylistener if it is within a method?

    I cannot figure out why the timer won't start. I looked at an identical program and the timer works perfectly.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: How can I access this keylistener if it is within a method?

    Regarding access to your key listener. Why not just declare it as an instance variable. You can then still create it inside the method. Then you will have access to it outside the method. If it's value is null, then you know it has not been created.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Method can't access arrays
    By Renthalkx in forum New To Java
    Replies: 4
    Last Post: 04-13-2013, 06:01 PM
  2. Replies: 2
    Last Post: 05-11-2011, 06:29 PM
  3. why i cant access a method in a sub class?
    By javanew in forum New To Java
    Replies: 7
    Last Post: 05-03-2010, 06:29 PM
  4. multiple threads access the same method.
    By bhanu in forum New To Java
    Replies: 3
    Last Post: 02-16-2009, 06:54 AM
  5. Method access or field access
    By carderne in forum New To Java
    Replies: 2
    Last Post: 12-06-2008, 06:20 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
  •