jueves, 24 de junio de 2010

PRACTICA 2: Juegos con figuras


Hola a todos!
Para aplicar lo aprendido en la clase intente hacer un pequeno juego tipo PAC-MAN, solo que mas arcaico.
NO he podido implementar correctamente el codigo, aun tiene algunos errores, pero esto es lo que intento implementar:
" En la ventana se dibuja en circulo amarillo y la pantalla esta llena de pequenas pastillas, el juego termina cuando se toman todas las pastillas. "

Otro de mis objetivos es implementar RUNNABLE en el circulo amarillo para que se mueva solito ^_^

Este es el codigo fuente:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class Objeto extends JPanel implements KeyListener, ActionListener {

private double x;
private double y;
private double ancho;
private double altura;

private static final double PASO = 0.02;
private static final double CAMBIO = 0.01;
//private static final double MIN = 0.05;
//private static final double MAX = 0.95;

public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("SALIR")) {
System.exit(1);
} else if (cmd.equals("RESET")) {
this.repaint();
}
return;
}

private void dibujaObjeto(Graphics2D p, int ancho, int altura) {
p.setColor(Color.YELLOW);
p.fill(new Ellipse2D.Double(this.x*ancho,
this.y*altura,
this.ancho*ancho,
this.altura*altura));
return;
}

public void paint(Graphics g) {
double i, j;
super.paint(g);
g.setColor(Color.WHITE);
for(i=0.1; i<1.0; i+=0.1) {
for(j=1.0; j>=0.1; j-=0.1) {
((Graphics2D)g).fill(new Ellipse2D.Double(i*super.getWidth(),j*super.getHeight(),0.02*super.getWidth(),0.02*super.getHeight()));
}
}
this.dibujaObjeto((Graphics2D)g, super.getWidth(), super.getHeight());
return;
}

private void reset() {
this.x = 0.0;
this.y = 0.0;
this.ancho = 0.05;
this.altura = 0.05;
return;
}

public Objeto() {
super();
this.reset();
System.out.println("BIENVENIDO");
}

public void keyPressed(KeyEvent e) {
int c = e.getKeyCode();
switch (c) {
case KeyEvent.VK_DOWN:
this.y += Objeto.PASO;
break;
case KeyEvent.VK_UP:
this.y -= Objeto.PASO;
break;
case KeyEvent.VK_LEFT:
this.x -= Objeto.PASO;
break;
case KeyEvent.VK_RIGHT:
this.x += Objeto.PASO;
break;
}

if (this.x < 0) {
this.x = 0.0;
} else if (this.x > 0.9) {
this.x = 0.9;
}

if (this.y < 0) {
this.y = 0.0;
} else if (this.y > 0.9) {
this.y = 0.9;
}

super.repaint();
return;
}

public void keyReleased(KeyEvent e) {
return;
}

public void keyTyped(KeyEvent e) {
return;
}

public static void main (String[] args) {
JFrame ventana = new JFrame();
ventana.setTitle("Practica con Objetos 2D");
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana.setLocation(100, 200);
ventana.setSize(500, 500);

Objeto o = new Objeto();
o.setBackground(Color.BLACK);

JPanel p1 = new JPanel();
p1.setFocusable(true);
p1.setLayout(new BorderLayout());

JPanel p2 = new JPanel();
p2.setLayout(new GridLayout());

JButton b = new JButton("Reset");
b.setActionCommand("RESET");
b.addKeyListener(o);
b.addActionListener(o);
p2.add(b);
b = new JButton("Exit");
b.setActionCommand("SALIR");
p2.add(b);
b.addActionListener(o);

p1.addKeyListener(o);
p1.add(o, BorderLayout.CENTER);
p1.add(p2, BorderLayout.SOUTH);

ventana.setContentPane(p1);
ventana.setVisible(true);

return;
}
}


DESCARGA: Objeto.java

1 comentario: