I want both players to play at the same time, not in turn. If possible - easier

public class OneClass implements KeyListener, ActionListener{ JFrame frame; JButton button1,button2,button_cel; String s=""; OneClass(){ //Стандарт, можно не читать frame=new JFrame(); frame.setFocusable(true); frame.addKeyListener(this); button1=new JButton(); button2=new JButton(); button1.addActionListener(this); button2.addActionListener(this); button_cel=new JButton(); button_cel.setSize(20, 20); button1.setFocusable(false); button2.setFocusable(false); frame.setLayout(null); frame.add(button1); frame.add(button2); button1.setBackground(Color.RED); button2.setBackground(Color.GREEN); frame.setLocation(0,0); frame.setSize(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height-35); button1.setSize(10,10); button2.setSize(10, 10); button1.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/2, Toolkit.getDefaultToolkit().getScreenSize().height/2-5); button2.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width/2-10, Toolkit.getDefaultToolkit().getScreenSize().height/2-5); frame.setVisible(true); frame.getContentPane().setBackground(new Color(240,240,240)); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ public void run(){ new OneClass(); } }); } //Вот тут-самое интерестное public void keyPressed(KeyEvent e) { button1.setSize(10, 10); button1.setText(""); if(e.getKeyCode()==87){button2.setLocation(button2.getLocation().x, button2.getLocation().y-10);} if(e.getKeyCode()==68){button2.setLocation(button2.getLocation().x+10, button2.getLocation().y);} if(e.getKeyCode()==83){button2.setLocation(button2.getLocation().x, button2.getLocation().y+10);} if(e.getKeyCode()==65){button2.setLocation(button2.getLocation().x-10, button2.getLocation().y);} if(e.getKeyCode()==38){button1.setLocation(button1.getLocation().x, button1.getLocation().y-10);} if(e.getKeyCode()==39){button1.setLocation(button1.getLocation().x+10, button1.getLocation().y);} if(e.getKeyCode()==40){button1.setLocation(button1.getLocation().x, button1.getLocation().y+10);} if(e.getKeyCode()==37){button1.setLocation(button1.getLocation().x-10, button1.getLocation().y);} } public void keyReleased(KeyEvent e) { System.out.println(e.getKeyCode()); } public void keyTyped(KeyEvent e) { } } } 

Here is a jar

Imports missed

  • @iamqwerty, you read this first. And then think about what exactly you are asking. Do you really think that everyone will rush to study this program? - avp
  • Added jar for better "perception" - dddkk

1 answer 1

As I understand it, the problem is that when someone pinches a button, then if at this time someone presses his own, then the first one stops moving?

The point is this. Your code relies on the fact that the system re-generates events if the button is clamped. But there is one feature. The fact is that if you press another button while holding the button pressed, the generation of repeated events for the first button pressed stops.

To get around this you need to act differently. Napimer, you can track which buttons were pressed and which were released. And then on the timer to perform actions.

Here, I sketched in vi a simple example to clarify the idea, so sorry about the formatting. And keep in mind that this is just an example. In life, do not do this ..

 import javax.swing.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.*; import java.util.Set; import java.util.HashSet; public class Main extends JFrame { private Point first = new Point(0,0); private Point second = new Point(1, 0); private Set<String> pressed = new HashSet<>(); public Main() { setSize(800, 600); setDefaultCloseOperation(EXIT_ON_CLOSE); addKeyListener(new KeyListener() { public void keyTyped(KeyEvent e) { } public void keyReleased(KeyEvent e) { pressed.remove(Character.toString(e.getKeyChar())); } public void keyPressed(KeyEvent e) { pressed.add(Character.toString(e.getKeyChar())); } }); Timer t = new Timer(100, new ActionListener() { public void actionPerformed(ActionEvent e) { setTitle(pressed.toString()); boolean repaint = false; if (pressed.contains("a")) { first = add(first, -1, 0); repaint = true; } if (pressed.contains("d")) { first = add(first, 1, 0); repaint = true; } if (pressed.contains("w")) { first = add(first, 0, -1); repaint = true; } if (pressed.contains("s")) { first = add(first, 0, 1); repaint = true; } if (pressed.contains("j")) { second = add(second, -1, 0); repaint = true; } if (pressed.contains("l")) { second= add(second, 1, 0); repaint = true; } if (pressed.contains("i")) { second = add(second, 0, -1); repaint = true; } if (pressed.contains("k")) { second = add(second, 0, 1); repaint = true; } if (repaint) { invalidate(); repaint(); } } }); t.start(); } public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D)g; g2d.translate(50, 50); g2d.scale(16, 16); g2d.setColor(Color.RED); g2d.fillRect((int)first.getX(), (int)first.getY(), 1, 1); g2d.setColor(Color.BLUE); g2d.fillRect((int)second.getX(), (int)second.getY(), 1, 1); } public static void main(String[] args) { new Main().setVisible(true); } private static Point add(Point p, int dx, int dy) { return new Point((int)p.getX() + dx, (int)p.getY() + dy); } } 
  • Thank. I understood, however, not everything, but I’ll read Shildt and understand :-) - dddkk
  • Shieldt will hardly help you in this matter ... well, there will be no ills from reading it. - cy6erGn0m