codice:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class Speranza extends JApplet implements ActionListener {
// Dichiarazione JButtons per file audio
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JButton button5;
JButton button6;
JButton button7;
JButton button8;
JButton button9;
// Dichiarazione JButtons per altri tasti
JButton stopButton;
JButton help;
JButton ripeti;
JButton HappyDays;
// Dichiarazione AudioClip
AudioClip clip[] = new AudioClip[10];
AudioClip branoCorrente;
JLabel testi = new JLabel();
JLabel about = new JLabel();
String[] str = {"Testi Canzoni", "1.Ray Charles - I've Got A Woman", "2.Carl Perkins - Blue Suede Shoes", "3.Dean Martin & Frank Sinatra - That's Amore",
"4.Elvis Presley - Hound Dog", "5.Elvis Presley - Tutti Frutti", "6.The Four Aces - Love Is A Many Splendored Thing",
"7.Chuck Berry & Bo Diddley - Roll over Beethoven", "8.Marilin Monroe - Bye bye baby", "9.Colonna Sonora - Happy Days"};
// Setta il layout
public void start() {
// BorderLayout che gestisce lo spazio dell'applet
BorderLayout b = new BorderLayout();
getContentPane().setLayout(b);
// Setta il font
Font font = new Font("TimesRoman", Font.BOLD, 11);
setFont(font);
// Inizializzazione dei JButtons
button1=new JButton("Song 1");
button2=new JButton("Song 2");
button3=new JButton("Song 3");
button4=new JButton("Song 4");
button5=new JButton("Song 5");
button6=new JButton("Song 6");
button7=new JButton("Song 7");
button8=new JButton("Song 8");
button9=new JButton("Song 9");
help = new JButton(" Help ");
HappyDays = new JButton("HappyDays");
ripeti = new JButton("RIPETI");
stopButton=new JButton("STOP");
JComboBox combo = new JComboBox(str);
// Aggiunta delle ActionListeners ai JButtons
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
stopButton.addActionListener(this);
help.addActionListener(this);
ripeti.addActionListener(this);
HappyDays.addActionListener(this);
combo.addActionListener(this);
// Inizializzazione di 2 JPanel nei quali verranno inseriti i Buttons
File file = new File("jukebox_largo2.jpg");
JPanelI s = new JPanelI(file);
JPanel p = new JPanel();
p.setBackground(Color.black);
// Griglia del JPanel s
GridBagConstraints c = new GridBagConstraints();
GridBagLayout gridbag = new GridBagLayout();
s.setLayout(gridbag);
// Aggiunta dei JButtons nel JPanel s
s.add(button1);
s.add(button2);
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(button3, c);
s.add(button3);
c.gridwidth = 1;
gridbag.setConstraints(button4, c);
s.add(button4);
gridbag.setConstraints(button5, c);
s.add(button5);
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(button6, c);
s.add(button6);
c.gridwidth = 1;
gridbag.setConstraints(button7, c);
s.add(button7);
gridbag.setConstraints(button8, c);
s.add(button8);
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(button9, c);
s.add(button9);
c.fill = GridBagConstraints.BOTH;
gridbag.setConstraints(ripeti, c);
s.add(ripeti);
c.gridwidth = 0;
gridbag.setConstraints(stopButton, c);
s.add(stopButton);
// Aggiunta del JPanel s
getContentPane().add("Center",s);
GridBagLayout gr = new GridBagLayout();
GridBagConstraints c1 = new GridBagConstraints();
p.setLayout(gr);
c1.insets.top = 10;
c1.insets.bottom = 60;
c1.insets.left = 10;
c1.insets.right = 10;
gr.setConstraints(HappyDays, c1);
p.add(HappyDays);
p.add(combo);
c1.insets.left = 10;
gr.setConstraints(help, c1);
p.add(help);
getContentPane().add("South",p);
getContentPane().add("East", testi);
getContentPane().add("West", about);
/*
initializing the audio clip, big advantage to use the variable (it then preloads the midi files into memory). The start/stop will be very fast, ok the startup is slow.
*/
try{
clip[0]=getAudioClip(new URL(getCodeBase(),"happy days.midi"));
clip[0].loop();
//play clip10 in the background, in the meantime load the other
clip[1]=getAudioClip(new URL(getCodeBase(),"Ray Charles - I've Got A Woman.wav"));
clip[2]=getAudioClip(new URL(getCodeBase(),"Carl Perkins - Blue Suede Shoes.wav"));
clip[3]=getAudioClip(new URL(getCodeBase(),"Dean Martin & Frank Sinatra - That's Amore.wav"));
clip[4]=getAudioClip(new URL(getCodeBase(),"Elvis Presley - Hound Dog.wav"));
clip[5]=getAudioClip(new URL(getCodeBase(),"Elvis Presley - Tutti Frutti.wav"));
clip[6]=getAudioClip(new URL(getCodeBase(),"The Four Aces - Love Is A Many Splendored Thing.wav"));
clip[7]=getAudioClip(new URL(getCodeBase(),"chuck berry & bo diddley - roll over beethoven.wav"));
clip[8]=getAudioClip(new URL(getCodeBase(),"MARILIN MONROE.- Bye Bye Baby.wav"));
clip[9]=getAudioClip(new URL(getCodeBase(),"COLONNA SONORA - Happy Days.wav"));
branoCorrente = clip[0];}
catch (MalformedURLException muEx) {}
}
//stop all the song, before starting a new one. Avoid the "composite" sound.
public void stop_all() {
clip[1].stop();
clip[2].stop();
clip[3].stop();
clip[4].stop();
clip[5].stop();
clip[6].stop();
clip[7].stop();
clip[8].stop();
clip[9].stop();
clip[0].stop();
}
public void destroy() {
stop_all();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==button1) {
stop_all();
branoCorrente = clip[1];
branoCorrente.play();
} else if (e.getSource()==button2) {
stop_all();
branoCorrente = clip[2];
branoCorrente.play();
} else if (e.getSource()==button3) {
stop_all();
branoCorrente = clip[3];
branoCorrente.play();
} else if (e.getSource()==button4) {
stop_all();
branoCorrente = clip[4];
branoCorrente.play();
} else if (e.getSource()==button5) {
stop_all();
branoCorrente = clip[5];
branoCorrente.play();
} else if (e.getSource()==button6) {
stop_all();
branoCorrente = clip[6];
branoCorrente.play();
} else if (e.getSource()==button7) {
stop_all();
branoCorrente = clip[7];
branoCorrente.play();
} else if (e.getSource()==button8) {
stop_all();
branoCorrente = clip[8];
branoCorrente.play();
} else if (e.getSource()==button9) {
stop_all();
branoCorrente = clip[9];
branoCorrente.play();
} else if (e.getSource()==stopButton)
stop_all();
else if (e.getSource()==ripeti)
branoCorrente.loop();
else if (e.getSource()==HappyDays) {
about.setIcon(new ImageIcon("AboutHappyDays.jpg"));
testi.setIcon(null);
} else {
JComboBox cb = (JComboBox)e.getSource();
String tex = (String)cb.getSelectedItem();
testi.setIcon(new ImageIcon(tex + ".jpg"));
about.setIcon(null);
}
}
}
import java.awt.*;
import javax.imageio.*;
import javax.swing.*;
import java.io.*;
import java.awt.image.BufferedImage;
class PanelI extends Panel {
BufferedImage img;
int width;
int height;
public PanelI (File f){
super(true); //crea un Panel con doubleBuffered true
try{
setImage(ImageIO.read(f));
} catch(Exception e) {}
}
public void setImage(BufferedImage img){
this.img = img;
width = img.getWidth();
height = img.getHeight();
}
// sovrascritto il metodo paintComponent passandogli l'immagine e centrandola
public void paintComponent(Graphics g){
super.paintComponent(g);Dimension d = getSize();
Insets in = getInsets();
int client_width = (d.width - in.right + in.left) / 2;
int client_height = (d.height - in.bottom + in.top) / 2;
int image_width = img.getWidth(this) / 2;
int image_height = img.getHeight(this) / 2;
g.drawImage(img,client_width - image_width, client_height - image_height, this);
}
}
Comunque quello che vorrei sapere è se posso visualizzare l'html da browser oppure mettere la JApplet in una JFrame e far funzionare il tutto con un main(ho provato, ma mi da errore con getCodeBase)...mi vanno bene entrambe le soluzioni, basta che funzionino!