Catching Java Assertion Errors

The Java assert statement can be used for conditionally checking program invariants. Assertions are enabled or disabled with the desired level of granularity by Java Virtual Machine flags, as described in the linked guide. So far, so similar to assertion facilities in other languages. However, whereas .NET Debug/Trace.Assert shows and logs a message by default, … Continue reading “Catching Java Assertion Errors”

JavaFX Chart Coloring

Working on a Java(FX) port of my hoplite simulator Myriarch, I ran into an unexpected problem with the history chart. This part of the “Simulation Report” dialog shows how each faction’s unit count changes over the course of the simulation. You can see a screenshot from the current build below. The control is a standard … Continue reading “JavaFX Chart Coloring”

Compiling Java Code

This post is a quick introduction aimed at programmers experienced with C# or other languages who are new to Java. It is intended as a companion piece to my article Java for C# Programmers which focuses on language differences. See the “Further Reading” section at the top of that article for the official Oracle tutorials … Continue reading “Compiling Java Code”

JavaFX Snapshot Scaling

If you try taking image snapshots of a JavaFX Node on a high-DPI system, you’ll find that the result is strangely blurry. This is an unfortunate side effect of the JavaFX DPI scaling introduced in Java SE 8u60. At resolutions greater than 120 DPI, JavaFX automatically treats all coordinates as abstract “layout pixels” with a … Continue reading “JavaFX Snapshot Scaling”

JavaFX Pane Clipping

Most JavaFX layout containers (base class Region) automatically position and size their children, so clipping any child contents that might protrude beyond the container’s layout bounds is never an issue. The big exception is Pane, a direct subclass of Region and the base class for all layout containers with publicly accessible children. Unlike its subclasses … Continue reading “JavaFX Pane Clipping”

JavaFX Spinner for Numbers

The JavaFX version of the popular up-down control is called Spinner. Like its Swing progenitor JSpinner, this control is much more flexible than a typical numerical up-down control. Spinner is designed for arbitrary sequences of objects, with number ranges constituting merely a special case. This has some unfortunate consequences when you do wish to make … Continue reading “JavaFX Spinner for Numbers”

Java Method Reference Evaluation

Along with lambda expressions, Java SE 8 introduced method references as a shorthand notation. These are mostly used to reference static methods (e.g. Double::toString) or constructors (e.g. String[]::new), and these uses are straightforward. However, method references to instance methods can yield results that differ from lambda expressions in surprising ways. This is because the invocation … Continue reading “Java Method Reference Evaluation”

JavaFX and JAVA_TOOL_OPTIONS

JavaFX provides some dedicated system properties to customize its behavior. Usually these are specified as command-line flags on the java/javaw invocation, using the syntax “java -D<i>name=value</i>” followed by the JavaFX application path. I’ve previously noted two especially useful ones, as of Java SE 8u66-77: -Dprism.order=sw enables software rendering, even if JavaFX thinks hardware acceleration is … Continue reading “JavaFX and JAVA_TOOL_OPTIONS”

Loop Closures in Java & C#

The lambda expressions introduced in Java 8 can capture, or “close over”, any local variable that’s within scope and effectively final (Java 8 Language Specification §15.27.2, §4.12.4). Interestingly, this includes the loop variables of enhanced for loops, or for-each loops as I prefer to call them. Cay Horstmann mentions this very useful but non-obvious fact … Continue reading “Loop Closures in Java & C#”