sábado, 21 de enero de 2012

Moviendo una Imagen .GIF usando teclado

Programa donde utilizamos las flechas para mover la imagen (KeyEvent) 


import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.net.URL;
import javax.swing.*;


public class caminando extends JFrame {

Image im1;
int codigo, x=101, y=121;
String dato = "";
int salto=5;
public caminando(){
setSize(500,500);
setTitle("Coche en Movimiento");
setVisible(true);
im1=(Toolkit.getDefaultToolkit().getImage("Viejo.gif"));
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent var){
ProgramacionTeclas(var);
}});
}

public void paint(Graphics a){
//fondo
a.setColor(Color.BLACK);
a.fillRect(0, 0, 500, 500);
//carretera
a.setColor(Color.white);
a.drawLine(100,120,450,120);
a.setColor(Color.magenta);
a.drawLine(100,160,410,160);
a.setColor(Color.cyan);
a.drawLine(410,160,410,380);
a.setColor(Color.blue);
a.drawLine(450,120,450,420);
a.setColor(Color.green);
a.drawLine(410,380,90,380);
a.setColor(Color.orange);
a.drawLine(450,420,90,420);
//meta
a.setFont(new Font("Arial",Font.BOLD,20));
a.setColor(Color.RED);
a.drawString("Meta", 35, 405);
//Inicio
a.setFont(new Font("Arial",Font.BOLD,20));
a.setColor(Color.red);
a.drawString("Inicio", 35, 145);
//coche
a.drawImage(im1,x,y,35,35,this);
}
public void ProgramacionTeclas(KeyEvent  z){
codigo=z.getKeyCode();
dato=KeyEvent.getKeyText(codigo);
mueve(dato);
}
public void mueve(String t ){
if(t.equalsIgnoreCase("Derecha")){
if(x<410&&x>90&&y==121){
x=x+salto;
repaint();
}
if(x<410&&x>30&&y==381){
x=x+salto;
repaint();
}
}
if(t.equalsIgnoreCase("Abajo")){
if(x>410&&y<380){
y=y+salto;
repaint();
}
}

if(t.equalsIgnoreCase("Arriba")){
if(x>410&&y>121){
y=y-salto;
repaint();
}
}
if(t.equalsIgnoreCase("Izquierda")){
if(x<412&&x>100&&y==121){
x=x-salto;
repaint();
}
if(x<412&&x>40&&y==381){
x=x-salto;
repaint();
}
}
}


public static void main (String  args[]){
caminando termo=new caminando();
termo.setLocation(200,50);
termo.setVisible(true);
}

}



jueves, 19 de enero de 2012

Aprendiendo a Multiplicar

Aprende a multiplicar, este programa genera Multiplicaciones aleatorias, si escribes mal la respuesta te marca un Error, si la escribes bien tendrás un Acierto.


import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;

public class Multiplica extends JFrame {
static URL ruta;
static AudioClip musica;
    
int posx;
JLabel lblTitulo=new JLabel("Aprendiendo a Multiplicar");
JLabel lblNumero1=new JLabel("10");
JLabel lblNumero2=new JLabel("10");
JLabel lblSigno1=new JLabel("*");
JLabel lblSigno2=new JLabel("=");
JTextField txtResultado=new JTextField();
ImageIcon im=new ImageIcon("Feliz.jpg");
ImageIcon im2=new ImageIcon("Triste.jpg");
JLabel lblFeliz=new JLabel(im);
JLabel lblTriste=new JLabel(im2);
JLabel lblAciertos=new JLabel("Aciertos");
JLabel lblErrores=new JLabel("Errores");
JLabel lblNumAciert=new JLabel("0");
JLabel lblNumError=new JLabel ("0");
Hilo m;
public Multiplica(){
Panel p=new Panel();
m=new Hilo();
m.start();
setTitle("Aprendiendo a Multiplicar");
setSize(500,500);
p.setLayout(null);
p.add(lblTitulo);
p.add(lblNumero1);
p.add(lblNumero2);
p.add(lblSigno1);
p.add(lblSigno2);
p.add(txtResultado);
p.add(lblFeliz);
p.add(lblTriste);
p.add(lblAciertos);
p.add(lblErrores);
p.add(lblNumAciert);
p.add(lblNumError);
lblTitulo.setFont(new Font("Arial", Font.BOLD, 20));
lblTitulo.setBounds(posx,10,400,30);
lblNumero1.setFont(new Font("Arial", Font.BOLD, 20));
lblNumero1.setBounds(10,50,30,30);
lblSigno1.setFont(new Font("Arial", Font.BOLD, 20));
lblSigno1.setBounds(50,50,30,30);
lblNumero2.setFont(new Font("Arial", Font.BOLD, 20));
lblNumero2.setBounds(90,50,30,30);
lblSigno2.setFont(new Font("Arial", Font.BOLD, 20));
lblSigno2.setBounds(140,50,30,30);
txtResultado.setFont(new Font("Arial", Font.BOLD, 20));
txtResultado.setBounds(190,50,30,30);
lblFeliz.setFont(new Font("Arial", Font.BOLD, 20));
lblFeliz.setBounds(10,150,100,100);
lblTriste.setFont(new Font("Arial", Font.BOLD, 20));
lblTriste.setBounds(10,270,100,100);
lblAciertos.setFont(new Font("Arial", Font.BOLD, 20));
lblAciertos.setBounds(150,150,100,100);
lblErrores.setFont(new Font("Arial", Font.BOLD, 20));
lblErrores.setBounds(150,270,100,100);
lblNumAciert.setFont(new Font("Arial", Font.BOLD, 20));
lblNumAciert.setBounds(280,175,50,50);
lblNumError.setFont(new Font("Arial", Font.BOLD, 20));
lblNumError.setBounds(280,300,50,50);
Container c=getContentPane();
c.add(p);
txtResultado.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent r){
int x1=Integer.parseInt(lblNumero1.getText());
int x2=Integer.parseInt(lblNumero2.getText());
int resultado=Integer.parseInt(txtResultado.getText());
int var =x2*x1;
if(resultado==var){
lblNumAciert.setText(String.valueOf(Integer.parseInt(lblNumAciert.getText())+1));
lblNumero1.setText(String.valueOf(Aleatorio()));
   lblNumero2.setText(String.valueOf(Aleatorio()));
txtResultado.requestFocus();
txtResultado.setText("");
try{
ruta=new URL("file:Destello.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
else {
lblNumError.setText(String.valueOf(Integer.parseInt(lblNumError.getText())+1));
   lblNumero1.setText(String.valueOf(Aleatorio()));
   lblNumero2.setText(String.valueOf(Aleatorio()));
txtResultado.requestFocus();
txtResultado.setText("");
try{
ruta=new URL("file:Lengua.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
} });
}
class Hilo extends Thread{
public void run(){
try{
while(true){
lblTitulo.setVisible(true);
Thread.sleep(500);
lblTitulo.setVisible(false);
Thread.sleep(500);
posx=posx-20;
lblTitulo.setBounds(posx,10,400,30);
if(posx<0){posx=400;}
}
}catch(Exception er){}
}
}

public int Aleatorio(){
return((int)(Math.random()*11));
}
}
//Esto va en otra clase

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

public class Panel extends JPanel {
public void paintComponent(Graphics g){
Dimension tamano=getSize();
ImageIcon imagenFondo=new ImageIcon("Fondo.jpg");
g.drawImage(imagenFondo.getImage(),0,0,tamano.width,tamano.height,null);
setOpaque(false);
super.paintComponent(g);
}
}

//Esto va en otra clase

public class mainMultiplica {

public static void main(String[] args) {
new Multiplica().show();

}

}

martes, 17 de enero de 2012

Juego: Piedra, Papel o Tijeras

Parte 2: Programando las acciones (Sonidos, Puntuación, JRadioButton, JLabel )


import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;

public class Juego extends JFrame{
static URL ruta;
static AudioClip musica;
    
int posx;
Hilo m;
JLabel lblTitulo=new JLabel("Piedra, Papel o Tijeras");
ImageIcon img[]={new ImageIcon("Piedra.jpg"),new ImageIcon("Papel.jpg"),new ImageIcon("Tijeras.jpg")};
ImageIcon im=new ImageIcon("piedra.jpg");
ImageIcon im2=new ImageIcon("papel.jpg");
ImageIcon im3=new ImageIcon("tijeras.jpg");
JLabel lblJugador=new JLabel("Jugador");
JLabel lblMaquina=new JLabel("Maquina");
JLabel lblOpcion1=new JLabel();
JLabel lblOpcion2=new JLabel();
ButtonGroup grupo= new ButtonGroup();
JRadioButton rbPiedra=new JRadioButton("Piedra");
JRadioButton rbPapel=new JRadioButton("Papel");
JRadioButton rbTijeras=new JRadioButton("Tijeras");
JLabel lblGanados=new JLabel("Ganados");
JLabel lblPerdidos=new JLabel("Perdidos");
JLabel lblEmpate=new JLabel("Empate");
JLabel lblGan=new JLabel("0");
JLabel lblPer=new JLabel("0");
JLabel lblEmp=new JLabel("0");
int imagenesSeleccionadas[]=new int[2];
int contador=0,parejas=0;
boolean ban=false;
JButton btnActivado=new JButton();
Juego(){
Panel p=new Panel();
setTitle("Piedra, Papel o Tijeras");
setSize(500,500);
m=new Hilo();
m.start();
p.setLayout(null);
lblTitulo.setFont(new Font("Arial", Font.BOLD, 20));
p.add(lblTitulo);
p.add(lblJugador);
lblJugador.setBounds(70,70,100,30);
p.add(lblMaquina);
lblMaquina.setBounds(230,70,100,30);
p.add(lblOpcion1);
lblOpcion1.setBounds(50,100,100,100);
p.add(lblOpcion2);
lblOpcion2.setBounds(200,100,100,100);
p.add(rbPiedra);
grupo.add(rbPiedra);
rbPiedra.setBounds(50,200,100,30);
p.add(rbPapel);
grupo.add(rbPapel);
rbPapel.setBounds(50,230,100,30);
p.add(rbTijeras);
grupo.add(rbTijeras);
rbTijeras.setBounds(50,260,100,30);
p.add(lblGanados);
lblGanados.setBounds(50,300,100,30);
p.add(lblPerdidos);
lblPerdidos.setBounds(50,340,100,30);
p.add(lblEmpate);
lblEmpate.setBounds(50,380,100,30);
p.add(lblGan);
lblGan.setBounds(130,300,100,30);
p.add(lblPer);
lblPer.setBounds(130,340,100,30);
p.add(lblEmp);
lblEmp.setBounds(130,380,100,30);
Container c=getContentPane();
c.add(p);
rbPiedra.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent t){
int piedra = 0;
if(rbPiedra.isSelected()==true){
lblOpcion1.setIcon(img[0]);
int n2=(int)(Math.random()*3);
switch(piedra){
case 0:
lblOpcion1.setIcon(img[0]);
break;
case 1:
lblOpcion1.setIcon(img[1]);
break;
case 2:
lblOpcion1.setIcon(img[2]);
break;
}
switch(n2){
case 0:
lblOpcion2.setIcon(img[0]);
break;
case 1:
lblOpcion2.setIcon(img[1]);
break;
case 2:
lblOpcion2.setIcon(img[2]);
break;
}
if(piedra==n2){
lblEmp.setText(String.valueOf(Integer.parseInt(lblEmp.getText())+1));
try{
ruta=new URL("file:Empate.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
if(piedra==0&&n2==1){
lblPer.setText(String.valueOf(Integer.parseInt(lblPer.getText())+1));
try{
ruta=new URL("file:Lengua.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
if(piedra==0&&n2==2){
lblGan.setText(String.valueOf(Integer.parseInt(lblGan.getText())+1));
                     try{
ruta=new URL("file:Destello.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
}
}
});
rbPapel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent t){
int papel = 1;
if(rbPapel.isSelected()==true){
lblOpcion1.setIcon(img[1]);
int n2=(int)(Math.random()*3);
switch(papel){
case 0:
lblOpcion1.setIcon(img[0]);
break;
case 1:
lblOpcion1.setIcon(img[1]);
break;
case 2:
lblOpcion1.setIcon(img[2]);
break;
}
switch(n2){
case 0:
lblOpcion2.setIcon(img[0]);
break;
case 1:
lblOpcion2.setIcon(img[1]);
break;
case 2:
lblOpcion2.setIcon(img[2]);
break;
}
if(papel==n2){
lblEmp.setText(String.valueOf(Integer.parseInt(lblEmp.getText())+1));
                     try{
ruta=new URL("file:Empate.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
if(papel==1&&n2==2){
lblPer.setText(String.valueOf(Integer.parseInt(lblPer.getText())+1));
try{
ruta=new URL("file:Lengua.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
if(papel==1&&n2==0){
lblGan.setText(String.valueOf(Integer.parseInt(lblGan.getText())+1));
                     try{
ruta=new URL("file:Destello.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
}
}
});
rbTijeras.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent t){
int tijeras = 2;
if(rbTijeras.isSelected()==true){
lblOpcion1.setIcon(img[2]);
int n2=(int)(Math.random()*3);
switch(tijeras){
case 0:
lblOpcion1.setIcon(img[0]);
break;
case 1:
lblOpcion1.setIcon(img[1]);
break;
case 2:
lblOpcion1.setIcon(img[2]);
break;
}
switch(n2){
case 0:
lblOpcion2.setIcon(img[0]);
break;
case 1:
lblOpcion2.setIcon(img[1]);
break;
case 2:
lblOpcion2.setIcon(img[2]);
break;
}
if(tijeras==n2){
lblEmp.setText(String.valueOf(Integer.parseInt(lblEmp.getText())+1));
                       try{
ruta=new URL("file:Empate.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
if(tijeras==2&&n2==0){
lblPer.setText(String.valueOf(Integer.parseInt(lblPer.getText())+1));
try{
ruta=new URL("file:Lengua.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
if(tijeras==2&&n2==1){
lblGan.setText(String.valueOf(Integer.parseInt(lblGan.getText())+1));
                     try{
ruta=new URL("file:Destello.wav");
musica = Applet.newAudioClip(ruta);
musica.play();
}
catch(Exception ex){
System.out.println("Error"+ex);
}
}
}
}
});
}
class Hilo extends Thread{
public void run(){
try{
while(true){
lblTitulo.setVisible(true);
Thread.sleep(500);
lblTitulo.setVisible(false);
Thread.sleep(500);
posx=posx-20;
lblTitulo.setBounds(posx,10,250,30);
if(posx<0){posx=400;}
}
}catch(Exception er){}
}
}
public int Aleatorio(){
return((int)(Math.random()*11));
}

}



Juego: Piedra, Papel o Tijeras

Parte 1: Creando Interfaz 


import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;

public class Juego extends JFrame{
JLabel lblTitulo=new JLabel("Piedra, Papel o Tijeras");
ImageIcon img[]={new ImageIcon("Piedra.jpg"),new ImageIcon("Papel.jpg"),new ImageIcon("Tijeras.jpg")};
ImageIcon im=new ImageIcon("piedra.jpg");
ImageIcon im2=new ImageIcon("papel.jpg");
ImageIcon im3=new ImageIcon("tijeras.jpg");
JLabel lblJugador=new JLabel("Jugador");
JLabel lblMaquina=new JLabel("Maquina");
JLabel lblOpcion1=new JLabel();
JLabel lblOpcion2=new JLabel();
ButtonGroup grupo= new ButtonGroup();
JRadioButton rbPiedra=new JRadioButton("Piedra");
JRadioButton rbPapel=new JRadioButton("Papel");
JRadioButton rbTijeras=new JRadioButton("Tijeras");
JLabel lblGanados=new JLabel("Ganados");
JLabel lblPerdidos=new JLabel("Perdidos");
JLabel lblEmpate=new JLabel("Empate");
JLabel lblGan=new JLabel("0");
JLabel lblPer=new JLabel("0");
JLabel lblEmp=new JLabel("0");
int imagenesSeleccionadas[]=new int[2];
int contador=0,parejas=0;
boolean ban=false;
JButton btnActivado=new JButton();
Juego(){
Panel p=new Panel();
setTitle("Piedra, Papel o Tijeras");
setSize(500,500);

p.setLayout(null);
lblTitulo.setFont(new Font("Arial", Font.BOLD, 20));
p.add(lblTitulo);
p.add(lblJugador);
lblJugador.setBounds(70,70,100,30);
p.add(lblMaquina);
lblMaquina.setBounds(230,70,100,30);
p.add(lblOpcion1);
lblOpcion1.setBounds(50,100,100,100);
p.add(lblOpcion2);
lblOpcion2.setBounds(200,100,100,100);
p.add(rbPiedra);
grupo.add(rbPiedra);
rbPiedra.setBounds(50,200,100,30);
p.add(rbPapel);
grupo.add(rbPapel);
rbPapel.setBounds(50,230,100,30);
p.add(rbTijeras);
grupo.add(rbTijeras);
rbTijeras.setBounds(50,260,100,30);
p.add(lblGanados);
lblGanados.setBounds(50,300,100,30);
p.add(lblPerdidos);
lblPerdidos.setBounds(50,340,100,30);
p.add(lblEmpate);
lblEmpate.setBounds(50,380,100,30);
p.add(lblGan);
lblGan.setBounds(130,300,100,30);
p.add(lblPer);
lblPer.setBounds(130,340,100,30);
p.add(lblEmp);
lblEmp.setBounds(130,380,100,30);
Container c=getContentPane();
c.add(p);
}
}

//Esto va en otra clase 

public class mainJuego {
public static void main(String[] args) {
new Juego().setVisible(true);
}

}

//Esto va en otra clase

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

public class Panel extends JPanel {
public void paintComponent(Graphics g){
Dimension tamano=getSize();
ImageIcon imagenFondo=new ImageIcon("Fondo.jpg");
g.drawImage(imagenFondo.getImage(),0,0,tamano.width,tamano.height,null);
setOpaque(false);
super.paintComponent(g);
}
}