Since 1992


                   Programming domain objects
                   About JavaGantt model
                   Undo / Redo support
                   Localization support
                   Building a treetable
                   Painting a chart
                   Working with the time scale
                   JavaGantt actions
       Promote your software
 Contact Us
© 2012 BeeSoft ®

Painting a chart

JavaGantt paints its chart in layers. Each layer paints just the things it is programmed for. You can assemble the chart from the layers as you need. So the chart preparation can look like this:

	JavaGantt gantt = ...
	gantt.addLayer (new BackgroundLayer ());
	gantt.addLayer (new CalendarLayer ());
	gantt.addLayer (new GridLayer ());
	gantt.addLayer (new TodayLayer ());
	gantt.addLayer (new GanttNodeLayer ());
	gantt.addLayer (new LabelLayer ());
	gantt.addLayer (new DependencyLayer ());

All these layers are available in the JavaGantt distribution. Note, the order you add them is important - in this order they are invoked to paint.

But you are not limited to use just these layers. You can write your own, or modify existing, or to omit some of them, to get the original look for your application.

Each layer is derived from eu.beesoft.gantt.chart.Layer class. This class provides a basic support for painting and mouse operations.

The good news is that you don't need to take a care of adding objects to layers and their synchronization with JavaGantt model. Layers work directly with JavaGantt model nodes. A GanttTreeNode holds its bounds - it is a rectangle where this node is painted in JavaGantt ChartComponent (Layer) coordinates. This can your work greatly facilitate.

Now we can see how to customize some layer. We have the GanttNodeLayer which paints timeline objects (tasks, summaries and milestones). And now we want to paint the tasks differently. We want to paint task's time line with colorized information about task completion.

The easiest way to implement this is to subclass GanttNodeLayer and override method paintTask():

public class MyLayer extends GanttNodeLayer {

	protected void paintTask (GanttNode node, Graphics g) {
		Rectangle bounds = node.getBounds ();
		int x = bounds.x;
		int y = bounds.y;
		int width = bounds.width;
		int height = bounds.height;

		// don't fill whole space for node, it looks uglily
		if (width > 6) {
			x += 2;
			width -= 5;
		y = y + height / 4 - 1;
		height = height / 2 - 1;

		// paint border around task time line
		g.setColor (getTaskBorderColor ());
		g.drawRect (x, y, width, height);

		// edit coordinates

		// get the task completion and compute threshold
		Task task = (Task) node.getUserObject ();
		double completion = task.getCompletion(); // value between 0.0 and 100.0
		int threshold = (int) (width * completion) / 100;

		// paint completed part of task with darker color
		if (threshold > 0) {
			g.setColor (getTaskInteriorColor ().darker());
			g.fillRect (x, y, threshold, height);

		// paint incomplete part of task with brighter color
		if (threshold < 100) {
			g.setColor (getTaskInteriorColor ().brighter());
			g.fillRect (x + threshold, y, width - threshold, height);