Results 1 to 11 of 11
  1. #1
    JWallace is offline Member
    Join Date
    Aug 2016
    Posts
    4
    Rep Power
    0

    Default New to Java, cant get on screen object to move.

    So I am very new to Java. I finished the codecademy tutorial and am about half way through the tutorial videos on Caveofprogramming.com.
    I decided as a very basic project I would write a program where I could move a circle over a plane on the screen using the arrow keys as a way to develop my skills in Java. I used a how to video I found on youtube (https://www.youtube.com/watch?v=xIqeK2hzx1I) as a basis for how to get on screen objects to respond to me pressing the keyboard. However, I have been unable to get my circle to move at all.

    I would really appreciate any information as to where I went wrong in my code.
    Sorry if I make any formatting errors as this is my first time posting.

    Java Code:
    package Package;
    
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    public class App extends Applet implements Runnable, KeyListener{
    	final int WIDTH=500, HEIGHT=500;
    	Thread thread;
    	HumanCircle p1;
    	public void init(){
    		this.resize(WIDTH, HEIGHT);
    		
    		this.addKeyListener(this);
    		p1 = new HumanCircle(1);
    		thread = new Thread(this);
    		thread.start();
    		
    		
    	}
    	public void paint(Graphics g){
    		g.setColor(Color.blue);
    		g.fillRect(0, 0, WIDTH, HEIGHT);
    		p1.draw(g);
    	}
    	public void update(Graphics g){
    		paint(g);
    		
    	}
    	
    	public void run() {
    		p1.move();
    		
    		for(;;){
    		
    			repaint();
    			
    			try {
    				Thread.sleep(10);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    	
    	public void keyPressed(KeyEvent e) {
    		
    		if(e.getKeyCode() == KeyEvent.VK_UP){
    			p1.setUpAccel(true);
    		}else if(e.getKeyCode() == KeyEvent.VK_DOWN){
    			p1.setDownAccel(true);
    		}
    		if(e.getKeyCode() == KeyEvent.VK_LEFT){
    			p1.setLeftAccel(true);
    		}else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
    			p1.setRightAccel(true);
    		}
    	}
    	
    	public void keyReleased(KeyEvent e) {
    		if(e.getKeyCode() == KeyEvent.VK_UP){
    			p1.setUpAccel(false);
    		}
    		else if(e.getKeyCode() == KeyEvent.VK_DOWN){
    			p1.setDownAccel(false);
    		}
    		if(e.getKeyCode() == KeyEvent.VK_LEFT){
    			p1.setLeftAccel(false);
    		}
    		else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
    			p1.setRightAccel(false);
    		}
    		
    	}
    	
    	public void keyTyped(KeyEvent e) {
    		
    		
    	}
    
    }
    
    package Package;
    
    import java.awt.Graphics;
    
    public interface Circle {
    public void draw(Graphics g);
    public void move();
    public int getY();
    public int getX();
    
    }
    
    package Package;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class HumanCircle implements Circle{
    	
    	double y, yVel, x, xVel;
    	boolean upAccel, downAccel, leftAccel, rightAccel;
    	int player;
    	final double GRAVITY = 0.94;
    	
    	public HumanCircle(int player) {
    		upAccel=false; downAccel=false; leftAccel=false; rightAccel=false;
    		xVel=0; yVel=0;
    		if(player == 1)
    			y=20; 
    		else
    			y=480;
    		if(player == 1)
    			x=20;
    		else
    			x=480;
    	}
    	
    	public void draw(Graphics g) {
    		g.setColor(Color.WHITE);
    		g.fillOval((int)x-20, (int)y-20, 40, 40);
    		
    	}
    
    	public void move() {
    		if(upAccel){
    			yVel -= 3;
    		}
    		else if(downAccel){
    			yVel +=3;
    		}
    		else if(!upAccel && !downAccel){
    			yVel *= GRAVITY;
    		}
    		if(leftAccel){
    			xVel -= 3;
    		}
    		else if(rightAccel){
    			xVel += 3;
    		}
    		else if(!leftAccel && !rightAccel){
    			xVel *= GRAVITY; 
    		}
    		y += yVel;
    		x += xVel;
    	}
    	public void setUpAccel(boolean input){
    		upAccel=input;
    		
    	}
    	public void setDownAccel(boolean input){
    		downAccel=input;
    		
    	}
    	public void setLeftAccel(boolean input){
    		leftAccel=input;
    		
    	}	
    	public void setRightAccel(boolean input){
    		rightAccel=input;
    		
    	}
    	public int getY() {
    		return (int)y;
    		
    	}
    
    	public int getX() {
    		return (int)x;
    		
    	}
    	
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: New to Java, cant get on screen object to move.

    How are you trying to debug the code to see what it is doing? Try adding some print statements in all the important methods to see if they are called and to display the values of the variables that are used to control the ball's position. The print out will show you what the code is doing or NOT doing and help you find the problems.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: New to Java, cant get on screen object to move.

    As much as I like applets (for embedded programs in web pages), I recommend you avoid them and any tutorials that use
    them as a base for learning. There are security problems with them and future browsers are unlikely to support them.

    I would focus on extending JPanel and putting the panel in an instance of JFrame. Override paintComponent, not paint, and you will
    achieve pretty much the same thing. As for debugging, simply follow Norm's advice.

    Note: My suggestions are based on Swing. Others probably favor JavaFX over Swing. But definitely not applets.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: New to Java, cant get on screen object to move.

    Here is what I would do:
    I would wonder where the program starts and which lines of code are called after that.
    Then I would wonder what happens when a key is pressed.
    Where does the position of the circle get changed?
    What happens after the position has been changed?
    Last edited by benji2505; 08-18-2016 at 02:22 AM.

  5. #5
    JWallace is offline Member
    Join Date
    Aug 2016
    Posts
    4
    Rep Power
    0

    Default Re: New to Java, cant get on screen object to move.

    Thank you very much for the response. I added print statements to the code to see what was and wasn't running. I found that the keyPressed keyReleased and keyTyped methods wouldn't print anything. How would I go about fixing this?

    Java Code:
    public void keyPressed(KeyEvent e) {
    		System.out.println("key pressed");
    		if(e.getKeyCode() == KeyEvent.VK_UP){
    			p1.setUpAccel(true);
    			System.out.println("up pressed");
    		}else if(e.getKeyCode() == KeyEvent.VK_DOWN){
    			p1.setDownAccel(true);
    		}
    		if(e.getKeyCode() == KeyEvent.VK_LEFT){
    			p1.setLeftAccel(true);
    		}else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
    			p1.setRightAccel(true);
    		}
    	}
    	
    	public void keyReleased(KeyEvent e) {
    		System.out.println("key released");
    		if(e.getKeyCode() == KeyEvent.VK_UP){
    			p1.setUpAccel(false);
    		}
    		else if(e.getKeyCode() == KeyEvent.VK_DOWN){
    			p1.setDownAccel(false);
    		}
    		if(e.getKeyCode() == KeyEvent.VK_LEFT){
    			p1.setLeftAccel(false);
    		}
    		else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
    			p1.setRightAccel(false);
    		}
    		
    	}
    	
    	public void keyTyped(KeyEvent e) {
    		System.out.println("keytyped");
    		
    	}
    Last edited by Norm; 08-18-2016 at 02:49 PM. Reason: Changed end code tag - / before code

  6. #6
    JWallace is offline Member
    Join Date
    Aug 2016
    Posts
    4
    Rep Power
    0

    Default Re: New to Java, cant get on screen object to move.

    Thank you, If I ever get this one to work for my next project I will try this using Jpanel.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: New to Java, cant get on screen object to move.

    I found that the keyPressed keyReleased and keyTyped methods wouldn't print anything. How would I go about fixing this?
    Does the component with the key listeners have the focus? Try clicking on it first to give it the focus.

    The Component class has some methods for enabling focus and for acquiring focus.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    JWallace is offline Member
    Join Date
    Aug 2016
    Posts
    4
    Rep Power
    0

    Default Re: New to Java, cant get on screen object to move.

    Quote Originally Posted by Norm View Post
    Does the component with the key listeners have the focus? Try clicking on it first to give it the focus.

    The Component class has some methods for enabling focus and for acquiring focus.
    Sorry if this sounds like a stupid question as I am very new to this, but what do you mean by the focus?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: New to Java, cant get on screen object to move.

    Focus is documented in the API doc for the Component class: http://docs.oracle.com/javase/8/docs...Component.html
    and here: https://docs.oracle.com/javase/tutor...isc/focus.html
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: New to Java, cant get on screen object to move.

    This is a good time to mention something about programming. Don't try to use new features all at once in your program. For example,
    write a simple application to experiment with the key listener. Typing in characters, displaying them. Printing messages for different events.

    Same thing for graphics and other classes, capabilities, and listeners. Get familiar with the basics. Then it is much easier to "glue" all that stuff together.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: New to Java, cant get on screen object to move.

    I ran the code here, here is what I did:
    I clicked into the Applet to focus on it (Go Norm!!).
    The KeyListeners fire the event correctly.
    Not knowing what the idea of the movement is (see my former comment), I moved the p1.move() method inside the infinite loop and that gets it moving.

Similar Threads

  1. Replies: 10
    Last Post: 02-18-2014, 10:44 PM
  2. Replies: 8
    Last Post: 08-16-2012, 09:41 PM
  3. move object
    By melonka.sg in forum New To Java
    Replies: 3
    Last Post: 01-06-2011, 06:28 PM
  4. Move from one screen fto other screen
    By jprgmr75 in forum CLDC and MIDP
    Replies: 0
    Last Post: 01-02-2011, 06:32 PM
  5. get the details from GUI screen into an java object.
    By venkatesh09 in forum AWT / Swing
    Replies: 3
    Last Post: 07-28-2009, 12:46 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
  •