How to implement a gradient background renderer
Tips & TricksSummary
This article shows how to implement an own background renderer (gradient renderer) for the Graph2DView.
Description
The Graph2DView allows to set a custom background renderer that implements the BackgroundRenderer - interface.
The following sample code presents a custom BackgroundRenderer implementation, that paints a background using a gradient painter.
/**
* This BackgroundRenderer implementation paints the background using a gradient painter
* from blue (top left corner) to white (bottom right corner).
*/
class GradientBackgroundRenderer implements BackgroundRenderer {
private Graph2DView view;
GradientBackgroundRenderer(Graph2DView view) {
this.view = view;
}
public void paint(Graphics2D gfx, int x, int y, int w, int h) {
final Rectangle visibleRect = view.getVisibleRect();
//Store old paint
final Paint oldPaint = gfx.getPaint();
//Draw a rectangle in the visible area, using the GradientPainter.
GradientPaint gradientPaint = new GradientPaint(visibleRect.x, visibleRect.y, Color.BLUE,
visibleRect.x + visibleRect.width, visibleRect.y + visibleRect.height, Color.WHITE);
gfx.setPaint(gradientPaint);
gfx.fillRect(visibleRect.x, visibleRect.y, visibleRect.width, visibleRect.height);
//Reset old paint
gfx.setPaint(oldPaint);
}
}
In order to apply the GradientBackgroundRenderer on the view, please use the method:
Graph2DView.setBackgroundRenderer(BackgroundRenderer renderer)
//view is a Graph2DView instance
view.setBackgroundRenderer(new GradientBackgroundRenderer(view));
Categories this article belongs to:
yFiles for Java > yFiles Viewer > Displaying and Editing Graphs
Applies to:
yFiles for Java 2: 2.7, 2.8, 2.9, 2.10, 2.11, 2.12, 2.13, 2.14, 2.15, 2.16, 2.17, 2.18
Keywords:
BackgroundRenderer - GradientBackgroundRenderer - background - gradient