From: Finn B. <bc...@us...> - 2000-11-12 22:50:33
|
Update of /cvsroot/jython/htdocs/applets In directory slayer.i.sourceforge.net:/tmp/cvs-serv20224 Added Files: button.ht checkbox.ht choice.ht converter.ht coordinates.ht index.ht issues.ht label.ht links.h list.ht problems.ht Log Message: First version. --- NEW FILE --- Title: Button Example <h3>Using Buttons</h3> <p>This example shows how to use Buttons from Jython. Three buttons should be displayed in the applet below. <applet code="ButtonDemo" archive="appletdemo.jar" width = 500 height = 35 alt="This browser doesn't support JDK 1.1 applets."> <param name=cabbase0 value="appletdemo.cab"> <h3>Something has gone wrong loading this applet.</h3> </applet> <p>The complete source code for this example is included below. <blockquote><pre><hr> from java import awt, applet class ButtonDemo(applet.Applet): def init(self): self.b1 = awt.Button('Disable middle button', actionPerformed=self.disable) self.b2 = awt.Button('Middle button') self.b3 = awt.Button('Enable middle button', enabled=0, actionPerformed=self.enable) self.add(self.b1) self.add(self.b2) self.add(self.b3) def enable(self, event): self.b1.enabled = self.b2.enabled = 1 self.b3.enabled = 0 def disable(self, event): self.b1.enabled = self.b2.enabled = 0 self.b3.enabled = 1 <hr></pre></blockquote> The init method creates three buttons with the appropriate labels. It also specifies the actions to be performed when the first and third buttons are clicked. These actions are specified using <a href="../docs/properties.html">event properties</a>. Finally, this method adds these three buttons to the applet (which is using the default FlowLayout). <p>The enable and disable methods change the states of the buttons as appropriate. Notice that this is done using the enabled <a href="../docs/properties.html">property</a> of the Buttons. <p> --- NEW FILE --- Title: Checkbox Example <h3>Using Checkboxes</h3> <p>This example shows how to use Checkboxes from Jython. <p> <applet code="CheckboxDemo" archive="appletdemo.jar" width = 350 height = 90 alt="This browser doesn't support JDK 1.1 applets."> <h3>Something has gone wrong loading this applet.</h3> </applet> <p>The complete source code for this example is included below. <blockquote><pre><hr> from java import awt, applet class CheckboxDemo(applet.Applet): def init(self): cb1 = awt.Checkbox('Checkbox 1') cb2 = awt.Checkbox('Checkbox 2') cb3 = awt.Checkbox('Checkbox 3', state=1) p1 = awt.Panel(layout=awt.FlowLayout()) p1.add(cb1) p1.add(cb2) p1.add(cb3) cbg = awt.CheckboxGroup() cb4 = awt.Checkbox('Checkbox 4', cbg, 0) cb5 = awt.Checkbox('Checkbox 5', cbg, 0) cb6 = awt.Checkbox('Checkbox 6', cbg, 0) p2 = awt.Panel(layout=awt.FlowLayout()) p2.add(cb4) p2.add(cb5) p2.add(cb6) self.setLayout(awt.GridLayout(0, 2)) self.add(p1) self.add(p2) self.validate() <hr></pre></blockquote> The first panel (p1) holds three checkboxes that are not grouped together in any way. The third checkbox is initially set to checked using its state <a href="../docs/properties.html">property</a> as a keyword argument. The second panel holds a group of three checkboxes that all belong to the same checkbox group. <p> --- NEW FILE --- Title: Choice Example <h3>Using Choices</h3> <p>This example shows how to use Choices from Jython. <p> <applet code="ChoiceDemo" archive="appletdemo.jar" width = 350 height = 40 alt="This browser doesn't support JDK 1.1 applets."> <h2>Something has gone wrong loading this applet.</h2> </applet> <p>The complete source code for this example is included below. <blockquote><pre><hr> from java import awt, applet class ChoiceDemo(applet.Applet): def init(self): self.choices = awt.Choice(itemStateChanged = self.change) for item in ['ichi', 'ni', 'san', 'yon']: self.choices.addItem(item) self.label = awt.Label() self.change() self.add(self.choices) self.add(self.label) def change(self, event=None): selection = self.choices.selectedIndex, self.choices.selectedItem self.label.text = 'Item #%d selected. Text = "%s".' % selection <hr></pre></blockquote> The init method first creates a Choice object and sets its callback for when a new item is selected. This callback is specified as the itemStateChanged <a href="../docs/properties.html">event property</a>. Then four items are added to the choice object and the rest of the layout is initialized. <p> The change method is invoked whenever the selection in the choice object changes. It uses Python string formatting operator to display the current state to the user. <p> --- NEW FILE --- Title: Metric to English Conversion Applet</title> <h3>A Metric to English Converter</h3> <p>This example shows how to put a number of different gui components together to build a complete system. It does the amazing task of converting between the metric and english measurement systems. <p> <applet code="Converter" archive="appletdemo.jar" width = 500 height = 150 alt="This browser doesn't support JDK 1.1 applets."> <param name=cabbase0 value="appletdemo.cab"> <h3>Something has gone wrong loading this applet.</h3> </applet> <p>The complete source code for this example is included below. <blockquote><pre><hr> from java import awt from java.applet import Applet from java.awt.event import ActionListener, ItemListener, AdjustmentListener from pawt import GridBag basicUnits = [ ['Metric System', [('Centimeters', 0.01), ('Meters', 1.0), ('Kilometers', 1000.0)]], ['U.S. System', [('Inches', 0.0254), ('Feet', 0.305), ('Yards', 0.914), ('Miles', 1613.0)]]] class SimpleBorder: def paint(self, g): g.drawRect(0,0,self.size.width-1, self.size.height-1) def getInsets(self): return awt.Insets(5,5,5,5) class Converter(Applet, SimpleBorder): def init(self, unitSets=basicUnits): self.setLayout(awt.GridLayout(2,0,5,5)) self.panels = [] for name, units in unitSets: panel = ConversionPanel(name, units, self) self.panels.append(panel) self.add(panel) def convert(self, master): value = master.getValue() multiplier = master.getMultiplier() for panel in self.panels: if panel is not master: panel.setValue(multiplier/panel.getMultiplier()*value) class ConversionPanel(awt.Panel, SimpleBorder, ActionListener, AdjustmentListener, ItemListener): max, block = 10000, 100 def __init__(self, title, units, controller): self.units = units self.controller = controller awt.Panel.__init__(self) bag = GridBag(self, fill='HORIZONTAL') label = awt.Label(title, awt.Label.CENTER) bag.addRow(label) self.text = awt.TextField('0', 10, actionListener=self) bag.add(self.text, weightx=1.0) self.chooser = awt.Choice(itemListener=self) for name, multiplier in units: self.chooser.add(name) bag.addRow(self.chooser) self.slider = awt.Scrollbar(awt.Scrollbar.HORIZONTAL, maximum=self.max+10, blockIncrement=self.block, adjustmentListener=self) bag.add(self.slider) def getMultiplier(self): return self.units[self.chooser.selectedIndex][1] def getValue(self): try: return float(self.text.getText()) except: return 0.0 def actionPerformed(self, e): self.setSlider(self.getValue()) self.controller.convert(self) def itemStateChanged(self, e): self.controller.convert(self) def adjustmentValueChanged(self, e): self.text.setText(str(e.getValue())) self.controller.convert(self) def setValue(self, v): self.text.setText(str(v)) self.setSlider(v) def setSlider(self, f): if f > self.max: f = self.max if f < 0: f = 0 self.slider.value = int(f) <hr></pre></blockquote> Hopefully after looking over the previous examples, this code should be fairly easy to read. If not, you'll have to wait until I have the time to write up a good explanation for this example. <p> --- NEW FILE --- Title: Simple Drawing Example <h3>Drawing to a Canvas</h3> <p>This example shows how to draw to a Canvas from Jython. This is low level drawing using the basic Java Graphics API's. <p> <applet code="CoordinatesDemo" archive="appletdemo.jar" width = 300 height = 200 alt="This browser doesn't support JDK 1.1 applets."> <param name=cabbase0 value="appletdemo.cab"> <h3>Something has gone wrong loading this applet.</h3> </applet> <p>The complete source code for this example is included below. <blockquote><pre><hr> from java import applet, awt from pawt import GridBag class CoordinatesDemo(applet.Applet): def init(self): bag = GridBag(self) self.framedArea = FramedArea(self) bag.addRow(self.framedArea, weighty=1.0, fill='BOTH') self.label = awt.Label('Click within the framed area') bag.addRow(self.label, weightx=1.0, weighty=0.0, fill='HORIZONTAL') def updateLabel(self, point): text = 'Click occurred at coordinate (%d, %d).' self.label.text = text % (point.x, point.y) class FramedArea(awt.Panel): def __init__(self, controller): awt.Panel.__init__(self) self.background = awt.Color.lightGray self.setLayout(awt.GridLayout(1,0)) self.add(CoordinateArea(controller)) def getInsets(self): return awt.Insets(4,4,5,5) def paint(self, g): d = self.size g.color = self.background g.draw3DRect(0, 0, d.width-1, d.height-1, 1) g.draw3DRect(3, 3, d.width-7, d.height-7, 1) class CoordinateArea(awt.Canvas): def __init__(self, controller): awt.Canvas.__init__(self) self.mousePressed = self.push self.controller = controller def push(self, e): try: self.point.x = e.x self.point.y = e.y except AttributeError: self.point = awt.Point(e.x, e.y) self.repaint() def paint(self, g): if hasattr(self, 'point'): self.controller.updateLabel(self.point) g.fillRect(self.point.x-1, self.point.y-1, 2, 2) <hr></pre></blockquote> This example defines three classes. The first is the actual applet, CoordinatesDemo sets up the label where text is displayed, and the FramedArea panel where drawing takes place. This is an example of using the GridBag utility class provided with Jython to make using the GridBagLayout style easier. This is a very powerful layout manager, and I strongly encourage you to consider using it for your own applications. Hopefully the GridBag utility class will make this powerful layout manager much easier to use in Jython. <p> The second class is a very simple framed area. It simply draws a border around an interior CoordinateArea. <p> The final class is the CoordinateArea which does the actual drawing. At creation time, this class sets up the callback so that its push method will be invoked when the user presses the mouse within its frame. The push method simply updates the current location of the point (setting it if it hasn't been defined yet). <p> The paint method does the actual drawing (only if the user has clicked on some point). It first updates its controller's label widget with the current coordinates, and then it draws the point the user has selected using the fillRect method on the Graphics object. <p> --- NEW FILE --- Title: Easy Applets with Jython <H3>Jython Applet Demos</H3> <UL> <B>Note: You must be running a <A HREF="problems.html">JDK 1.1 compliant web browser</A> to view the applets on this page.</B> </UL> <P> <CENTER> <APPLET Code="JythonLoader" WIDTH="220" HEIGHT="90" ALIGN="BOTTOM"> </APPLET> </CENTER> Wait until the Jython library has completely loaded before continuing. This library is about 200K and should take around 1 minute to download over a 28.8 modem. You should expect considerably faster download times if you have a better network connection. <em>Here is the <a href="JythonLoader.java">Java source</a> code for the Jython loading display you see above.</em> <P>Once the library has loaded, your first Jython applet will appear below: <CENTER> <APPLET ARCHIVE="appletdemo.jar" CODE="HelloWorld" WIDTH="160" HEIGHT="50" ALIGN="BOTTOM" alt="This browser doesn't support JDK 1.1 applets."> <PARAM NAME="cabbase0" VALUE="appletdemo.cab"> <H3>Something has gone wrong loading this applet.</H3> </APPLET> </CENTER> If you don't see the text "Hello from Jython!" above, then something has gone wrong. See <A HREF="problems.html">what to do when Jython applets don't work</A> for suggestions on how to fix the problem. <P>The complete source code for this applet is displayed below: <BR> <blockquote><pre> <HR> from java.applet import Applet class HelloWorld(Applet): def paint(self, g): g.drawString("Hello from Jython!" 20, 30) <HR> </pre></blockquote> <H3>More Examples of Jython Applets</H3> Examples based on <A HREF="http://java.sun.com/docs/books/tutorial">The Java Tutorial</A> Examples <UL> <LI>Using the basic AWT GUI elements <UL> <LI><A HREF="button.html">ButtonDemo</A> <LI><A HREF="checkbox.html">CheckboxDemo</A> <LI><A HREF="choice.html">ChoiceDemo</A> <LI><A HREF="label.html">LabelDemo</A> <LI><A HREF="list.html">ListDemo</A> </UL> <LI>Low-level drawing <UL> <LI><A HREF="coordinates.html">CoordinatesDemo</A> </UL> <LI>Putting it all together <UL> <LI><A HREF="converter.html">Converter</A> - a simple metric to english conversion applet </UL> </UL> </BODY> </HTML> --- NEW FILE --- Title: Applet Issues <h3>Jython Applet Issues</h3> <ol> <li>The Jython libraries add about 150K to the size of any applets. Due to the caching behavior of most browsers, this is a one-time cost that users must pay only the first time they download any applet (from your web pages) which uses Jython. Currently this problem is more severe as a result of Netscapes limitations on using multiple jar files for a single applet. This requires bundling the library and the applet code together. Netscape is aware of this problem and plans to fix it in a future release. <li>Can't use "exec" or "eval" in (unsigned) applets. Because Jython takes the approach of compiling Python source code directly to Java bytecodes, it can only dynamically interpret Python code if it can dynamically load Java bytecode. This is currently impossible without creating a custom ClassLoader and this operation violates Java's security model. Any applet that wants to dynamically execute arbitrary strings of Python code must be signed and given permission by the user. <p>This is much less of a problem for Python than it would be for most languages because the dynamic features of the language mean that exec and eval are actually needed very rarely in practice. <P>I'm hoping that JavaSoft will eventually fix the design of the ClassLoader API to make this possible in a secure fashion. It is currently possible to kludge together a solution by shipping the bytes for the new Python class back to the server and then downloading them back to the client, but I can't imagine this effort will be worthwhile. However, the fact that it can be done convinces me that there is no inherent security problem with the functionality that Jython wants. <P>If somebody has a really strong need for exec and/or eval in a Jython applet, it would certainly be possible to add this support. This would involve implementing a true Python interpreter instead of the current dynamic Python -> Java bytecode compiler. I haven't yet seen an application that is sufficiently compelling to convince me that this effort would be worth my time (not to mention the hassle of maintaining two parallel code bases - one for the interpreter and one for the compiler). <li>When will Netscape truly support JDK 1.1? </ul> <p> --- NEW FILE --- Title: Label Example <h3>Using Labels</h3> <p>This example shows how to use Labels from Jython. <p> <applet code="LabelDemo" archive="appletdemo.jar" width = 150 height = 100 alt="This browser doesn't support JDK 1.1 applets."> <h3>Something has gone wrong loading this applet.</h3> </applet> <p>The complete source code for this example is included below. <blockquote><pre><hr> from java import applet from java.awt import Label, GridLayout class LabelDemo(applet.Applet): def init(self): self.setLayout(GridLayout(0,1)) self.add(Label('Left')) self.add(Label('Center', Label.CENTER)) self.add(Label('Right', Label.RIGHT)) <hr></pre></blockquote> Three labels are created with different alignments. The first has the default left alignment and the other two are centered and right aligned. <p> --- NEW FILE --- <!-- -*- html -*- --> <h3>Home</h3> <li><a href="../index.html">Overview</a> <li><a href="../license.html">License</a> <li><a href="../download.html">Jython 2.0</a> <li><a href="../install.html">Installing</a> <li><a href="../platform.html">JVM Compatibility</a> <h3>Applets</h3> <li><a href="index.html">Demos</a> <li><a href="button.html">ButtonDemo</a> <li><a href="checkbox.html">CheckboxDemo</a> <li><a href="choice.html">ChoiceDemo</a> <li><a href="label.html">LabelDemo</a> <li><a href="list.html">ListDemo</a> <li><a href="coordinates.html">CoordinatesDemo</a> <li><a href="converter.html">ConvertDemo</a> <h3>Applet Problems</h3> <li><a href="problems.html">Here's what to do</a> <li><a href="issues.html">Other applet issues</a> --- NEW FILE --- Title: List Example <h3>Using Lists</h3> <p>This example shows how to use Lists from Jython. <p> <applet code="ListDemo" archive="appletdemo.jar" width = 500 height = 150 alt="This browser doesn't support JDK 1.1 applets."> <h3>Something has gone wrong loading this applet.</h3> </applet> <p>The complete source code for this example is included below. <blockquote><pre><hr> from java import applet, awt from java.awt.event import ItemEvent from pawt import GridBag class ListDemo(applet.Applet): def fillList(self, list, names): list.actionPerformed=self.action list.itemStateChanged=self.change for name in names: list.add(name) def init(self): self.spanish = awt.List(4, 1) self.fillList(self.spanish, ['uno', 'dos', 'tres', 'cuatro', 'cinco', 'seis', 'siete']) self.italian = awt.List() self.fillList(self.italian, ['uno', 'due', 'tre', 'quattro', 'cinque', 'sei', 'sette']) self.output = awt.TextArea(10, 40, editable=0) bag = GridBag(self) bag.add(self.output, fill='BOTH', weightx=1.0, weighty=1.0, gridheight=2) bag.addRow(self.spanish, fill='VERTICAL') bag.addRow(self.italian, fill='VERTICAL') self.language = {self.spanish:'Spanish', self.italian:'Italian'} def action(self, e): list = e.source text = 'Action event occurred on "%s" in %s.\n' self.output.append(text % (list.selectedItem, self.language[list])) def change(self, e): list = e.source if e.stateChange == ItemEvent.SELECTED: select = 'Select' else: select = 'Deselect' text = '%s event occurred on item #%d (%s) in %s.\n' params = (select, e.item, list.getItem(e.item), self.language[list]) self.output.append(text % params) <hr></pre></blockquote> The fillList method is defined to take a list and a set of names and insert those names as list items. It also sets the action methods for the list for both single and double clicking. <p> This init method creates and fills lists of spanish and italian numbers. It uses a GridBag to layout the lists and a text widget in the applet. This GridBag is a wrapper placed on the awt.GridBagLayout and GridBagConstraints classes to make this very powerful layout method easier to use. <p> The action method is invoked whener a list item is double clicked. It uses Python's % operator on strings for convenient text formatting. <p> The change method is invoked when a list item is selected or deselected. It's primary difference from the Java implementation is to use a dictionary of languages instead of a case statement to determine the appropriate text to display. This is an example of the ease of use of standard data types (lists and dictionaries) from Python. <p> --- NEW FILE --- Title: Solving Applet Problems <H3>Solving Jython Applet Problems</H3> <P>If you are having problems getting a Jython applet to run on your browser, you should follow the following steps. <p>1. Make sure that you have a jdk 1.1 compliant browser. At the moment these include: <OL> <LI><A HREF="http://www.javasoft.com/products/hotjava">SUN's HotJava</A> <UL> <LI>Unsurprisingly, this browser works extremely well with Java applets </UL> <LI><A HREF="http://www.microsoft.com/ie/">Microsoft's Internet Explorer 4.0</A> <UL> <LI>Somewhat surprisingly, IE 4.0 is far superior to Netscape 4.0 for JDK 1.1 compliance and performance. This might have finally been fixed with Navigator 4.06, but I haven't had the time to do a detailed comparision recently. </UL> <LI><A HREF="http://home.netscape.com/browsers/">Netscape's Navigator 4.06 or later</A> <UL> <LI>The latest release of Netscape's browser finally comes with acceptable JDK 1.1 support without needing any patches. All I can say is, it's about time! <LI>Note: It has been reported that the JVM in the Linux release of Navigator 4.06 has serious problems that render it unusable. Similar problems might occur on other Unix platforms. </UL> <LI><I>SUN's Java Activator - not currently enabled</I> <UL> <LI><I>Jython applets should almost certainly work with the Java Activator, but I haven't yet invested the time to get the demo of this up and running.</I> </UL> </OL> <P>2. Make sure you don't have any old releases of Jython in your class path. <OL> <LI>If you are running a SUN (or SUN-like) VM you need to make sure your CLASSPATH environment variable is not pointing to any directories with Jython .class files <LI>If you are running a MS VM, you should probably check out the registry entry under LOCAL_MACHINE/Software/Microsoft/Java VM/Classpath </OL> <P>3. Send mail to <A HREF="mailto:jyt...@li...">jython-dev</A> explaining your problem. Include the following information: <OL> <LI>What OS (name and version) and Browser (name and version) you are running <LI>A copy of your Java console window after failing to run an applet <UL> <LI>Under IE, you need to explicitly enable the Java console, or Java logging </UL> <LI>The URL of the applet that didn't work for you </OL> <p> |