Simulacion De Bola En Java

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Simulacion De Bola En Java as PDF for free.

More details

  • Words: 222
  • Pages: 2
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(); } }

Related Documents

Simulacion
May 2020 15
Simulacion
October 2019 23
Simulacion
June 2020 15
Simulacion De Un Crucero
October 2019 19