Exemplo n.º 1
0
class InductionApplet(JApplet):
    def init(self):
        global exampleList
        self.thinFont = Font("Dialog", 0, 10)

        self.pane = self.getContentPane()
        self.examples = exampleList.keys()
        self.examples.sort()
        self.exampleSelector = JList(self.examples, valueChanged=self.valueChanged)
        self.exampleSelector.setSelectionMode(ListSelectionModel.SINGLE_SELECTION)
        self.exampleSelector.setLayoutOrientation(JList.VERTICAL)
        self.exampleSelector.setPreferredSize(Dimension(150,500))
        self.exampleSelector.setBackground(Color(0.95, 0.95, 0.98))
        self.exampleSelector.setFont(self.thinFont)

        self.centerPanel = JPanel(BorderLayout())
        self.canvas = GraphCanvas()
        self.canvas.setApplet(self)
        self.buttonRow = JPanel(FlowLayout())
        self.backButton = JButton("<", actionPerformed = self.backAction)
        self.backButton.setFont(self.thinFont)
        self.continueButton = JButton("continue >",
                                      actionPerformed=self.continueAction)
        self.continueButton.setFont(self.thinFont)
        self.scaleGroup = ButtonGroup()
        self.linearButton = JRadioButton("linear scale",
                                         actionPerformed=self.linearAction)
        self.linearButton.setSelected(True)
        self.linearButton.setFont(self.thinFont)
        self.logarithmicButton = JRadioButton("logarithmic scale",
                                      actionPerformed=self.logarithmicAction)
        self.logarithmicButton.setFont(self.thinFont)
        self.aboutButton = JButton("About...",
                                   actionPerformed=self.aboutAction)
        self.aboutButton.setFont(self.thinFont)
        self.scaleGroup.add(self.linearButton)
        self.scaleGroup.add(self.logarithmicButton)
        self.buttonRow.add(self.backButton)
        self.buttonRow.add(self.continueButton)
        self.buttonRow.add(JLabel(" "*5))
        self.buttonRow.add(self.linearButton)
        self.buttonRow.add(self.logarithmicButton)
        self.buttonRow.add(JLabel(" "*20));
        self.buttonRow.add(self.aboutButton)
        self.centerPanel.add(self.canvas, BorderLayout.CENTER)
        self.centerPanel.add(self.buttonRow, BorderLayout.PAGE_END)

        self.helpText = JTextPane()
        self.helpText.setBackground(Color(1.0, 1.0, 0.5))
        self.helpText.setPreferredSize(Dimension(800,80))
        self.helpText.setText(re_sub("[ \\n]+", " ", """
        Please select one of the examples in the list on the left!
        """))
        self.pane.add(self.exampleSelector, BorderLayout.LINE_START)
        self.pane.add(self.centerPanel, BorderLayout.CENTER)
        self.pane.add(self.helpText, BorderLayout.PAGE_END)
        self.graph = None
        self.simulation = None
        self.touched = ""
        self.selected = ""
        self.gfxDriver = None

    def start(self):
        self.gfxDriver = awtGfx.Driver(self.canvas)
        #self.gfxDriver.setAntialias(True)
        if self.gfxDriver.getSize()[0] < 200:  # konqueror java bug work around
            self.gfxDriver.w = 650
            self.gfxDriver.h = 380
        self.graph = Graph.Cartesian(self.gfxDriver, 1, 0.0, 1000, 1.0,
                                     title="Results",
                                     xaxis="Rounds", yaxis="Success Rate")

    def stop(self):
        pass

    def destroy(self):
        pass

    def refresh(self):
        if self.graph != None: self.graph.redraw()

    def valueChanged(self, e):
        global exampleList
        newSelection = self.examples[self.exampleSelector.getSelectedIndex()]
        if newSelection != self.touched:
            self.touched = newSelection
            text = re_sub("[ \\n]+", " ", exampleList[self.touched][-1])
            self.helpText.setText(text)
        if not e.getValueIsAdjusting() and newSelection != self.selected:
            self.selected = newSelection
            smallFontPen = copy.copy(Gfx.BLACK_PEN)
            smallFontPen.fontSize = Gfx.SMALL
            ex = exampleList[self.selected]
            myStyleFlags = self.graph.styleFlags
            if self.simulation != None:  self.simulation.stop()
            self.gfxDriver.resizedGfx() # konqueror 3.5.5 java bug workaround
            self.graph = Graph.Cartesian(self.gfxDriver, 1, 0.0, ex[3], 1.0,
                                         title=ex[0],
                                         xaxis="Rounds", yaxis="Success Rate",
                                         styleFlags = myStyleFlags,
                                         axisPen = smallFontPen,
                                         captionPen = smallFontPen)
            self.zoomFrame = [(1, 0.0, ex[3], 1.0)]
            self.simulation = Simulation(self.graph, ex[1], ex[2], ex[3], ex[4])
            RunAsThread(self.simulation.simulation).start()

    def determineCurrentZoomFrame(self):
        i = 0
        for zf in self.zoomFrame:
            if self.graph.x2 <= zf[2]: break
            i += 1
        return i

    def backAction(self, e):
        if self.simulation == None:  return
        wasRunning = self.simulation.isRunning
        self.simulation.stop()
        if wasRunning or len(self.zoomFrame) <= 1:  return
        zi = self.determineCurrentZoomFrame()
        if zi > 0 and zi < len(self.zoomFrame):
            x1, y1, x2, y2 = self.zoomFrame[zi-1]
            self.graph.adjustRange(x1, y1, x2, y2)

    def continueAction(self, e):
        if self.simulation == None:  return
        wasRunning = self.simulation.isRunning
        self.simulation.stop()
        zi = self.determineCurrentZoomFrame()
        if zi == len(self.zoomFrame)-1:
            if wasRunning or self.simulation.world.round == self.zoomFrame[zi][2]:
                if self.graph.styleFlags & Graph.LOG_X == 0:
                    self.simulation.rounds *= 2
                else:
                    self.simulation.rounds *= 10
                self.zoomFrame.append((1, 0.0, self.simulation.rounds, 1.0))
                self.graph.adjustRange(1, 0.0, self.simulation.rounds, 1.0)
            RunAsThread(self.simulation.simulation).start()
        else:
            x1, y1, x2, y2 = self.zoomFrame[zi+1]
            self.graph.adjustRange(x1, y1, x2, y2)

    def linearAction(self, e):
        if self.graph != None and (self.graph.styleFlags & Graph.LOG_X) != 0:
            if self.simulation != None:  self.simulation.stop()
            self.graph.setStyle(self.graph.styleFlags & ~Graph.LOG_X, redraw=True)
            if self.simulation != None:
                RunAsThread(self.simulation.simulation).start()

    def logarithmicAction(self, e):
        if self.graph != None and (self.graph.styleFlags & Graph.LOG_X) == 0:
            if self.simulation != None:  self.simulation.stop()
            self.graph.setStyle(self.graph.styleFlags | Graph.LOG_X, redraw=True)
            if self.simulation != None:
                RunAsThread(self.simulation.simulation).start()

    def aboutAction(self, e):
        aboutText = """Induction Applet v. 0.1

        (c) 2007 University of Düsseldorf

        Authors: Gerhard Schurz, Eckhart Arnold
        """
        aboutText = re_sub(" +", " ", aboutText)
        JOptionPane.showMessageDialog(self.getContentPane(), aboutText)