I'm trying to make the classic game of Mini Tennis, but can't seem to overcome this problem...
This is the code:
package java_1; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.JPanel; @SuppressWarnings("serial") public class MiniTennis_5 extends JPanel { Ball ball = new Ball(this); Racquet racquet = new Racquet(this); public MiniTennis_5() { addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { racquet.keyReleased(e); } @Override public void keyPressed(KeyEvent e) { racquet.keyPressed(e); } }); setFocusable(true); } private void move() { ball.move(); racquet.move(); } @Override public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ball.paint(g2d); racquet.paint(g2d); } public static void main(String[] args) throws InterruptedException { JFrame frame = new JFrame("Mini Tennis"); MiniTennis_5 game = new MiniTennis_5(); frame.add(game); frame.setSize(300, 400); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); while (true) { game.move(); game.repaint(); Thread.sleep(10); } } }
I compiled, but there was some kind of error while running it. I've tried debugging it, but I can't find the problem...