//cd c:\HPBasic\Toppage-sample3\MySwing\Slideshow100 //set CLASSPATH=. //javac Slideshow50.java //appletviewer Slideshow50.html //appletviewer Slideshow50.java import java.applet.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.*; /* */ public class Slideshow50 extends JApplet implements ActionListener, Runnable { Container contentPane; JButton startButton; JButton stopButton; AudioClip onceClip; SlidePanel50 slidepanel50; public ImageIcon[] images; Thread t; int n = 7; //NUM_IMAGES public String[] str={"コスモス(昭和記念公園 2003)","コスモス1","コスモス2", "コスモス3","コスモス4","コスモス5","コスモス6"}; String slideName = "kosmos"; String midiFile= "coroptica.mid"; public int i=0; public int height; public ImageIcon img; public String str2; public void init(){ contentPane = getContentPane(); JPanel minipanel= new JPanel(); startButton = new JButton("Start"); stopButton = new JButton("Stop"); startButton.addActionListener(this); stopButton.addActionListener(this); 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) {} } Dimension d = getSize(); height = d.height; System.out.println("init height = " + height); contentPane.add(minipanel,BorderLayout.NORTH); minipanel.add(startButton); minipanel.add(stopButton); minipanel.setBackground(Color.green); slidepanel50=new SlidePanel50(); contentPane.add(slidepanel50,BorderLayout.CENTER); slidepanel50.setBackground(Color.yellow); } 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(); /*img=images[i]; str2=str[i]; System.out.println("run i = " + i + " str2 = " + str2); slidepanel50.init(); */ i=0; while(t == mythread) { start(); Thread.sleep(3000); i++; if (i == n) i=0; } } catch (Exception e) { } } public void start() { img=images[i]; str2=str[i]; System.out.println("start i = " + i + " str2 = " + str2); slidepanel50.init(); } public void stop() { t=null; } } class SlidePanel50 extends JPanel { Slideshow50 ss; int k; String string; ImageIcon imgicon; int height2; public void init() { ss =new Slideshow50(); k=ss.i; string=ss.str2; height2= ss.height; System.out.println("SlidePanel50 init k= " + k + " string = " + string + " height2 = " + height2); imgicon=ss.img; repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); System.out.println("paintComponent" + " string = " + string + " height2 = " + height2); g.setFont(new Font("Serif", Font.BOLD, 30)); g.setColor(Color.blue); g.drawString(string,220,height2-50); imgicon.paintIcon(this,g,50,30); } } /* 1 SlidePanel50から、Slideshow50のオブジェクト変数、str2,img,heightを呼び、表示させようとした。いずれもpublicとして「事なれり」と思ったら、これがだめ。 初期値しか呼べないようだ。 2 そこで、getimg,getstr,getheightとして、メソッドを使おうとしてもだめ。heightにいたっては、initで600となるのに、getheightでは初期値の0。同じクラスの中で値が伝わらないのはなぜ? イベントやスレッドのため? よんでいるのがSlidePanel50からだからか? 3 initではなく、SlidePanel50の冒頭にっていってもだめ。 Slideshow50 ss=new Slideshow50(); int k=ss.i; String string=ss.str2; ImageIcon imgicon; int height2= ss.height; C:\HPBasic\Toppage-sample3\MySwing\Slideshow100>appletviewer Slideshow50.java init height = 600 start i = 0 str2 = コスモス(昭和記念公園 2003) SlidePanel50 init k= 0 string = null height2 = 0 paintComponent string = null height2 = 0 java.lang.NullPointerException: string is null at sun.java2d.SunGraphics2D.drawString(SunGraphics2D.java:2534) at SlidePanel50.paintComponent(Slideshow50.java:129) str2はnullでなかったこともある? C:\HPBasic\Toppage-sample3\MySwing\Slideshow100>appletviewer Slideshow init height = 600 start i = 0 geti i = 0 getstr str2 = コスモス(昭和記念公園 2003) getheight height = 0 SlidePanel5 init k= 0 string = コスモス(昭和記念公園 2003) height = 0 paintComponent string = コスモス(昭和記念公園 2003) height = 0 start i = 0 geti i = 0 getstr str2 = コスモス(昭和記念公園 2003) getheight height = 0 SlidePanel5 init k= 0 string = コスモス(昭和記念公園 2003) height = 0 paintComponent string = コスモス(昭和記念公園 2003) height = 0 start i = 1 geti i = 0 getstr str2 = コスモス(昭和記念公園 2003) getheight height = 0 SlidePanel5 init k= 0 string = コスモス(昭和記念公園 2003) height = 0 paintComponent string = コスモス(昭和記念公園 2003) height = 0 start i = 2 geti i = 0 getstr str2 = コスモス(昭和記念公園 2003) getheight height = 0 SlidePanel5 init k= 0 string = コスモス(昭和記念公園 2003) height = 0 paintComponent string = コスモス(昭和記念公園 2003) height = 0 */