-
Notifications
You must be signed in to change notification settings - Fork 46
/
LinePainting.scala
66 lines (56 loc) · 1.96 KB
/
LinePainting.scala
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
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2007-2014, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
package scala.swing.examples
import scala.swing.Swing._
import scala.swing.{SimpleSwingApplication, MainFrame, Panel}
import scala.swing.event._
import java.awt.{Color, Graphics2D, Point, geom}
/**
* Dragging the mouse draws a simple graph
*
* @author Frank Teubler, Ingo Maier
*/
object LinePainting extends SimpleSwingApplication {
lazy val ui = new Panel {
background = Color.white
preferredSize = (200, 200)
focusable = true
listenTo(mouse.clicks, mouse.moves, keys)
reactions += {
case e: MousePressed =>
moveTo(e.point)
requestFocusInWindow()
case e: MouseDragged => lineTo(e.point)
case e: MouseReleased => lineTo(e.point)
case KeyTyped(_, 'c', _, _) =>
path = new geom.GeneralPath
repaint()
case _: FocusLost => repaint()
}
/* records the dragging */
var path = new geom.GeneralPath
def lineTo(p: Point): Unit = {
path.lineTo(p.x, p.y); repaint()
}
def moveTo(p: Point): Unit = {
path.moveTo(p.x, p.y); repaint()
}
override def paintComponent(g: Graphics2D) = {
super.paintComponent(g)
g.setColor(new Color(100, 100, 100))
g.drawString("Press left mouse button and drag to paint." +
(if (hasFocus) " Press 'c' to clear." else ""), 10, size.height - 10)
g.setColor(Color.black)
g.draw(path)
}
}
def top = new MainFrame {
title = "Simple Line Painting Demo"
contents = ui
}
}