Skip to content

Commit 5ab8f07

Browse files
committed
update PixelWindow, improve documentation comments
1 parent 870c75e commit 5ab8f07

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

src/main/scala/introprog/PixelWindow.scala

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package introprog
22

3-
/** PixelWindow events and application management. */
3+
/** A window for pixel-based drawing in an underlying Swing window. */
44
object PixelWindow {
55
/** Immediately exit running application, close all windows, kills all threads. */
66
def exit(): Unit = System.exit(0)
@@ -71,13 +71,13 @@ object PixelWindow {
7171
}
7272
}
7373

74-
/** A window with a canvas for pixel-based drawing.
74+
/** A window with a canvas for pixel-based drawing implemented using Swing.
7575
*
7676
* @constructor Create a new window for pixel-based drawing.
7777
* @param width the number of horizontal pixels of the drawing canvas inside the window
7878
* @param height number of vertical pixels of the drawing canvas inside the window
7979
* @param title the title of the window
80-
* @param background the background color filling the canvas and used when clearing pixels
80+
* @param background the background color filling the canvas when clearing pixels
8181
* @param foreground the foreground color used as default argument for color parameters
8282
*/
8383
class PixelWindow(
@@ -97,16 +97,33 @@ class PixelWindow(
9797
new java.util.concurrent.LinkedBlockingQueue[java.awt.AWTEvent](queueCapacity)
9898

9999
@volatile private var _lastEventType = Event.Undefined
100+
101+
/** The event type of the latest event in the event queue.
102+
*
103+
* Returns Event.Undefined if no event has occurred. See also [[introprog.PixelWindow.awaitEvent]]
104+
*/
100105
def lastEventType: Int = _lastEventType
101106

102107
@volatile private var _lastKeyText = ""
108+
109+
/** A string representing the last key pressed.
110+
*
111+
* Returns an empty string if no key event has occurred.
112+
*/
103113
def lastKey: String = _lastKeyText
104114

105-
@volatile private var _lastMousePos = (0, 0)
115+
@volatile private var _lastMousePos = (-1, -1)
116+
117+
/** A pair of integers with the coordinates of the last mouse event.
118+
*
119+
* Returns `(-1, -1)` if no mouse event has occurred.
120+
*/
121+
106122
def lastMousePos: (Int, Int) = _lastMousePos
107123

108-
initFrame()
124+
initFrame() // initialize listeners, show frame, etc.
109125

126+
/** Event dispatching, translating internal AWT events to exposed events. */
110127
private def handleEvent(e: java.awt.AWTEvent): Unit = e match {
111128
case me: java.awt.event.MouseEvent =>
112129
_lastMousePos = (me.getX, me.getY)
@@ -141,10 +158,13 @@ class PixelWindow(
141158
throw new IllegalArgumentException(s"Unknown WindowEvent: $e")
142159
}
143160
case _ =>
144-
throw new IllegalArgumentException(s"Unknown AWTEvent: $e")
161+
throw new IllegalArgumentException(s"Unknown Event: $e")
145162
}
146163

147-
/** Wait for next event until `timeoutInMillis` and if time is out `lastEventType` is `Undefined`*/
164+
/** Wait for next event until `timeoutInMillis` milliseconds.
165+
*
166+
* If time is out the `lastEventType` is `Undefined`.
167+
*/
148168
def awaitEvent(timeoutInMillis: Long): Unit = {
149169
val e = eventQueue.poll(timeoutInMillis, java.util.concurrent.TimeUnit.MILLISECONDS)
150170
if (e != null) handleEvent(e) else _lastEventType = Event.Undefined
@@ -160,35 +180,43 @@ class PixelWindow(
160180
g.drawLine(x1, y1, x2, y2)
161181
}
162182

183+
/** Fill a rectangle with upper left corner at `(x, y)` using `color` */
163184
def fill(x: Int, y: Int, width: Int, height: Int, color: java.awt.Color = foreground): Unit =
164185
canvas.withGraphics { g =>
165186
g.setColor(color)
166187
g.fillRect(x, y, width, height)
167188
}
168189

190+
/** Set the color of the pixel at `(x, y)`. */
169191
def setPixel(x: Int, y: Int, color: java.awt.Color = foreground): Unit =
170192
canvas.withImage { img =>
171193
img.setRGB(x, y, color.getRGB)
172194
}
173195

196+
/** Clear the pixel at `(x, y)` using the `background` class parameter. */
174197
def clearPixel(x: Int, y: Int): Unit =
175198
canvas.withImage { img =>
176199
img.setRGB(x, y, background.getRGB)
177200
}
178201

202+
/** Return the color of the pixel at `(x, y)`. */
179203
def getPixel(x: Int, y: Int): java.awt.Color = Swing.await {
180204
new java.awt.Color(canvas.img.getRGB(x, y))
181205
}
182206

207+
/** Show the window. Has no effect if the window is already visible. */
183208
def open(): Unit = Swing { frame.setVisible(true) }
184209

210+
/** Hide the window. Has no effect if the window is already hidden. */
185211
def close(): Unit = Swing { frame.setVisible(false); frame.dispose() }
186212

213+
/** Clear all pixels using the `background` class parameter. */
187214
def clear(): Unit = canvas.withGraphics { g =>
188215
g.setColor(background)
189216
g.fillRect(0, 0, width, height)
190217
}
191218

219+
/** Draw `text` at `(x, y)` using `color`, `size`, `style and `fontName`. */
192220
def drawText(
193221
text: String,
194222
x: Int,
@@ -209,6 +237,7 @@ class PixelWindow(
209237
}
210238
}
211239

240+
/** Create the underlying window and add listeners for event management. */
212241
private def initFrame(): Unit = Swing {
213242
Swing.init() // first time calls setPlatformSpecificLookAndFeel
214243
javax.swing.JFrame.setDefaultLookAndFeelDecorated(true)

0 commit comments

Comments
 (0)