1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import javafx.application.Application import javafx.scene.Scene import javafx.scene.control.Button import javafx.scene.layout.TilePane import javafx.stage.Stage class PianoApp : Application() { private val notas = listOf( Nota("DO", 261), Nota("RE", 293), Nota("MI", 329), Nota("FA", 349), Nota("SOL", 391), Nota("LA", 440), Nota("SI", 493) ) override fun start(primaryStage: Stage) { val root = TilePane() root.hgap = 5.0 root.vgap = 10.0 for (nota in notas) { root.children.add(createButton(nota)) } val scene = Scene(root, 600.0, 200.0) primaryStage.title = "Piano App" primaryStage.scene = scene primaryStage.show() } private fun createButton(nota: Nota): Button { val button = Button(nota.nombre) button.minWidth = 80.0 button.minHeight = 200.0 button.setOnAction { playSound(nota.frecuencia) } return button } private fun playSound(frecuencia: Int) { val command = "powershell.exe" val frequencyCommand = "[System.Console]::Beep($frecuencia,500)" val processBuilder = ProcessBuilder(command, "-Command", frequencyCommand) try { val process = processBuilder.start() process.waitFor() } catch (e: Exception) { e.printStackTrace() } } data class Nota(val nombre: String, val frecuencia: Int) companion object { @JvmStatic fun main(args: Array<String>) { launch(PianoApp::class.java, *args) } } } |