Results 1 to 10 of 10
  1. #1
    TOAA is offline Member
    Join Date
    Jun 2015
    Posts
    8
    Rep Power
    0

    Default help with adding a key listener in my game

    Im starting small with this Java game, i want one of the pixels to move at my commands, im having trouble adding in code because:
    1. i dont know where to place the key listener
    2. im not sure which code to use



    package package1;

    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.util.Scanner;

    import javax.swing.JFrame;

    public class Game extends Canvas implements Runnable {

    /**
    *
    */
    private static final long serialVersionUID = 2141238736248682380L;


    JFrame frame; //window of the game

    public final int width = 2560, height = 1440; //width and height of window
    public final Dimension gameSize = new Dimension(width, height); // condense width/height into 1 variable
    public final String TITLE = "Pong InDev";



    boolean gameRunning = false; //whether the game is running

    public void run() {
    while (gameRunning) { //if gameRunning = true, then...

    }
    }

    public Game() { //main method of class 'Game'


    frame = new JFrame();

    this.setMinimumSize(gameSize);
    this.setPreferredSize(gameSize);
    this.setMaximumSize(gameSize);

    frame.add(this, BorderLayout.CENTER);
    frame.pack();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setVisible(true);;
    frame.setResizable(false);
    frame.setTitle(TITLE);
    frame.setLocationRelativeTo(null);





    }

    public void paint(Graphics g) { //main part of program, pixels are being drawn with the default color black
    g.drawRect(0,0,0,0);

    try {
    Thread.sleep(100); //1000 milliseconds is one second.
    } catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
    }

    g.drawRect(1, 0, 0, 0);

    try {
    Thread.sleep(100); //1000 milliseconds is one second.
    } catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
    }
    g.drawRect(0,1,0,0);



    }




    public static void main(String[] args) { // main purpose of these lines are to run the JFrame and ask for user input so that
    //something happens with the pixels, but i dont want this, i want a key listener
    // so that if i press 'ENTER' key my pixel moves to another location
    char option;
    Scanner in = new Scanner(System.in);

    System.out.println("Please enter any character:");
    option=in.next().charAt(0);

    switch (option){
    case 'h':
    System.out.println("You won 32 inch LCD");
    break;


    }
    new Game(); //without this the program wont run
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: help with adding a key listener in my game

    Before you go on: don't use a Canvas in Swing applications; use a JPanel instead. A Canvas is an AWT component and it doesn't play by Swing's rules.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    TOAA is offline Member
    Join Date
    Jun 2015
    Posts
    8
    Rep Power
    0

    Default Re: help with adding a key listener in my game

    thanks~ what code do i use for a JPanel key listener? thanks

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: help with adding a key listener in my game

    Quote Originally Posted by TOAA View Post
    thanks~ what code do i use for a JPanel key listener? thanks
    Carefully read the API documentation for the JPanel class: it directly extends a JComponent which extends a Container which in turn extends a Component. You can add KeyListeners to a Component, so you can add them to a JPanel (a derived class is a base class).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: help with adding a key listener in my game

    Moved from New to Java to AWT/Swing.

    Swing is designed to work with key bindings. Check out the tutorial: How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

    Also go through BB Code List - Java Programming Forum - Learn Java Programming before you post any more code here.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: help with adding a key listener in my game

    Quote Originally Posted by JosAH View Post
    You can add KeyListeners to a Component, so you can add them to a JPanel (a derived class is a base class).
    And that won't work until you setFocusable(true) because by default, a JPanel is not focusable.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: help with adding a key listener in my game

    TOAA, did you lose interest in http://www.java-forums.org/new-java/...sess-java.html?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: help with adding a key listener in my game

    Quote Originally Posted by DarrylBurke View Post
    And that won't work until you setFocusable(true) because by default, a JPanel is not focusable.
    That shows (once again) how little I know about Swing *darn* (btw, I noticed that the compiler only generates code for the implicit method in those lambda expressions (and no more generated separate class files), but it
    uses the 'invokedynamic' instruction to jump to that code; there's a lot to study for me again ;-)

    kind regards,

    Jos (<-- rambling on a Sunday)
    Build a wall around Donald Trump; I'll pay for it.

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

    Default Re: help with adding a key listener in my game

    TOAA - Clean up your names and Strings. Another post like this and you'll be banned.
    Your last post has been unapproved.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: help with adding a key listener in my game

    User banned for profanity.
    Thread closed.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Adding a mouse listener to my Draughts game
    By MXA92 in forum AWT / Swing
    Replies: 4
    Last Post: 03-23-2012, 12:45 AM
  2. adding action listener
    By droidus in forum AWT / Swing
    Replies: 11
    Last Post: 01-23-2012, 03:34 AM
  3. Adding a key listener problem
    By trishtren in forum New To Java
    Replies: 3
    Last Post: 06-02-2011, 02:49 PM
  4. Adding a listener
    By IWI in forum Advanced Java
    Replies: 0
    Last Post: 12-31-2010, 03:05 PM
  5. CheckedTreeDialog adding Handler/Listener
    By negroscuro in forum SWT / JFace
    Replies: 5
    Last Post: 03-30-2009, 06:30 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
  •