//cd c:\HPBasic\Toppage-sample3\MySwing\Slideshow100 //javac Slideshow30.java //set CLASSPATH=. //appletviewer Slideshow30.html //appletviewer Slideshow30.java import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.*; /* */ public class Slideshow30 extends JApplet implements ActionListener,Runnable { Container contentPane; SlidePanel30 slidepanel30; JButton startButton; JButton stopButton; AudioClip onceClip; Thread t; int i = 1; int n = 7; //NUM_IMAGES public void init(){ contentPane = getContentPane(); System.out.println("Slideshow30 init 1"); slidepanel30 = new SlidePanel30(); System.out.println("Slideshow30 init 2"); Dimension d =new Dimension(600,480); 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(slidepanel30,BorderLayout.CENTER); minipanel.add(startButton); minipanel.add(stopButton); minipanel.setBackground(Color.green); slidepanel30.setBackground(Color.yellow); } public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if (source == startButton) { String midiFile = "coroptica.mid"; 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) { i++; if (i > n) i=1; System.out.println("run " + " i = " + i); start(); Thread.sleep(3000); } } catch (Exception e) { } } public void start(){ String imageName = "kosmos" + i + ".jpg"; String str; URL baseURL = getCodeBase(); ImageIcon icon = new ImageIcon(getURL(baseURL,imageName)); switch(i) { case 1: str="コスモス1"; break; case 2: str="コスモス2"; break; case 3: str="コスモス3"; break; case 4: str="コスモス4"; break; case 5: str="コスモス5"; break; case 6: str="コスモス6"; break; case 7: str="コスモス7"; break; default: str="不明"; } System.out.println("start 1" + " i = " + i + " str = " + str); slidepanel30=new SlidePanel30(icon,str); System.out.println("start 2"); slidepanel30.init(); } protected URL getURL(URL codeBase, String filename) { URL url = null; try { url = new URL(codeBase, filename); } catch (java.net.MalformedURLException e) { System.out.println("Couldn't create image: badly specified URL"); return null; } return url; } public void stop() { t=null; } } class SlidePanel30 extends JPanel { ImageIcon icon; String str; SlidePanel30(){ System.out.println("SlidePanel30 引数なし"); } SlidePanel30(ImageIcon icon,String str){ this.icon = icon; this.str= str; System.out.println("SlidePanel30" + " str = " + str ); } public void init() { System.out.println("SlidePanel30 init"); repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); System.out.println("paintComponent"); g.drawImage(icon.getImage(),0,0,null); g.setFont(new Font("Serif", Font.BOLD, 30)); g.setColor(Color.blue); g.drawString("" + str,300,510); // System.out.println("paint i = " + i + " imageName = " + imageName); } } /* C:\HPBasic\Toppage-sample3\MySwing\Slideshow100>appletviewer Slideshow30.java Slideshow30 init 1 SlidePanel30 引数なし Slideshow30 init 2 start 1 i = 1 str = コスモス1 SlidePanel30 str = コスモス1 start 2 SlidePanel30 init paintComponent java.lang.NullPointerException at SlidePanel30.paintComponent(Slideshow30.java:148) */