Skip to content

Commit 51f9f25

Browse files
authored
Merge pull request #5 from jos-rosenqvist-3812/getpixel-require
Improve getPixel error handling
2 parents bd7b383 + 6f6e1d7 commit 51f9f25

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)