Título: Estadio de futbol
Publicado por: Burnhack en 24 Marzo 2008, 01:19 am
Bueno aqui dejo mi code , de mi primer applet . Un estadio de futbol con unas pelotas rebotando por el campo. Ya aplique varias mejoras, ahora simplemente le falta una que es la mejor, que las pelotas reboten entre si...espero que disfruteis de la aplicacion y intenteis completar la mejora Saludos import java.applet.Applet; import java.awt.Graphics; import java.awt.Image;
public class Futbol extends Applet implements Runnable {
Image campo, aux; Balon balon, balon1, balon2, balon3; Thread t; public void init(){ setSize (640, 407); campo = getImage(getCodeBase(), "img/campo.png"); aux = createImage(640,407); balon = new Balon (getImage (getCodeBase(), "img/balon0.png"), 270, 153, 5, 2 * Math.PI * 45 / 360, this); balon1 = new Balon (getImage (getCodeBase(), "img/balon1.png"), 0, 0, 5, 2 * Math.PI * 307 / 360, this); balon2 = new Balon (getImage (getCodeBase(), "img/balon2.png"), 0, 300, 5, 2 * Math.PI * 103 / 360, this); balon3 = new Balon (getImage (getCodeBase(), "img/balon3.png"), 540, 0, 5, 2 * Math.PI * 200 / 360, this);}
public void start(){ t = new Thread(this); if(t != null) t.start(); } public void run (){ while (true){ repaint (); balon.mover(0, 0, 639, 406);//*Blanco y negro movimiento balon1.mover(0, 0, 639, 406);//*Madrid movimiento balon2.mover(0, 0, 639, 406);//*Athletico de madrid movimiento balon3.mover(0, 0, 639,406 );//*Barça movimiento try{ Thread.sleep(40);//*Velocidad/ }catch (Exception e){ } } } public void update(Graphics g){ paint (g); } public void paint (Graphics g){ aux.getGraphics().drawImage(campo, 0, 0, this); balon.paint(aux.getGraphics()); balon1.paint(aux.getGraphics()); balon2.paint(aux.getGraphics()); balon3.paint(aux.getGraphics()); g.drawImage(aux, 0, 0, this); } }
Título: Re: Estadio de futbol [By Burnhack]
Publicado por: Burnhack en 24 Marzo 2008, 01:20 am
Y esta es la clase balon jejeje import java.awt.Graphics; import java.awt.Image; import java.awt.image.ImageObserver;
public class Balon { int x, y, vx, vy, v; double d; Image img; ImageObserver io; public Balon(Image img, int x, int y,int v, double d, ImageObserver io){ this.x = x; this.y = y; this.v = v; this.d = d; this.img = img; this.io = io; vx = v - (int)Math.round(Math.cos(d)); vy = v - (int)Math.round(Math.sin(d)); } public void mover (int minX,int minY,int maxX,int maxY){ x += vx; y += vy; if (x + img.getWidth(io) > maxX){ x = maxX-img.getWidth(io) ; vx = -vx; }else if (x < 0){ x = 0; vx = -vx; }
if (y + img.getHeight(io)> maxY){ y = maxY-img.getHeight(io); vy = -vy; }else if (y < 0){ y = 0; vy = -vy; } } public void paint (Graphics g){ g.drawImage(img, x, y, io); } }
Título: Re: Estadio de futbol [By Burnhack]
Publicado por: Burnhack en 24 Marzo 2008, 01:24 am
Necesitareis descargaros estas imagenes que son las de los balones, aunque son bajadas de internet, las transforme en un png para la transparencia de capas. Y bueno espero que os guste esta entretenida animacion ajaja
Saludos
http://www.megaupload.com/es/?d=IX2RO8BC http://rapidshare.com/files/101853080/img.rar.html
Título: Re: Estadio de futbol [By Burnhack]
Publicado por: Burnhack en 25 Marzo 2008, 12:53 pm
Es una pequeña mejoria , los balones salen de cada esquina y ayudaran a rebotar por los diferentes angulos.
Saludos
Título: Re: Estadio de futbol [By Burnhack]
Publicado por: Casidiablo en 25 Marzo 2008, 19:19 pm
Jeje.. bastante curioso...
Título: Re: Estadio de futbol
Publicado por: Burnhack en 18 Abril 2008, 20:53 pm
Bueno como empece a trabajar con paneles, decidi agregarle uno a este applet que tenia ya por aqui , jeje una mini mejora, aun estoy estudiando el metodo para que choquen los balones que todavia nose me ocurrio jeje. Ya pronto liberare un code parecido a este con figuras geometricas en las que podremos modificar las figuras tambien. import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.awt.image.ImageObserver;
public class Futbol extends Applet implements Runnable, ActionListener{ Balon v[]; int cont; Image campo; Image aux; Image panelIzquierdo; Thread t; List lstBalon; TextField txtAncho, txtAlto, txtVelocidad, txtX, txtY, txtDireccion; Button cmdAgregar, cmdEliminar; CheckboxGroup cbgTipo; Checkbox chkBalon1, chkBalon, chkBalon2, chkBalon3;
public void init(){ cont = 0; v = new Balon [1000]; setLayout(new BorderLayout()); setSize(840, 407); //Crear el panel Derecho Panel panelDerecho = new Panel(); panelDerecho.setBackground(Color.gray); panelDerecho.setPreferredSize(new Dimension(200, 400)); //Añadir la etiqueta "Balones" al panel derecho Label lblAux = new Label("Balones"); lblAux.setPreferredSize(new Dimension(200, 20)); lblAux.setAlignment(Label.CENTER); panelDerecho.add(lblAux); //Añadir la lista para las balones al panel derecho lstBalon = new List(); lstBalon.setPreferredSize(new Dimension(170, 170)); panelDerecho.add(lstBalon); //Añadir campos de datos txtX = agregarTextField(panelDerecho, "X"); txtY = agregarTextField(panelDerecho, "Y"); txtVelocidad = agregarTextField(panelDerecho, "Velocidad"); txtDireccion = agregarTextField(panelDerecho, "Dirección"); //Añadir tipo de balon cbgTipo = new CheckboxGroup(); chkBalon= new Checkbox ("Normal", cbgTipo, true); chkBalon.setPreferredSize(new Dimension(70, 20)); panelDerecho.add(chkBalon); chkBalon1 = new Checkbox ("R.Madrid", cbgTipo, false); chkBalon1.setPreferredSize(new Dimension(70, 20)); panelDerecho.add(chkBalon1); chkBalon2 = new Checkbox ("AtlMadrid", cbgTipo, false); chkBalon2.setPreferredSize(new Dimension(70, 20)); panelDerecho.add(chkBalon2); chkBalon3 = new Checkbox ("Barça", cbgTipo, false); chkBalon3.setPreferredSize(new Dimension(70, 20)); panelDerecho.add(chkBalon3); //Añadir botones cmdAgregar = new Button("Agregar"); cmdAgregar.addActionListener(this); cmdEliminar = new Button("Eliminar"); cmdEliminar.addActionListener(this); cmdAgregar.setPreferredSize(new Dimension (90, 30)); cmdEliminar.setPreferredSize(new Dimension (90, 30)); panelDerecho.add(cmdAgregar); panelDerecho.add(cmdEliminar); //Añadir el panel Derecho al Applet add(panelDerecho, BorderLayout.EAST); //Crear el panel izquierdo campo = getImage(getCodeBase(), "img/campo.png"); aux = createImage(640, 407); } public TextField agregarTextField(Panel p, String e){ Label lblAux= new Label(e); TextField txtAux = new TextField(); lblAux.setPreferredSize(new Dimension (90, 20)); txtAux.setPreferredSize(new Dimension (90, 20)); p.add(lblAux); p.add(txtAux); return txtAux; } public Choice agregarChoice(Panel p, String e){ Label lblAux= new Label(e); Choice cboAux = new Choice(); lblAux.setPreferredSize(new Dimension (90, 20)); cboAux.setPreferredSize(new Dimension (90, 20)); p.add(lblAux); p.add(cboAux); return cboAux; }
public void start(){ t = new Thread(this); if (t != null) t.start(); }
public void run(){ while (true){ repaint (); for (int i=0; i<cont; i++) v[i].mover(0, 0, 639, 406); try { Thread.sleep(40); } catch (Exception e) { } } } public void actionPerformed(ActionEvent e){ if (e.getSource() == cmdAgregar){ Balon aux; if(cbgTipo.getSelectedCheckbox()== chkBalon) { lstBalon.add("Normal"); aux = new Balon(getImage (getCodeBase(), "img/balon0.png"), Integer.parseInt(txtX.getText()), Integer.parseInt(txtY.getText()), Integer.parseInt(txtVelocidad.getText()), Integer.parseInt(txtDireccion.getText()),this); } else if(cbgTipo.getSelectedCheckbox()== chkBalon1) { lstBalon.add("R.Madrid"); aux = new Balon (getImage (getCodeBase(), "img/balon1.png"), Integer.parseInt(txtX.getText()), Integer.parseInt(txtY.getText()), Integer.parseInt(txtVelocidad.getText()), Integer.parseInt(txtDireccion.getText()),this); } else if(cbgTipo.getSelectedCheckbox()== chkBalon2) { lstBalon.add("AtlMadrid"); aux = new Balon (getImage (getCodeBase(), "img/balon2.png"), Integer.parseInt(txtX.getText()), Integer.parseInt(txtY.getText()), Integer.parseInt(txtVelocidad.getText()), Integer.parseInt(txtDireccion.getText()),this); } else { lstBalon.add("Barça"); aux=new Balon (getImage (getCodeBase(), "img/balon3.png"), Integer.parseInt(txtX.getText()), Integer.parseInt(txtY.getText()), Integer.parseInt(txtVelocidad.getText()), Integer.parseInt(txtDireccion.getText()),this); } //Añadir el balon agregarBalon(aux); } else if (e.getSource() == cmdEliminar){ int i =lstBalon.getSelectedIndex(); if (i>=0){ eliminarBalon(i); lstBalon.remove(i); } } } public boolean agregarBalon(Balon b){ if (cont == 1000) return false; else{ v[cont ++] = b; return true; } }
public boolean eliminarBalon(int i){ if (cont == 0 || i >= cont) return false; else{ for (int j=i; j<cont; j++) v[j] = v[j+1]; cont--; return true; } }
public void update (Graphics g){ paint (g); } public void paint(Graphics g){ aux.getGraphics().drawImage(campo, 0, 0, this); for (int i=0; i<cont; i++) v[i].paint(aux.getGraphics()); g.drawImage(aux, 0, 0, this); } }
|