Mostrando entradas con la etiqueta Codigo. Mostrar todas las entradas
Mostrando entradas con la etiqueta Codigo. Mostrar todas las entradas

lunes, 12 de marzo de 2012

Juego Memorama


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


public class memo extends JFrame{

JButton inijuego=new JButton("INICIAR JUEGO");
JButton salir=new JButton("SALIR DEL JUEGO");

JButton b1;
JButton b2;

static JButton ima1=new JButton();
static JButton ima2=new JButton();
static JButton ima3=new JButton();
static JButton ima4=new JButton();
static JButton ima5=new JButton();
static JButton ima6=new JButton();
JPanel p=new JPanel();

Activa act=new Activa();
int y[], contador, contador2;
static ImageIcon x[]=new ImageIcon[4];

memo(){
setTitle("Mi Memorama");
setSize(1000,400);
Container contenedor=getContentPane();
contenedor.add(p);
p.setLayout(null);
p.add(inijuego);inijuego.setBounds(800,50,170,30);
p.add(salir);salir.setBounds(800,100,170,30);
   p.add(ima1);ima1.setBounds(0,10,170,150);
   p.add(ima2);ima2.setBounds(200,10,170,150);
   p.add(ima3);ima3.setBounds(400,10,170,150);
   p.add(ima4);ima4.setBounds(0,200,170,150);
   p.add(ima5);ima5.setBounds(200,200,170,150);
   p.add(ima6);ima6.setBounds(400,200,170,150);
 
   x[0]=new ImageIcon("f.jpg");
   x[1]=new ImageIcon("f1.jpg");
   x[2]=new ImageIcon("f2.jpg");
   x[3]=new ImageIcon("f3.jpg");
     
   ima1.setIcon(x[3]);
   ima2.setIcon(x[3]);
   ima3.setIcon(x[3]);
   ima4.setIcon(x[3]);
   ima5.setIcon(x[3]);
   ima6.setIcon(x[3]);
   y=Aleatorios.calcula();

ima1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent w){

ima1.setIcon(x[y[0]]);
if(contador==0){
b1=ima1;
contador++;}
else{
b2=ima1;
act=new Activa();
act.start();
contador=0;}
}});

ima2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent w){
ima2.setIcon(x[y[1]]);
if(contador==0){
b1=ima2;
contador++;}
else{
b2=ima2;
act=new Activa();
act.start();
contador=0;}
}});

ima3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent w){
ima3.setIcon(x[y[2]]);
if(contador==0){
b1=ima3;
contador++;}
else{
b2=ima3;
act=new Activa();
act.start();
contador=0;}
}});

ima4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent w){
ima4.setIcon(x[y[3]]);
if(contador==0){
b1=ima4;
contador++;}
else{
b2=ima4;
act=new Activa();
act.start();
contador=0;}
}});

ima5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent w){
ima5.setIcon(x[y[4]]);
if(contador==0){
b1=ima5;
contador++;}
else{
b2=ima5;
act=new Activa();
act.start();
contador=0;}
}});

ima6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent w){
ima6.setIcon(x[y[5]]);
if(contador==0){
b1=ima6;
contador++;}
else{
b2=ima6;
act=new Activa();
act.start();
contador=0;}
}});

inijuego.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent w){
inicializa.inicializa();
y=Aleatorios.calcula();
}});
}



public static class inicializa{
public static void inicializa(){
ima1.setIcon(x[3]);
ima2.setIcon(x[3]);
ima3.setIcon(x[3]);
ima4.setIcon(x[3]);
ima5.setIcon(x[3]);
ima6.setIcon(x[3]);

ima1.setEnabled(true);
ima2.setEnabled(true);
ima3.setEnabled(true);
ima4.setEnabled(true);
ima5.setEnabled(true);
ima6.setEnabled(true);
}
}

class Activa extends Thread{
public void run(){
try{
Thread.sleep(800);
if(b1.getIcon().equals(b2.getIcon())==true){
b2.setEnabled(false);
b1.setEnabled(false);
}
else{
b1.setIcon(x[3]);
b2.setIcon(x[3]);
}
contador2++;
}catch (Exception er){
}
}
}
public static class Aleatorios{
public static  int ar[][]=new int[6][3];
public static  void aleatorios(){

int p=3, a=0;

for(int i=0; i<p; i++){

for(int j=0; j<p; j++){

for(int k=0; k<p; k++){

if(i!=j && j!=k && i!=k){


ar[a][0]=i;
ar[a][1]=j;
ar[a++][2]=k;


}
}

}


}

}

public static int[] calcula(){
aleatorios();

int ar2[]=new int[6];
int x=(int)(Math.random()*6);
int y=(int)(Math.random()*6);

for(int m=0; m<ar2.length; m++){

if(m<3){

ar2[m]=ar[x][m];
}
else{
ar2[m]=ar[y][m-3];

}


}

return ar2;
}}

public static void main(String[] arg){
new memo().setVisible(true);
}}

Perímetro y Área

En este código utilizamos un JMenuBar y el JToolBar y lo que hace es sacar perímetro y área de algunas figuras.


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


public class Dibujar extends JFrame{
JMenuBar barra=new JMenuBar();
JMenu Perimetro=new JMenu("Perimetro");
JMenuItem mnuCuadrado=new JMenuItem("Cuadrado");
JMenuItem mnuRectangulo=new JMenuItem("Rectangulo");
JMenuItem mnuCirculo=new JMenuItem("Circulo");
JMenuItem mnuOvalo=new JMenuItem("Ovalo");
JMenu Area=new JMenu("Area");
JMenuItem mnuCuadrado2=new JMenuItem("Cuadrado");
JMenuItem mnuRectangulo2=new JMenuItem("Rectangulo");
JMenuItem mnuCirculo2=new JMenuItem("Circulo");
JMenuItem mnuOvalo2=new JMenuItem("Ovalo");
ImageIcon img[]={new ImageIcon("Cuadradosin.jpg"),new ImageIcon("Rectangulosin.jpg"), new ImageIcon("Circulosin.jpg"), new ImageIcon("Ovalosin.jpg"),new ImageIcon("Cuadrado.jpg"),new ImageIcon("Rectangulo.jpg"), new ImageIcon("Circulo.jpg"), new ImageIcon("Ovalo.jpg")};
JToolBar barra2=new JToolBar();
JButton btnCuadradosin=new JButton(img[0]);
JButton btnRectangulosin=new JButton(img[1]);
JButton btnCirculosin=new JButton(img[2]);
JButton btnOvalosin=new JButton(img[3]);
JButton btnCuadrado=new JButton(img[4]);
JButton btnRectangulo=new JButton(img[5]);
JButton btnCirculo=new JButton(img[6]);
JButton btnOvalo=new JButton(img[7]);
JLabel lblLado=new JLabel("Lado: ");
JTextField txtLado=new JTextField();
JLabel lblLargo=new JLabel("Largo: ");
JTextField txtLargo=new JTextField();
JLabel lblAncho=new JLabel("Ancho: ");
JTextField txtAncho=new JTextField();
JLabel lblBase=new JLabel("Base: ");
JTextField txtBase=new JTextField();
JLabel lblAltura=new JLabel("Altura: ");
JTextField txtAltura=new JTextField();
JLabel lblRadio=new JLabel("Radio: ");
JTextField txtRadio=new JTextField();
JLabel lblejeMy=new JLabel("Eje Mayor: ");
JTextField txtejeMy=new JTextField();
JLabel lblejeMn=new JLabel("Eje Menor: ");
JTextField txtejeMn=new JTextField();
JButton btnCalcular=new JButton("Calcular");
JPanel panel1,panel2;
int op=0;
public Dibujar(){
setTitle("Dibujar");
setSize(800,500);
panel1=new JPanel();
panel2=new JPanel();
panel1.setLayout(null);
panel1.add(lblLado);
lblLado.setBounds(10, 50, 100, 30);
panel1.add(txtLado);
txtLado.setBounds(50, 55, 60, 20);
panel1.add(lblLargo);
lblLargo.setBounds(10, 50, 100, 30);
panel1.add(txtLargo);
txtLargo.setBounds(50, 55, 60, 20);
panel1.add(lblAncho);
lblAncho.setBounds(10,80,100,30);
panel1.add(txtAncho);
txtAncho.setBounds(50,85,100,20);
panel1.add(lblBase);
lblBase.setBounds(10, 50, 100, 30);
panel1.add(txtBase);
txtBase.setBounds(50, 55, 60, 20);
panel1.add(lblAltura);
lblAltura.setBounds(10,80,100,30);
panel1.add(txtAltura);
txtAltura.setBounds(50,85,100,20);
panel1.add(lblRadio);
lblRadio.setBounds(10, 50, 100, 30);
panel1.add(txtRadio);
txtRadio.setBounds(50, 55, 60, 20);
panel1.add(lblejeMy);
lblejeMy.setBounds(10, 70, 100, 30);
panel1.add(txtejeMy);
txtejeMy.setBounds(70,75,60,20);
panel1.add(lblejeMn);
lblejeMn.setBounds(10, 90, 100, 30);
panel1.add(txtejeMn);
txtejeMn.setBounds(70, 95, 60, 20);
panel1.add(btnCalcular);
btnCalcular.setBounds(20,150,100,30);
barra2.add(btnCuadradosin);
barra2.add(btnRectangulosin);
barra2.add(btnCirculosin);
barra2.add(btnOvalosin);
barra2.add(btnCuadrado);
barra2.add(btnRectangulo);
barra2.add(btnCirculo);
barra2.add(btnOvalo);
barra.add(Perimetro);
Perimetro.add(mnuCuadrado);
Perimetro.add(mnuRectangulo);
Perimetro.add(mnuCirculo);
Perimetro.add(mnuOvalo);
barra.add(Area);
Area.add(mnuCuadrado2);
Area.add(mnuRectangulo2);
Area.add(mnuCirculo2);
Area.add(mnuOvalo2);
Container c=getContentPane();
c.setLayout(null);
c.add(barra);
barra.setBounds(0,0,600,30);
c.add(barra2);
barra2.setBounds(0,31,600,60);
c.add(panel1);
panel1.setBounds(0,91,600,340);
c.add(panel2);
panel2.setBounds(251,91,499,340);
invisibles();
mnuCuadrado.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent cua){
invisibles();
lblLado.setVisible(true);
txtLado.setVisible(true);
btnCalcular.setVisible(true);
op=1;
}
});
mnuRectangulo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent cua){
invisibles();
lblLargo.setVisible(true);
txtLargo.setVisible(true);
lblAncho.setVisible(true);
txtAncho.setVisible(true);
btnCalcular.setVisible(true);
op=2;
}
});
mnuCirculo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent cua){
invisibles();
lblRadio.setVisible(true);
txtRadio.setVisible(true);
btnCalcular.setVisible(true);
op=3;
}
});
mnuOvalo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent cua){
invisibles();
//lblRadio.setVisible(true);
//txtRadio.setVisible(true);
lblejeMy.setVisible(true);
txtejeMy.setVisible(true);
lblejeMn.setVisible(true);
txtejeMn.setVisible(true);
btnCalcular.setVisible(true);
op=4;
}
});
mnuCuadrado2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent cua){
invisibles();
lblBase.setVisible(true);
txtBase.setVisible(true);
btnCalcular.setVisible(true);
op=5;
}
});
mnuRectangulo2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent cua){
invisibles();
lblBase.setVisible(true);
txtBase.setVisible(true);
lblAltura.setVisible(true);
txtAltura.setVisible(true);
btnCalcular.setVisible(true);
op=6;
}
});
mnuCirculo2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent cua){
invisibles();
lblRadio.setVisible(true);
txtRadio.setVisible(true);
btnCalcular.setVisible(true);
op=7;
}
});
mnuOvalo2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent cua){
invisibles();
//lblRadio.setVisible(true);
//txtRadio.setVisible(true);
lblejeMy.setVisible(true);
txtejeMy.setVisible(true);
lblejeMn.setVisible(true);
txtejeMn.setVisible(true);
btnCalcular.setVisible(true);
op=8;
}
});
btnCuadradosin.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent c){
invisibles();
lblLado.setVisible(true);
txtLado.setVisible(true);
btnCalcular.setVisible(true);
op=1;
}
});
btnRectangulosin.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent c){
invisibles();
lblLargo.setVisible(true);
txtLargo.setVisible(true);
lblAncho.setVisible(true);
txtAncho.setVisible(true);
btnCalcular.setVisible(true);
op=2;
}
});
btnCirculosin.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent c){
invisibles();
lblRadio.setVisible(true);
txtRadio.setVisible(true);
btnCalcular.setVisible(true);
op=3;
}
});
btnOvalosin.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent c){
invisibles();
//lblRadio.setVisible(true);
//txtRadio.setVisible(true);
lblejeMy.setVisible(true);
txtejeMy.setVisible(true);
lblejeMn.setVisible(true);
txtejeMn.setVisible(true);
btnCalcular.setVisible(true);
op=4;
}
});
btnCuadrado.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent c){
invisibles();
lblBase.setVisible(true);
txtBase.setVisible(true);
btnCalcular.setVisible(true);
op=5;
}
});
btnRectangulo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent c){
invisibles();
lblBase.setVisible(true);
txtBase.setVisible(true);
lblAltura.setVisible(true);
txtAltura.setVisible(true);
btnCalcular.setVisible(true);
op=6;
}
});
btnCirculo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent c){
invisibles();
lblRadio.setVisible(true);
txtRadio.setVisible(true);
btnCalcular.setVisible(true);
op=7;
}
});
btnOvalo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent c){
invisibles();
//lblRadio.setVisible(true);
//txtRadio.setVisible(true);
lblejeMy.setVisible(true);
txtejeMy.setVisible(true);
lblejeMn.setVisible(true);
txtejeMn.setVisible(true);
btnCalcular.setVisible(true);
op=8;
}
});
btnCalcular.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ca){
if(op==1){
Graphics w=panel2.getGraphics();
w.setColor(Color.black);
w.fillRect(0, 0, 600, 340);
w.setColor(Color.blue);
w.drawRect(70, 50, 180, 180);
int lado=Integer.parseInt(txtLado.getText());
int p=lado*4;
w.setFont(new Font("Arial", Font.BOLD,20));
w.drawString("El perimetro del cuadrado es " + p, 70, 280);
}
if(op==2){
Graphics w=panel2.getGraphics();
w.setColor(Color.black);
w.fillRect(0, 0, 600, 340);
w.setColor(Color.blue);
w.drawRect(70, 50, 200, 100);
int largo=Integer.parseInt(txtLargo.getText());
int ancho=Integer.parseInt(txtAncho.getText());
int p=largo*2+ancho*2;
w.setFont(new Font("Arial", Font.BOLD,20));
w.drawString("El perimetro del rectangulo es " + p, 70, 280);
}
if(op==3){
Graphics w=panel2.getGraphics();
w.setColor(Color.black);
w.fillRect(0, 0, 600, 340);
w.setColor(Color.blue);
w.drawOval(70, 50, 100, 100);
double radio=Integer.parseInt(txtRadio.getText());
//double p=2*3.1416*radio;
double p=(2*radio)*3.1416;
w.setFont(new Font("Arial", Font.BOLD,20));
w.drawString("El perimetro del circulo es " + p, 70, 280);
}
if(op==4){
Graphics w=panel2.getGraphics();
w.setColor(Color.black);
w.fillRect(0, 0, 600, 340);
w.setColor(Color.blue);
w.drawOval(70, 50, 200, 100);
//double radio=Integer.parseInt(txtRadio.getText());
float ejeMy=Integer.parseInt(txtejeMy.getText());
float ejeMn=Integer.parseInt(txtejeMn.getText());
float X=(ejeMy+ejeMn)/2;
double Y=Math.sqrt((ejeMy+ejeMn)/2);
double p=Math.PI*(X+Y);
w.setFont(new Font("Arial", Font.BOLD,20));
w.drawString("El perimetro del ovalo es " + p, 70, 280);
}
if(op==5){
Graphics w=panel2.getGraphics();
w.setColor(Color.black);
w.fillRect(0, 0, 600, 340);
w.setColor(Color.blue);
w.fillRect(70, 50, 180, 180);
int base=Integer.parseInt(txtBase.getText());
int p=base*base;
w.setFont(new Font("Arial", Font.BOLD,20));
w.drawString("El area del cuadrado es " + p, 70, 280);
}
if(op==6){
Graphics w=panel2.getGraphics();
w.setColor(Color.black);
w.fillRect(0, 0, 600, 340);
w.setColor(Color.blue);
w.fillRect(70, 50, 200, 100);
int base=Integer.parseInt(txtBase.getText());
int altura=Integer.parseInt(txtAltura.getText());
int p=base*altura;
w.setFont(new Font("Arial", Font.BOLD,20));
w.drawString("El area del rectangulo es " + p, 70, 280);
}
if(op==7){
Graphics w=panel2.getGraphics();
w.setColor(Color.black);
w.fillRect(0, 0, 600, 340);
w.setColor(Color.blue);
w.fillOval(70, 50, 100, 100);
double radio=Integer.parseInt(txtRadio.getText());
double p=(radio*radio)*Math.PI;
//double res=Math.PI;
//double p=3.1416*radio(double)Math.pow(2);
//double res = (double)Math.pow(p, radio);
w.setFont(new Font("Arial", Font.BOLD,20));
w.drawString("El area del circulo es " + p, 70, 280);
}
if(op==8){
Graphics w=panel2.getGraphics();
w.setColor(Color.black);
w.fillRect(0, 0, 600, 340);
w.setColor(Color.blue);
w.fillOval(70, 50, 200, 100);
//double radio=Integer.parseInt(txtRadio.getText());
double ejeMy=Integer.parseInt(txtejeMy.getText());
double ejeMn=Integer.parseInt(txtejeMn.getText());
double p=Math.PI*(ejeMy*ejeMn);
w.setFont(new Font("Arial", Font.BOLD,20));
w.drawString("El area del ovalo es " + p, 70, 280);
}
}
});
}

public void invisibles() {
lblLado.setVisible(false);
txtLado.setVisible(false);
lblLargo.setVisible(false);
txtLargo.setVisible(false);
lblAncho.setVisible(false);
txtAncho.setVisible(false);
lblBase.setVisible(false);
txtBase.setVisible(false);
lblAltura.setVisible(false);
txtAltura.setVisible(false);
lblRadio.setVisible(false);
txtRadio.setVisible(false);
lblejeMy.setVisible(false);
txtejeMy.setVisible(false);
lblejeMn.setVisible(false);
txtejeMn.setVisible(false);
}

}

// Este es el main

public class mainDibujar {
public static void main(String[] args) {
new Dibujar().setVisible(true);

}

}

Descargar:

https://rapidshare.com/files/256148255/Dibujar.rar

jueves, 8 de marzo de 2012

Operaciones

Operaciones básicas (Suma, Resta, Divicion, Multiplicación Residuo)



import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Operaciones extends JFrame{
//Etiquetas
JLabel lblNumero1=new JLabel("Numero: ");
JLabel lblNumero2=new JLabel("Numero: ");
//Cajas de Texto
JTextField txtNumero1=new JTextField(10);
JTextField txtNumero2=new JTextField(10);
//Etiquetas
JLabel lblOperaciones=new JLabel("Operaciones");
JLabel lblSuma=new JLabel("Suma");
JLabel lblResta=new JLabel("Resta");
JLabel lblMulti=new JLabel("Multiplica");
JLabel lblDivide=new JLabel("Divide");
JLabel lblResiduo=new JLabel("Residuo");
//Botones Operaciones
JButton btnSuma=new JButton("+");
JButton btnResta=new JButton("-");
JButton btnMulti=new JButton("*");
JButton btnDivide=new JButton("/");
JButton btnResiduo=new JButton("%");
//Boton Limpiar
JButton btnLimpiar=new JButton("Limpiar");
//Etiqueta y caja de texto Resultado
JLabel lblres=new JLabel("Resultado: ");
JTextField txtres=new JTextField(10);
JPanel panel=new JPanel();
public Operaciones(){
setTitle("Operaciones");
setSize(350,300);
panel.setLayout(null);
lblNumero1.setBounds(20,10,100,30);
panel.add(lblNumero1);
txtNumero1.setBounds(80,15,100,20);
panel.add(txtNumero1);
lblNumero2.setBounds(20,40,100,30);
panel.add(lblNumero2);
txtNumero2.setBounds(80,45,100,20);
panel.add(txtNumero2);
lblOperaciones.setBounds(100,80,100,20);
panel.add(lblOperaciones);
lblSuma.setBounds(20,100,100,20);
panel.add(lblSuma);
lblResta.setBounds(80,100,100,20);
panel.add(lblResta);
lblDivide.setBounds(140,100,100,20);
panel.add(lblDivide);
lblMulti.setBounds(190,100,100,20);
panel.add(lblMulti);
lblResiduo.setBounds(260,100,100,20);
panel.add(lblResiduo);
panel.add(btnSuma);
btnSuma.setBounds(20,120,45,20);
panel.add(btnResta);
btnResta.setBounds(75,120,45,20);
btnMulti.setBounds(190,120,45,20);
panel.add(btnMulti);
btnDivide.setBounds(135,120,45,20);
panel.add(btnDivide);
btnResiduo.setBounds(260,120,45,20);
panel.add(btnResiduo);
btnLimpiar.setBounds(100,150,90,20);
panel.add(btnLimpiar);
lblres.setBounds(20,190,100,30);
panel.add(lblres);
txtres.setBounds(90,195,100,20);
panel.add(txtres);
Container c=getContentPane();
c.add(panel);
btnSuma.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
int a=Integer.parseInt(txtNumero1.getText());
int b=Integer.parseInt(txtNumero2.getText());
int c=a+b;
txtres.setText(String.valueOf(c));
}
});
        btnResta.addActionListener(new ActionListener(){
       
        @Override
public void actionPerformed(ActionEvent e){
        int a=Integer.parseInt(txtNumero1.getText());
        int b=Integer.parseInt(txtNumero2.getText());
        int c=a-b;
       
        txtres.setText(String.valueOf(c));
}
});
        
        btnMulti.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
        int a=Integer.parseInt(txtNumero1.getText());
        int b=Integer.parseInt(txtNumero2.getText());
        int c=a*b;
       
        txtres.setText(String.valueOf(c));
       
        }
        });
        
        btnDivide.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
        int a=Integer.parseInt(txtNumero1.getText());
        int b=Integer.parseInt(txtNumero2.getText());
        int c=a/b;
       
        txtres.setText(String.valueOf(c));
       
        }
        });
        
        btnResiduo.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent e) {
int a=Integer.parseInt(txtNumero1.getText());
int b=Integer.parseInt(txtNumero2.getText());
int c=a%b;
txtres.setText(String.valueOf(c));
}
        });
        
        btnLimpiar.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent vr){
        txtNumero1.setText("");
txtNumero2.setText("");
txtres.setText("");
        }
        });
}

}

//Metodo main


public class TestOperaciones {


public static void main(String[] args) {
new Operaciones().setVisible(true);


}

}



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();
}
}