//cd c:\HPBasic\Toppage-sample3\MySwing4 //set CLASSPATH=. //javac slideshow\Slideshow.java package slideshow; import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.*; public class Slideshow extends JApplet implements ActionListener, Runnable { SlidePanel slidepanel; JButton startButton; JButton stopButton; AudioClip onceClip; Thread t; Image[] images; int height; int i = 0; public String slideName; public String midiFile; public String[] str; public int n ; //NUM_IMAGES public void init(){ Container contentPane = getContentPane(); slidepanel = new SlidePanel(); Dimension d = getSize(); height = d.height; JPanel minipanel= new JPanel(); startButton = new JButton("Start"); stopButton = new JButton("Stop"); startButton.addActionListener(this); stopButton.addActionListener(this); contentPane.add(minipanel,BorderLayout.NORTH); contentPane.add(slidepanel,BorderLayout.CENTER); minipanel.add(startButton); minipanel.add(stopButton); minipanel.setBackground(Color.green); slidepanel.setBackground(Color.yellow); images = new Image[n]; for (int j = 0; j < n; j++) { String imageName = slideName + j + ".jpg"; try { loadImage(new URL(getDocumentBase(),imageName),j); // images[j] = getImage(new URL(getDocumentBase(),imageName)); } catch (Exception e) {} } } public void loadImage(URL url,int j) throws InterruptedException { // thrown by MediaTracker.waitFor images[j] = getImage(url); MediaTracker tracker = new MediaTracker(this); tracker.addImage(images[j], 0); tracker.waitForID(0); /* int imageWidth = images[j].getWidth(null); int imageHeight = images[j].getHeight(null); resize(imageWidth, imageHeight);*/ } public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if (source == startButton) { onceClip = getAudioClip(getCodeBase(),midiFile); onceClip.play(); //AudioPlay // スレッドを起動する t = new Thread(this); t.start(); } if (source == stopButton) { onceClip.stop(); //AudioStop stop(); } } public void run() { try { Thread mythread = Thread.currentThread(); while(t == mythread) { slidepanel.init(); Thread.sleep(3000); i++; if (i == n) i=0; } } catch (Exception e) { } } public void stop() { t=null; } class SlidePanel extends JPanel { public void init() { repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(images[i],50,40,null); g.setFont(new Font("Serif", Font.BOLD, 30)); g.setColor(Color.blue); g.drawString(str[i],100,height-50); } } }