//cd c:\HPBasic\Toppage-sample3\MySwing //set CLASSPATH=. //javac Slideshow.java //appletviewer Slideshow.html //appletviewer Slideshow.java 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; ImageIcon[] images; Thread t; int i = 0; int height; int n = 7; //NUM_IMAGES String[] str={"コスモス(昭和記念公園 2003)","コスモス1","コスモス2", "コスモス3","コスモス4","コスモス5","コスモス6"}; String slideName = "kosmos"; String midiFile= "coroptica.mid"; 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 ImageIcon[n]; for (int j = 0; j < n; j++) { String imageName = slideName + j + ".jpg"; try{images[j]=new ImageIcon(new URL(getCodeBase(),imageName)); } catch (Exception e) {} } } 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); // System.out.println("paint i = " + i + " str = " + str); images[i].paintIcon(this,g,50,30); // g.drawImage(icon.getImage(),50,30,null); g.setFont(new Font("Serif", Font.BOLD, 30)); g.setColor(Color.blue); g.drawString("" + str[i],220,height-50); } } } /* ついにできたと思ったもの。1週間かかる。ただし、これは内部クラスに書き換えた。 Slideshow3とほとんど同じだが、説明文を配列にしたこと、getURLメソッドを簡略化したくらいか? コメントにした String imageName = "../image/slideshow/kosmos" + j + ".jpg"; でもネットでは大丈夫だが、ローカルではだめなので、写真は同一フォルダにおく。 URL getURL(URL codeBase, String filename) に代わる try { URL url = new URL(baseURL,imageName); } catch (MalformedURLException e) { } は失敗 Slideshow.java:58: シンボルを解決できません。 シンボル: 変数 url 場所 images[j] = new ImageIcon(url); その後、内部クラスとした(下記はめその流れ)。別クラスでは再現できなかった。 init height = 600 paintComponent i= 0 str[i] = コスモス(昭和記念公園 2003) height = 600 run i = 0 SlidePanel init i= 0 str[i]コスモス(昭和記念公園 2003) paintComponent i= 0 str[i] = コスモス(昭和記念公園 2003) height = 600 run i = 1 SlidePanel init i= 1 str[i]コスモス1 paintComponent i= 1 str[i] = コスモス1 height = 600 run i = 2 SlidePanel init i= 2 str[i]コスモス2 paintComponent i= 2 str[i] = コスモス2 height = 600 run i = 3 SlidePanel init i= 3 str[i]コスモス3 paintComponent i= 3 str[i] = コスモス3 height = 600 */