Giter Club home page Giter Club logo

proscalafx's Introduction

ScalaFX

Join the chat at https://gitter.im/scalafx/scalafx

Scala CI Maven Central Scaladoc

ScalaFX is a UI DSL written within the Scala Language that sits on top of JavaFX. This means that every ScalaFX application is also a valid Scala application. By extension, it supports full interoperability with Java and can run anywhere the Java Virtual Machine (JVM) and JavaFX are supported.

If you have ScalaFX related questions please use ScalaFX Discussions, or ScalaFX Users Group, or ScalaFX on StackOverflow. Please report any problems using ScalaFX Issue Tracker.

Getting Started

ScalaFX binaries are published in the Maven Central repository: http://search.maven.org/#search|ga|1|scalafx

The official website for ScalaFX is http://scalafx.org.

ScalaFX Dependencies

ScalaFX 11+ is the current actively maintained version. ScalaFX 11+ is intended to support Java 11 and newer.

SBT

Here is how you can add dependency using SBT.

libraryDependencies += "org.scalafx" %% "scalafx" % "21.0.0-R32"

Note that in ScalaFX version prior to 20.0.0-R31 and SBT older than 1.6, you needed to explicitly provide dependency on JavaFX modules including platform dependent modules. This is no longer needed.

You can find examples of SBT setup in section Demo Projects and Examples below.

Mill

If you're using Mill:

object yourProject extends ScalaModule {
  def scalaVersion = "3.0.0"

  // Customize coursier resolution to discover the OS-specific artifacts required by JavaFX
  // Note: this requires mill >= 0.9.7 (with pr/775 merged)
  def resolutionCustomizer = T.task {
    Some((r: coursier.core.Resolution) =>
      r.withOsInfo(coursier.core.Activation.Os.fromProperties(sys.props.toMap))
    )
  }

  // Add dependency on JavaFX libraries
  val javaFXVersion = "16"
  val scalaFXVersion = "16.0.0-R25"
  val javaFXModules = List("base", "controls", "fxml", "graphics", "media", "swing", "web")
    .map(m => ivy"org.openjfx:javafx-$m:$javaFXVersion")

  def ivyDeps = {
    Agg(
      ivy"org.scalafx::scalafx:$scalaFXVersion",
      //...
    ) ++ javaFXModules
  }
}

You can find sample ScalaFX Mill project here: scalafx-millproject

Gradle

Example of build.gradle:

plugins {
    id 'scala'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.13'
}

repositories {
    mavenCentral()
}

javafx {
    version = "21"
    modules = ['javafx.controls', 'javafx.media']
}

dependencies {
    implementation 'org.scala-lang:scala3-library_3:3.3.1'
    implementation 'org.scalafx:scalafx_3:21.0.0-R32'
}

application {
    mainClass = 'hello.ScalaFXHelloWorld'
}

A complete sample Gradle project can ge found in ScalaFX-Hello-World-Gradle.

What is in the version number

ScalaFX version number has two part. The first part corresponds to the latest JavaFX version it was tested with. The second part is an incremental release number. For instance, version 15.0.1-R20 means that it was tested with JavaFX version 15.0.1 and that is the 20th release of ScalaFX.

Legacy Releases

ScalaFX 10

with Java 10 use:

libraryDependencies += "org.scalafx" %% "scalafx" % "10.0.2-R15"
ScalaFX 8

To use ScalaFX with SBT and Java 8 add following dependency (to use the latest scalafx you might need Java version at least 1.8.40):

libraryDependencies += "org.scalafx" %% "scalafx" % "8.0.192-R14"
ScalaFX 2

With Java 7 use:

libraryDependencies += "org.scalafx" %% "scalafx" % "2.2.76-R11"

Demo Projects and Examples

The ScalaFX Organization page on GitHub contains several sample project that illustrate use of ScalaFX. The simplest one, and recommended to start with, is scalafx-hello-world. There is also a Gradle version here: ScalaFX-Hello-World-Gradle.

Development Snapshots

Snapshot releases are also regularly published on Sonatype Snapshots. To use a snapshot build you may need to add "Sonatype OSS Snapshots" resolver to you SBT configuration:

resolvers += Opts.resolver.sonatypeSnapshots

If you just want to download a recent snapshot build you can also use Travis CI build site https://travis-ci.org/scalafx/scalafx

Software License

This software licensed under BSD Open Source.

The License text for this software can be found in LICENSE.txt in the root folder of the project.

Software Required

The following software is needed to build ScalaFX:

  1. SBT v.1.6.0 or better
  2. Scala. ScalaFX 12 builds with Scala 2.10.2 or newer.
  3. Java 17 or better is required as of JavaFX 20 / ScalaFX 20.0.0-R31 (this is JavaFX 20 requirement

It works with Windows, Mac OS X, and Linux ports.

Project Structure

The current project directory structure:

./notes
./project
./scalafx
./scalafx-demos

Where . is the root folder of the project.

The notes folder contains release notes for past releases.

The scalafx folder is the sub-project for the ScalaFX Framework.

The scalafx-demos is the sub-project for the ScalaFX Framework Demonstrations, some are a bit out of date, help needed here :).

The project folder is reserved for SBT build system setup.

Source Code Branching Policy

The current development is for ScalaFX 20. The development is done on the master branch. Releases are done on the stable branch. Releases are tagged with version number. Pull requests are only accepted off a branch created from the master branch. When working on a pull request, create a separate branch for each feature or bug fix. This way the main development branch is not blocked by a pull request and pull requests are easier to merge individually.

The ScalaFX 8 and 2.2 development is no longer active. For those who need it, the code is on branches: SFX-8 and SFX-2. Past releases are on SFX-8-stable and SFX-2-stable branches.

Authors

ScalaFX was originally created by Stephen Chin, Java Champion, Oracle JavaOne program chair; and Sven Reimers, a member of the Netbeans Dream Team.

Credits

The most up to date list of contributors to the project can be found on the Contributors page.

Community

We request all the team members to follow the Typelevel Code of Conduct in our mailing list, issue discussion, Gitter room or any of ScalaFX meetups.

For more info on Contribute, check our Contributing page.

proscalafx's People

Contributors

esamson avatar intracer avatar jpsacha avatar rafonso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

proscalafx's Issues

`java.lang.ClassCastException` in the reversi app

This app produces lots of java.lang.ClassCastException.

package proscalafx.ch04.reversi.ui

import proscalafx.ch04.reversi.model.{Black, Owner, ReversiModel, White}

import scalafx.Includes._
import scalafx.application.JFXApp.PrimaryStage
import scalafx.application.{ConditionalFeature, JFXApp, Platform}
import scalafx.geometry.Pos
import scalafx.scene.control.Button
import scalafx.scene.effect.{DropShadow, InnerShadow}
import scalafx.scene.layout._
import scalafx.scene.paint.Color
import scalafx.scene.shape.Ellipse
import scalafx.scene.text.{Font, FontWeight, Text}
import scalafx.scene.transform.{Rotate, Scale, Translate}
import scalafx.scene.{PerspectiveCamera, Scene}


object Reversi extends JFXApp {

  val restart = new Button() {
    text = "Restart"
    onAction = handle {ReversiModel.restart()}
  }

  val game = new BorderPane() {
    top = createTitle()
    center = new StackPane() {
      children = List(
        createBackground(),
        createTiles()
      )
    }
    bottom = createScoreBoxes()
  }

  stage = new PrimaryStage() {
    scene = new Scene(600, 400) {
      root = new AnchorPane() {
        children = List(
          game,
          restart
        )
      }
    }
  }

  AnchorPane.setTopAnchor(game, 0d)
  AnchorPane.setBottomAnchor(game, 0d)
  AnchorPane.setLeftAnchor(game, 0d)
  AnchorPane.setRightAnchor(game, 0d)
  AnchorPane.setRightAnchor(restart, 10d)
  AnchorPane.setTopAnchor(restart, 10d)


  if (Platform.isSupported(ConditionalFeature.Scene3D)) {
    stage.scene().camera = new PerspectiveCamera() {
      fieldOfView = 60
    }.delegate
  }


  //---------------------------------------------------------------------------


  private def createTitle() = new TilePane {
    snapToPixel = false
    children = List(
      new StackPane {
        style = "-fx-background-color: black"
        children = new Text {
          text = "ScalaFX"
          font = Font.font(null, FontWeight.Bold, 18)
          fill = Color.White
          alignmentInParent = Pos.CenterRight
        }
      },
      new Text {
        text = "Reversi"
        font = Font.font(null, FontWeight.Bold, 18)
        alignmentInParent = Pos.CenterLeft
      })
    prefTileHeight = 40
    prefTileWidth <== parent.selectDouble("width") / 2
  }


