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

    Default Losing sleep over this

    Hi guys, the title might be a bit of an exaggeration, but I am sweaty and frustrated over this. What I am trying to do is build a program that will make an oval (from paintComponent) jump up 50 y's, and drop 50's a few secs after. So far, I have gotten it to lift (but you have to keep pressing), and then it drops by itself. So, for example, you press space 5 times till it reaches the limit, then it drops. Anyway, the main problem I have had is with getting it to go up alone. I made a keylistener to recognize space. The problem is the location of the keylistener. It cannot be located within the actionPerformed method, since then you cannot add it to the frame. So, I had to locate it outside, and that means I cannot do if you press space, continuously lift the ball 50 y coords over the course of two seconds.
    (Note: I am using a timer, and that is why I was able to get the ball to drop)

    TL;DR How could I make a program that will lift a ball for a brief amount of time and then drop it? (Much like many games where the ball jumps over things, this is the main objective)

    Thanks all

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Losing sleep over this

    Why on earth should you wan to add a KeyListener in an actionPerformed( ... ) method; you should read a bit more on the Swing classes hierarchy, its Event model and understand why it is as it is before you stumble on with all that misery ... b.t.w. you can add a KeyListener to a JFrame because ultimately a JFrame extends a Component ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Losing sleep over this

    Quote Originally Posted by JosAH View Post
    Why on earth should you wan to add a KeyListener in an actionPerformed( ... ) method; you should read a bit more on the Swing classes hierarchy, its Event model and understand why it is as it is before you stumble on with all that misery ... b.t.w. you can add a KeyListener to a JFrame because ultimately a JFrame extends a Component ...

    kind regards,

    Jos
    Hi Jos,

    I wasn't necessarily saying to make a keylistener within actionPerformed. What I was saying was that I am not sure how to get an if statement within actionPerformed saying "if you pressed space, continuously move up by 10 y's a second". I know how to do the 10 y's a second part, but am unsure how to recognize if space was pressed from within actionPerformed. So, I thought the only way to do this could be creating a keylistener IN the actionPerformed, so it could recognize if space was pressed. The problem there is it would be within a method so it cannot be .addKeyListener(); to the frame.

    Thanks for your help

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Losing sleep over this

    An ActionListener has nothing to do with a KeyListener; it handles a 'higher' level event (such as a button press or when a Swing Timer fires); your KeyListener should only change the state of your ActionListener (responsible for handling that Time event), so that the latter stops or starts moving an object on the screen. It would be a sensible thing to create, say, an 'Animator' class that implements the ActionListener interface and has its state made changeable (by the KeyListener).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Losing sleep over this

    Have you thought about something like this?

    Java Code:
    public class MyEventHandlers extends KeyAdapter implements ActionListener {
       boolean spaceBarDown = false;
       // now both listeners have access to the state of the space bar.
      // rest of code includes desired listener methods.  
    }
    Make it an inner class and only use a single instance which may then be an argument to an addKeyListener and an addActionListener method call. Now the value can be set or cleared in the keyPressed/keyReleased methods and the timer has access to its current value in the actionPerformed method.

    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: Losing sleep over this

    jim, I tried your way. The ball is always dropping down, which is good since that is what I want. However, When I press space, it is lifting it by a minimal amount, no matter what I try to do. Also, even though I tell the program to stop pushing it down when space is pressed, it continues to do so. Main problem here is: ball drops, but when space is pressed it only counteracts drop, does not lift. Also, when space is pressed no matter what lift value you put it only lifts by about 5 pixels.

    Here is code:

    Java Code:
     import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    import javax.swing.*;
    public class Ball extends JComponent {
    	int ballX=0;
    	int ballY=490;
    	int ballDiameter=80;
    	int ballXspeed=10;
    	int ballYspeed=10;
    	int ballY2speed=150;
    	public int time=0;
    	public boolean spaceBar=false;
    	public void paintComponent(Graphics g){
    		g.drawString(Integer.toString(time),50,50);
    		g.fillOval(ballX,ballY, ballDiameter, ballDiameter );
    		repaint();
    	}
    	
    	
    		
    	public void setItDown(){
    		if(spaceBar=false){
    			ballYspeed=0;
    			ballY-=ballY2speed;
    		}
    		if(spaceBar=true){
    			ballYspeed=1;
    			ballY+=ballYspeed;
    			
    		}
    	}
    	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){
            	 ballY-=ballYspeed;
            	 spaceBar=false;
            	 repaint();
             }
             else{
            	 spaceBar=false;
             }
    		}
    
    		@Override
    		public void keyReleased(KeyEvent e) {
    		}
    
    	};
    }
    the set it down method is called every 0.1 seconds by a timer.

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Losing sleep over this

    It looks like spaceBar is never set to true (at least, not where it should be). And you are mixing up = and ==? Of course, using == is not necessary for determining boolean state anyway.

    I assume you are using Eclipse, right? Go to preferences-->java-->compiler-->errors/warnings. Under potential programming problems there is a setting you might be interested in (it defaults to ignore). I suggest you set it to 'error'.

    Regards,
    Jim
    Last edited by jim829; 02-02-2014 at 09:27 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Losing data from array - not sure why
    By neveser in forum New To Java
    Replies: 8
    Last Post: 12-02-2012, 07:19 AM
  2. JTabbedPane losing listener
    By mendescholl in forum AWT / Swing
    Replies: 1
    Last Post: 02-03-2012, 03:01 AM
  3. Losing Packets/Bytes using DataInputStream and socket
    By ajordanneve in forum Networking
    Replies: 0
    Last Post: 03-24-2009, 05:22 AM
  4. Problems with JFrame losing the Design view
    By chris@gaiag.net in forum NetBeans
    Replies: 7
    Last Post: 07-23-2008, 07:35 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
  •