Jump to: navigation, search

Difference between revisions of "GEF/GEF4/Graphics"

< GEF‎ | GEF4
m
m
Line 9: Line 9:
 
== IGraphics ==
 
== IGraphics ==
  
The heart of the GEF 4 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. Therefore, you have to create an IGraphics implementation for either AWT or SWT. Let us consider the following SWT example:
+
The heart of the GEF 4 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. Therefore, you have to create a specific IGraphics implementation. Let us consider the following SWT example:
  
 
<source lang="java">
 
<source lang="java">
 +
package org.eclipse.gef4.graphics.examples;
 +
 +
import org.eclipse.gef4.geometry.planar.Ellipse;
 +
import org.eclipse.gef4.graphics.Color;
 +
import org.eclipse.gef4.graphics.IGraphics;
 
import org.eclipse.gef4.graphics.swt.DisplayGraphics;
 
import org.eclipse.gef4.graphics.swt.DisplayGraphics;
 
import org.eclipse.swt.SWT;
 
import org.eclipse.swt.SWT;
Line 20: Line 25:
  
 
public class SWTGraphicsExample implements PaintListener {
 
public class SWTGraphicsExample implements PaintListener {
 +
 
public static void main(String[] args) {
 
public static void main(String[] args) {
 
new SWTGraphicsExample("GEF 4 Graphics - SWT");
 
new SWTGraphicsExample("GEF 4 Graphics - SWT");
Line 34: Line 40:
 
shell.addPaintListener(this);
 
shell.addPaintListener(this);
  
while (!shell.isDisposed())
+
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
+
if (!display.readAndDispatch()) {
 
display.sleep();
 
display.sleep();
 +
}
 +
}
 
}
 
}
  
 
public void paintControl(PaintEvent e) {
 
public void paintControl(PaintEvent e) {
 
DisplayGraphics g = new DisplayGraphics(e.gc);
 
DisplayGraphics g = new DisplayGraphics(e.gc);
                // do your drawings here
+
renderScene(g);
 +
g.cleanUp();
 
}
 
}
 +
 +
public void renderScene(IGraphics g) {
 +
g.getFillProperties().setColor(new Color(255, 0, 0, 255));
 +
g.fill(new Ellipse(50, 50, 350, 200));
 +
}
 +
 
}
 
}
 
</source>
 
</source>
 +
 +
As you can see in this example, the DisplayGraphics 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 do not need to adjust our rendering code when switching to AWT, for example. Although we are not going to discuss anything but the rendering code in detail here, there is an important thing to notice: The DisplayGraphics will allocate system resources during the rendering process. To fully free those resources, we have to call the cleanUp() method on the DisplayGraphics when we are done with our drawing code.
  
 
The different geometric objects provided by the GEF 4 Geometry component can be easily displayed via the GEF 4 Graphics component. Additionally, the
 
The different geometric objects provided by the GEF 4 Geometry component can be easily displayed via the GEF 4 Graphics component. Additionally, the

Revision as of 09:17, 31 August 2012

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 GEF 4 Graphics component provides a level of abstraction over different drawing toolkits. 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 underlying drawing toolkits. Of course, this is not always viable, but rather the greatest common divisor of the underlying drawing toolkits is accessible via the abstraction layer.

IGraphics

The heart of the GEF 4 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. Therefore, you have to create a specific IGraphics implementation. Let us consider the following SWT example:

package org.eclipse.gef4.graphics.examples;
 
import org.eclipse.gef4.geometry.planar.Ellipse;
import org.eclipse.gef4.graphics.Color;
import org.eclipse.gef4.graphics.IGraphics;
import org.eclipse.gef4.graphics.swt.DisplayGraphics;
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 SWTGraphicsExample implements PaintListener {
 
	public static void main(String[] args) {
		new SWTGraphicsExample("GEF 4 Graphics - SWT");
	}
 
	public SWTGraphicsExample(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);
 
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
 
	public void paintControl(PaintEvent e) {
		DisplayGraphics g = new DisplayGraphics(e.gc);
		renderScene(g);
		g.cleanUp();
	}
 
	public void renderScene(IGraphics g) {
		g.getFillProperties().setColor(new Color(255, 0, 0, 255));
		g.fill(new Ellipse(50, 50, 350, 200));
	}
 
}

As you can see in this example, the DisplayGraphics 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 do not need to adjust our rendering code when switching to AWT, for example. Although we are not going to discuss anything but the rendering code in detail here, there is an important thing to notice: The DisplayGraphics will allocate system resources during the rendering process. To fully free those resources, we have to call the cleanUp() method on the DisplayGraphics when we are done with our drawing code.

The different geometric objects provided by the GEF 4 Geometry component can be easily displayed via the GEF 4 Graphics component. Additionally, the