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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
import javafx.animation.KeyFrame import javafx.animation.KeyValue import javafx.animation.Timeline import javafx.application.Application import javafx.scene.Scene import javafx.scene.control.Button import javafx.scene.layout.TilePane import javafx.stage.Stage import javafx.util.Duration import java.util.* class AutomatedPiano : 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) ) private val highlightTimeline = Timeline() 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 = "Automated Piano" primaryStage.scene = scene primaryStage.show() // Simular la reproducción de una melodía val timer = Timer() val melody = listOf("DO", "RE", "MI", "FA", "SOL", "LA", "SI", "DO") var index = 0 timer.scheduleAtFixedRate(object : TimerTask() { override fun run() { if (index < melody.size) { playSound(melody[index]) index++ } else { timer.cancel() timer.purge() } } }, 0, 500) // Tocar una nota cada 500 milisegundos } private fun createButton(nota: Nota): Button { val button = Button(nota.nombre) button.minWidth = 80.0 button.minHeight = 200.0 button.setOnAction { playSound(nota.nombre) highlightButton(button) } return button } private fun playSound(nota: String) { println("suena") } private fun getFrequency(nota: String): Int { return notas.find { it.nombre == nota }?.frecuencia ?: 0 } private fun highlightButton(button: Button) { // Detener la animación actual antes de comenzar una nueva highlightTimeline.stop() // Resaltar el botón durante 500 milisegundos val keyValue = KeyValue(button.styleProperty(), "-fx-background-color: yellow;") val keyFrame = KeyFrame(Duration.millis(500.0), keyValue) // Cambiar el color del botón de nuevo a su estado original después de 500 milisegundos val resetValue = KeyValue(button.styleProperty(), "") val resetFrame = KeyFrame(Duration.millis(500.0), resetValue) highlightTimeline.keyFrames.clear() highlightTimeline.keyFrames.addAll(keyFrame, resetFrame) highlightTimeline.play() } data class Nota(val nombre: String, val frecuencia: Int) companion object { @JvmStatic fun main(args: Array<String>) { launch(AutomatedPiano::class.java, *args) } } } |