Created
September 24, 2021 13:44
-
-
Save leontabak/fbf49cfa89ef246e1c4207dc8b45f668 to your computer and use it in GitHub Desktop.
Show GUI with Swing in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Color | |
import java.awt.Font | |
import java.awt.GridLayout | |
import java.awt.event.ActionListener | |
import java.awt.event.ActionEvent | |
import javax.swing.JButton | |
import javax.swing.JFrame | |
import javax.swing.JMenu | |
import javax.swing.JMenuBar | |
import javax.swing.JMenuItem | |
import javax.swing.Timer | |
import javax.swing.plaf.basic.BasicBorders.ButtonBorder | |
class ButtonListener(private val button: JButton) : ActionListener { | |
override fun actionPerformed(event: ActionEvent) { | |
button.background = Color.RED | |
} // fun actionPerformed() | |
} // ButtonListener | |
enum class Direction { | |
NORTH, | |
WEST, | |
SOUTH, | |
EAST | |
} // Direction | |
class Game() { | |
var board = listOf( | |
MutableList<JButton>(4) { JButton() }, | |
MutableList<JButton>(4) { JButton() }, | |
MutableList<JButton>(4) { JButton() }, | |
MutableList<JButton>(4) { JButton() }, | |
) | |
private var blankTileRow = 3 | |
private var blankTileCol = 3 | |
private val rng = kotlin.random.Random(System.nanoTime()) | |
init { | |
val buttonBackgroundColor = Color(216, 160, 104) | |
val shadow = Color(128, 160, 144) | |
val darkShadow = Color(96, 80, 72) | |
val highlight = Color(224, 236, 216) | |
val lightHighlight = Color(248, 248, 192) | |
val buttonBorder = ButtonBorder( | |
shadow, darkShadow, | |
highlight, lightHighlight | |
) | |
val buttonFont = Font("Arial", Font.BOLD, 48) | |
for (row in 0 until 4) | |
for (column in 0 until 4) { | |
val button = board[row][column] | |
val n = (row * 4) + column | |
val string = String.format("%02d", n) | |
button.text = if (n != 15) string else "" | |
button.background = buttonBackgroundColor | |
button.border = buttonBorder | |
button.font = buttonFont | |
} // for | |
} // init | |
fun move() { | |
val possibleMoves = mutableListOf<Direction>() | |
if (blankTileCol < 3) possibleMoves.add(Direction.EAST) | |
if (blankTileCol > 0) possibleMoves.add(Direction.WEST) | |
if (blankTileRow < 3) possibleMoves.add(Direction.SOUTH) | |
if (blankTileRow > 0) possibleMoves.add(Direction.NORTH) | |
val index = rng.nextInt(possibleMoves.size ) | |
var neighborsRow = blankTileRow | |
var neighborsCol = blankTileCol | |
when (possibleMoves[index]) { | |
Direction.EAST -> neighborsCol++ | |
Direction.WEST -> neighborsCol-- | |
Direction.SOUTH -> neighborsRow++ | |
Direction.NORTH -> neighborsRow-- | |
} // when | |
val neighborsText = board[neighborsRow][neighborsCol].text | |
val blankTileText = board[blankTileRow][blankTileCol].text | |
board[neighborsRow][neighborsCol].text = blankTileText | |
board[blankTileRow][blankTileCol].text = neighborsText | |
board[neighborsRow][neighborsCol].repaint() | |
board[blankTileRow][blankTileCol].repaint() | |
blankTileRow = neighborsRow | |
blankTileCol = neighborsCol | |
} // move() | |
} // Game | |
class GameBoard() : JFrame(), ActionListener { | |
private val game = Game() | |
init { | |
this.setSize(512, 512) | |
this.defaultCloseOperation = EXIT_ON_CLOSE | |
this.jMenuBar = JMenuBar() | |
val gameMenu = JMenu("# moves in game") | |
gameMenu.font = Font("Arial", Font.ITALIC, 24) | |
this.jMenuBar.add(gameMenu) | |
for (n in arrayOf(8, 16, 32, 64, 128)) { | |
val label = String.format("%04d moves", n) | |
val menuItem = JMenuItem(label) | |
menuItem.font = Font("Arial", Font.ITALIC, 24) | |
gameMenu.add(menuItem) | |
} // for | |
val container = this.contentPane | |
container.background = Color(112, 192, 224) | |
container.layout = GridLayout(4, 4) | |
for (row in 0 until 4) | |
for (column in 0 until 4) { | |
val button = game.board[row][column] | |
button.addActionListener(ButtonListener(button)) | |
container.add(button) | |
} // for | |
this.isVisible = true | |
} // init | |
override fun actionPerformed( event: ActionEvent ) { | |
game.move() | |
} // actionPerformed( ActionEvent ) | |
} // GameBoard | |
fun main() { | |
val gameBoard = GameBoard() | |
val timer = Timer(500, gameBoard) | |
timer.start() | |
} // main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment