Skip to content

Commit 6f6e1d7

Browse files
Improve getPixel error handling
Adding a require to getPixel makes the exception for out-of-bounds coordinates be thrown in the calling thread, which makes the stack trace easier to understand.
1 parent bd7b383 commit 6f6e1d7

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/main/scala/introprog/PixelWindow.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,9 @@ class PixelWindow(
200200
}
201201

202202
/** Return the color of the pixel at `(x, y)`. */
203-
def getPixel(x: Int, y: Int): java.awt.Color = Swing.await {
204-
new java.awt.Color(canvas.img.getRGB(x, y))
203+
def getPixel(x: Int, y: Int): java.awt.Color = {
204+
require(x >= 0 && x < width && y >= 0 && y < width, "Tried to read a pixel outside the window")
205+
Swing.await { new java.awt.Color(canvas.img.getRGB(x, y)) }
205206
}
206207

207208
/** Show the window. Has no effect if the window is already visible. */

0 commit comments

Comments
 (0)