• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Part: II Graphics

Part II: Graphics

The SWT graphics routines model the kinds of constructs that are found in most graphical user interfaces: points, rectangles, regions, fonts, colors, images, and primitive graphics operations, such as line and circle drawing. These routines were created to allow applications to do the following tasks.

  1. Control the appearance of SWT widgets by setting various aspects of their presentation, for example, by changing foreground and background colors, and setting the font used to display text.

  2. Draw simple application-specific graphics, such as charts, flow diagrams, and images.

  3. Create entirely new widgets whose presentations are drawn using graphics primitives (in other words, creating “custom” widgets).

We have already dealt in detail with the task of controlling a widget's appearance by setting its font, foreground and background colors, and so forth, so we will not cover that again here. In this part of the book, we will look at the details of the graphics routines that SWT provides, but before we get to that, it is important to note what the SWT graphics support does not provide.

3D Three-dimensional modeling and rendering support is not directly part of the API because it is not required by most applications. However, the SWT implementers have created an experimental Java binding to OpenGL[1] called org.eclipse.swt.opengl. OpenGL is the most widely used standard for 2D and 3D graphics programming and is available on all the supported platforms, making it an excellent choice as the basis for an SWT 3D library. Unfortunately, the work on this is ongoing, and as of the writing of this book, there is recent evidence that the larger Java community is also becoming interested in creating a standard Java OpenGL binding. Because updating a book is quite a bit harder than updating a Web page, we will avoid talking further about org.eclipse.swt.opengl here. If you are interested in learning more about this, you should check out the developer resources on the SWT home page at Eclipse.org (http://www.eclipse.org/swt).

[1] More information about OpenGL can be found at http://www.opengl.org/.

Advanced 2D The SWT graphics routines do not cover all possible 2D drawing requirements. Compositing, complex image transformations, multicolored text, and other such advanced features are not provided by the base library. However, OpenGL does provide advanced 2D as well as 3D drawing capabilities, making the possibilities of interfacing SWT and OpenGL very exciting.

Video and animation SWT does not have specific support for dealing with video streams or animation. Applications that need to display video can usually make use of the strong platform integration SWT provides to embed the system video player within the application (for example, via OLE on Microsoft Windows). Simple animation is possible using threading and the image drawing support described in the Images chapter. This is sufficient to create rollover images, spinning balls, and other similar capabilities you might require to create custom user interface elements, but it is clearly not an animation package.

Although all of the above topics are interesting and represent areas where the SWT implementers may focus their attention in the future, they are really beyond the scope of the problem that SWT graphics is trying to solve. The graphics routines were designed to provide access to the basic graphical capabilities of the platform, allowing applications to control the appearance of native widgets, to create new user-drawn custom widgets, and to do the kinds of simple graphics that applications frequently require. As is always true in SWT, they were built to map directly onto native platform capabilities, with a focus on good performance and a simple API.

There are excellent examples of the use of the graphics routines throughout the SWT library. In the custom widgets package, StyledText and its related support classes make extensive use of them; CTabFolder and CLabel are also good examples. The emulated widgets that are used when a platform widget is unavailable or incomplete on a particular platform are also typically written in terms of the graphics routines. The emulated CoolBar class is one such.

As a measure of how much can be done with SWT graphics, it is worth looking at the Graphical Editing Framework (GEF) created at Eclipse.org. GEF is a powerful tool for mapping application models onto graphical editors. It includes an advanced 2D graphics library, called Draw2d, which was built entirely on top of SWT. For more information, see http://www.eclipse.org/gef.

The remainder of this part of the book contains five chapters describing various aspects of the SWT graphics library.

  • Graphics Fundamentals

  • Color

  • Fonts

  • Images

  • Cursors

All of these chapters contain information that is important for every SWT application. If you are new to SWT, reading through them in order is a good idea. Later chapters assume that you are familiar with the material from the earlier ones.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint