code_junkie

Cross-platform gui toolkit for deploying Python applications

Nov 14th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. from javax.swing import *
  2.  
  3. frame = JFrame("Hello Jython")
  4. label = JLabel("Hello Jython!", JLabel.CENTER)
  5. frame.add(label)
  6. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
  7. frame.setSize(300, 300)
  8. frame.show()
  9.  
  10. from javax.swing import *
  11.  
  12. frame = JFrame("Hello Jython")
  13. label = JLabel("Hello Jython!", JLabel.CENTER)
  14. frame.add(label)
  15. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
  16. frame.setSize(300, 300)
  17. frame.show()
  18.  
  19. from __future__ import nested_scopes
  20. import java.lang as lang
  21. import java.util as util
  22. import java.awt as awt
  23. import javax.swing as swing
  24.  
  25. class Particle:
  26.  
  27. def __init__(self,initX,initY):
  28. self.x = initX
  29. self.y = initY
  30. self.rng = util.Random()
  31.  
  32. def move(self):
  33. self.x += self.rng.nextInt(10) - 5
  34. self.y += self.rng.nextInt(20) - 10
  35.  
  36. def draw(self,g2):
  37. g2.drawRect(self.x,self.y,10,10)
  38.  
  39. class ParticleCanvas(awt.Canvas):
  40.  
  41. def __init__(self,newSize):
  42. awt.Canvas.__init__(self,size=(newSize,newSize))
  43.  
  44. def paint(self,g2):
  45. for p in self.particles:
  46. p.draw(g2)
  47.  
  48. class ParticleApplet(swing.JApplet):
  49.  
  50. def init(self):
  51. self.canvas = ParticleCanvas(self.getWidth())
  52. self.contentPane.add(self.canvas)
  53.  
  54. def start(self):
  55. n = 10
  56. particles = []
  57. for i in range(n):
  58. particles.append(Particle(150,150))
  59. self.canvas.particles = particles
  60.  
  61. self.threads = []
  62. for i in range(n):
  63. self.threads.append(self.makeThread(particles[i]))
  64. self.threads[i].start()
  65.  
  66. def makeThread(self,p):
  67.  
  68. class MyRunnable(lang.Runnable):
  69. def run(this):
  70. try:
  71. while 1:
  72. p.move()
  73. self.canvas.repaint()
  74. lang.Thread.sleep(100)
  75. except lang.InterruptedException:
  76. return
  77.  
  78. return lang.Thread(MyRunnable())
Add Comment
Please, Sign In to add comment