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.
AWS has launched the open-source Model Context Protocol (MCP) Servers, revolutionizing AI-powered code assistants. These servers enhance development speed and security, ensuring adherence to AWS best practices. With features like automated Infrastructure as Code and cost insights, MCP democratizes AWS expertise and empowers developers to optimize cloud solutions effortlessly.
By Steef-Jan WiggersAWS has recently announced the general availability of Amazon VPC Route Server. This new option simplifies dynamic routing in a VPC, allowing developers to advertise routing information via Border Gateway Protocol (BGP) from virtual appliances and dynamically update the VPC route tables associated with subnets and internet gateways.
By Renato LosioIan Hoffman discusses Slack's architectural evolution from workspace-centric to Unified Grid. He explains scaling challenges & Enterprise Grid complexities, and shares lessons learned during this significant architectural shift, drawing insightful parallels to the history of astronomy and emphasizing the importance of questioning foundational assumptions in software development.
By Ian HoffmanIn this podcast, Shane Hastie, Lead Editor for Culture & Methods, spoke with Trisha Gee about the challenges and importance of addressing flaky tests, their impact on developer productivity and morale, best practices for testing, and broader concepts of measuring and improving developer productivity.
By Trisha GeeStefania Chaplin and Azhir Mahmood explain how to navigate the complexities of AI in highly regulated industries. They discuss MLOps pipelines, data security, evolving legislation (GDPR, EU AI Act), and the critical frameworks for responsible, secure, and explainable AI. Learn practical prevention techniques, XAI methods, and future trends in AI for cybersecurity and beyond.
By Stefania Chaplin, Azhir Mahmood
© 2025 Created by Michael Levin.
Powered by