Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: help using KeyAdapter
    primarykey
    data
    text
    <p>I'm using java trying to make a basic game but am having some trouble with a KeyAdapter. I've used a very similar format before and thought I understood it, but this has me stumped.</p> <p>Any help would be appreciated, here is the main code I'm working with</p> <pre><code>public class Board extends JPanel implements ActionListener{ Timer timer; Tank tank = new Tank(); boolean boardset; public Board(){ setBackground(Color.BLACK); ImageIcon alien1ii = new ImageIcon(this.getClass().getResource("si_Alien1.png")); Image alien1 = alien1ii.getImage(); ImageIcon alien2ii = new ImageIcon(this.getClass().getResource("si_Alien2.png")); Image alien2 = alien2ii.getImage(); ImageIcon alien3ii = new ImageIcon(this.getClass().getResource("si_Alien3.png")); Image alien3 = alien3ii.getImage(); timer = new Timer(5, this); timer.start(); addKeyListener(new TAdapter()); JButton button = new JButton(new AbstractAction("hello2"){ @Override public void actionPerformed(ActionEvent e){ boardset = false; } }); this.add(button); //actual game setFocusable(true); setDoubleBuffered(true); } public void paint(Graphics g){ super.paint(g); g.setColor(Color.WHITE); Graphics2D g2d = (Graphics2D) g; g2d.drawImage(tank.getTank(), tank.getx(), tank.getY(), this); g2d.drawLine(0, (tank.getY()+25), 400, (tank.getY()+25)); Toolkit.getDefaultToolkit().sync(); g.dispose(); } public class TAdapter extends KeyAdapter{ public void keyPressed(KeyEvent e){ tank.keyPressed(e); System.out.println("pressedddddddddddddddddd"); } public void keyReleased(KeyEvent e){ tank.keyReleased(e); } } public void setBoardset(boolean x){ boardset = x; } public boolean getBoardset(){ return boardset; } @Override public void actionPerformed(ActionEvent e) { repaint(); tank.move(); } } </code></pre> <p>Seems to me like this should be pretty straightforward, right now I'm using this print statement to see if the class is actually recognizing key strokes at all: public class TAdapter extends KeyAdapter{ public void keyPressed(KeyEvent e){ tank.keyPressed(e); System.out.println("pressedddddddddddddddddd"); }</p> <p>However, there is no output. So I suspect it is not recognizing any keystrokes at all. But I can't figure out why. If anybody has any suggestions I would appreciate it. Obviously I have more code I can share if anybody thinks it would be useful in figuring out this bug.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload