Kathlain
Registrado: 29 Nov 2009 Mensajes: 1
|
Publicado: Dom 29 Nov, 2009 23:00 pm
Título del mensaje: Re: applet no em compila |
|
|
Quien me ayuda a pasar este codigo swing java en applets es que me la pase buscando y no encuentro algo especifico. ayuda plz!!!
| Código: | import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.sound.sampled.*;
import java.io.*;
//prograna principal
class Navidad {public static void main(String args[]){
new Navidad1();//se llama a la clase navidad
}}
class Navidad1 extends JFrame implements ItemListener, ActionListener{//se extiende el frame y se implementan las acciones
JFrame frame = new JFrame("Navidad");//titulo de frame
CheckboxGroup musica = new CheckboxGroup();//se crea un grupo de radiobutton
//el checkboxgroup se usa para cuando solo quiere que se seleccione uno de tantos checkbox o radiobutton que agregaste
Checkbox m1 = new Checkbox("musica", musica, true);//se crea el check m1 y se implementa en el checkboxgroup y con propieda true para que salga seleccionado
Checkbox m2 = new Checkbox("no musica", musica, false);//se crea el check m1 y se implementa en el checkboxgroup y con propieda false
ImageIcon imagen1 = new ImageIcon("navidad11.jpg");//se crea la imagen 1
ImageIcon imagen2 = new ImageIcon("navidad8.jpg");//se crea la imagen 2
ImageIcon imagen3 = new ImageIcon("navidad7.jpg");//se crea la imagen 3
ImageIcon imagen4 = new ImageIcon("navidad12.jpg");//se crea la imagen 4
ImageIcon imagen5 = new ImageIcon("navidad9.jpg");//se crea la imagen 5
JLabel label1 = new JLabel();//se crea el label
JButton boton1 = new JButton("Continuar");//se crea el button
Choice fondo = new Choice();//se crea el choice o el listbox
File file = new File("vidtomp32.wav");//se carga el archivo
Clip sonido;
Timer timer;
public Navidad1(){ //contructor de la clase
try {
sonido = AudioSystem.getClip(); // Se obtiene un Clip de sonido
sonido.open(AudioSystem.getAudioInputStream(file));// Se carga con un fichero wav
sonido.start();// Comienza la reproducción
} catch (Exception e) {//excepcion si no encuentra el sonido
System.out.println("" + e);//imprime la excepcion
}
timer = new Timer (1000, this);// se crea el tiempo
timer.start();//se inica el tiempo
fondo.addItem("Navidad 1");//se le agrega el item 0 al choice
fondo.addItem("Navidad 2");//se le agrega el item 1 al choice
fondo.addItem("Navidad 3");//se le agrega el item 2 al choice
fondo.addItem("Navidad 4");//se le agrega el item 3 al choice
fondo.addItem("Navidad 5");//se le agrega el item 4 al choice
fondo.addItem("Seleccione fondo");//se le agrega el item 5 al choice
fondo.select(5);//para que inicie seleccionado el item 5
frame.getContentPane().add(m1);//se le agrega el checkbox al frame
frame.getContentPane().add(m2);//se le agrega el checkbox al frame
frame.getContentPane().add(fondo);//se le agrega el choice al frame
frame.getContentPane().add(boton1);//se le agrega el boton al frame
frame.getContentPane().add(label1);//se le agrega el label al frame
//siempre ay que dejar un label o un panel de ultimo, porque java agarra el ultimo objeto y lo expande como si fuera el fondo
fondo.setBounds(10, 10, 100, 20);//se le da tamaño y posicion al choice
m1.setBounds(20, 100, 100, 20);//se le da tamaño y posicion al checkbox
m2.setBounds(150, 100, 100, 20);//se le da tamaño y posicion al checkbox
boton1.setBounds(275, 300, 100, 25);//se le da tamaño y posicion al choice
label1.setIcon(imagen1);//se le inserta la imagen 1 al label
frame.setSize( 640,480 );//se le da tamaño al frame
frame.setLocationRelativeTo(null);//se le da una posicion null osea que va aparecer en el medio de la pantalla
frame.setVisible( true );//para que el frame sea visible
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//para cuando le das clip al boton cerrar del frame este cierre
boton1.addActionListener(this);//se la agrega una accion al boton
fondo.addItemListener(this);//se le agrega una accion choice
m1.addItemListener(this);//se le agrega una accion checkbox
m2.addItemListener(this);//se le agrega una accion checkbox
}
public void actionPerformed(ActionEvent ev){//metodo para el evento del button y del tiempo
if(ev.getSource() == boton1){//compara el evento de boton1
frame.hide();//pone el frame invisible
sonido.close();//cierra el sonido
new Navidad2();}//llama a la clase navidad2
//esta parte es mas bien para cuando se acabe de reproducir el sonido se repita
if(ev.getSource() == timer){//compara el evento de tiempo
//compara la longitud en milisegundo del sonido con el tiempo que lleva reproduciendoce
if(sonido.getMicrosecondLength()==sonido.getMicrosecondPosition()){
try{
sonido.close();//cierra el sonido
sonido.open(AudioSystem.getAudioInputStream(file));//abre el sonido nuevamente
sonido.start();}//inicia el sonido
catch (Exception e) {//excepcion si no encuentra el sonido
System.out.println("" + e);}//imprime la excepcion
}}
}
public void itemStateChanged(ItemEvent evento){//metodo para el evento choice y checkbox
if(evento.getSource()==fondo){//compara el vento del choice
if(fondo.getSelectedIndex()==0){
label1.setIcon(imagen1);
}
else if(fondo.getSelectedIndex()==1){
label1.setIcon(imagen2);
}
else if(fondo.getSelectedIndex()==2){
label1.setIcon(imagen3);
}
else if(fondo.getSelectedIndex()==3){
label1.setIcon(imagen4);
}
else if(fondo.getSelectedIndex()==4){
label1.setIcon(imagen5);
}
}
if(evento.getSource()==m1 || evento.getSource()==m2){//compara el evento de checkbox
if (m1.getState()==true){//si el estado del checkbox m1 es true pausa inicia el sonido
sonido.start();
}
else if (m2.getState()==true){//si el estado del checkbox m1 es true detiene el sonido
sonido.stop();
}
}}
}
class Navidad2 extends JFrame implements ItemListener, ActionListener{
CheckboxGroup musica = new CheckboxGroup();
Checkbox m1 = new Checkbox("musica", musica, true);
Checkbox m2 = new Checkbox("no musica", musica, false);
ImageIcon imagen1 = new ImageIcon("navidad10.jpg");
JLabel label1 = new JLabel();
JLabel label2 = new JLabel("Introduce tu Nombre");
JButton boton1 = new JButton("Mensaje");
JButton boton2 = new JButton("Salir");
JTextField text1= new JTextField( 20 );
File file = new File("vidtomp32.wav");
Clip sonido;
Timer timer;
public Navidad2() {
super("Navidad");
try {
sonido = AudioSystem.getClip(); // Se obtiene un Clip de sonido
sonido.open(AudioSystem.getAudioInputStream(file));// Se carga con un fichero wav
sonido.start();// Comienza la reproducción
} catch (Exception e) {
System.out.println("" + e);
}
timer = new Timer (1000, this);
timer.start();
this.getContentPane().add(m1);
this.getContentPane().add(m2);
this.getContentPane().add(boton1);
this.getContentPane().add(boton2);
this.getContentPane().add(text1);
this.getContentPane().add(label2);
this.getContentPane().add(label1);
this.getContentPane().setBackground(Color.black);//color del fondo del frame
//posicion y tamaño
m1.setBounds(20, 100, 100, 20);
m2.setBounds(150, 100, 100, 20);
boton1.setBounds(275, 300, 100, 25);
label2.setBounds(20, 30, 150, 20);
text1.setBounds(150, 30, 100, 20);
boton1.setBounds(20, 200, 100, 20);
boton2.setBounds(230, 250, 100, 20);
label1.setIcon(imagen1);
this.setSize( 640,480 );
this.setLocationRelativeTo(null);
this.setVisible( true );
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
boton1.addActionListener(this);
boton2.addActionListener(this);
m1.addItemListener(this);
m2.addItemListener(this);
}
public void actionPerformed(ActionEvent ev){
if(ev.getSource() == boton1){
JOptionPane.showMessageDialog(null, text1.getText() + " Feliz Navidad!!!!!!!");
}
if(ev.getSource() == boton2){
System.exit(0);
}
if(ev.getSource() == timer){
if(sonido.getMicrosecondLength()==sonido.getMicrosecondPosition()){
try{
sonido.close();
sonido.open(AudioSystem.getAudioInputStream(file));
sonido.start();}
catch (Exception e) {
System.out.println("" + e);
}
}}
}
public void itemStateChanged(ItemEvent evento){
if(evento.getSource()==m1 || evento.getSource()==m2){
if (m1.getState()==true){
sonido.start();
}
else if (m2.getState()==true){
sonido.stop();
}
}}
public void paint( Graphics g ){
super.paint( g );
//aqui pones todos los codigos de graficos line, rectangle, circle, etc etc etc
}
} |
|
|
|
|