Category Archives: Java

WIP : SlickPong

Here is another project I am currently working on, it is a pong like game called SlickPong. Why SlikPong? Because Slick is the name of the Java library I am using to develop this game.

Slick is a 2D Java game library based on LWJGL. This library really ease the development of games, a bad point about it is the lack of documentation in the API but you will find plenty of tutorials to fill this void.

For now the ball can bounce on the screen and I handle the player movement. I am currently working on the collisions between the player & the ball. You can follow this project on GitHub, get the sources if you want: http://github.com/flhacqueba/SlickPong

Enhance native looking of Netbeans on Linux

Netbeans is an IDE that I use to develop Java softwares but, if you have already launched this software on Linux you may have noticed that the font rendering and the global UI aren’t that good, and it is not comfortable to work in an environment like that.

I am talking about the ugly Swing’s default theme and more especially the lack of a decent font rendering. Fortunately, you can fix those issues with options. You have to passed those arguments when you are launching Netbeans.

First of all, we will see the options needed to fix the fonts:

netbeans -J-Dswing.aatext=true -J-Dawt.useSystemAAFontSettings=on

If you try it now, I bet you will like the fonts, it is much better. Second of all, the GUI well I should called it the Look & Feel of the software. We will change the Swing L&F to your current GTK L&F:

netbeans --laf com.sun.java.swing.plaf.gtk.GTKLookAndFeel

And you are done, you should now have something like that (depends on your GTK theme of course, but it does not look like a foolish software anymore).

It is important to know that you should put those options in the netbeans.conf file which contains a variable with all the options netbeans should load on startup. It depends on the installation folder of netbeans but in my case the location of this file is /usr/share/netbeans/etc/netbeans.conf. Edit this file and add the options to the netbeans_default_option variable. It should looks like that:

netbeans_default_options="-J-Dswing.aatext=true -J-Dawt.useSystemAAFontSettings=
on --laf com.sun.java.swing.plaf.gtk.GTKLookAndFeel"

Create charts in Java with JFreeChart

When you have to write a report or do a presentation you will probably need charts. We are about to see how to create charts efficiently in Java with a free API called JFreeChart.
By using this library you can generate any kind of chart: line, bar, pie, area, etc. You can fully customize the look & feel of your chart and even create 3D charts.

To use the library you should follow the following schema:

  1. Retrieve and format your data
  2. Create and fill the dataset of your chart
  3. Initialize your chart
  4. Customize its look & feel
  5. Render it

To make it easier we will start without retrieving the data and we will add some basic stuff in our dataset. You should know that you are able to get your data from a database (using JDBC) or from an XML file, and any other common data storage you can use.

Use JFreeChart in your project

Once you have downloaded the library’s archive, you need to add jcommon-x.x.xx.jar and jfreechart-x.x.xx.jar in your project’s classpath.

Create a 3D Pie Chart

/* Initialize the dataset and fill it. */
DefaultPieDataset dataset = new DefaultPieDataset();
 
dataset.setValue("IT Articles", 5);
dataset.setValue("Projects", 3);
dataset.setValue("Public", 1);
 
/* Create a 3D Pie chart. */
JFreeChart chart = ChartFactory.createPieChart3D("Posts on this blog", dataset, true, true, false);
 
/* Save the chart on the hard drive. */
ChartUtilities.saveChartAsJPEG(new File("data/posts-3d-pie.jpg"), chart, 500, 280);

It seems easy, isn’t it? Well, I will still explain this snippet.
I have followed the schema I gave you before. I create a dataset and fill it, then I create my chart and save it in a file (you can also instantly render it by using a BufferedImage for a website or a software application). The library use a factory to create charts which means each and every time you want to create a chart, from any kind (bar, line, etc), you will have to use this factory class.
Here is what you will get with this piece of code:

Customize the chart’s render

This library give you the ability to fully customize the look & feel of your chart: colors, legends, values’ format and so on.
In this article we will cover how to display the value of each category in our labels.

To do that we can create a custom rendering class which will be dedicated to define the label of each category. JFreeChart give us an interface for that, we will need to implements the required methods.

public class CustomPieSectionLabelGenerator implements PieSectionLabelGenerator
{
	/* other stuff... */
 
	@Override
	public String generateSectionLabel(PieDataset dataset, Comparable key)
	{
		StringBuilder label = new StringBuilder();
 
		if(dataset.getItemCount() > 0 && key != null)
		{
			/* I want to display the key
			 * but I also want to disply the value as an integer.
			 */
			label.append(key);
			label.append(": ");
			label.append(dataset.getValue(key).intValue());
		}
 
		return label.toString();
	}
}

With this custom label generator we will see ‘It Articles: 5‘ instead of ‘IT Articles‘ in the chart.
Last step: configure the chart to use our custom generator class.

/* Initialize the dataset and fill it. */
DefaultPieDataset dataset = new DefaultPieDataset();
 
dataset.setValue("IT Articles", 5);
dataset.setValue("Projects", 3);
dataset.setValue("Public", 1);
 
/* Create a 3D Pie chart. */
JFreeChart chart = ChartFactory.createPieChart3D("Posts on this blog", dataset, true, true, false);
 
/* We also want to display our value next to the legend. */
PiePlot3D plot = (PiePlot3D) chart.getPlot();
plot.setLabelGenerator(new CustomPieSectionLabelGenerator());
 
/* Save the chart on the hard drive. */
ChartUtilities.saveChartAsJPEG(new File("data/posts-3d-pie-custom.jpg"), chart, 500, 280);

Every time you will want to customize something it will follow the same pattern. Create a custom rendering class and implements the correct methods, configure your chart adequately. Take a look at the API documentation for more information.
Here is the result:

There are plenty of API in Java to generate charts and if you want to create a fully report I recommend iReport.