Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "GEF/GEF4/Graphics"

< GEF‎ | GEF4
m
m
Line 6: Line 6:
  
 
The GEF4 Graphics component provides a level of abstraction over different drawing toolkits (natives). At the moment, SWT and AWT backends are in development. Its goal is to be as complete as possible, i.e. you should have access to all the functionality that you have access to when working directly with one of the natives (SWT or AWT). Of course, this is not always viable. But we aim to provide at least the greatest common divisor of the functionality that is offered by the individual natives and additionally, decent simulations for operations which are not offered by all natives.
 
The GEF4 Graphics component provides a level of abstraction over different drawing toolkits (natives). At the moment, SWT and AWT backends are in development. Its goal is to be as complete as possible, i.e. you should have access to all the functionality that you have access to when working directly with one of the natives (SWT or AWT). Of course, this is not always viable. But we aim to provide at least the greatest common divisor of the functionality that is offered by the individual natives and additionally, decent simulations for operations which are not offered by all natives.
 +
 +
== Features ==
 +
 +
{|border="0" borderwidth="10"
 +
!width="*"|
 +
!width="130" align="center"|GEF4 Graphics
 +
!width="130" align="center"|GEF 3.x Draw2d
 +
!width="130" align="center"|SWT
 +
!width="130" align="center"|AWT
 +
!width="*" align="center"|Additional information
 +
|-
 +
|'''Drawing geometric primitives'''
 +
|-
 +
|Lines, polylines, rectangles, polygons, arcs, and ellipses
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|[[#How to draw graphics primitives|how-to]]
 +
|-
 +
|Arbitrary Bezier curves
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|<div style="background-color:#a90; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+/-<sup><span style="position:absolute">&nbsp;&nbsp;<span style="border-bottom:1px dotted; cursor:help;" title="Implemented for quadratic and cubic Bezier curves.">1</span></span></sup></div>
 +
|<div style="background-color:#a90; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+/-<sup><span style="position:absolute">&nbsp;&nbsp;<span style="border-bottom:1px dotted; cursor:help;" title="Implemented for quadratic and cubic Bezier curves.">1</span></span></sup></div>
 +
|<div style="background-color:#a90; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+/-<sup><span style="position:absolute">&nbsp;&nbsp;<span style="border-bottom:1px dotted; cursor:help;" title="Implemented for quadratic and cubic Bezier curves.">1</span></span></sup></div>
 +
|[[#How to draw arbitrary Bezier curves|how-to]]
 +
|-
 +
|'''Displaying text'''
 +
|-
 +
|Basic text drawing
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|[[#How to draw text|how-to]]
 +
|-
 +
|Font styles: italic, bold, and underlined
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|<div style="background-color:#a90; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+/-<sup><span style="position:absolute">&nbsp;&nbsp;<span style="border-bottom:1px dotted; cursor:help;" title="Missing font style: underlined.">2</span></span></sup></div>
 +
|<div style="background-color:#a90; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+/-<sup><span style="position:absolute">&nbsp;&nbsp;<span style="border-bottom:1px dotted; cursor:help;" title="Missing font style: underlined.">2</span></span></sup></div>
 +
|<div style="background-color:#a90; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+/-<sup><span style="position:absolute">&nbsp;&nbsp;<span style="border-bottom:1px dotted; cursor:help;" title="Missing font style: underlined.">2</span></span></sup></div>
 +
|[[#How to change the font style|how-to]]
 +
|-
 +
|'''Image operations'''
 +
|-
 +
|Basic image drawing
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|[[#How to display images|how-to]]
 +
|-
 +
|Resizing
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|[[#How to resize images|how-to]]
 +
|-
 +
|Blurring
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|<div style="background-color:darkred; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">-</div>
 +
|<div style="background-color:darkred; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">-</div>
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|[[#How to blur images|how-to]]
 +
|-
 +
|Combining images via standard arithmetic or logical operations
 +
|<div style="background-color:darkgreen; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">+</div>
 +
|<div style="background-color:darkred; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">-</div>
 +
|<div style="background-color:darkred; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">-</div>
 +
|<div style="background-color:darkred; color:white; font-weight:bold; border-radius:8px; -moz-border-radius:8px; text-align:center;">-</div>
 +
|[[#How to do image arithmetic|how-to]]
 +
|}
 +
 +
'''Footnotes:'''
 +
 +
#Implemented for quadratic and cubic Bezier curves.
 +
#Missing font style: underlined.
 +
 +
=== How to draw graphics primitives ===
  
 
== IGraphics ==
 
== IGraphics ==
Line 106: Line 186:
  
 
When looking at the code, there are two important observations:
 
When looking at the code, there are two important observations:
# The GEF4 Geometry component is integrated into the IGraphics interface, so that we can easily display geometry objects.<br />Notably, the power of the draw() and fill() operations is directly coupled with the power of the Geometry component. All drawing primitives, such as lines, rectangles, ellipses, and many more, are represented by corresponding Geometry objects.
+
# The GEF4 Geometry component is integrated into the IGraphics interface, so that we can easily display geometry objects. Notably, the power of the draw() and fill() operations is directly coupled with the power of the Geometry component. All drawing primitives, such as lines, rectangles, ellipses, and many more, are represented by corresponding Geometry objects.
 
# The drawing operations of the IGraphics are influenced by a set of attributes which are managed by the IGraphics (draw-color, fill-color, etc.). A full set of attributes is referred to as a GraphicsState.
 
# The drawing operations of the IGraphics are influenced by a set of attributes which are managed by the IGraphics (draw-color, fill-color, etc.). A full set of attributes is referred to as a GraphicsState.
  

Revision as of 09:12, 1 March 2013

Note to non-wiki readers: This documentation is generated from the Eclipse wiki - if you have corrections or additions it would be awesome if you added them in the original wiki page.


Introduction

The GEF4 Graphics component provides a level of abstraction over different drawing toolkits (natives). At the moment, SWT and AWT backends are in development. Its goal is to be as complete as possible, i.e. you should have access to all the functionality that you have access to when working directly with one of the natives (SWT or AWT). Of course, this is not always viable. But we aim to provide at least the greatest common divisor of the functionality that is offered by the individual natives and additionally, decent simulations for operations which are not offered by all natives.

Features

GEF4 Graphics GEF 3.x Draw2d SWT AWT Additional information
Drawing geometric primitives
Lines, polylines, rectangles, polygons, arcs, and ellipses
+
+
+
+
how-to
Arbitrary Bezier curves
+
+/-  1
+/-  1
+/-  1
how-to
Displaying text
Basic text drawing
+
+
+
+
how-to
Font styles: italic, bold, and underlined
+
+/-  2
+/-  2
+/-  2
how-to
Image operations
Basic image drawing
+
+
+
+
how-to
Resizing
+
+
+
+
how-to
Blurring
+
-
-
+
how-to
Combining images via standard arithmetic or logical operations
+
-
-
-
how-to

Footnotes:

  1. Implemented for quadratic and cubic Bezier curves.
  2. Missing font style: underlined.

How to draw graphics primitives

IGraphics

The heart of the GEF4 Graphics component is the IGraphics interface. Normally, you will be handed over an IGraphics to accomplish your displaying tasks by a surrounding framework, but it can be used stand-alone, too. Thereto, you have to create a specific IGraphics implementation. Let us consider the following SWT example:

[Example 001: Simple Graphics]

/*******************************************************************************
 * Copyright (c) 2012, 2013 itemis AG and others.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Matthias Wienand (itemis AG) - initial API and implementation
 *     
 *******************************************************************************/
package org.eclipse.gef4.graphics.examples.doc;
 
import org.eclipse.gef4.geometry.planar.Ellipse;
import org.eclipse.gef4.geometry.planar.Polygon;
import org.eclipse.gef4.geometry.planar.Rectangle;
import org.eclipse.gef4.graphics.IGraphics;
import org.eclipse.gef4.graphics.LineCap;
import org.eclipse.gef4.graphics.LineJoin;
import org.eclipse.gef4.graphics.color.Color;
import org.eclipse.gef4.graphics.swt.SwtGraphics;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
 
public class Example001 implements PaintListener {
 
	public static void main(String[] args) {
		new Example001("Simple Graphics");
	}
 
	public Example001(String title) {
		Display display = new Display();
 
		Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED);
		shell.setText(title);
		shell.setBounds(0, 0, 640, 480);
		shell.open();
 
		shell.addPaintListener(this);
		shell.redraw(); // platform independently triggers a PaintEvent
 
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
 
	@Override
	public void paintControl(PaintEvent e) {
		SwtGraphics g = new SwtGraphics(e.gc);
		renderScene(g);
		g.cleanUp();
	}
 
	public void renderScene(IGraphics g) {
		// The rendering code goes here. It is independent of the actual
		// IGraphics implementation. Therefore, it is independent of the
		// underlying drawing toolkit, too.
 
		final Ellipse ellipse = new Ellipse(50, 50, 350, 200);
		final Rectangle rectangle = new Rectangle(100, 160, 125, 220);
		final Polygon triangle = new Polygon(260, 170, 190, 300, 330, 300);
 
		g.setFill(new Color(255, 0, 0)).setDraw(new Color(128, 0, 0))
				.setDashArray(25, 10);
 
		g.fill(ellipse).draw(ellipse.getOutline());
 
		g.setFill(new Color(0, 0, 255)).setLineJoin(LineJoin.ROUND)
				.setLineCap(LineCap.ROUND);
 
		g.fill(rectangle).draw(rectangle.getOutline());
 
		g.setFill(new Color(0, 255, 0)).setDraw(new Color(0, 128, 0))
				.setLineJoin(LineJoin.MITER);
 
		g.fill(triangle).draw(triangle.getOutline());
	}
 
}

Simple GEF4 Graphics example (SWT)

[Note: You can tryout the examples in your local GEF4 installation. Every example shown in this documentation exists in the org.eclipse.gef4.graphics.examples project under the org.eclipse.gef4.graphics.examples.doc package. The examples are easily found via their identification number which is written atop the source code.]

As you can see in this example, the SwtGraphics is an IGraphics implementation. It can be constructed via a SWT GC which is passed-in to the paintControl() event handler. The renderScene() method does not know anything about the actual IGraphics implemention that is used. That's why we would not need to adjust our rendering code when switching to AWT, for example. You may wonder why we have to call the cleanUp() method on the IGraphics. In the case of SWT, this is necessary to fully free the system resources that will be allocated during the rendering process.

When looking at the code, there are two important observations:

  1. The GEF4 Geometry component is integrated into the IGraphics interface, so that we can easily display geometry objects. Notably, the power of the draw() and fill() operations is directly coupled with the power of the Geometry component. All drawing primitives, such as lines, rectangles, ellipses, and many more, are represented by corresponding Geometry objects.
  2. The drawing operations of the IGraphics are influenced by a set of attributes which are managed by the IGraphics (draw-color, fill-color, etc.). A full set of attributes is referred to as a GraphicsState.

IGraphics drawing operations

There are four different drawing operations available on an IGraphics: draw(), fill(), write(), and blit().

  • Using the IGraphics#draw(ICurve) and IGraphics#draw(Path) methods, you can display the (contour of the) passed-in geometry object.
    • For a reference of all available geometry objects, take a look at the GEF4 Geometry component (GEF/GEF4/Geometry).
    • Influencing attributes: draw-pattern (Color, Gradient, and Image), stroke (dash-array, dash-begin, line-cap, line-join, line-width, miter-limit)
  • Analogical, the IGraphics#fill(IShape), IGraphics#fill(IMultiShape), and IGraphics#fill(Path) methods will fill the interior of the passed-in geometry object.
    • Influencing attributes: fill-pattern (Color, Gradient, and Image)
  • Using the IGraphics#blit(Image) method, you can display the passed-in Image object.
    • Influencing attributes: interpolation-hint
  • Using the IGraphics#write(String) method, you can display the passed-in String object.
    • Influencing attributes: font, write-pattern (Color, Gradient, and Image), background-color

GraphicsState

A GraphicsState represents one set of IGraphics attributes. The following list presents all available attributes, default values, and a short description of the attributes.

  • InterpolationHint = QUALITY
    When displaying a transformed Image, the InterpolationHint specifies whether to apply a fast and low-quality (InterpolationHint#SPEED) interpolation, or to apply a slower but higher-quality (InterpolationHint#QUALITY) interpolation.
  • Draw-Pattern = black
    Specifies the draw-pattern which consists of a Color, a Gradient, an Image, and a Mode. The pattern's mode specifies which of the three values (Color, Gradient, and Image) is queried for the drawing color.
  • Dash-Array = {}
    Specifies a double[] consisting of distance values which alternatingly specify opaque and transparent sections.
  • Dash-Begin = 0
    Specifies the initially assumed covered distance when applying the DashArray.
  • LineCap = FLAT
    Specifies how to display unconnected end points of displayed lines. A FLAT LineCap will not extend a drawn line beyond its unconnected end points. A ROUND LineCap will draw a semi-circle at the unconnected end points of a displayed line. A SQUARE LineCap will extend a drawn line beyond its unconnected end points by half the line's width.
  • LineJoin = BEVEL
    Specifies how to display the connection point of two displayed lines. A BEVEL LineJoin fills the bent corner triangular. A MITER LineJoin fills the bent corner up to the intersection point of the two outermost lines if its distance to the middle intersection is less than or equal to the MiterLimit. In case of exceeding the MiterLimit, the BEVEL LineJoin is used.
  • Line-Width = 1
    Specifies the width of displayed lines.
  • Miter-Limit = 10
    Restricts the use of the MITER LineJoin, because for low intersection angles, the intersection point may lie far away from the original connection point. Its value is the maximal quotient of the distance between the intersection point and the connection point and the line width.
  • Fill-Pattern = black
    Specifies the fill-pattern which consists of a Color, a Gradient, an Image, and a Mode. The pattern's mode specifies which of the three values (Color, Gradient, and Image) is queried for the filling color.
  • Write-Pattern = black
    Specifies the write-pattern which consists of a Color, a Gradient, an Image, and a Mode. The pattern's mode specifies which of the three values (Color, Gradient, and Image) is queried for the writing color.
  • Write-Background = transparent white
    Specifies the background color for write() operations.

Additional to these attributes, a GraphicsState consists of four general attributes, which will affect all drawing operations:

  • AffineTransform = identity transform
    Transforms the coordinate system before applying any drawing operations.
  • Clip = null
    Specifies a Path which serves as the clipping area for all subsequent drawing operations. Setting the clip to null disables clipping.
  • Antialiasing = true
    If enabled, anti-aliasing is used to smooth drawn edges.
  • Xor-mode = false
    If enabled, combines source and destination colors via binary exclusive-or when drawing.

Back to the top