Note that there are some explanatory texts on larger screens.

plurals
  1. POJava last added button allows agent.move, rest dont
    primarykey
    data
    text
    <p>I'm new to java, and have been working on creating a code to get a small picture of a car to move using the keys. My problem is when I add more than 1 buttons to the panel. The function of the button in the code I post is nothing, just prints "button [i] clicked" message. The purpose is for them to read a file and update the locations of the car from the data in the file. This is supposed to be a piece on my Reinforcement Learning project. I thought it would be a good opportunity to learn java since this "graphics package" is not necessary for the project, just a "nice" addition. The code is here:</p> <pre><code>package graphics; public class Board extends JPanel implements ActionListener { private Timer timer; private Agent agent; private String button = "button.png"; private Image image; protected JButton b1; protected JButton b2; protected JButton b3; public Board() { //Keylistener added for the agent to respond to arrow keys addKeyListener(new TAdapter()); agent = new Agent(); timer = new Timer(10, this); //10ms timer calls action performed timer.start(); //This part for the button. ImageIcon i = new ImageIcon(this.getClass().getResource(button)); image = i.getImage(); b1 = new JButton("1", i); b1.setVerticalTextPosition(AbstractButton.CENTER); b1.setHorizontalTextPosition(AbstractButton.LEADING); b1.setActionCommand("Active1"); b2 = new JButton("2", i); b2.setVerticalTextPosition(AbstractButton.CENTER); b2.setHorizontalTextPosition(AbstractButton.LEADING); b2.setActionCommand("Active2"); b3 = new JButton("3", i); b3.setVerticalTextPosition(AbstractButton.CENTER); b3.setHorizontalTextPosition(AbstractButton.LEADING); b3.setActionCommand("Active3"); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); add(b1); add(b2); add(b3); setFocusable(true); setBackground(Color.BLACK); setDoubleBuffered(true); } public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D)g; //Transformations for the agent to be painted based upon its position and orientation AffineTransform trans = new AffineTransform(); trans.rotate(Math.toRadians(agent.getTh()), agent.getX()+64, agent.getY()+64); trans.translate(agent.getX(), agent.getY()); g2d.drawImage(agent.getImage(), trans, this); // Draws agent with said transformations Toolkit.getDefaultToolkit().sync(); g.dispose(); } public void actionPerformed(ActionEvent ae) { b1.setEnabled(true); b2.setEnabled(true); b3.setEnabled(true); if (ae.getActionCommand()=="Active1") { b1.setEnabled(false); b2.setEnabled(true); b3.setEnabled(true); System.out.println("Clicked 1"); agent.reset(); } if(ae.getActionCommand()=="Active2") { b1.setEnabled(true); b2.setEnabled(false); b3.setEnabled(true); System.out.println("Clicked 2"); agent.reset(); } if (ae.getActionCommand()=="Active3") { b1.setEnabled(true); b2.setEnabled(true); b3.setEnabled(false); System.out.println("Clicked 3"); agent.reset(); } agent.move(); repaint(); } private class TAdapter extends KeyAdapter { public void keyReleased(KeyEvent e) { agent.keyReleased(e); } public void keyPressed(KeyEvent e) { agent.keyPressed(e); } } } </code></pre> <p>Now, the problem is this. If I click button 1, or button 2, the other buttons are disabled and it says "Clicked 1 (or 2)" which is fine. But the agent.move() and repaint() aren't invoked. The car doesn't move when I press the keys. If I then click button 3, the other two buttons are disabled and the car moves with the keys.</p> <p>If I add buttons in a different order add(b3); add(b2); add(b1); then the same happens but this time its button 1 that works fine.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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