Results 1 to 13 of 13

Thread: Keyboard input

  1. #1

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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 :

    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]

  2. #2
    Guest

    Thumbs up Rename Maze_java.txt to Maze.java

    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);
    }
    }
    });
    Attached Files Attached Files

  3. #3

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Thumbs up

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

  4. #4
    Guest

    Thumbs up

    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/tutor...lestreams.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)

  5. #5

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  6. #6
    Guest

    Wink In Ireland?

    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

  7. #7
    Guest

    Cool How about Telecommuting?

    That way, I'd avoid getting killed.

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

  8. #8

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Yeah whatever you want to do goes basically.
    There are just ****all programmers here.

    - jamie
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  9. #9

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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
    Attached Files Attached Files
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  10. #10
    Guest

    Thumbs up

    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.
    Attached Files Attached Files

  11. #11

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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..)

    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]

  12. #12

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    ... heres the code
    Attached Files Attached Files
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  13. #13
    Guest

    Lightbulb Rename to Maze.java

    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).
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width