Skip to content

Commit 5559fdc

Browse files
committed
improve documentation, minor refactoring and fixes
1 parent 877be92 commit 5559fdc

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

src/main/scala/introprog/Dialog.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package introprog
22

3+
/** A module with utilities for creating standard GUI dialogs. */
34
object Dialog {
45
import javax.swing.{JFileChooser, JOptionPane, JColorChooser}
56

src/main/scala/introprog/IO.scala

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

3+
/** A model with input/output operations from/to the underlying file system. */
34
object IO {
5+
/** Load a string from a text file called `fileName` using encoding `enc`. */
46
def loadString(fileName: String, enc: String = "UTF-8"): String = {
57
//This implementation risk leak open file handles:
68
// scala.io.Source.fromFile(fileName, enc).mkString
@@ -11,6 +13,7 @@ object IO {
1113
result
1214
}
1315

16+
/** Load string lines from a text file called `fileName` using encoding `enc`. */
1417
def loadLines(fileName: String, enc: String = "UTF-8"): Vector[String] = {
1518
//This implementation risk leak open file handles:
1619
// scala.io.Source.fromFile(fileName, enc).getLines.toVector
@@ -21,39 +24,49 @@ object IO {
2124
result
2225
}
2326

24-
def saveString(s: String, fileName: String, enc: String = "UTF-8"): Unit = {
27+
/** Save `text` to a text file called `fileName` using encoding `enc`. */
28+
def saveString(text: String, fileName: String, enc: String = "UTF-8"): Unit = {
2529
val f = new java.io.File(fileName)
2630
val pw = new java.io.PrintWriter(f, enc)
27-
try pw.write(s) finally pw.close()
31+
try pw.write(text) finally pw.close()
2832
}
2933

34+
/** Save `lines` to a text file called `fileName` using encoding `enc`. */
3035
def saveLines(lines: Seq[String], fileName: String, enc: String = "UTF-8"): Unit =
3136
saveString(lines.mkString("\n"), fileName, enc)
3237

38+
/** Load a serialized object from a binary file called `fileName`. */
3339
def loadObject[T](fileName: String): T = {
3440
val f = new java.io.File(fileName)
3541
val ois = new java.io.ObjectInputStream(new java.io.FileInputStream(f))
3642
try { ois.readObject.asInstanceOf[T] } finally ois.close()
3743
}
3844

45+
/** Serialize `obj` to a binary file called `fileName`. */
3946
def saveObject[T](obj: T, fileName: String): Unit = {
4047
val f = new java.io.File(fileName)
4148
val oos = new java.io.ObjectOutputStream(new java.io.FileOutputStream(f))
4249
try oos.writeObject(obj) finally oos.close()
4350
}
4451

52+
/** Test if a file with name `fileName` exists. */
4553
def isExisting(fileName: String): Boolean = new java.io.File(fileName).exists
4654

55+
/** Create a directory with name ´dir´ if it does not exist. */
4756
def createDirIfNotExist(dir: String): Boolean = new java.io.File(dir).mkdirs()
4857

58+
/** Return the path name or the current user's home directory. */
4959
def userDir: String = System.getProperty("user.home")
5060

61+
/** Return the path name or the current working directory. */
5162
def currentDir: String =
5263
java.nio.file.Paths.get(".").toAbsolutePath.normalize.toString
5364

65+
/** Return a sequence of file names in the directory `dir`. */
5466
def list(dir: String = "."): Vector[String] =
5567
Option(new java.io.File(dir).list).map(_.toVector).getOrElse(Vector())
5668

69+
/** Change name of file `from`, DANGER: silently replaces existing `to`. */
5770
def move(from: String, to: String): Unit = {
5871
import java.nio.file.{Files, Paths, StandardCopyOption}
5972
Files.move(Paths.get(from), Paths.get(to), StandardCopyOption.REPLACE_EXISTING)

src/main/scala/introprog/PixelWindow.scala

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

3-
/** A window for pixel-based drawing in an underlying Swing window. */
3+
/** A module with utilities for event handling in `PixelWindow` instances. */
44
object PixelWindow {
55
/** Immediately exit running application, close all windows, kills all threads. */
66
def exit(): Unit = System.exit(0)
77

88
/** Idle waiting for `millis` milliseconds. */
99
def delay(millis: Long): Unit = Thread.sleep(millis)
1010

11-
/** An object with strings describing events that can happen in a PixelWindow, see [[introprog.PixelWindow.Event]] */
11+
/** An object with integers representing events that can happen in a PixelWindow. */
1212
object Event {
1313
/** An integer representing a key down event.
1414
*
@@ -57,7 +57,7 @@ object PixelWindow {
5757
*/
5858
val Undefined = 0
5959

60-
/** Returns a descriptive string with name of the `event` number */
60+
/** Returns a descriptive text for each `event`. */
6161
def show(event: Int): String = event match {
6262
case KeyPressed => "KeyPressed"
6363
case KeyReleased => "KeyReleased"
@@ -71,14 +71,14 @@ object PixelWindow {
7171
}
7272
}
7373

74-
/** A window with a canvas for pixel-based drawing implemented using Swing.
74+
/** A window with a canvas for pixel-based drawing.
7575
*
7676
* @constructor Create a new window for pixel-based drawing.
77-
* @param width the number of horizontal pixels of the drawing canvas inside the window
78-
* @param height number of vertical pixels of the drawing canvas inside the window
77+
* @param width the number of horizontal pixels
78+
* @param height number of vertical pixels
7979
* @param title the title of the window
80-
* @param background the background color filling the canvas when clearing pixels
81-
* @param foreground the foreground color used as default argument for color parameters
80+
* @param background the color used when clearing pixels
81+
* @param foreground the foreground color, default color in drawing operations
8282
*/
8383
class PixelWindow(
8484
val width: Int = 800,
@@ -165,7 +165,7 @@ class PixelWindow(
165165
*
166166
* If time is out, `lastEventType` is `Undefined`.
167167
*/
168-
def awaitEvent(timeoutInMillis: Long): Unit = {
168+
def awaitEvent(timeoutInMillis: Long = 1): Unit = {
169169
val e = eventQueue.poll(timeoutInMillis, java.util.concurrent.TimeUnit.MILLISECONDS)
170170
if (e != null) handleEvent(e) else _lastEventType = Event.Undefined
171171
}
@@ -180,7 +180,7 @@ class PixelWindow(
180180
g.drawLine(x1, y1, x2, y2)
181181
}
182182

183-
/** Fill a rectangle with upper left corner at `(x, y)` using `color` */
183+
/** Fill a rectangle with upper left corner at `(x, y)` using `color`. */
184184
def fill(x: Int, y: Int, width: Int, height: Int, color: java.awt.Color = foreground): Unit =
185185
canvas.withGraphics { g =>
186186
g.setColor(color)
@@ -205,10 +205,10 @@ class PixelWindow(
205205
}
206206

207207
/** Show the window. Has no effect if the window is already visible. */
208-
def open(): Unit = Swing { frame.setVisible(true) }
208+
def show(): Unit = Swing { frame.setVisible(true) }
209209

210210
/** Hide the window. Has no effect if the window is already hidden. */
211-
def close(): Unit = Swing { frame.setVisible(false); frame.dispose() }
211+
def hide(): Unit = Swing { frame.setVisible(false); frame.dispose() }
212212

213213
/** Clear all pixels using the `background` class parameter. */
214214
def clear(): Unit = canvas.withGraphics { g =>

src/main/scala/introprog/Swing.scala

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package introprog
22

3-
/** A module with Swing utilities. */
3+
/** A module with Swing utilities used by [[introprog.PixelWindow]]. */
44
object Swing {
55

66
private def runInSwingThread(callback: => Unit): Unit =
77
javax.swing.SwingUtilities.invokeLater(() => callback)
88

9-
/** Run `callback` in Swing thread using `javax.swing.SwingUtilities.invokeLater`. */
9+
/** Run `callback` asynchronously in the Swing thread. */
1010
def apply(callback: => Unit): Unit = runInSwingThread(callback)
1111

12+
/** Run `callback` in the Swing thread and block until completion. */
1213
def await[T: scala.reflect.ClassTag](callback: => T): T = {
1314
val ready = new java.util.concurrent.CountDownLatch(1)
1415
val result = new Array[T](1)
@@ -20,24 +21,27 @@ object Swing {
2021
result(0)
2122
}
2223

24+
/** Return a sequence of available look and feel options. */
2325
def installedLookAndFeels: Vector[String] =
2426
javax.swing.UIManager.getInstalledLookAndFeels.toVector.map(_.getClassName)
2527

28+
/** Find a look and feel with a name including `partOfName`. */
2629
def findLookAndFeel(partOfName: String): Option[String] =
2730
installedLookAndFeels.find(_.toLowerCase contains partOfName)
2831

32+
/** Test if the current operating system name includes `partOfName`. */
2933
def isOS(partOfName: String): Boolean =
3034
scala.sys.props("os.name").toLowerCase.contains(partOfName.toLowerCase)
3135

32-
var isInit = false
36+
private var isInit = false
3337

3438
/** Init the Swing GUI toolkit and set platform-specific look and feel.*/
3539
def init(): Unit = if (!isInit) {
3640
setPlatformSpecificLookAndFeel()
3741
isInit = true
3842
}
3943

40-
def setPlatformSpecificLookAndFeel(): Unit = {
44+
private def setPlatformSpecificLookAndFeel(): Unit = {
4145
import javax.swing.UIManager.setLookAndFeel
4246
if (isOS("linux")) findLookAndFeel("gtk").foreach(setLookAndFeel)
4347
else if (isOS("win")) findLookAndFeel("win").foreach(setLookAndFeel)
@@ -72,11 +76,13 @@ object Swing {
7276
true
7377
}
7478

79+
/** Execute `action` in the Swing thread with graphics context as param. */
7580
def withGraphics(action: java.awt.Graphics2D => Unit) = runInSwingThread {
7681
action(img.createGraphics())
7782
repaint()
7883
}
7984

85+
/** Execute `action` in the Swing thread with raw image as param. */
8086
def withImage(action: java.awt.image.BufferedImage => Unit) = runInSwingThread {
8187
action(img)
8288
repaint()

0 commit comments

Comments
 (0)