Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Diff of /htdocs/applets/converter.ht [000000] .. [110918] Maximize Restore

  Switch to side-by-side view

--- a
+++ b/htdocs/applets/converter.ht
@@ -0,0 +1,120 @@
+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>