Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have recently started learning java.I want to make a game like https://sites.google.com/site/millseagles/home/Games/multiplayer/tron I have made it in c++ once using a simple graphics lib. I have the graphics part down i plan to use small images and use http://horstmann.com/sjsu/graphics/ this basic graphics lib.I can't figure out keyboard input i want it so if you press an arrow the picture adds a small green square(I have a green.png).I can't figure out to use keyboard listeners.I get all these errors.I just need a simple lib that i can say getKey() or something and i can use if() to figure out the action.this is the code I have.I was messing with the key event but don't understand it.

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.*;

 public class game implements KeyListener
 {

public void keyReleased(KeyEvent e){} 

public void keyTyped(KeyEvent e){} 
public game()//snake like game
{

}
public  void test() 
{

    int x=30,y=30;//middle total 60x60

    tile[] map=new tile[3600];//tile is a class i made that is a picture and some int and bool using the simple lib i linked 60 by 60 tiles
    for(int i=0;i<3600;i++)
    {
        map[i]=new tile();
    }


}

 public void keyPressed(KeyEvent e)//this does not work i want it to work when a key is clicked
 { 
     while(x>0)//this part works when it is not in the keypressed function
       {

        map[(y*60)+x].load(4);//4 refrences a green rectangle image
        map[(y*60)+x].draw(x,y,10);//draw it based on x and y 10 pixels sized tiles
       x--;//make a line going left 



       }

} 
}

I know this may be messy.I have tested my code it works it just breaks when i try to implement keyboard events.If you can point me to a much more beginner friendly lib that would be great.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
160 views
Welcome To Ask or Share your Answers For Others

1 Answer

You simply have to add the listener to something (e.g. the window where the game is being played).

I will give you an example, where we will simply display the code of the key being stroked.

This is the class where you produce the interface:

import java.awt.Dimension;
import javax.swing.JFrame;

public class Game {

    public static void main(String[] args) {
        /* Creating a window (300x400) */
        JFrame frame = new JFrame("Add your own title");
        frame.setPreferredSize(new Dimension(300, 400));

        /* This is the part where we add the keyListener (notice that I am also sending
         * this window as a parameter so that the listener can modify it)*/
        frame.addKeyListener(new ArrowListener(frame));

        /* Making the window visible */
        frame.pack();
        frame.setVisible(true);
    }
}

And this is the class where we have the listener:

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class ArrowListener implements KeyListener {
    /* We keep the window as an instance variable so we can modify it once the event is triggered */
    JFrame frame;

    /* This is the constructor */
    public ArrowListener(JFrame j) {
        frame = j;
    }

    /* This is where the magic happens */
    public void keyPressed(KeyEvent e) {
        /* Modify this with what you actually want it to do */

        /* We clear the panel so we can add new text without any other text behind it */
        frame.getContentPane().removeAll();

        /* We add some text that actually shows the keyCode (left arrow = 37, top = 38, right = 39, bottom = 40) */
        frame.add(new JLabel("Key Code #" + String.valueOf(e.getKeyCode())));

        /* Redrawing the window */
        frame.revalidate();
    }

    /* These two are part of the contract we made when we decided to
     * implement the KeyListener */
    public void keyTyped(KeyEvent e) { /* Do nothing */ }
    public void keyReleased(KeyEvent e) { /* Do nothing */ }
}

Note: when running the program press the keys to see the text appearing on the window.

I didn't get around the library you were using, but I used the most popular one called swing (tutorial)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...