PDA

Click to See Complete Forum and Search --> : Keyboard input


plenderj
Mar 5th, 2001, 07:52 AM
Howdy.

Im learning Java at the moment, and I'm making a maze game, in which, imaginatively enough, the user has to try and solve mazes.
Anyway, I'm trying to do the userinput part, all I really need is up down left and right, but I'm not sure how to get keyboard events.
I also need the app to get input from the map.txt file, which contains vertices in the maze.
The file is in the format :
MazeName :My maze
Vertices :1
Vertex :4000,3200,2000,1000
MazeName :My maze2
Vertices :2
Vertex :1600,1600,1600,3000
Vertex :2400,1600,2400,3000

This is the code im using :


import java.awt.*;
import java.awt.event.*;
import java.io.*;
class Maze extends Frame {
static int NumVertices = 128;
static myVertex[] Vertex = new myVertex[129];
static Frame f = new Maze();
public static void main(String[] args) {
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

f.addKeyListener(new KeyAdapter() {
private void processKeyEvent(KeyEvent e) {
f.setTitle("testing");
}
});


f.setSize(300,300);
f.setResizable(false);
f.setBackground(Color.white);
f.setVisible(true);
//f.setTitle("Maze");


}
public void paint(Graphics g) {
g.setColor(Color.red);
for (int i = 0 ; i <= NumVertices ; i++) {
g.drawLine(Vertex[i].x1, Vertex[i].y1, Vertex[i].x2, Vertex[i].y2);
}
}
}

class myVertex {
int x1;
int y1;
int x2;
int y2;
}


Any help is much oblidged.
Thanks,
jamie.

Mar 6th, 2001, 12:01 AM
This should get you going.

addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
int i = e.getKeyCode();
switch(i){
case KeyEvent.VK_UP:
f.setTitle("UP");
break;
case KeyEvent.VK_DOWN:
f.setTitle("DOWN");
break;
case KeyEvent.VK_LEFT:
f.setTitle("LEFT");
break;
case KeyEvent.VK_RIGHT:
f.setTitle("RIGHT");
break;
default:
f.setTitle("The int KeyCode is " + i);
}
}
});

plenderj
Mar 6th, 2001, 01:56 AM
Hey thanks a million, the code is great.

In college we've been given a stupid JavaGently text class which does everything for us.
So you don't happen to have the URL to any good examples on File I/O do you ?

Thanks,
Jamie.

Mar 6th, 2001, 11:34 AM
http://java.sun.com
http://java.sun.com/docs/books/tutorial
http://java.sun.com/docs/books/tutorial/essential
http://java.sun.com/docs/books/tutorial/essential/io
http://java.sun.com/docs/books/tutorial/essential/io/filestreams.html

Use the rightward arrow to navigate the site or you might digress and leave the tutorial subject.
At the top-left and bottom-left of each page.
<-- TOC -->
(Previous, Table of Contents, Next)

plenderj
Mar 6th, 2001, 11:48 AM
Thats great thanks.
By the way, you dont happen to come in pocket-size do you ?
You'd be dead handy to have here :)

- jamie

Mar 6th, 2001, 05:12 PM
How are the career opportunities?

Actually, we're at the point where we have palm-tops that can browse the internet live or with offline viewing software; depending on how much you want to spend.

You are welcome ;)

Mar 7th, 2001, 03:49 PM
That way, I'd avoid getting killed.

It would also fit in well with my "Virtually" motif.

plenderj
Mar 8th, 2001, 01:58 AM
Yeah whatever you want to do goes basically.
There are just ****all programmers here.

- jamie

plenderj
Mar 8th, 2001, 02:04 AM
Hey, could you possibly tell me whats wrong with the code enclosed.
When its parsing the data,
by using the ParseMapData() function, there seems to be domething funny going on.

Its reading from the mazes.maz file, and none of the if statements inside the parsemapdata() function appear to be working.

You'll see what I mean.

Also, do you know how I could modify the input stream code stuff to stop the depreciated error occuring ?

Thanks,
Jamie

Mar 8th, 2001, 02:50 PM
In the ParseMapData method, your string comparisons should use the String.equals() method:
String1.equals(String2)
if (TempString.equals("MazeName :")) {

else if (TempString.equals("Vertex :")) {

Using "==" tests if they refer to the same object, not that the object they refer to has the same data. There is a difference since you have two different objects.

The JDK documentation freely available at java.sun.com shows what class/method to use for deprecated classes/methods.
In this case, I used this form:
BufferedReader in = new BufferedReader(new FileReader("foo.in"));
http://java.sun.com/products/
http://java.sun.com/j2se/1.3/
http://java.sun.com/j2se/1.3/docs.html

Then there was a null pointer with an uninitialized array element; fixed with:
MazeData[CurrentMazeNumber] = new myMaze();
MazeData[CurrentMazeNumber].Name = Data.substring(10);

Also, for better print output, change the 11 to a 10 in
TempString2 = Data.substring(10);
The substring method has a -1 counting technique.

I'd have to look into the painting, because I don't think you are getting anything that you were expecting graphically.

plenderj
Mar 20th, 2001, 03:11 AM
Hiya,

Just wondering if you could take a look at the enclosed code.
Ya see, what I'm trying to do would be the following (in vb..)


Private Type myMaze
Name As String
SomethingElse As Something
Vertices() As myVertex
End Type
Private Type myVertex
x1 As Integer
x2 As Integer
y1 As Integer
y2 As Integer
End Type


But its not really working ...:)
Any ideas ?
Also, is the way im writing the code OK ?

thanks,
jamie.

plenderj
Mar 20th, 2001, 03:14 AM
... heres the code ;)

Mar 25th, 2001, 10:48 AM
You can search for "//EDIT" to see what I changed.

I stopped with the NullPointerException(s) generated from the paint method.

Make sure those values are set when paint is called. You might want to have a boolean test if the values are initialized before applying your paint technique (simple return from paint unless initialized).