  private def createBackground() = new Region {
    style = "-fx-background-color: radial-gradient(radius 100%, white, gray)"
  }


  private def createTiles(): GridPane = {
    val board = new GridPane
    for {
      i <- 0 until ReversiModel.BOARD_SIZE
      j <- 0 until ReversiModel.BOARD_SIZE
    } {
      val square = new ReversiSquare(i, j)
      val piece = new ReversiPiece()
      piece.owner <== ReversiModel.board(i)(j)
      board.add(new StackPane {
        children = List(square, piece)
      }, i, j)
    }

    if (Platform.isSupported(ConditionalFeature.Scene3D)) {
      val scale = new Scale(.45, .8, 1, 300, 60, 0)
      val translate = new Translate(75, -2, -150)
      val xRot = new Rotate {
        angle = -40
        pivotX = 300
        pivotY = 150
        pivotZ = 0
        axis = Rotate.XAxis
      }
      val yRot = new Rotate {
        angle = -5
        pivotX = 300
        pivotY = 150
        pivotZ = 0
        axis = Rotate.YAxis
      }
      val zRot = new Rotate {
        angle = -6
        pivotX = 300
        pivotY = 150
        pivotZ = 0
        axis = Rotate.ZAxis
      }

      board.transforms ++= List(scale, translate, xRot, yRot, zRot)
    }

    board
  }


  private def createScoreBoxes() = new TilePane() {
    snapToPixel = false
    prefColumns = 2
    children = List(
      createScore(Black),
      createScore(White)
    )
    prefTileWidth <== parent.selectDouble("width") / 2
  }


  private def createScore(owner: Owner): StackPane = {

    val innerShadow = new InnerShadow() {
      color = Color.DodgerBlue
      choke = 0.5
    }
    val noInnerShadow = null.asInstanceOf[javafx.scene.effect.InnerShadow]

    val backgroundRegion = new Region() {
      style = "-fx-background-color: " + owner.opposite.colorStyle
      effect <== when(ReversiModel.turn === owner) choose innerShadow otherwise noInnerShadow
    }

    val dropShadow = new DropShadow() {
      color = Color.DodgerBlue
      spread = 0.2
    }
    val noDropShadow = null.asInstanceOf[javafx.scene.effect.DropShadow]

    val piece = new Ellipse() {
      radiusX = 32
      radiusY = 20
      fill = owner.color
      effect <== when(ReversiModel.turn === owner) choose dropShadow otherwise noDropShadow
    }

    val score = new Text() {
      font = Font.font(null, FontWeight.Bold, 100)
      fill = owner.color
      text <== ReversiModel.score(owner).asString()
    }

    val remaining = new Text() {
      font = Font.font(null, FontWeight.Bold, 12)
      fill = owner.color
      text <== ReversiModel.turnsRemaining(owner).asString() + " turns remaining"
    }

    new StackPane() {
      children = List(
        backgroundRegion,
        new FlowPane() {
          hgap = 20
          vgap = 10
          alignment = Pos.Center
          children = List(
            score,
            new VBox() {
              alignment = Pos.Center
              spacing = 10
              children = List(
                piece,
                remaining
              )
            }
          )
        }
      )
    }
  }
}

delegate method

Here is some code from the reversi app:

package proscalafx.ch04.reversi.ui

import javafx.scene.{layout => jfxsl}

import proscalafx.ch04.reversi.model.ReversiModel

import scalafx.Includes._
import scalafx.animation.FadeTransition
import scalafx.geometry.{HPos, VPos}
import scalafx.scene.effect.{Light, Lighting}
import scalafx.scene.input.MouseEvent
import scalafx.scene.layout.Region
import scalafx.util.Duration

class ReversiSquare(val x: Int, val y: Int) extends Region {

  private val highlight = new Region {
    opacity = 0
    style = "-fx-border-width: 3; -fx-border-color: dodgerblue"
  }

//  override val delegate: jfxsl.Region = new jfxsl.Region {
//    getChildren.add(highlight)
//    protected override def layoutChildren() {
//      layoutInArea(highlight, 0, 0, getWidth, getHeight, getBaselineOffset, HPos.Center, VPos.Center)
//    }
//  }

