added TextDisplay and GraphicsDisplay
This commit is contained in:
53
src/magic_squares/GraphicsDisplay.scala
Normal file
53
src/magic_squares/GraphicsDisplay.scala
Normal file
@@ -0,0 +1,53 @@
|
||||
package magic_squares
|
||||
|
||||
import hevs.graphics.FunGraphics
|
||||
|
||||
import java.awt.{Color, Font}
|
||||
import javax.swing.SwingConstants
|
||||
|
||||
trait GraphicsDisplay extends Displayable {
|
||||
private var _win: Option[FunGraphics] = None
|
||||
private val WIDTH: Int = 400
|
||||
private val HEIGHT: Int = 400
|
||||
private val MARGIN: Int = 20
|
||||
private val FONT: Font = new Font("Arial", Font.PLAIN, 24)
|
||||
override def display(g: Grid): Unit = {
|
||||
if (_win.isEmpty) {
|
||||
_win = Some(new FunGraphics(WIDTH, HEIGHT))
|
||||
}
|
||||
val win: FunGraphics = _win.get
|
||||
win.clear()
|
||||
val size: Int = g.length
|
||||
|
||||
val w: Int = WIDTH - 2 * MARGIN
|
||||
val h: Int = HEIGHT - 2 * MARGIN
|
||||
val gridSize: Int = math.min(w, h)
|
||||
val cellSize: Int = gridSize / size
|
||||
val ox: Int = (WIDTH - gridSize) / 2
|
||||
val oy: Int = (HEIGHT - gridSize) / 2
|
||||
|
||||
win.drawRect(ox, oy, gridSize, gridSize)
|
||||
for (i: Int <- 1 until size) {
|
||||
win.drawLine(ox + cellSize * i, oy, ox + cellSize * i, oy + gridSize)
|
||||
win.drawLine(ox, oy + cellSize * i, ox + gridSize, oy + cellSize * i)
|
||||
}
|
||||
|
||||
for (y: Int <- 0 until size) {
|
||||
for (x: Int <- 0 until size) {
|
||||
val v: Int = g(y)(x)
|
||||
if (v != 0) {
|
||||
win.drawString(
|
||||
(ox + cellSize * (x + 0.5)).toInt,
|
||||
(oy + cellSize * (y + 0.5)).toInt,
|
||||
v.toString,
|
||||
FONT,
|
||||
Color.BLACK,
|
||||
SwingConstants.CENTER,
|
||||
SwingConstants.CENTER
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
win.syncGameLogic(5)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user