miércoles, 7 de marzo de 2012

Moviendo Imagen con JToolBar


Se utiliza Hilos y JToolBar



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

public class BarraBotones extends JFrame{
JToolBar barra=new JToolBar();
ImageIcon im[]={new ImageIcon("fizq.jpg"),new ImageIcon("faba.jpg"),new ImageIcon("farr.jpg"),new ImageIcon("fder.jpg")};
ImageIcon imagen=new ImageIcon("Naruto2.jpg");
JLabel lblImage=new JLabel(imagen);

JButton btnizq=new JButton(im[0]);
JButton btnaba=new JButton(im[1]);
JButton btnarr=new JButton(im[2]);
JButton btnder=new JButton(im[3]);
JPanel panel=new JPanel();

hilo hiloder;
hilo2 hiloizq;
hilo3 hiloaba;
hilo4 hiloarr;

 BarraBotones(){
setTitle("Menu Iconos");
setSize(1000,500);
setVisible(true);
Container contenedor=getContentPane();
contenedor.add(panel);

panel.setLayout(null);

panel.add(barra);
barra.setBounds(400,0,250,50);
barra.add(btnizq);
barra.add(btnaba);
barra.add(btnarr);
barra.add(btnder);


panel.add(lblImage);
lblImage.setBounds(500,100,100,100);

hiloder=new hilo();
hiloizq=new hilo2();
hiloaba=new hilo3();
hiloarr=new hilo4();

btnizq.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent var){
if(hiloder.isAlive()==true){
hiloder.stop();
}
if(hiloaba.isAlive()==true){
hiloaba.stop();
}
if(hiloarr.isAlive()==true){
hiloarr.stop();
}
hiloizq=new hilo2();
hiloizq.start();
}
});

btnaba.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent var){
if(hiloder.isAlive()==true){
hiloder.stop();
}
if(hiloizq.isAlive()==true){
hiloizq.stop();
}
if(hiloarr.isAlive()==true){
hiloarr.stop();
}
hiloaba=new hilo3();
hiloaba.start();
}
});

btnarr.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent var){
if(hiloder.isAlive()==true){
hiloder.stop();
}
if(hiloizq.isAlive()==true){
hiloizq.stop();
}
if(hiloaba.isAlive()==true){
hiloaba.stop();
}
hiloarr=new hilo4();
hiloarr.start();
}
});

btnder.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent var){
if(hiloizq.isAlive()==true){
hiloizq.stop();
}
if(hiloaba.isAlive()==true){
hiloaba.stop();
}
if(hiloarr.isAlive()==true){
hiloarr.stop();
}
hiloder=new hilo();
hiloder.start();
}
});
}//CIERRE DEL CONSTRUCTOR
class hilo extends Thread{
public void run(){
int x=0;
try{
while(true){
Thread.sleep(100);
lblImage.setBounds(x,100,100,100);
x=x+20;
if(x>1000){
x=0;
}
}
}catch (Exception er){}
}
}
class hilo2 extends Thread{
public void run(){
int x=1000;
try{
while(true){
Thread.sleep(100);
lblImage.setBounds(x,100,100,100);
x=x-20;
if(x<0){
x=1000;
}
}
}catch(Exception er){}
}
}
class hilo3 extends Thread{
public void run(){
int x=0;
try{
while(true){
Thread.sleep(100);
lblImage.setBounds(500,x,100,100);
x=x+20;
if(x>500){
x=0;
}
}
}catch(Exception er){}
}
}
class hilo4 extends Thread{
public void run(){
int x=500;
try{
while(true){
Thread.sleep(100);
lblImage.setBounds(500,x,100,100);
x=x-20;
if(x<0){
x=500;
}
}
}catch(Exception er){}
}
}

public static void main(String[]arg){
new BarraBotones().show();
}
}

1 comentario: