RE:Necesito un poco de ayuda
No seas vago.
jejeje esta bien todos pasamos por eso, siempre nesecitamos un poco de ayuda te mando el siguiente codigo (que espero te sirva de algo) con la condicion de que repases Hilos o Thread y multi Thread que luego te ayudaran mucho.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package elmalo;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.*;
/**
*
* @author wilder
*/
public class Main extends JFrame implements Runnable,KeyListener{
private Thread hilo;
private JLabel imagenL;
private JLabel jugador;
private ImageIcon imagenI;
public Main(){
super("MOVIMIENTO DEL MALO");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
imagenI=new ImageIcon("imagen.png");
imagenL=new JLabel(imagenI);
imagenL.setBounds(350,180,48,48);
jugador=new JLabel(imagenI);
jugador.setBounds(50,180,48,48);
add(imagenL);
add(jugador);
addKeyListener(this);
setBounds(200,50,800,450);
setVisible(true);
start();
}
public void start(){
if(hilo==null){
hilo=new Thread(this);
hilo.start();
}
}
public void run(){
int ram;
try{
while(true){
ram=(int)(Math.random()*4)+1;
if(ram==1)
imagenL.setLocation(imagenL.getX()-20,imagenL.getY());
if(ram==2)
imagenL.setLocation(imagenL.getX()+20,imagenL.getY());
if(ram==3)
imagenL.setLocation(imagenL.getX(),imagenL.getY()-20);
if(ram==4)
imagenL.setLocation(imagenL.getX(),imagenL.getY()+20);
Thread.sleep(100);
}
}
catch(Exception e){
System.out.println(e.toString());
}
}
public void keyPressed(KeyEvent e){
try{
if(e.getKeyCode()==38)
jugador.setLocation(jugador.getX(),jugador.getY()-20);
if(e.getKeyCode()==39)
jugador.setLocation(jugador.getX()+20,jugador.getY());
if(e.getKeyCode()==40)
jugador.setLocation(jugador.getX(),jugador.getY()+20);
if(e.getKeyCode()==37)
jugador.setLocation(jugador.getX()-20,jugador.getY());
}
catch(Exception er){
System.out.println(er.toString());
}
}
public void keyTyped(KeyEvent e){
}
public void keyReleased(KeyEvent e){
}
public static void main(String[] args) {
new Main();
}
}