graficosjava

Anuncio
public interface Dibujable {
public boolean debug=true;
public void dibuja(Graphics g);
public Rectangle getRectangle();
}
public class Rectangulo implements Dibujable {
private int x1, y1, x2, y2;
public Rectangulo(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
// getter y setter
@Override
public void dibuja(Graphics g) {
g.drawRect(x1, y1, x2 ­ x1, y2 ­ y1);
if (debug) {
g.drawRect((int) getRectangle().getX(), (int) getRectangle().getY(), (int)
getRectangle().getWidth(), (int) getRectangle().getHeight());
}
}
@Override
public Rectangle getRectangle() {
return new Rectangle(x1, y1, x2 ­ x1, y2 ­ y1);
}
public boolean colision(Dibujable d) {
return getRectangle().intersects(d.getRectangle());
}
}
public class Sprite implements Dibujable {
private int x, y;
BufferedImage img;
public Sprite(int x, int y, BufferedImage img) {
this.x = x;
this.y = y;
this.img = img;
}
// getter y setter
@Override
public void dibuja(Graphics g) {
g.drawImage(img, x, y, null);
if (debug) {
g.drawRect((int) getRectangle().getX(), (int) getRectangle().getY(), (int)
getRectangle().getWidth(), (int) getRectangle().getHeight());
}
}
@Override
public Rectangle getRectangle() {
return new Rectangle(x, y, img.getWidth(), img.getHeight());
}
public boolean colision(Dibujable d)
{
return getRectangle().intersects(d.getRectangle());
}
}
public class PruebaGraficosYColisiones extends javax.swing.JFrame {
Rectangulo r1, r2;
ArrayList<Dibujable> objetos = new ArrayList<>();
Sprite s;
Clip sonidoColision, musicaFondo;
public PruebaGraficosYColisiones() {
try {
initComponents();
r1 = new Rectangulo(50, 10, 100, 50);
r2 = new Rectangulo(20, 40, 30, 50);
BufferedImage img=ImageIO.read(new
File(getClass().getResource("/assets/flappy.png").getFile()));
s = new Sprite(5, 5, img);
objetos.add(r1);
objetos.add(r2);
objetos.add(s);
BufferedImage bomba=ImageIO.read(new
File(getClass().getResource("/assets/bomba.png").getFile()));
for(int i=0;i<100;i++)
{
objetos.add(new Sprite((int)(Math.random()*jPanel1.getWidth()),(int)
(Math.random()*jPanel1.getHeight()),bomba));
}
} catch (IOException ex) {
Logger.getLogger(PruebaGraficosYColisiones.class.getName()).log(Level.SEVERE, null,
ex);
}
sonidoColision = cargarSonido("/assets/colision.wav");
musicaFondo = cargarSonido("/assets/musica.wav");
musicaFondo.loop(Clip.LOOP_CONTINUOUSLY);
musicaFondo.start();
}
private Clip cargarSonido(String sonido) {
Clip clip = null;
try {
clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(new
File(getClass().getResource(sonido).getFile())));
} catch (Exception ex) {
Logger.getLogger(PruebaGraficosYColisiones.class.getName()).log(Level.SEVERE, null,
ex);
}
return clip;
}
private void formKeyPressed(java.awt.event.KeyEvent evt) {
switch (evt.getKeyCode()) {
case 65:
s.moverIzquierda();
break;
case 68:
s.moverDerecha();
break;
case 87:
s.moverArriba();
break;
case 83:
s.moverAbajo();
break;
}
dibuja2();
}
public void dibuja2() {
Image offscreen =createImage(jPanel1.getWidth(), jPanel1.getHeight());
Graphics offgc = offscreen.getGraphics();
boolean colision = false;
for (Dibujable p : objetos) {
p.dibuja(offgc);
if (p != s) {
colision = colision || s.colision(p);
}
}
if (colision && !sonidoColision.isRunning()) {
sonidoColision.start();
sonidoColision.setMicrosecondPosition(0);
}
jPanel1.getGraphics().drawImage(offscreen, 0, 0, this);
setTitle((colision == true) ? "Si" : "no");
}
public void dibuja() {
boolean colision = false;
jPanel1.getGraphics().clearRect(1,1,jPanel1.getWidth(),jPanel1.getHeight());
for (Dibujable p : objetos) {
p.dibuja(jPanel1.getGraphics());
if (p != s) {
colision = colision || s.colision(p);
}
}
if (colision && !sonidoColision.isRunning()) {
sonidoColision.setMicrosecondPosition(0);
sonidoColision.start();
}
setTitle((colision == true) ? "Si" : "no");
}
public static void main(String args[]) {
…...
}
private javax.swing.JPanel jPanel1;
}
Descargar
Fichas relacionados
Crear fichas