HTML: <applet name="SlideBar" code="SlideBarApplet.class" width=400 height="150" MAYSCRIPT> <param name="RED" value="1"/> <param name="GREEN" value="2"/> <param name="BLUE" value="3"/> </applet>
Applet: getParameter("BLUE") getParameter("GrEeN") getParameter("red")
JScriptと連携するAppletを呼び出す時には、<applet>タグに、MAYSCRIPT属性を追加する必要がある。
HTML: <applet name="SlideBar" code="SlideBarApplet.class" width=400 height="150" MAYSCRIPT>
HTML: <applet name="SlideBar" code="SlideBarApplet.class" width=400 height="150" MAYSCRIPT> <param name="RED" value="1"/> <param name="GREEN" value="2"/> <param name="BLUE" value="3"/> </applet>のsetColor(String,Strng,String)メソッドを呼び出す場合、
HTML: function sendValue(){ document.SlideBar.setColor( document.ColorForm.red.value , document.ColorForm.green.value , document.ColorForm.blue.value ); }appletオブジェクトは、window.documet.applet[0]としても指定できる。
HTML: function sendValue(){ document.applet[0].setColor( document.ColorForm.red.value , document.ColorForm.green.value , document.ColorForm.blue.value ); }
HTML: function recvValue( red , green , blue ){ document.ColorForm.red.value = red; document.ColorForm.green.value = green; document.ColorForm.blue.value = blue; }
Applet: Object[] args = new Object[3]; args[0] = new Integer(rSlider.getValue()); args[1] = new Integer(gSlider.getValue()); args[2] = new Integer(bSlider.getValue()); JSObject win = JSObject.getWindow(this); win.call("recvValue", args);
JRE1.4.1以前 C:\j2sdk1.4.1\jre\lib\jaws.jar JRE1.4.2 C:\j2sdk1.4.2\jre\lib\plugin.jar JRE1.5.0 C:\Program Files\Java\jre1.5.0\lib\plugin.jar
SlideBarApplet.html : <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=shift_jis"> <script language="JavaScript"> function recvValue( red , green , blue ){ document.ColorForm.red.value = red; document.ColorForm.green.value = green; document.ColorForm.blue.value = blue; } function sendValue(){ document.SlideBar.setColor( document.ColorForm.red.value , document.ColorForm.green.value , document.ColorForm.blue.value ); } </script> </head> <body> <table border="1"><tr><td> Applet: <br/> <applet name="SlideBar" code="SlideBarApplet.class" width=400 height="150" MAYSCRIPT> <param name="RED" value="1"/> <param name="GREEN" value="2"/> <param name="BLUE" value="3"/> </applet> </td></tr> <tr><td> HTML: <br/> <form name="ColorForm"> <p> R:<INPUT type="text" name="red" value="255"/> G:<INPUT type="text" name="green" value="255"/> B:<INPUT type="text" name="blue" value="255"/> </p> <p align="center"> <input type="button" value="Appletへ送信" onclick="sendValue();"/> </p> </form> </td></tr></table> </body> </html>
SlideBarApplet.java: public class SlideBarApplet extends JApplet implements ChangeListener, ActionListener { private JSlider bSlider = null; private JButton button = null; private JSlider gSlider = null; private JSlider rSlider = null; /** * HTMLの<param name="RED" value="xxx">タグから * valueを受け取るメソッド */ public int getInitialRed(){ return Integer.parseInt(getParameter("RED")); } /** * HTMLの<param name="GREEN" value="xxx">タグから * valueを受け取るメソッド */ public int getInitialGreen(){ return Integer.parseInt(getParameter("GREEN")); } /** * HTMLの<param name="BLUE" value="xxx">タグから * valueを受け取るメソッド */ public int getInitialBlue(){ return Integer.parseInt(getParameter("BLUE")); } /** * JScriptから呼び出されるメソッド */ public void setColor(String red, String green, String blue) { rSlider.setValue(Integer.parseInt(red)); gSlider.setValue(Integer.parseInt(green)); bSlider.setValue(Integer.parseInt(blue)); updateButton(); repaint(); } public void init() { rSlider = new JSlider(0, 255, getInitialRed() ); gSlider = new JSlider(0, 255, getInitialGreen() ); bSlider = new JSlider(0, 255, getInitialBlue() ); JPanel p = new JPanel(); button = new JButton(); button.setPreferredSize( new Dimension(200,25) ); p.add(button); rSlider.addChangeListener(this); gSlider.addChangeListener(this); bSlider.addChangeListener(this); button.addActionListener(this); getContentPane().setLayout(new GridLayout(4, 1)); getContentPane().add(rSlider); getContentPane().add(gSlider); getContentPane().add(bSlider); getContentPane().add(p); } public void start() { updateButton(); } /** * Appletのスライドバーが操作された */ public void stateChanged(ChangeEvent e) { updateButton(); repaint(); } /** * Appletのボタンが押された */ public void actionPerformed(ActionEvent e) { Object[] args = new Object[3]; args[0] = new Integer(rSlider.getValue()); args[1] = new Integer(gSlider.getValue()); args[2] = new Integer(bSlider.getValue()); JSObject win = JSObject.getWindow(this); win.call("recvValue", args); } private void updateButton() { int red = rSlider.getValue(); int green = gSlider.getValue(); int blue = bSlider.getValue(); Color color = new Color(red, green, blue); button.setForeground(color); button.setText("HTMLに送信 [R" + red + "G" + green + "B" + blue + "]"); } }