File: CoordinatesDemo.py

package info (click to toggle)
jython 2.2.1-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 18,708 kB
  • ctags: 46,200
  • sloc: python: 150,937; java: 86,267; xml: 1,080; perl: 104; sh: 93; makefile: 81; ansic: 24
file content (69 lines) | stat: -rw-r--r-- 1,581 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""A rough translation of an example from the Java Tutorial
http://java.sun.com/docs/books/tutorial/

This example shows how to do very simple Canvas Drawing
"""

from java import applet, awt
from pawt import GridBag


class CoordinatesDemo(applet.Applet):
    def init(self):
	bag = GridBag(self)

	self.framedArea = FramedArea(self)
	bag.addRow(self.framedArea, weighty=1.0, fill='BOTH')

	self.label = awt.Label('Click within the framed area')
	bag.addRow(self.label, weightx=1.0, weighty=0.0, fill='HORIZONTAL')

    def updateLabel(self, point):
	text = 'Click occurred at coordinate (%d, %d).'
	self.label.text = text % (point.x, point.y)



class FramedArea(awt.Panel):
    def __init__(self, controller):
	self.background = awt.Color.lightGray
	self.setLayout(awt.GridLayout(1,0))

	self.add(CoordinateArea(controller))

    def getInsets(self):
	return awt.Insets(4,4,5,5)

    def paint(self, g):
	d = self.size

	g.color = self.background
	g.draw3DRect(0, 0, d.width-1, d.height-1, 1)
	g.draw3DRect(3, 3, d.width-7, d.height-7, 1)



class CoordinateArea(awt.Canvas):
    def __init__(self, controller):
	self.mousePressed = self.push
	self.controller = controller

    def push(self, e):
	try:
	    self.point.x = e.x
	    self.point.y = e.y
	except AttributeError:
	    self.point = awt.Point(e.x, e.y)

	self.repaint()

    def paint(self, g):
	if hasattr(self, 'point'):
	    self.controller.updateLabel(self.point)
	    g.fillRect(self.point.x-1, self.point.y-1, 2, 2)



if __name__ == '__main__':
    import pawt
    pawt.test(CoordinatesDemo(), size=(300, 200))