2013-04-09 138 views
0

最近我開始學習Jython,現在我有一個簡單的問題。我想提高我的動畫質量。不幸的是,我不知道如何爲我的小程序添加雙緩衝。你可以幫幫我嗎? 此致敬禮!Jython雙緩衝

from javax.swing import JToolBar 
from javax.swing import JButton 
from javax.swing import JFrame 
import time 
from java import awt 
from java.awt import BorderLayout 


class Canvas(awt.Canvas): 
    u"Canvas - drawing area" 
    def __init__(self,winSize = 400): 
     self.play = False 
     self.background=awt.Color.black 
     self.winSize = winSize 
     self.l = 0 
    def playSim(self, play): 
     if play == True: 
      self.play = True 
      self.repaint() 
     else: self.play = False 
    def paint(self, g):  
     g.fillRect(50, int(self.winSize/4), self.l, int(self.winSize/2)) 
     if self.l < self.winSize: self.l += 1 
     else: self.l = 0 
     time.sleep(0.02) 
     if self.play == True: self.repaint() 


class Example(JFrame): 
    def __init__(self): 
     super(Example, self).__init__() 
     self.initUI() 

    def initUI(self): 
     winSize = 600 
     toolbar = JToolBar() 
     self.playButton = JButton("Start", actionPerformed=self.playButtonPress) 
     toolbar.add(self.playButton) 
     self.add(toolbar, BorderLayout.NORTH) 

     self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) 
     self.setSize(winSize, winSize) 
     self.setResizable(False) 
     self.setLocationRelativeTo(None) 
     self.setVisible(True) 
     self.canvas = Canvas(winSize) 
     self.getContentPane().add(self.canvas) 
     self.setTitle("TEST") 
     self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) 

    def playButtonPress(self, e): 
     if self.playButton.getLabel() == "Start": 
      self.canvas.playSim(True) 
      self.playButton.setLabel("Stop") 
     else: 
      self.playButton.setLabel("Start") 
      self.canvas.playSim(False) 

if __name__ == '__main__': 
    Example() 
+0

請指出你做了什麼研究,爲什麼他們沒有幫助。例如。是http://stackoverflow.com/questions/581085/double-buffering-with-wxpython適用,還是該電話不可用? – TheDuke 2013-04-09 20:49:37

回答

0

我解決我的問題,最近:

from javax.swing import JToolBar 
from javax.swing import JButton 
from javax.swing import JFrame 
import time 
from java import awt 
from java.awt import BorderLayout 


class Canvas(awt.Canvas): 
    u"Canvas - drawing area" 
    def __init__(self,winSize = 400): 
     self.play = False 
     self.background=awt.Color.black 
     self.winSize = winSize 
     self.l = 0 
     self.bi = BufferedImage(winSize, winSize, BufferedImage.TYPE_INT_RGB) 
     self.offScreenGraphics = self.bi.getGraphics() 
    def playSim(self, play): 
     if play == True: 
      self.play = True 
      self.repaint() 
     else: self.play = False 
    def paint(self, g):  
     self.offScreenGraphics.fillRect(50, int(self.winSize/4), self.l, int(self.winSize/2)) 
     if self.l < self.winSize: self.l += 1 
     else: self.l = 0 
     g.drawImage(self.bi, 0, 0, None) 
     time.sleep(0.02) 
     if self.play == True: self.repaint() 


class Example(JFrame): 
    def __init__(self): 
     super(Example, self).__init__() 
     self.initUI() 

    def initUI(self): 
     winSize = 600 
     toolbar = JToolBar() 
     self.playButton = JButton("Start", actionPerformed=self.playButtonPress) 
     toolbar.add(self.playButton) 
     self.add(toolbar, BorderLayout.NORTH) 

     self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) 
     self.setSize(winSize, winSize) 
     self.setResizable(False) 
     self.setLocationRelativeTo(None) 
     self.setVisible(True) 
     self.canvas = Canvas(winSize) 
     self.getContentPane().add(self.canvas) 
     self.setTitle("TEST") 
     self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) 

    def playButtonPress(self, e): 
     if self.playButton.getLabel() == "Start": 
      self.canvas.playSim(True) 
      self.playButton.setLabel("Stop") 
     else: 
      self.playButton.setLabel("Start") 
      self.canvas.playSim(False) 

if __name__ == '__main__': 
    Example() 

現在我另一個(而瑣碎的)問題: 如何從這個Python文件 *類文件*這將是使準備在網站上發佈它作爲一個小程序?