Draw a draggable triangle in Java

This example shows how to draw a triangle on a JFrame and make it draggable so it can be moved around by using the mouse. The example contains two classes, one which is the main frame (the JFrame) and one that extends JPanel that does the actual painting and that implements the necessary interfaces needed to make the triangle draggable.

Continue reading

Take a screenshot and save as image in Java

This code example shows how to take a screen capture and save the data to a file as a PNG-image. The code consists of three methods (plus main method), one that gets the screen size, one that captures the actual screenshot, and one that writes the image data to the file.

Continue reading

Java interface default methods

Java interfaces cannot implement code in the methods declared, instead as you probably know they have to be overridden and implemented in the class that implement that particular interface. However, one feature in Java since version 8 is that you can define methods as default in an interface and provide the implementation for it as well, which means that the class or classes that implement that particular interface don’t have to override those methods, but they can still be reached from the caller of that class.

Continue reading

How to create threads using lambda expressions

When we create a thread we either extend the Thread class or create an object to pass in to the Thread class constructor that implement the Runnable interface. This example shows how to implements the Runnable interface by declaring anonymous classes and pass them to the Thread constructor using lambda expressions.

Continue reading

List possible Time Zones or ZoneIds in Java

Sometimes you might need to get the date and time for a specific time zone. A specific case would be when you need to pass in the ZoneId to one of the classes LocalDate, LocalTime or LocalDateTime. This code example shows which ZoneIds that can be used by fetching them from the system and printing them out.

Continue reading

Get current date and time with LocalDateTime class

Before Java 8 we had to either create an instance of the Date class or use the Calendar class to get the current date and time. But with the new date API that arrived with Java 8 there are new classes that can accomplish this. To get the current date and time we can use the LocalDateTime class together with the TextStyle class to get the date and time and print them out.
This is how it’s done:

The Date is printed out in different formats. The format is decided by the String.format class where we specify how it should look. In the call to String.format we specify the format for the output. The %d means we will insert a digit at that position, and %s means we will insert a string. The values follow as parameters to the method after the initial format parameter.
The output from the code above is (or was when this was written):

Display date in different formats with the LocalDate class

Since Java 8 a new date API is available. This example shows how to display todays date in different formats using this new API. The LocalDate class has a method called now() to get the current date. The LocalDate class then have methods to get day, month and year in different formats (either as number, text, short text etc). This is accomplished with the TextStyle class.
In the example we use String.format() to output the data in the format we want. The %d means we want to output a digit (integer etc.) and the %s means we want to output a string.

Continue reading