Created
January 23, 2013 21:17
-
-
Save rladstaetter/4613498 to your computer and use it in GitHub Desktop.
creates a jagged line using JavaFX
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
class JaggedLine extends javafx.application.Application { | |
val canvasWidth = 1024 | |
val canvasHeight = 768 | |
val jaggingSections = 100 | |
val jaggedFactor = 2 | |
override def start(primaryStage: Stage): Unit = { | |
primaryStage.setTitle("Level1") | |
val root = new BorderPane() | |
val drawingBoard = new Group() | |
val background = { | |
val b = new Rectangle(0, 0, canvasWidth, canvasHeight) | |
b.setFill(Color.BLACK) | |
b | |
} | |
drawingBoard.getChildren().add(background) | |
drawingBoard.getChildren.addAll(jaggedlines(Vec(canvasWidth / 10, canvasHeight / 2), Vec(canvasWidth * 9 / 10, canvasHeight / 2), jaggingSections, Color.WHITESMOKE)) | |
root.setCenter(drawingBoard) | |
primaryStage.setScene(new Scene(root, canvasWidth, canvasHeight)) | |
primaryStage.show() | |
} | |
def jaggedlines(source: Vec, dest: Vec, count: Int, color: Color): List[Group] = { | |
val totalVec = (source - dest) | |
val maxLen = totalVec.length | |
val onedir = totalVec.onedir | |
val length = totalVec.length / count | |
val normal = onedir.normal | |
val elongation = jaggedFactor * maxLen / count | |
val positions = List(source) ++ (for (i <- 1 to (count - 1)) yield source + onedir * length * i + normal * elongation * Random.nextDouble) ++ List(dest) | |
(for (List(a, b) <- positions.sliding(2)) yield mkLine(a, b, color)).toList | |
} | |
// poor mans helper classes and functions | |
case class Vec(x: Double, y: Double) { | |
def -(that: Vec) = Vec(that.x - x, that.y - y) | |
def +(that: Vec) = Vec(x + that.x, y + that.y) | |
def *(factor: Double) = Vec(factor * x, factor * y) | |
def /(l: Double) = if (l != 0) Vec(x / l, y / l) else sys.error("div.0") | |
def length = scala.math.sqrt(x * x + y * y) | |
def onedir = this / length | |
def normal = Vec(-y, x) | |
} | |
def mkRandColor = { | |
def randInt = (Random.nextFloat * 255).toInt | |
Color.rgb(randInt, randInt, randInt) | |
} | |
def mkLine(source: Vec, dest: Vec, color: Color): Group = { | |
val g = new Group() | |
val refline = new Line(source.x, source.y, dest.x, dest.y) | |
refline.setStroke(color) | |
val line = new Line(source.x, source.y, dest.x, dest.y) | |
line.setStroke(color) | |
line.setStrokeWidth(4) | |
val bloom = new Bloom(1.0) | |
val blur = new GaussianBlur() | |
blur.setInput(bloom) | |
line.setEffect(blur) | |
g.getChildren().addAll(refline, line) | |
g | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment