Results 1 to 4 of 4
  1. #1
    haldur is offline Member
    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    Default Own class with keylsitener

    Hi,

    Im trying to make a small sidescrolling game with a JFrame and a "GamePanel extends JPanel".
    The Idea is to make the whole game in an overridden paintComponent-method in GamePanel.

    Have another class character with a method paintCharacter(Graphics g).
    I want a keylistener togheter with this class so if arrowkeys are pressed,
    the Player-class itself can automatically change its positions.
    The only way I know of to add keylisteners is to extend JPanel and addKeyListener.

    Are there any obvious flaws with my choice of design her? If so, you're welcome to tell me.

    Regards // Haldur

    public class Player {
    private int posX, posY;

    public void paintChar(Graphics g) { // }

    private class KeyList implements KeyListener {
    public void keyTyped(KeyEvent e) {}
    public void keyPressed(KeyEvent e) { // if left, right, up or down, change posX or posY}
    public void keyReleased(KeyEvent e) {}
    }
    }
    Last edited by haldur; 12-01-2013 at 04:29 PM.

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

    Default Re: Own class with keylsitener

    The classname should NOT be the same as an existing class in Java SE.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    haldur is offline Member
    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    Default Re: Own class with keylsitener

    Quote Originally Posted by Norm View Post
    The classname should NOT be the same as an existing class in Java SE.
    That depends, its not in the same package. Java SE itself has several Timer-classes (in different packages).

    I am more interested in comments regarding the whole keylistener-implementation thank you.

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

    Default Re: Own class with keylsitener

    Yes different packages allow the classes to have the same name. However it is confusing for anyone reading the code to see a well known Java SE class being used strangely. To prevent confusion, use a unique name.

    keylistener-implementation
    Sorry, I don't see any code in the key listeners.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: 12-13-2012, 03:06 AM
  2. Replies: 1
    Last Post: 11-29-2012, 09:46 AM
  3. Replies: 0
    Last Post: 12-07-2011, 11:55 AM
  4. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  5. Replies: 5
    Last Post: 01-26-2011, 06:38 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
  •