Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Incorrect call to class?

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Location
    Sydney, Australia
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Incorrect call to class?

    Hey all,

    I was just wondering if my calling the ship.keyPressed() method from the Main.class is the proper way to implement keyboard controls for the ship object (assuming I wish to use key presses for other than just the ship). This approach works fine though I can't help but feel it's somehow redundant.
    Any feedback would be appreciated.

    // Main.java
    public class Main extends JFrame {
     
    	public class KeyHandler extends KeyAdapter {
    		public void keyPressed(KeyEvent e_) {
    			ship.keyPressed(e_);
    		}
    	}
    }
     
    //-----------------------------------
    // Ship.java
    public class Ship {
     
    	int x, y;
     
    	public Ship(int x_, int y_) {
    		x = x_;
    		y = y_;
    		speed = 1;
    	}
     
    	public void keyPressed(KeyEvent e_) {
    		if (e_.getKeyCode() == e_.VK_W) {
    			y -= speed;
    		}
    		if (e_.getKeyCode() == e_.VK_S) {
    			y += speed;
    		}
    	}
    }


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    590
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: Incorrect call to class?

    What I'd do is something like this:
    public class KeyHandler extends KeyAdapter {
    		public void keyPressed(KeyEvent e_) {
    		    if (e_.getKeyCode() == e_.VK_W) {
    			ship.left();
    		    }
    		    if (e_.getKeyCode() == e_.VK_S) {
    			ship.right();
    		    }
    		}
    	}
    and in Ship:
    public void right(){
        y += speed;
    }

  3. The Following User Says Thank You to PhHein For This Useful Post:

    Drekinn (July 29th, 2013)

  4. #3
    Junior Member
    Join Date
    Jul 2013
    Location
    Sydney, Australia
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Incorrect call to class?

    Ah yes, far more logical and efficient. Thank you.

Similar Threads

  1. Call class method from another class
    By NewbieJavaProgrammer in forum Object Oriented Programming
    Replies: 1
    Last Post: November 21st, 2012, 05:56 AM
  2. Call method(s) within the same class
    By mwr76 in forum Object Oriented Programming
    Replies: 8
    Last Post: September 26th, 2011, 12:58 AM
  3. How do I call a class in a main class
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2011, 03:11 AM
  4. How to call int value from another class
    By IDK12 in forum Java Theory & Questions
    Replies: 7
    Last Post: January 14th, 2011, 08:18 PM
  5. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM

Tags for this Thread