Results 1 to 19 of 19
Thread: Java Key Listener Need Help
- 08-01-2011, 04:20 PM #1
- 08-01-2011, 04:23 PM #2
Are you sure you don't want key bindings?
And have you read the KeyListener tutorial?How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 08-01-2011, 06:59 PM #3
What's the difference? Is the key bindings for like "Binding" the "W" key to an event that happens?
Yupp, I've read the KeyListener tutorial. (2 times)
- 08-01-2011, 07:17 PM #4
What does the tutorial say? How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
And what about it did you not understand? If you put together an SSCCE showing where you're stuck, it would be easier to help you.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 08-02-2011, 11:55 AM #5
Still don't understand it.
I would be more than happy if someone can give me a small code of how to use the "W", "A", "S", "D" keys in a game. (The example can just be that when you klick one of the keys, the Frame says (For W) "You Walked Upward", and so on. Thanks!
I'm sorry for not understanding the tutorials. I guess that I'm still a nobie when it comes to making games. But with some help I will learn how you can do. So, thanks again!
Best Regards Alerhau
- 08-02-2011, 12:15 PM #6
Member
- Join Date
- Jul 2011
- Posts
- 53
- Rep Power
- 0
An example so you might better understand it: (using a Japplet and KeyListener)
Java Code:import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JApplet; public class test extends JApplet implements KeyListener { /** * */ private static final long serialVersionUID = 1L; public void init() { addKeyListener(this); } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub int keyCode = e.getKeyCode(); switch( keyCode ) { case KeyEvent.VK_SPACE: System.out.print("You released the SPACEBAR!!!"); break; case KeyEvent.VK_ENTER: System.out.print("You released ENTER!!!"); break; } } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } }Last edited by Reskaillev; 08-02-2011 at 01:09 PM.
- 08-02-2011, 01:03 PM #7
@Reskaillev : Do you really want to print "You pressed ..." in the keyReleased(...) method?
@Alerhau: For your purpose, avoid using a KeyListener and use key bindings as detailed in the linked tutorial.
db
- 08-02-2011, 01:05 PM #8
Member
- Join Date
- Jul 2011
- Posts
- 53
- Rep Power
- 0
- 08-02-2011, 02:59 PM #9
*Reskaillev* Thankyou for the code. I still do not know where to put it in a JFrame code tho.
* Am I gonna put it outside the code class?
(I have one class that opens the window. Also where I have the size of the window and all of that stuff there. Then I have one class for all of the code. That is the "Code" class.)
- 08-02-2011, 03:04 PM #10
^ Direct evidence that spoonfeeding is not helpful. ^
What don't you understand about the tutorials and posted code? What have you tried "to put it in a JFrame code"? What does that mean? What does "outside the code class" mean?How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 08-02-2011, 03:05 PM #11
Also, would be nice with some code of the Key Binding, just to see the difference.
- 08-02-2011, 03:24 PM #12
Still haven't bothered to go through the tutorial, huh? Sometimes I wonder why we waste our time posting links.would be nice with some code of the Key Binding
db
- 08-02-2011, 03:43 PM #13
I have been going through the tutorial a couple of times. But I am a person that need code sometimes in order to learn. So, I would be more than happy if I can get the same code as you wrote (Reskaillev), but instead of doing it in an Applet it would be great with doing it in a JFrame. Thanks!
Alerhau
- 08-02-2011, 03:46 PM #14
I think most people are better at doing their work when somebody else does it for them. That won't help you learn though. What part of the tutorial is confusing?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
-
Ahem, the tutorials have code samples. Learn away.
Much better for you to try to write it yourself and post your attempt here if you run into problems.So, I would be more than happy if I can get the same code as you wrote (Reskaillev), but instead of doing it in an Applet it would be great with doing it in a JFrame. Thanks!
- 08-02-2011, 07:00 PM #16
I've tried it out and I got it working in a new project.
But when I got back to my old one it did'nt work... I'm confused. So I realised that the only way that I can get help now is to post my code.
First, I will tell you some about the code/program. I'm building a small Dwarf Fortress / Minecraft game in 2D. Right now in order to move, you will have to press buttons (JButtons). Now I wanna change so that you can move over the "Grid" as I call it without clicking the buttons (JButtons). But I can't get it working with the KeyListener. At the bottom of the code you can see what I've done so far. Please help me fix the problem. Thanks!
Java Code:public class Gui extends JFrame{ public int xpos = 5; public int ypos = 5; /*Booleans*/ //HATCHETS. public boolean HasHatchet = true; public boolean HasGravelHatchet = false; public boolean HasStoneHatchet = false; public boolean HasIronHatchet = false; public boolean HasSpaceHatchet = false; public boolean HasAdminHatchet = true; //HOES. public boolean HasHoe = false; //PICKAXES. public boolean HasPickaxe = true; public boolean HasGravelPickaxe = false; public boolean HasStonePickaxe = false; public boolean HasIronPickaxe = false; public boolean HasSpacePickaxe = false; public boolean HasAdminPickaxe = true; /*Booleans*/ /*Res*/ //Flooring & Seeds public int Floor_Grass = 0; public int Floor_Stone = 0; //Walls public int Wall_Wood = 0; public int Wall_Stone = 0; public int Wall_Sand = 0; /*Res*/ public Icon[] Spr = {new ImageIcon(getClass().getResource("img\\Character.png")), new ImageIcon(getClass().getResource("img\\Sand.png")), new ImageIcon(getClass().getResource("img\\Stone.png"))}; public Color[] Col = {new Color(255, 220, 155), Color.DARK_GRAY, Color.GRAY, new Color(100,250,30), new Color(70, 15, 15), new Color(40, 55, 5)}; public Panel[][] Grid = new Panel[18][25]; public int[][] Values = {{2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {2, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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id.length, Grid[0].length, 1, 1)); int i=0, x=0; while(i < Grid.length) { while(x < Grid[0].length) { Grid[i][x] = new Panel(Spr[Values[i][x]]); x++; } i++; x=0; } i = 0; x = 0; while(i < Grid.length) { while(x < Grid[0].length) { Center.add(Grid[i][x]); x++; } i++; x=0; } /*Movement & Buttons*/ Grid[xpos][ypos].Sprite.setIcon(Spr[0]); JButton Up = new JButton(" ▲ "); JButton Le = new JButton(" < "); JButton Ri = new JButton(" > "); JButton Do = new JButton(" ▼ "); JLabel Objv = new JLabel(); JPanel Bar = new JPanel(); Bar.setLayout(new GridLayout(13, 1, 0, 0)); Bar.setBackground(Color.WHITE); Bar.add(Up); Bar.add(Le); Bar.add(Ri); Bar.add(Do); Bar.add(Objv); add(Bar, BorderLayout.EAST); //Movement Buttons; Up.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(Values[xpos-1][ypos] == 1) { //SAND: Testing the sand blocks; Grid[xpos][ypos].Sprite.setIcon(Spr[Values[xpos][ypos]]); Grid[xpos-1][ypos].Sprite.setIcon(Spr[0]); xpos-=1; //END: Of the sand; } else if(Values[xpos-1][ypos] == 2) { //STONE: Testing the stone blocks; if(HasPickaxe == true) { try { if(HasGravelPickaxe == true) { Thread.sleep(7000); } else if(HasStonePickaxe == true) { Thread.sleep(5000); } else if(HasIronPickaxe == true) { Thread.sleep(3000); } else if(HasSpacePickaxe == true) { Thread.sleep(1000); } else if(HasAdminPickaxe == true) { Thread.sleep(0); } Wall_Stone += 1; Values[xpos-1][ypos] = 1; Grid[xpos-1][ypos].Sprite.setIcon(Spr[1]); }catch(Exception E){} } //END: Of the stone; } else if(Values[xpos-1][ypos] == 3) { //GrassTestWithAndOutHoe if(HasHoe == true) { Values[xpos-1][ypos] = 4; } Grid[xpos][ypos].setBackground(Col[Values[xpos][ypos]]); Grid[xpos-1][ypos].setBackground(Color.BLACK); xpos-=1; } else if(Values[xpos-1][ypos] == 4) { //DirtTest Grid[xpos][ypos].setBackground(Col[Values[xpos][ypos]]); Grid[xpos-1][ypos].setBackground(Color.BLACK); xpos-=1; } else if(Values[xpos-1][ypos] == 5) { //WoodTestChopDownAndCollision if(HasHatchet == true) { try{ if(HasStoneHatchet == true) { Thread.sleep(6000); } else if(HasIronHatchet == true) { Thread.sleep(4000); } else if(HasSpaceHatchet == true) { Thread.sleep(2000); } else if(HasAdminHatchet == true) { Thread.sleep(0); } } catch(Exception E) {} Wall_Wood++; Values[xpos-1][ypos] = 4; Grid[xpos-1][ypos].setBackground(Col[Values[xpos-1][ypos]]); } } } }); Le.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(Values[xpos][ypos-1] == 1) { } else if(Values[xpos][ypos-1] == 0) { Grid[xpos][ypos].setBackground(Col[Values[xpos][ypos]]); Grid[xpos][ypos-1].setBackground(Color.BLACK); ypos-=1; } else if(Values[xpos][ypos-1] == 3) { if(HasHoe == true) { Values[xpos][ypos-1] = 4; } Grid[xpos][ypos].setBackground(Col[Values[xpos][ypos]]); Grid[xpos][ypos-1].setBackground(Color.BLACK); ypos-=1; } else if(Values[xpos][ypos-1] == 4) { Grid[xpos][ypos].setBackground(Col[Values[xpos][ypos]]); Grid[xpos][ypos-1].setBackground(Color.BLACK); ypos-=1; } else if(Values[xpos][ypos-1] == 5) { if(HasHatchet == true) { try{ if(HasStoneHatchet == true) { Thread.sleep(6000); } else if(HasIronHatchet == true) { Thread.sleep(4000); } else if(HasSpaceHatchet == true) { Thread.sleep(2000); } else if(HasAdminHatchet == true) { Thread.sleep(0); } } catch(Exception exept) {} Wall_Wood++; Values[xpos][ypos-1] = 4; Grid[xpos][ypos-1].setBackground(Col[Values[xpos][ypos-1]]); } } } }); Ri.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(Values[xpos][ypos+1] == 1) { } else if(Values[xpos][ypos+1] == 0) { Grid[xpos][ypos].setBackground(Col[Values[xpos][ypos]]); Grid[xpos][ypos+1].setBackground(Color.BLACK); ypos+=1; } else if(Values[xpos][ypos+1] == 3) { if(HasHoe == true) { Values[xpos][ypos+1] = 4; } Grid[xpos][ypos].setBackground(Col[Values[xpos][ypos]]); Grid[xpos][ypos+1].setBackground(Color.BLACK); ypos+=1; } else if(Values[xpos][ypos+1] == 4) { Grid[xpos][ypos].setBackground(Col[Values[xpos][ypos]]); Grid[xpos][ypos+1].setBackground(Color.BLACK); ypos+=1; } else if(Values[xpos][ypos+1] == 5) { if(HasHatchet == true) { try{ if(HasStoneHatchet == true) { Thread.sleep(6000); } else if(HasIronHatchet == true) { Thread.sleep(4000); } else if(HasSpaceHatchet == true) { Thread.sleep(2000); } else if(HasAdminHatchet == true) { Thread.sleep(0); } } catch(Exception exept) {} Wall_Wood++; Values[xpos][ypos+1] = 4; Grid[xpos][ypos+1].setBackground(Col[Values[xpos][ypos+1]]); } } } }); Do.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(Values[xpos+1][ypos] == 1) { } else if(Values[xpos+1][ypos] == 0) { Grid[xpos][ypos].setBackground(Col[Values[xpos][ypos]]); Grid[xpos+1][ypos].setBackground(Color.BLACK); xpos+=1; } else if(Values[xpos+1][ypos] == 3) { if(HasHoe == true) { Values[xpos+1][ypos] = 4; } Grid[xpos][ypos].setBackground(Col[Values[xpos][ypos]]); Grid[xpos+1][ypos].setBackground(Color.BLACK); xpos+=1; } else if(Values[xpos+1][ypos] == 4) { Grid[xpos][ypos].setBackground(Col[Values[xpos][ypos]]); Grid[xpos+1][ypos].setBackground(Color.BLACK); xpos+=1; } else if(Values[xpos+1][ypos] == 5) { //WoodTestChopDownAndCollision if(HasHatchet == true) { try{ if(HasStoneHatchet == true) { Thread.sleep(6000); } else if(HasIronHatchet == true) { Thread.sleep(4000); } else if(HasSpaceHatchet == true) { Thread.sleep(2000); } else if(HasAdminHatchet == true) { Thread.sleep(0); } } catch(Exception exept) {} Wall_Wood++; Values[xpos+1][ypos] = 4; Grid[xpos+1][ypos].setBackground(Col[Values[xpos+1][ypos]]); } } } }); addKeyListener(new KeyListener() { @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub if(e.getKeyCode() == KeyEvent.VK_W) { if(Values[xpos-1][ypos] == 1) { //SAND: Testing the sand blocks; Grid[xpos][ypos].Sprite.setIcon(Spr[Values[xpos][ypos]]); Grid[xpos-1][ypos].Sprite.setIcon(Spr[0]); xpos-=1; //END: Of the sand; } else if(Values[xpos-1][ypos] == 2) { //STONE: Testing the stone blocks; if(HasPickaxe == true) { try { if(HasGravelPickaxe == true) { Thread.sleep(7000); } else if(HasStonePickaxe == true) { Thread.sleep(5000); } else if(HasIronPickaxe == true) { Thread.sleep(3000); } else if(HasSpacePickaxe == true) { Thread.sleep(1000); } else if(HasAdminPickaxe == true) { Thread.sleep(0); } Wall_Stone += 1; Values[xpos-1][ypos] = 1; Grid[xpos-1][ypos].Sprite.setIcon(Spr[1]); }catch(Exception E){} } //END: Of the stone; } else if(Values[xpos-1][ypos] == 3) { //GrassTestWithAndOutHoe if(HasHoe == true) { Values[xpos-1][ypos] = 4; } Grid[xpos][ypos].setBackground(Col[Values[xpos][ypos]]); Grid[xpos-1][ypos].setBackground(Color.BLACK); xpos-=1; } else if(Values[xpos-1][ypos] == 4) { //DirtTest Grid[xpos][ypos].setBackground(Col[Values[xpos][ypos]]); Grid[xpos-1][ypos].setBackground(Color.BLACK); xpos-=1; } else if(Values[xpos-1][ypos] == 5) { //WoodTestChopDownAndCollision if(HasHatchet == true) { try{ if(HasStoneHatchet == true) { Thread.sleep(6000); } else if(HasIronHatchet == true) { Thread.sleep(4000); } else if(HasSpaceHatchet == true) { Thread.sleep(2000); } else if(HasAdminHatchet == true) { Thread.sleep(0); } } catch(Exception E) {} Wall_Wood++; Values[xpos-1][ypos] = 4; Grid[xpos-1][ypos].setBackground(Col[Values[xpos-1][ypos]]); } } } } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } }); /*Movement*/ } }
- 08-02-2011, 07:03 PM #17
That was my "Gui" class. I also have an "Engine" class that runs the "Gui".
- 08-02-2011, 07:05 PM #18
Note... The "XPOS" & "YPOS" is working in the wrong directions! O.O (Nothing to worry about.)
Last edited by Alerhau; 08-02-2011 at 07:16 PM.
- 08-02-2011, 07:39 PM #19
You'll have much better luck if you boil that down to an SSCCE like you were asked. People don't really have time to wade through that much code.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
GIOVYNET Java Serial Comm event listener
By Edudlc in forum New To JavaReplies: 5Last Post: 05-17-2011, 02:38 PM -
Java Addition Program Help with Action Listener
By Syanara in forum New To JavaReplies: 21Last Post: 05-12-2011, 07:03 PM -
Java SOAP Listener
By njitram in forum Advanced JavaReplies: 1Last Post: 01-29-2011, 09:59 PM -
java Code to start MDB Listener
By ganeshkumarm in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 01-20-2011, 12:45 AM -
Adding listener to non-Java object?
By cruxblack in forum Advanced JavaReplies: 5Last Post: 07-30-2007, 02:19 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks