Add key input detection

Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com>
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
Nic0Nic0Nii 3 years ago
parent 535348d317
commit db188a76b1
  1. BIN
      bin/RabiClone.jar
  2. 16
      src/sig/RabiClone.java
  3. 24
      src/sig/engine/Panel.java

Binary file not shown.

@ -1,23 +1,37 @@
package sig;
import javax.swing.JFrame;
import java.awt.event.KeyEvent;
import sig.engine.Panel;
public class RabiClone {
public static final String PROGRAM_NAME="Sig's Java Project Template";
public static int UPCOUNT=0;
public static Panel p
public static void main(String[] args) {
JFrame f = new JFrame(PROGRAM_NAME);
Panel p = new Panel(f);
p = new Panel(f);
p.init();
f.add(p);
f.addComponentListener(p);
f.addKeyListener(p);
f.setSize(1280,720);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
p.render();
while (true) {
if (KeyHeld(KeyEvent.VK_UP)) {
System.out.println("Up Held: "+UPCOUNT++);
}
}
}
private static Boolean KeyHeld(int key) {
return p.KEYS.getOrDefault(key,false);
}
}

@ -7,18 +7,21 @@ import java.awt.Toolkit;
import java.awt.image.ColorModel;
import java.awt.image.MemoryImageSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.MouseInputListener;
import java.awt.event.KeyListener;
import sig.RabiClone;
public class Panel extends JPanel implements Runnable,ComponentListener {
public class Panel extends JPanel implements Runnable,ComponentListener,KeyListener {
JFrame window;
public int pixel[];
final int CIRCLE_PRECISION=32;
@ -38,6 +41,7 @@ public class Panel extends JPanel implements Runnable,ComponentListener {
final long TARGET_FRAMETIME = 8333333l;
boolean mouseHeld=false;
java.awt.Point mousePos=new java.awt.Point(0,0);
public HashMap<Integer,Boolean> KEYS = new HashMap<>();
public Panel(JFrame f) {
super(true);
@ -404,4 +408,22 @@ public class Panel extends JPanel implements Runnable,ComponentListener {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
KEYS.put(e.getKeyCode(),true);
//System.out.println("Key List: "+KEYS);
}
@Override
public void keyReleased(KeyEvent e) {
KEYS.put(e.getKeyCode(),false);
//System.out.println("Key List: "+KEYS);
}
}
Loading…
Cancel
Save