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.
LlamaFirewall is a security framework aimed at safeguarding AI agents against prompt injection, goal misalignment, and insecure code generation. It achieved over 90% efficacy in reducing attack success rates when evaluated on the AgentDojo benchmark. Additionally, developers can update its behavior by adding new security guardrails.
By Sergio De SimoneAt Meta's first-ever LlamaCon event, the company announced several new tools for building with their Llama AI models: a limited preview of the Llama API that allows developers to experiment with different models, and new Llama Protection Tools for securing AI applications.
By Anthony AlfordCharlotte de Jong Schowenburg shares concrete strategies grounded in business psychology to enhance your influence as a senior software developer, software architect, or engineering leader. She provides practical techniques, including effective feedback delivery and leveraging the Pygmalion effect, to build trust, foster autonomy, and drive positive behavioral changes within your technical teams.
By Charlotte de Jong SchouwenburgGoogle has released a new AI model called DolphinGemma, which has been developed to assist researchers in analyzing and interpreting dolphin vocalizations. The project is part of an ongoing collaboration with the Wild Dolphin Project (WDP) and researchers at Georgia Tech, and it focuses on identifying patterns in the natural communication of Atlantic spotted dolphins.
By Robert KrzaczyńskiIn tech, leadership is about more than managing projects—it's about influencing teams and driving collaboration. This article explores how understanding the brain’s states—rational, emotional, and survival—can enhance decision-making and innovation. By mastering emotional regulation, trust, and psychological safety, leaders can empower teams to thrive and unlock their full potential.
By Charlotte de Jong Schouwenburg
© 2025 Created by Michael Levin.
Powered by