Skip to content

Commit ac61ef3

Browse files
committed
- add paint_app sketch
1 parent 503f9db commit ac61ef3

File tree

5 files changed

+106
-1
lines changed

5 files changed

+106
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ The official source code for the examples, as well as solutions to challenges, f
4444

4545
## Chapter 11: Mouse and Keyboard Interaction
4646

47-
<a href="chapter-11-mouse_and_keyboard_interaction/scratch_art"><img src="img/ch11-scratch_art.png" height="200" /></a>
47+
<a href="chapter-11-mouse_and_keyboard_interaction/scratch_art"><img src="img/ch11-scratch_art.png" height="200" /></a><a href="chapter-11-mouse_and_keyboard_interaction/paint_app"><img src="img/ch11-paint_app.png" height="200" /></a>
4848

4949
## Miscellaneous
5050

Binary file not shown.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
def setup():
2+
size(600, 600)
3+
background('#004477')
4+
ernest = createFont('Ernest.ttf', 20)
5+
textFont(ernest)
6+
noLoop()
7+
cursor(CROSS)
8+
9+
10+
swatches = ['#FF0000', '#FF9900', '#FFFF00', '#00FF00', '#0099FF', '#6633FF']
11+
brushcolor = swatches[2]
12+
brushshape = ROUND
13+
brushsize = 3
14+
painting = False
15+
paintmode = 'free'
16+
palette = 60
17+
18+
19+
def draw():
20+
print(frameCount)
21+
global painting, paintmode
22+
23+
if mouseX < palette:
24+
paintmode = 'select'
25+
26+
if paintmode == 'free':
27+
28+
if painting:
29+
stroke(brushcolor)
30+
strokeCap(brushshape)
31+
strokeWeight(brushsize)
32+
line(mouseX, mouseY, pmouseX, pmouseY)
33+
34+
elif frameCount > 1:
35+
painting = True
36+
37+
# black panel
38+
noStroke()
39+
fill('#000000'); rect(0, 0, palette, height)
40+
# color swatches
41+
p, p2 = palette, palette/2
42+
fill(swatches[0]); square(0, 0, p2)
43+
fill(swatches[1]); square(p2, 0, p2)
44+
fill(swatches[2]); square(0, p2, p2)
45+
fill(swatches[3]); square(p2, p2, p2)
46+
fill(swatches[4]); square(0, p, p2)
47+
fill(swatches[5]); square(p2, p, p2)
48+
# brush preview
49+
fill(brushcolor)
50+
if brushshape == ROUND:
51+
circle(30, 123, brushsize)
52+
paintmode = 'free'
53+
# clear button
54+
fill('#FFFFFF')
55+
text('CLEAR', 10, height-12)
56+
57+
58+
def mousePressed():
59+
# start painting
60+
if mouseButton == LEFT:
61+
loop()
62+
# swatch select
63+
if mouseButton == LEFT and mouseX < palette and mouseY < 90:
64+
global brushcolor
65+
brushcolor = get(mouseX, mouseY)
66+
67+
68+
def mouseReleased():
69+
# stop painting
70+
if mouseButton == LEFT:
71+
global painting
72+
painting = False
73+
noLoop()
74+
75+
76+
def mouseWheel(e):
77+
# resize the brush
78+
global brushsize, paintmode
79+
paintmode = 'select'
80+
brushsize += e.count
81+
if brushsize < 3:
82+
brushsize = 3
83+
if brushsize > 45:
84+
brushsize = 45
85+
redraw()
86+
87+
88+
def keyPressed():
89+
global brushcolor, paintmode
90+
paintmode = 'select'
91+
# color swatch shortcuts
92+
if key.isdigit():
93+
k = int(key) - 1
94+
if k < len(swatches):
95+
brushcolor = swatches[k]
96+
redraw()
97+
98+
99+
def mouseClicked():
100+
# clear canvas
101+
if mouseButton == LEFT and mouseX < 60 and mouseY > height-30:
102+
fill('#004477')
103+
rect(palette, 0, width, height)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mode=Python
2+
mode.id=jycessing.mode.PythonMode

img/ch11-paint_app.png

1.47 KB
Loading

0 commit comments

Comments
 (0)