import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Demo extends JComponent { private final static int ANCHO = 512; private final static int ALTO = 384; private final static int DIAMETRO = 20; private float x, y; private float vx, vy; public Demo() { setPreferredSize(new Dimension(ANCHO, ALTO)); x = 10; y = 20; vx = 300; vy = 400; } private void fisica(float dt) { x += vx * dt; y += vy * dt; if (vx < 0 && x <= 0 || vx > 0 && x + DIAMETRO >= ANCHO) vx = -vx; if (vy < 0 && y < 0 || vy > 0 && y + DIAMETRO >= ALTO) vy = -vy; } public void paint(Graphics g) { g.setColor(Color.WHITE); g.fillRect(0, 0, ANCHO, ALTO); g.setColor(Color.RED); g.fillOval(Math.round(x), Math.round(y), DIAMETRO, DIAMETRO); } private void dibuja() throws Exception { SwingUtilities.invokeAndWait(new Runnable() { public void run() { paintImmediately(0, 0, ANCHO, ALTO); } }); } public void cicloPrincipalJuego() throws Exception { long tiempoViejo = System.nanoTime(); while (true) { long tiempoNuevo = System.nanoTime(); float dt = (tiempoNuevo - tiempoViejo) / 1000000000f; tiempoViejo = tiempoNuevo; fisica(dt); dibuja(); } }
public static void main(String[] args) throws Exception { JFrame jf = new JFrame("Demo"); jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); jf.setResizable(false); Demo demo1 = new Demo(); jf.getContentPane().add(demo1); jf.pack(); jf.setVisible(true); demo1.cicloPrincipalJuego(); } }