Skip to content

Instantly share code, notes, and snippets.

@danwatford
Last active August 29, 2015 14:01
Show Gist options
  • Save danwatford/db1564be85c0f570d5e5 to your computer and use it in GitHub Desktop.
Save danwatford/db1564be85c0f570d5e5 to your computer and use it in GitHub Desktop.
/**
* A Panel which will draw a grid of cells as either On or Off according to a given function.
*/
class OnOffGrid(val cellsX: Int, val cellsY: Int, cellWidth: Int = 20, cellHeight: Int = 20) extends Panel with Publishe
r {
preferredSize = new Dimension(cellsX * cellWidth, cellsY * cellHeight)
listenTo(mouse.clicks)
reactions += {
case MouseClicked(_, point, _, _, _) => publish(CellClicked(this, point.x / cellWidth, point.y / cellHeight))
}
/** Function to determine whether a particular cell at position (x, y) should be on (true) or off (false). */
var onOff: (Int, Int) => Boolean = { (_, _) => false }
override def paintComponent(g: Graphics2D) {
for (x <- 0 to cellsX; y <- 0 to cellsY; on = onOff(x, y)) {
drawCell(g, x, y, on)
}
}
def drawCell(g: Graphics2D, x: Int, y: Int, on: Boolean) {
val r = new Rectangle(x * cellWidth, y * cellHeight, cellWidth, cellHeight)
if (on) g.setColor(Color.BLACK)
else g.setColor(Color.WHITE)
g.draw(r)
g.fill(r)
}
}
/**
* Event signalling that a grid cell has been clicked.
*/
case class CellClicked(source: OnOffGrid, x: Int, y: Int) extends UIEvent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment