ScalaFX
ScalaFX-i kasutamiseks on vajalik SBT projekti build.sbt-sse lisada vastav sõltuvus:
// Add dependency on ScalaFX library
libraryDependencies += "org.scalafx" %% "scalafx" % "14-R19"
// Determine OS version of JavaFX binaries
lazy val osName = System.getProperty("os.name") match {
case n if n.startsWith("Linux") => "linux"
case n if n.startsWith("Mac") => "mac"
case n if n.startsWith("Windows") => "win"
case _ => throw new Exception("Unknown platform!")
}
// Add dependency on JavaFX libraries, OS dependent
lazy val javaFXModules = Seq("base", "controls", "fxml", "graphics", "media", "swing", "web")
libraryDependencies ++= javaFXModules.map( m =>
"org.openjfx" % s"javafx-$m" % "14.0.1" classifier osName
)
Näiteülesanded
Uurida järgnevat programmi ja sellest aru saada!
import javafx.animation.Interpolator
import scalafx.Includes._
import scalafx.animation.{KeyValue, Timeline}
import scalafx.application.JFXApp
import scalafx.scene.Scene
import scalafx.scene.paint.Color
import scalafx.scene.shape.Circle
import scala.language.postfixOps
import scalafx.scene.effect.Reflection
object P6rkamine extends JFXApp {
object bounce extends Interpolator {
def curve(t: Double): Double = t*t
}
stage = new JFXApp.PrimaryStage {
title = "P6rkamine"
width = 400
height = 300
scene = new Scene {
fill = Color.LightGreen
// val r = new Reflection(240,1,0.7,0)
val c = new Circle {
radius = 10
centerX = 150
centerY = 20
// effect = r
}
val t = new Timeline {
cycleCount = Timeline.Indefinite
autoReverse = true
keyFrames = at(1 s){ Set[KeyValue[_, _ <: Object]](
c.centerY -> 160 tween bounce
// , r.topOffset -> 0 tween bounce
)}
}
t.play()
content = c
}
}
}
Ülesanded:
- muutke akna taustavärv ja ringi värv;
- tehke ring suuremaks;
- lisage ka teine põrkav pall.
Mis juhtub, kui sisse kommenteerida väljakommenteeritud read?
Viia lõpuni valuutavahetuse programmi implementeerimine
import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.collections.ObservableBuffer
import scalafx.geometry.Insets
import scalafx.scene.Scene
import scalafx.scene.control.{ChoiceBox, TextField}
import scalafx.scene.layout.{HBox, VBox}
import scala.language.postfixOps
import scala.util.Try
object RahaTeisendus extends JFXApp {
def getRates: Map[String, Double] =
Map("AED" -> 4.144,"AUD" -> 1.5525,"BGN" -> 1.9558,"CAD" -> 1.5117,"CHF" -> 1.1324,"CNY" -> 7.9063,"CZK" -> 25.967,"DKK" -> 7.4622,"GBP" -> 0.89135,"HKD" -> 8.9078,"HRK" -> 7.416,"HUF" -> 323.42,"JPY" -> 128.99,"NOK" -> 9.7318,"PLN" -> 4.2916,"RON" -> 4.6531,"RUB" -> 75.542,"SEK" -> 10.317,"SGD" -> 1.5603,"TRY" -> 5.8747,"USD" -> 1.1387,"ZAR" -> 15.5182)
def convert(n: String, curr: String): String = {
val nint = Try(n.toDouble).getOrElse(0d)
val rate = getRates.withDefaultValue(1d)(curr)
"%.2f".format(nint*rate)
}
stage = new JFXApp.PrimaryStage {
title = "RahaTeisendus"
scene = new Scene {
val dt = new ChoiceBox[String]{
// valuuta valik
}
val oF = new TextField {
// väljund-summa
}
val iF = new TextField {
// sisend-summa
}
root = ??? // paigutus
}
}
}
Lõpptulemus võiks välja näha selline:
Harjutusülesanded
Ülesanne 1
import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.scene.Scene
import scalafx.scene.control.{Button, TextArea}
import scalafx.scene.layout.{BorderPane, HBox, VBox}
import scalafx.scene.text.Text
import scala.language.postfixOps
object Tekstiprogramm extends JFXApp {
stage = new JFXApp.PrimaryStage {
title = "Tekstiprogramm"
width = 300
height = 300
scene = new Scene {
val read = new Text
val s6nad = new Text
val symbolid = new Text
val ta = new TextArea{
// ???
}
val tyhjenda = new Button {
text = "Tühjenda"
onAction = handle {
???
}
}
root = new BorderPane {
// paigutus
}
}
}
}
(Loe ka JavaFX-i BorderPane kasutamisest)
Nõuded:
- Programmis on tekstiala, kuhu saab sisestada mitmerealist teksti.
- Tekstiala suurus peab kasvama ja kahanema koos akna suurusega.
- Nupp "Tühjenda" tühjendab tekstiala sisu
- Tekstiala all on toodud sisestatud teksti kohta statistikat: ridade arv, sõnade arv(võime eeldada, et sõnade vahel on tühik) ja sümbolite arv.
Lõpptulemus võiks välja näha selline:
Ülesanne 2
import scalafx.Includes._
import scalafx.animation.Timeline
import scalafx.application.JFXApp
import scalafx.scene.Scene
import scalafx.scene.control.Button
import scalafx.scene.layout.{HBox, VBox}
import scalafx.scene.shape.Circle
import scala.language.postfixOps
import scala.math._
object V6idujooks extends JFXApp {
stage = new JFXApp.PrimaryStage {
title = "V6idujooks"
width = 400
height = 300
// (x,y) on stardipositsiooni koordinaat
class Jooksja(x: Int, y: Int) extends Circle {
def start(): Unit = ???
def reset(): Unit = ???
}
scene = new Scene {
// loome mõned jooksjad
val xs = for (x <- 70 to 240 by 40) yield new Jooksja(20, x)
val start = new Button{
text = "start"
}
val reset = new Button{
text = "reset"
}
content = Seq(new VBox(new HBox(start, reset))) ++ xs
}
}
}
Nõuded:
- Progamm simuleerib võidujooksu -- ringid on jooksjad.
- Vajutades "Start" jookstakse akna servani (x koordinaat 380)
- Jooksmise kiirus valida juhuslikult: finisisse jõutakse ajaga 1-2 sekundit (kasutage math.random)
- Vajutades "Reset", longivad jooksjad stardipositsioonile (3 sek) ja on siis valmis uuesti startima.
Lõpptulemus võiks välja näha selline:
Ülesanne 3*
Implementeerige ROT13 teisendaja: sisendi teksti iga ladina tähe koodile liidab 13 modulo 26. Seda võiks teha kahe TextArea-ga.