在Jython中进行双缓冲

时间:2013-04-09 20:24:44

标签: jython double-buffering

最近我开始学习Jython,现在我的问题很简单。我想提高动画的质量。不幸的是我不知道如何为我的applet添加双缓冲。你可以帮帮我吗? 最好的问候!

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()

1 个答案:

答案 0 :(得分: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文件 *制作类文件*,它可以作为小程序在网站上发布?