Codetown ::: a software developer's community
Welcome to Kotlin Thursdays and Happy Halloween! This week, we will be exploring image processing in Kotlin. If you have used applications like Instagram, Snapchat, or even Photoshop, you may already be familiar with image processing and not even know it!
Image processing is not specific to Kotlin, but playing with images can help us think about how algorithms are applied in a 2-D matrix and develop computational thinking. Once we get the basics down, the world is your oyster!
We will be leveraging TornadoFX, a JavaFX framework written in Kotlin for native desktop development. Let's get started setting up our TornadoFX project!
Think of these resources as supplemental if you happen to be more curious. We always encourage looking into documentation for things you use!
In the spirit of open-source languages and resources, it's only a natural choice to use IntelliJ Community edition.
First, make sure you're set up with TornadoFX on your IntelliJ. If you don't have IntelliJ, you can download it for free here!
Choose any photo, or take this one below and save it in a resource folder in your project!
1. On IntelliJ, go to Preferences > Plugins and make sure Kotlin and TornadoFX is installed.
2. General consensus amongst TornadoFXers is that Maven is easier to user - and Maven is great as long as you do not require lot of dependencies for your project. Otherwise, it's recommended to use Gradle. For the scope of this project, it is not necessary to understand how to use either of build tools, but don't hesitate to leave comments/questions/concerns here if you happen to run into any trouble throughout the project. Let's go ahead and select tornadofx-maven-project, which will nicely set up much of our project immediately.
3. If you haven't already, go to your pom.xml file and check to see if your kotlin version is updated. As of November 1st, 2018, the latest Kotlin version is 1.3.0, but feel free to refer to your plugins to check versioning. If you don't have your changes imported automatically, hit "Import Changes". If it builds successfully, you should be good to go!
In your source code, you'll notice a brief hierarchy - within com.example.demo, you have an app folder which stores the files MyApp and Styles, and you also have a view folder which contains the MainView.
App
To create a TornadoFX application, you must have at least one class that extendsApp. An App is the entry point to the application and specifies the initial View.
package com.example.demo.app
import com.example.demo.view.MainView
import tornadofx.App
class MyApp: App(MainView::class, Styles::class)
Stylesheet
Traditionally in JavaFX, a stylesheet is defined in a plain CSS text file included in the project. However, TornadoFX allows creating stylesheets with pure Kotlin code. This has the benefits of compilation checks, auto-completion, and other perks that come with statically-typed code. A companion object is declared to hold class-level properties that can easily be retrieved throughout the app. Then, the init() block is used to apply styling to classes. For more on styling, see Type-Safe CSS.
package com.example.demo.app
import ...
class Styles: Stylesheet( ) {
companion object {
val heading by cssclass()
}
init {
label and heading {
padding = box(10.px)
fontSize = box(20.px)
fontWeight = FontWeight.BOLD
}
}
View
A View contains the display logic as well as a layout of Nodes. TornadoFX provides a builder syntax that will streamline your UI code. These builders allow us to express the UI as a hierarchical structure, which enables you to visualize the resulting UI easily. Note that all builders are written in lowercase, so as not to confuse them with manual instantiation of the UI element classes.
We're going to modify the code to render our rooster image. Always write a little code and check if it works. You don't want to keep writing something that is broken in the first place!
package com.example.demo.app
import ...
class MainView: View("I am a chicken") {
override val root = hbox {
imageview("rooster.png")
hboxConstraints {
prefWidth = 640.0
prefHeight = 427.0
}
}
}
According to TornadoFX documentation on images, it doesn't appear much different from JavaFX Images and ImageViews, but a TornadoFX allows far less syntax in programming over JavaFX:
Image image = new Image(new FileInputStream("path of image"));
ImageView imageView = new ImageView(image);
imageView.setX(50);
imageView.setY(25);
imageView.setFitHeight(455);
imageView.setFitWidth(500);
Group root = new Group(imageView);
Scene scene = new Scene(root, 600, 500);
}
Java implementation
imageview("tornadofx.jpg")
scaleX = . 50
scaleY = . 50
}
Kotlin implementation
Let's click run and make sure we're able to render the method!
Noice.
Now that we've figured out how to render an image, we're going to talk about how we can access pixels in an image. Images are essentially 2-dimensional matrixes in which we are able to iteratively access an image's pixel value by row and column. We then can apply operations to those pixels and alter the image as a whole.
We can use JavaFX's WritableImage to allow a canvas while storing the URI as an Image. This way, we are given access to PixelReader and PixelWriter. Super convenient!
package com.example.demo.app
import ...
class MainView: View("I am a chicken") {
private val image = Image("rooster.png")
private val width = image.width.toInt( )
private val height = image.width.toInt( )
private val width = WritableImage(image.pixelReader width, height)
override val root = hbox {
imageview(wImage).apply {
makeDuller(wImage)
makeDuller(wImage)
makeDuller(wImage)
}
hboxConstraints {
prefWidth = 640.0
prefHeight = 427.0
}
}
private fun makeDuller = (image: WritableImage) {
val pixelWriter = image.pixelWriter
for ( x in 0 until width) {
for ( y in 0 until height) {
val color = image.pixelReader.getColor(x, y)
pixelWriter.setColor(x, y, color.desaturate( ))
}
}
}
}
}
We used Kotlin's apply three times because we wanted to make sure we could REALLY see the difference. In Kotlin, you can use the call apply where the scope calls this (itself) as the receiver and the result is the object with applied functions.
In the function makeDuller, we access the color of an individual pixel that we read and subsequently use pixelWriter to set that pixel a different color. Double noice.
We've figured out how to grab individual pixels of an image and manipulate them. Next week, we'll be going over pixel math and using math pixel to create our own image processing filters. Stay tuned!
Having a blast? Check out part 2 of this series!
Tags:
Codetown is a social network. It's got blogs, forums, groups, personal pages and more! You might think of Codetown as a funky camper van with lots of compartments for your stuff and a great multimedia system, too! Best of all, Codetown has room for all of your friends.
Created by Michael Levin Dec 18, 2008 at 6:56pm. Last updated by Michael Levin May 4, 2018.
Check out the Codetown Jobs group.

Rafael Ring discusses the architectural evolution of server-driven UI at Nubank, moving from static mobile binaries to a sophisticated scripted framework called Catalyst. He explains how they implemented a tree-walk interpreter in Flutter to render dynamic layouts and logic from JSON payloads.
By Rafael Ring
Oracle has released version 26 of the Java programming language and virtual machine. As the first non-LTS release since JDK 25, the final feature set includes 10 JEPs, five of which are still progressing through the preview and incubator stages. This release focuses on Java library improvements, language innovation, performance and security.
By Michael Redlich
At QCon London, TrueLayer engineer Ethan Brierley reframed Rust lifetimes using the Polonius borrow checker's mental model: lifetimes as sets of loans rather than regions of code. He built from borrow checker basics through variance and subtyping to higher-ranked lifetimes with serde, showing how the loans perspective makes previously confusing lifetime errors intuitive.
By Steef-Jan Wiggers
At QCon London 2026, Lada Indra, Head of Data Platform at Pleo, shared insights from his experience across high-scale data systems. He illustrated both the risks of poorly aligned teams and the practical strategies that organizations can adopt to bridge the gap.
By Daniel Dominguez
This article explores that question through the lens of a real-world Rust project: a system responsible for controlling fleets of autonomous mobile robots. While Rust's memory safety is a strong foundation, its true power lies in the type system and ownership rules. The session will go beyond memory safety and explore ways to encode behavior and protocols directly into types.
By Andy Brinkmeyer
© 2026 Created by Michael Levin.
Powered by