Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (808)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (872)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Keyboard input isn't working  (Read 4049 times)
0 Members and 1 Guest are viewing this topic.
Offline sean333

Senior Newbie





« Posted 2015-07-10 20:17:37 »

I have a very simple game which consists of nothing but a blue rectangle (the paddle) against a red background. The program is made up of three classes: Game, Paddle, and Input. The input allows for both mouse and keyboard functionality, but for some reason only the mouse input is working, not the keyboard. I've reviewed the code several times and can't figure out what is causing the problem. Can anyone check my code and tell me what I'm doing wrong?
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JPanel implements Runnable {
   
   private static final long serialVersionUID = 1L;
   
   private Image dbImage;
   private Graphics dbG;
   
   static Thread thread;
   
   public static final int APPLICATION_WIDTH = 400;
   public static final int APPLICATION_HEIGHT = 300;
   
   private Input input;
   private Paddle player;
   
   public Game() {
      Dimension size = new Dimension(APPLICATION_WIDTH, APPLICATION_HEIGHT);
      setSize(size);
      setPreferredSize(size);
      setFocusable(true);
     
      input = new Input();
      addKeyListener(input);
      addMouseMotionListener(input);
     
      thread = new Thread(this);
     
      player = new Paddle(50, 250);
   }

   @Override
   public void run() {
      while(true) {
         player.update();
         repaint();
         try {
            Thread.sleep(17);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }  
   }
   
   public void update (Graphics g) {
      dbImage = createImage(APPLICATION_WIDTH, APPLICATION_HEIGHT);
      dbG = dbImage.getGraphics();
      paint(dbG);
      dbG.drawImage(dbImage, 0, 0, this);
     
   }

   public void paintComponent (Graphics g) {
      super.paintComponent(g);  
      g.setColor(Color.red);
      g.fillRect(0, 0, APPLICATION_WIDTH, APPLICATION_HEIGHT);
      player.paintComponent(g);  
   }
   
   public static void main(String args[]) {
      Game g = new Game();
      thread.start();
      JFrame frame = new JFrame();
      frame.setResizable(false);
      frame.add(g);
      frame.pack();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
}


import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

public class Input extends KeyAdapter implements MouseMotionListener {
   
   private static boolean[] keys = new boolean[2 << 16];
   private static int mx;

   public void keyPressed(KeyEvent e) {
      keys[e.getKeyCode()] = true;
   }

   public void keyReleased(KeyEvent e) {
      keys[e.getKeyCode()] = false;
   }
   
   public void mouseDragged(MouseEvent e) {
     
   }

   public void mouseMoved(MouseEvent e) {
      mx = e.getX();
   }
   
   public static boolean isKeyPressed(int keycode) {
      return keys[keycode];
   }
   
   public static int getMX() {
      return mx;
   }
}


import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;

public class Paddle {
   
   private int x, y;
   private final int WIDTH = 100;
   private final int HEIGHT = 10;
   
   public Paddle(int x, int y) {
      this.x = x;
      this.y = y;
   }

   public void update() {
      x = Input.getMX();
     
      if (Input.isKeyPressed(KeyEvent.VK_LEFT))
         x -= 10;        
      if (Input.isKeyPressed(KeyEvent.VK_RIGHT))
         x += 10;    
     
   }
   
   public void paintComponent(Graphics g) {
      g.setColor(Color.blue);
      g.fillRect(x, y, WIDTH, HEIGHT);
   }
}
Offline bitbytebytes

Junior Devvie


Medals: 2



« Reply #1 - Posted 2015-07-10 21:04:19 »

Not sure what you exactly mean by not working, but i would think assigning the VK value to mx on keyPressed would cause a problem.  Example: on a left arrow you are assigning VK_LEFT (0x25) to mx then on update you are assigning mx to x and then subtracting 60, you will always get the same result.  Or am I missing something?
Offline sean333

Senior Newbie





« Reply #2 - Posted 2015-07-10 22:38:19 »

I guess what I want is to allow the user to control the paddle with either the mouse or the keyboard.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DarkCart

JGO Kernel


Medals: 124
Projects: 9
Exp: 50 years


It's all in the mind, y'know.


« Reply #3 - Posted 2015-07-11 01:42:33 »

Try adding a setFocusable(true) after the addKeyListener(..) and addMouseMotionListener(..)

The darkest of carts.
Offline sean333

Senior Newbie





« Reply #4 - Posted 2015-07-11 01:47:01 »

Didn't work.
Offline bitbytebytes

Junior Devvie


Medals: 2



« Reply #5 - Posted 2015-07-11 02:16:29 »

Your problem now is that you are resetting x to mx every update.  Mx is the current mouse position, that's why when you let go of the left or right arrow keys the paddle returns to it's previous position(mx).
Offline sean333

Senior Newbie





« Reply #6 - Posted 2015-07-11 02:29:35 »

How can I resolve this?
Offline bitbytebytes

Junior Devvie


Medals: 2



« Reply #7 - Posted 2015-07-11 02:43:28 »

You could create moveRight and moveLeft methods that the player update method could call.  If you press the left arrow call moveLeft, or if the mouse current position is less than the previous position call moveLeft, for example.
Offline sean333

Senior Newbie





« Reply #8 - Posted 2015-07-11 02:51:43 »

Still have the same problem

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;

public class Paddle {
   
   private int x, y;
   private final int WIDTH = 100;
   private final int HEIGHT = 10;
   
   public Paddle(int x, int y) {
      this.x = x;
      this.y = y;
   }

   public void update() {
     
      x = Input.getMX();
     
      if (Input.isKeyPressed(KeyEvent.VK_LEFT))
         moveLeft();  
         
      if (Input.isKeyPressed(KeyEvent.VK_RIGHT))
         moveRight();      
     
   }

   public void moveLeft() {
      x -= 10;
   }
   
   public void moveRight() {
      x += 10;
   }
   
   public void paintComponent(Graphics g) {
      g.setColor(Color.blue);
      g.fillRect(x, y, WIDTH, HEIGHT);
   }
}
Offline bitbytebytes

Junior Devvie


Medals: 2



« Reply #9 - Posted 2015-07-11 02:55:36 »

I'd rather you figure it out yourself so, figure out how to make the mouse trigger the move methods, and quit assigning mx to x.  Use mx to figure out which way you need to move the paddle.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ryukujinishi

Senior Devvie


Medals: 12
Projects: 2



« Reply #10 - Posted 2015-07-11 05:20:47 »

I'd rather you figure it out yourself so, figure out how to make the mouse trigger the move methods, and quit assigning mx to x.  Use mx to figure out which way you need to move the paddle.

I'm with him on having you figure it out on your own. I know these things can be difficult when you're starting out, but your issue is one of logic. You're going about it the wrong way. Basically, you're making the x of the paddle moot. Currently, the actual position of the paddle is entirely reliant on the mouse. Think in relative terms instead of the absolute position of the mouse. Draw it out on paper, write down your thought process. Use a piece of paper and move it your your hand. Whatever will help you get past this. Figuring out this simple issue is very important for your growth as a programmer.

You've made a mistake and that's a good thing. Right now you have something that does not work. So now you're going to either find the correct solution or make another mistake. Eventually with enough mistakes you WILL figure it out. Keep posting your progress even if it still doesn't work. We'll help guide you. When you solve it on your own, it's something you won't forget. And it'll feel very good.
Offline sean333

Senior Newbie





« Reply #11 - Posted 2015-07-11 05:41:47 »

Thank you bitbytebytes and ryukujinishi. I'll keep at it and let you guys know how it goes.
Offline sean333

Senior Newbie





« Reply #12 - Posted 2015-07-11 18:44:28 »

Did it! I simply set the value of mx in both the moveLeft and moveRight functions! What do you guys think?
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;

public class Paddle {
   
   private int x, y;
   private final int WIDTH = 100;
   private final int HEIGHT = 10;
   
   public Paddle(int x, int y) {
      this.x = x;
      this.y = y;
   }

   public void update() {
     
      x = Input.getMX();
     
     
      if (Input.isKeyPressed(KeyEvent.VK_LEFT))
         moveLeft();  
         
      if (Input.isKeyPressed(KeyEvent.VK_RIGHT))
         moveRight();      
     
   }

   public void moveLeft() {
      x -= 10;
      Input.setMx(x);
   }
   
   public void moveRight() {
      x += 10;
      Input.setMx(x);
   }
   
   public void paintComponent(Graphics g) {
      g.setColor(Color.blue);
      g.fillRect(x, y, WIDTH, HEIGHT);
   }
}
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (847 views)
2019-09-04 15:33:17

hadezbladez (5795 views)
2018-11-16 13:46:03

hadezbladez (2603 views)
2018-11-16 13:41:33

hadezbladez (6211 views)
2018-11-16 13:35:35

hadezbladez (1499 views)
2018-11-16 13:32:03

EgonOlsen (4734 views)
2018-06-10 19:43:48

EgonOlsen (5793 views)
2018-06-10 19:43:44

EgonOlsen (3276 views)
2018-06-10 19:43:20

DesertCoockie (4175 views)
2018-05-13 18:23:11

nelsongames (5501 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!