Java - Imagenes en Java Movimiento )= problema

 
Vista:

Imagenes en Java Movimiento )= problema

Publicado por Orfeo de lira (1 intervención) el 05/11/2012 09:51:24
aqui ahi un problema ayudenme con esta clase porfavor no me muestra el oso solo el fodo

[code]

import javax.swing.*;

import java.awt.event.*;
import java.awt.*;

public class Graficos {
private JLabel[] et = new JLabel[4];
private JFrame frame;
private JPanel panel;
private int x;
private int y;
private final String RUTA = "C:/Documents and Settings/Administrador.HOGAR-412B9C0B7/Escritorio/logos/";

public Graficos() {
ImageIcon[] img = new ImageIcon[4];
img[3] = new ImageIcon(RUTA + "f.jpg");
frame = new JFrame("Piano");
panel = new JPanel();
panel.setLayout(null);
panel.setFocusable(true);
panel.setBounds(0,0,600,600);
for(int i=0; i<3; i++) {
img[i] = new ImageIcon(RUTA + "i ("+ (i+1) + ").png");
et[i] = new JLabel(img[i]);
if(i<2) {
et[i].setBounds(i*112,10,112,152);
} else {
et[i].setBounds(0,10,200,200);
}
panel.add(et[i]);
}

x=0;
y=10;

et[3] = new JLabel(img[3]);
et[3].setBounds(0,0,1024,786);
panel.add(et[3]);
frame.setSize(1024,786);
frame.getContentPane().add(panel);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evnt) {
System.out.println(evnt.getPoint());
System.out.println(evnt.getButton());
}
});

panel.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent key) {
//System.out.println(key.getKeyCode());
switch(key.getKeyCode()) {
case 38:
y-=10;
et[0].setLocation(x,y);
break;
case 39:
x+=10;
et[0].setLocation(x,y);
break;
case 40:
y+=10;
et[0].setLocation(x,y);
break;
case 37:
x-=10;
et[0].setLocation(x,y);
break;
}
}
});

et[1].addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
et[1].setLocation(et[1].getX()+e.getX()-et[1].getWidth()/2,
et[1].getY()+e.getY()-et[1].getHeight()/2);
}
});
Thread t = new Thread(new MueveOso(et[2],et[0],et[1],panel));
t.start();
}

public static void main(String []args ) {
new Graficos();
}

}

class MueveOso implements Runnable {
private final int DERECHA = 1;
private final int IZQUIERDA = 0;
private final int INCREMENTOS = 10;
private int direccion;
protected JLabel oso;
protected JLabel obstaculo1;
protected JLabel obstaculo2;
protected JPanel panel;
protected int ancho;
protected int alto;
protected int x;
protected int y;
protected int borde; /*Hace referencia a lado de pantalla en que choco*/

public MueveOso(JLabel oso, JLabel obs, JLabel obs2, JPanel panel) {
this.oso = oso;
this.obstaculo1 = obs;
this.obstaculo2 = obs2;
this.panel = panel;
x = oso.getX()+(oso.getWidth()/2);
y = oso.getY()+(oso.getHeight()/2);
direccion = DERECHA;
borde = 1;
}

public void run() {
while(true) {
mueveOso();
try {
Thread.sleep(100);
}catch(Exception ex) {
}
}
}

private boolean colision() {
int antes = -1;
if(x+(oso.getWidth()/2) >= panel.getWidth()) {
antes = borde;
borde = 3;
cambiaDireccion(antes,borde);
return true;
}
if(y+(oso.getHeight()/2) >= panel.getHeight()) {
antes = borde;
borde = 4;
cambiaDireccion(antes,borde);
return true;
}
if(x-(oso.getWidth()/2) <= 0) {
antes = borde;
borde = 1;
cambiaDireccion(antes,borde);
return true;
}
if(y-(oso.getHeight()/2) <= 0) {
antes = borde;
borde = 2;
cambiaDireccion(antes,borde);
return true;
}
return false;
}

private void cambiaDireccion(int antes, int despues) {
if(antes==3 && despues==1) {
if(direccion==DERECHA)
direccion=IZQUIERDA;
else
if(direccion==IZQUIERDA)
direccion=DERECHA;
}
if(antes==1 && despues==3) {
if(direccion==DERECHA)
direccion=IZQUIERDA;
else
direccion=DERECHA;
}
/*Horizontales*/
if(antes==2 && despues==4) {
if(direccion==DERECHA)
direccion=IZQUIERDA;
else
if(direccion==IZQUIERDA)
direccion=DERECHA;
}
if(antes==4 && despues==2) {
if(direccion==DERECHA)
direccion=IZQUIERDA;
else
direccion=DERECHA;
}
}

private void mueveOso() {
switch(borde) {
case 1:
if(direccion==DERECHA) {
y+=INCREMENTOS; x+=INCREMENTOS;
} else {
y-=INCREMENTOS; x+=INCREMENTOS;
}
break;
case 2:
if(direccion==DERECHA) {
y+=INCREMENTOS; x-=INCREMENTOS;
} else {
y+=INCREMENTOS; x+=INCREMENTOS;
}
break;
case 3:
if(direccion==DERECHA) {
y-=INCREMENTOS; x-=INCREMENTOS;
} else {
y+=INCREMENTOS; x-=INCREMENTOS;
}
break;
case 4:
if(direccion==DERECHA) {
y-=INCREMENTOS; x+=INCREMENTOS;
} else {
y-=INCREMENTOS; x-=INCREMENTOS;
}
break;
}
colision(); //Aqui regresa boleano por lo que a reproducir sonido en colision
//colisionObjetos();
y=limitePanel(0,panel.getHeight(),y);
x=limitePanel(0,panel.getWidth(),x);
oso.setLocation(x-(oso.getWidth()/2),y-(oso.getHeight()/2));
}

private int limitePanel(int min, int max, int cord) {
if(cord<min) {
return min;
}
if(cord>max) {
return max;
}
return cord;
}
}

/code]
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder