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 :
Code:
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.
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
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);
}
}
});
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.
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
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)
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.
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
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
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.
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.
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..)
Code:
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.
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
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).