  private val highlightTransition = new FadeTransition {
    node = highlight
    duration = Duration(200)
    fromValue = 0
    toValue = 1
  }

  style <== when(ReversiModel.legalMove(x, y)) choose
    "-fx-background-color: derive(dodgerblue, -60%)" otherwise
    "-fx-background-color: burlywood"

  effect = new Lighting {
    light = new Light.Distant {
      azimuth = -135
      elevation = 30
    }
  }

  prefHeight = 200
  prefWidth = 200

//  onMouseEntered = (e: MouseEvent) => {
//    if (ReversiModel.legalMove(x, y).get) {
//      highlightTransition.rate() = 1
//      highlightTransition.play()
//    }
//  }
//
//  onMouseExited = (e: MouseEvent) => {
//    highlightTransition.rate = -1
//    highlightTransition.play()
//  }

  onMouseClicked = (e: MouseEvent) => {
    ReversiModel.play(x, y)
//    highlightTransition.rate() = -1
//    highlightTransition.play()
  }
}

Note that I have commented out the delegate method and the app works the same as before.

Could you please explain a bit what this method actually does? It's never used in the app and I am having some difficulty understanding it. It's creating a new Region object with an overridden method layoutChildren, but what's the purpose?

TreeItem cannot resolve onmouseclicked

Very strange behavior.

package view.helperFunctions

import scalafx.scene.Node
import scalafx.scene.control.TreeItem
import scalafx.scene.input.MouseEvent
import scalafx.scene.control.{DatePicker, TitledPane, TreeItem, TreeView}

/**
  * Created by André Schmidt on 18.07.2017.
  */
class TEST {
  import scala.language.postfixOps
  import scalafx.Includes._

  val a = new TreeView[String](){
    onMouseClicked = (_: MouseEvent) => {}
  }

  val b = new TreeItem(""){
    onMouseClicked = (m: MouseEvent) => {}
  }

}

All looks fine. But at val b my idea tells me "cannot resolve symbol onMouseClicked"
In another file this is working!

private val dossierNode = new TreeItem[String](Messages("dashboard.dossier.title")){
    val newDossier = new TreeItem[String](Messages("generic.new")){
      onMouseClicked = (me: MouseEvent) => {
        if(me.clickCount == 2){
          //
        }
      }
    }

    children += newDossier
  }

package view.pane.dashboard

Anyone an idea?

fail to load sbt project

