For many years, Java programmers have been building GUI's with AWT, the "Abstract Window Toolkit". This was Java's first stab at creating a widget library for buttons, windows, text fields etc. AWT has it's many shortcomings, since it generally relies on every operating system having a corresponding native component. Sun soon came out with Swing, which many people saw as being far more flexible, since it did not rely on native OS components for its implementation.
More recently, a newer component toolkit has gained popularity. SWT, the "Standard Widget Toolkit", is the fundamental component library upon which Eclipse is built. More and more developers are adopting SWT, as Eclipse is becoming the universal standard for Java development environments.
I recently faced a challenge. If I had code that relied heavily on its interaction with the AWT-based Java2D drawing routines, how could I port that over to a new SWT-based program?
After some searching, I found an excellent article and some sample code at IBM's DeveloperWorks site. The author provides source for a small utility class, Graphics2DRenderer, that essentially allows you to convert an SWT-based graphics context to a java.awt.Graphics object.
I put this code to work in a very basic sample in an attempt to use Snowbound's RasterMaster for Java in an SWT program. Since Snowbound's painting routines rely on a java.awt.Graphics object, I needed to use the Graphics2DRenderer to generate an instance.
import java.awt.Graphics2D;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import swtgraphics2d.Graphics2DRenderer;
import Snow.Snowbnd;public class SWTExample
{Snowbnd simage ;
public SWTExample()
{
simage = new Snowbnd();
simage.IMG_decompress_bitmap("c:/imgs/rollsroy.png",0);
Display display = new Display();
final Shell shell = new Shell(display);
FillLayout layout = new FillLayout();
shell.setLayout(layout);Canvas canvas = new Canvas(shell, SWT.CENTER);
final Graphics2DRenderer renderer = new Graphics2DRenderer();canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
Point controlSize = ((Control) e.getSource()).getSize();GC gc = e.gc; // gets the SWT graphics context from the event
renderer.prepareRendering(gc); // prepares the Graphics2D renderer
// gets the Graphics2D context
Graphics2D g2d = renderer.getGraphics2D();/*
* Perform actions on g2d
*/simage.IMG_display_bitmap(g2d, 0,0,controlSize.x, controlSize.y);
/*
* Done with the g2d object
*/// now that we are done with Java 2D, renders Graphics2D operation
// on the SWT graphics context
renderer.render(gc);// now we can continue with pure SWT paint operations
gc.drawOval(0, 0, controlSize.x, controlSize.y);
}
});shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}public static void main(String[] args)
{
SWTExample swt = new SWTExample();
}}
-Alex
If you want to code a simulation of turtle movement, then Graphics2DRenderer seems ok, but for other tasks - it's too SLOW!
Posted by: Sepulcro | June 14, 2006 at 12:48 PM