$ sbt compile
[info] Loading global plugins from /home/ilya/.sbt/0.13/plugins
[info] Updating {file:/home/ilya/.sbt/0.13/plugins/}global-plugins...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Loading project definition from /home/ilya/wm/fx/ProScalaFX/project
[info] Updating {file:/home/ilya/wm/fx/ProScalaFX/project/}proscalafx-build...
[info] Resolving org.scala-lang#scalap;2.10.5 ...
[info] Done updating.
[info] Set current project to ProScalaFX (in build file:/home/ilya/wm/fx/ProScalaFX/)
[info] Updating {file:/home/ilya/wm/fx/ProScalaFX/}proscalafx...
[info] Resolving org.scalafx#scalafx_2.11;8.0.60-R9-SNAPSHOT ...
[warn] module not found: org.scalafx#scalafx_2.11;8.0.60-R9-SNAPSHOT
[warn] ==== local: tried
[warn] /home/ilya/.ivy2/local/org.scalafx/scalafx_2.11/8.0.60-R9-SNAPSHOT/ivys/ivy.xml
[warn] ==== public: tried
[warn] https://repo1.maven.org/maven2/org/scalafx/scalafx_2.11/8.0.60-R9-SNAPSHOT/scalafx_2.11-8.0.60-R9-SNAPSHOT.pom
[warn] ==== sonatype-snapshots: tried
[warn] https://oss.sonatype.org/content/repositories/snapshots/org/scalafx/scalafx_2.11/8.0.60-R9-SNAPSHOT/scalafx_2.11-8.0.60-R9-SNAPSHOT.pom
[info] Resolving org.scala-lang#scalap;2.11.7 ...
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: UNRESOLVED DEPENDENCIES ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: org.scalafx#scalafx_2.11;8.0.60-R9-SNAPSHOT: not found
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn]
[warn] Note: Unresolved dependencies path:
[warn] org.scalafx:scalafx_2.11:8.0.60-R9-SNAPSHOT (/home/ilya/wm/fx/ProScalaFX/build.sbt#L24-25)
[warn] +- default:proscalafx_2.11:8.0.60-R9-SNAPSHOT
sbt.ResolveException: unresolved dependency: org.scalafx#scalafx_2.11;8.0.60-R9-SNAPSHOT: not found
at sbt.IvyActions$.sbt$IvyActions$$resolve(IvyActions.scala:294)
at sbt.IvyActions$$anonfun$updateEither$1.apply(IvyActions.scala:191)
at sbt.IvyActions$$anonfun$updateEither$1.apply(IvyActions.scala:168)
at sbt.IvySbt$Module$$anonfun$withModule$1.apply(Ivy.scala:155)
at sbt.IvySbt$Module$$anonfun$withModule$1.apply(Ivy.scala:155)
at sbt.IvySbt$$anonfun$withIvy$1.apply(Ivy.scala:132)
at sbt.IvySbt.sbt$IvySbt$$action$1(Ivy.scala:57)
at sbt.IvySbt$$anon$4.call(Ivy.scala:65)
at xsbt.boot.Locks$GlobalLock.withChannel$1(Locks.scala:93)
at xsbt.boot.Locks$GlobalLock.xsbt$boot$Locks$GlobalLock$$withChannelRetries$1(Locks.scala:78)
at xsbt.boot.Locks$GlobalLock$$anonfun$withFileLock$1.apply(Locks.scala:97)
at xsbt.boot.Using$.withResource(Using.scala:10)
at xsbt.boot.Using$.apply(Using.scala:9)
at xsbt.boot.Locks$GlobalLock.ignoringDeadlockAvoided(Locks.scala:58)
at xsbt.boot.Locks$GlobalLock.withLock(Locks.scala:48)
at xsbt.boot.Locks$.apply0(Locks.scala:31)
at xsbt.boot.Locks$.apply(Locks.scala:28)
at sbt.IvySbt.withDefaultLogger(Ivy.scala:65)
at sbt.IvySbt.withIvy(Ivy.scala:127)
at sbt.IvySbt.withIvy(Ivy.scala:124)
at sbt.IvySbt$Module.withModule(Ivy.scala:155)
at sbt.IvyActions$.updateEither(IvyActions.scala:168)
at sbt.Classpaths$$anonfun$sbt$Classpaths$$work$1$1.apply(Defaults.scala:1392)
at sbt.Classpaths$$anonfun$sbt$Classpaths$$work$1$1.apply(Defaults.scala:1388)
at sbt.Classpaths$$anonfun$doWork$1$1$$anonfun$90.apply(Defaults.scala:1422)
at sbt.Classpaths$$anonfun$doWork$1$1$$anonfun$90.apply(Defaults.scala:1420)
at sbt.Tracked$$anonfun$lastOutput$1.apply(Tracked.scala:37)
at sbt.Classpaths$$anonfun$doWork$1$1.apply(Defaults.scala:1425)
at sbt.Classpaths$$anonfun$doWork$1$1.apply(Defaults.scala:1419)
at sbt.Tracked$$anonfun$inputChanged$1.apply(Tracked.scala:60)
at sbt.Classpaths$.cachedUpdate(Defaults.scala:1442)
at sbt.Classpaths$$anonfun$updateTask$1.apply(Defaults.scala:1371)
at sbt.Classpaths$$anonfun$updateTask$1.apply(Defaults.scala:1325)
at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
at sbt.$tilde$greater$$anonfun$$u2219$1.apply(TypeFunctions.scala:40)
at sbt.std.Transform$$anon$4.work(System.scala:63)
at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:226)
at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:226)
at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:17)
at sbt.Execute.work(Execute.scala:235)
at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:226)
at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:226)
at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:159)
at sbt.CompletionService$$anon$2.call(CompletionService.scala:28)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
error sbt.ResolveException: unresolved dependency: org.scalafx#scalafx_2.11;8.0.60-R9-SNAPSHOT: not found
[error] Total time: 2 s, completed Mar 22, 2016 6:55:12 PM

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.