0130351180 pdf download






















The program creates a standard window and calls WIN32 API directly to draw a circle in the client area of the main program window. The circle is centered in the window and the size is adjusted automatically if the window is resized.

The WinMain function line 44 is the entry point of the program. The window class is then registered with Windows. A window is created based on the registered window class.

The window is displayed and updated. As in most GUI environments, Windows programming follows an event-driven model.

Messages are sent to a window and the message handler of the window performs certain actions in response to a message. The loop near the end of WinMain function is a standard message loop which is set up to dispatch the messages received.

The function MainWndProc line 5 is the window procedure responsible for handling the message events of the window. It is a callback function specified in the definition of the window class. The drawing is done through a device context obtained by a call to the BeginPaint function.

The Ellipse function line 33 draws a circle when the width and the height are the same. The EndPaint function ends the drawing. The center and radius of the circle are calculated based on the size of the window client area, which is obtained by calling the function GetClientRect. However, graphics programs that rely on operating-system functions are certainly not portable across platforms.

However, their APIs are different and incompatible at the level of system calls. It is easy to see the advantages of a standard interface for graphics programming. A graphics programming standard will provide a layer of abstraction necessary for device and platform independence.

In the short history of computer graphics, several graphics standards have risen to the prominence. It specifies basic graphics functions independent of computer platforms. Several levels are defined to accommodate different capabilities of the hardware systems. A specific implementation of GKS in a programming language will certainly require a syntax definition appropriate for the language.

A language binding is used to define the specific format of GKS in the programming language. Other language bindings such as Pascal and C are also available. They are useful for static unstructured graphics primitives, but they do not directly support more complex graphics models. They have additional functionalities for hierarchical organizations of graphics primitives and dynamic editing. The GSCR calls define color indices.

An extra point is added to close the curve. The GPL call line 36 draws the polyline. OpenGL is designed to be an open and vendor-neutral industry standard. It is available virtually on all computer platforms. In fact, many hardware vendors offer OpenGL interfaces for their graphics cards and devices. However, the deep root of OpenGL in the C language is still apparent.

The C binding is the most popular one. The GL library contains the core functions for basic graphics features, and the GLU library contains higherlevel utility functions built on top of GL functions. OpenGL itself does not have functions for constructing a user interface. The glutCreateWindow function creates a window.

The glutDisplayFunc function sets the display function, which is a callback function for graphics drawing. The glutMainloop function line 32 starts the event loop. The function display line 4 is defined as the display function for this program.

The vertices are calculated with a parametric equation for the circle and set with the function glVertex2f. Another simple OpenGL example is given in Listing 1. It displays a spinning 3D sphere Figure 1. Double buffering is applied in this example through the function glutInitDisplayMode line The projection matrix of the display is set to an orthogonal projection using GL functions.

The function call gluLookAt line 34 defines the view to have the eye positioned at 1, 1, 1 looking at 0, 0, 0. A sphere object is created with GLU functions. In this case, in addition to the display callback, an idle callback is also defined to drive the animation. The idle function line 13 calls glutPostRedisplay to request a call to the display function. In the display function, the model view matrix is rotated by a small angle. The sphere is then redrawn on the hidden buffer.

Finally the two buffers are swapped to show the new drawing. However, with the rapid development of computer hardware and software technology, it can be argued that an even higher level abstraction of graphics programming is now feasible. OpenGL offers a C-like procedural abstraction. It is not designed to directly accommodate the graphics modeling in an object-oriented programming paradigm. They are high-level object-oriented APIs with high portability. An overview of the Java language and its graphics facilities is given in the next three sections.

In recent years, Java has gained enormous popularity and has quickly become the programming language of choice for a wide range of applications. This platform independence makes Java the ideal language for delivering applications over the Internet. Java is designed from the ground up to support object-oriented programming OOP.

A Java program consists entirely of class definitions. Object instantiations and interactions constitute the main actions of a Java program. The Java language also maintains the simplicity, elegance, and efficiency of its predecessor, the C programming language.

While the language itself is very simple, the Java platform offers a comprehensive set of APIs application programming interfaces. The early versions of Java offered limited graphics support. Only minimal graphics features were included in JDK 1. Besides a simple set of features to create GUI elements, AWT offers capabilities to control certain rendering attributes such as drawing color and to draw simple graphics primitives such as lines, rectangles, and ovals.

There is also some support for images. However, these features are severely limited. For example, there is no way to control the width of drawing lines. Because of the limitations, early Java versions certainly did not provide adequate support for modern computer graphics programming.

Most Swing components are lightweight—they are not implemented as native components. The graphics support in Java 2 is also greatly enhanced. The Java 2D package provides comprehensive 2D graphics features.

A Swing example will be given in the next section. The program in Listing 1. It draws a circle in a frame Figure 1. If the user clicks on the frame, the circle will move to a new location with the center at the mouse pointer. It has a main window with a menu and a circle. The graphical drawing responds to a mouse click by redrawing the figure at the mouse location. It defines the main program window. The menu in the frame is created with objects of the classes MenuBar, Menu, and MenuItem lines 19— The actionPerformed method defined in the interface is the handler for the events.

Two other event handlers are defined in the constructor of the AWTDemo class. A WindowListener is defined as an anonymous inner class from WindowAdapter lines 27— It overrides the windowClosing method to terminate the program upon receiving the closing event for the window.

The other listener is a MouseListener, derived from the MouseAdapter class lines 34— The mouseClicked method is overridden to handle the mouse-click events. In the mouseClicked method, the mouse location is saved to the variables x and y, and a call to the repaint method is made to refresh the drawing and to move the figure to the new location.

The method paint line 44 draws a circle of radius 50 with the method drawOval in the Graphics object. The center of the circle is determined by the variables x and y. The main method creates and displays an instance of AWTDemo. The frame is set to the size by One graphics programming option for Java is OpenGL. There are several projects to develop Java language bindings for OpenGL.

The GLCanvas is a heavyweight component that will use the hardware acceleration. The GLJPanel is a lightweight component implemented in memory. No hardware acceleration is available to GLJPanel. A typical procedure for programming JOGL is outlined below. Add a GLEvent listener to the canvas object. Implement the listener by implementing the four methods: init, display, reshape, and displayChanged.

The well-designed APIs offer comprehensive support for many tasks of computer graphics. Together with the unique advantages of the Java programming language, they have made the combination of Java with Java 2D and Java 3D a very attractive option for graphics programming and learning computer graphics.

The graphics support in early versions of Java is very primitive and limited. Java 2D provides a rather complete set of functionalities to manipulate and render 2D graphics. It provides methods to render geometric shapes, images, and texts. The rendering process can be controlled by selecting transformation, paint, line properties, composition, clipping path, and other properties. The Swing components and Java 2D included in the Java 2 platform are more advanced than the graphics facilities in earlier Java platforms.

It uses certain advanced capabilities of Java 2D such as transparency, gradient paint, transformation, and font glyphs that are not available in AWT. See Figure 1. BOLD, ; g2. The circle is filled with a gradient paint and the text is semitransparent. The Graphics parameter in the method is cast to Graphics2D to take advantage of the extended 1. A circle is drawn with a gradient paint that changes its color based on locations. The composite rule is then set to achieve a degree of transparency.

The details of Java 2D programming are introduced in the later chapters. A Java program can often be written as both an applet and an application. The Demo2D class is a subclass of JApplet line 9 and can be executed as an applet. However, it also contains a main method line 10 so it can also be executed as an application.

The main method creates an instance of JFrame and adds an instance of Demo2D to the frame. It simulates the execution of the applet by calling the init method. The results from the application and the applet are almost identical. Most examples in this book will use this format. Java 2D is a standard part of the core Java 2 platform. This example can be compiled under such an SDK without additional packages. Java 3D provides an incredibly comprehensive framework for 3D graphics, including such advanced features as animation, 3D interaction, and sophisticated viewing.

Yet it still provides a relatively simple and intuitive programming interface. The Java 3D programming paradigm is very different from that of Java 2D. It closely follows the modeling—rendering paradigm. An abstract model known as the scene graph is used to organize and retain the visual objects and behaviors in the virtual scene.

The scene graph contains the complete information of the virtual graphics world. The Java 3D rendering engine renders the scene graph automatically. Java 3D renders a scene on a Canvas3D object. Canvas3D is a heavyweight component that does not work well with the new Swing components. Note The reason that Java 3D still does not have a lightweight canvas is to take advantage of hardware acceleration.

With heavyweight components, the hardware graphics acceleration provided through the native platform support is automatically used. It is possible to mix heavyweight and lightweight components, but care must be taken to avoid some undesirable effects. See Appendix B for more details. URL; import javax. RGBA, 90 image. This is a typical Java 3D application. The main task of the program is centered on the construction of a conceptual data structure called scene graph.

The visual effects of the program are achieved by creating a scene graph and placing the appropriate graphics elements into it.

The scene graph for the program is shown in Figure 1. It is a treelike structure containing objects such as the sphere, the 3D text, appearance, transforms, background, lights, and so on. The rendering of the scene is done automatically by the Java 3D engine. The results are shown in Figure 1. The concepts and techniques of programming Java 3D with scene graphs will be introduced in the later chapters.

The class Demo3D is defined as a subclass of Applet. A main method also exists in Demo3D to run the class as an application. The utility class MainFrame line 16 included in Java 3D provides the necessary functionality to run an applet in a frame. They are different in their objectives and techniques. However, close relationships exist among them, and the lines between them have been increasingly blurred. Image processing is concerned with techniques of processing digital raster images.

It typically deals with problems such as image enhancement, noise reduction, image compression, and edge detection. Image processing takes an existing image as input and performs appropriate actions. Computer graphics, on the other hand, generates synthetic images from a virtual world.

Image processing is closely related to computer graphics. The results of graphics rendering are usually images. Raster images are used extensively in computer graphics as graphics primitives. They are also used as textures to enhance graphics rendering.

Examples of basic image-processing techniques will be given in Chapter 4. Computer vision attempts to derive an understanding from images of the real world. In a way a computer vision system is the inverse of a computer graphics system. Its main goal is to reconstruct a virtual world from real-world images. Therefore, computer vision and computer graphics are complementary to each other. They provide different perspectives to a common system. Closely related mathematics areas include analytic geometry and linear algebra.

Analytic geometry provides a numerical representation for graphical objects. Linear algebra studies the operations and transformations of vector spaces, which are important in many fundamental problems of computer graphics. Relevant mathematical topics will be introduced in the chapters associated with the graphics problems. Appendix A provides a summary of graphics-related mathematical background. Foley, A. Feiner, and J. Hughs, Computer Graphics, Principles and Practices, 2d ed.

Hearn and M. Many websites for OpenGL are available. Sowizral, K. Rushforth, and M. JFrame A Swing class for the main window of an application. Frame An AWT class for the main window of an application. JPanel A Swing component that can be used as a container or a base class for a custom drawing canvas.

Graphics A class for all graphics drawing facilities in AWT. MouseListener An interface for listening and handling mouse events. A standardized software interface to specify the usage of functionalities provided by a software package. A standard graphics API. A graphics API. It has a programming interface typically associated with the C language.

Swing A new enhanced Java graphics package. OOP Object-oriented programming. A software engineering paradigm that views a program as a system of interrelated objects. Modeling and rendering are the two major topics of computer graphics. Usually 2D and 3D graphics systems have different structures and they may be implemented as separate packages.

This is the case in the Java platform. Java 2D and Java 3D are separate packages with different programming models. To reduce platform dependency and to achieve high levels of abstraction, many graphics APIs have been developed. The Java 2D and Java 3D packages are high-level, full-featured graphics systems. This book will use Java 2D and Java 3D as the main tools to introduce graphics programming.

But it maintains close ties to these fields, which also deal with visual images. Name a nongame application that uses 3D computer graphics. Locate small bright spots in a mammogram image. Construct a 3D model of a building from a set of its pictures. Display a simulation of the solar system with the sun and nine planets in motion. Recognize the brain region in a MRI scan and display a 3D model of the brain. Use computers to generate the scene of a car collision. Make a computer identification of a person from a photograph.

List the advantages of each API. Discuss the advantages and disadvantages of including graphics support in a standard language platform such as Java. Write a Java AWT program that draws a circle in the middle of the window. Write a Java Swing program that draws a circle in the middle of the window.

Write a Java GUI program that responds to a mouse click by drawing a filled circle at the mouse location. Edit, compile, and run the Java 2D program in Listing 1. Edit, compile, and run the Java 3D program in Listing 1. A 2D graphics system models the virtual world with a two-dimensional space. Compared to 3D graphics, 2D graphics is simpler in both modeling and rendering. The 2D rendering usually does not involve any complicated projections such as those in 3D graphics. Even though a 2D model cannot completely capture the full nature of a 3D space, 2D computer graphics is widely applied because of its simplicity and efficiency.

It is an essential ingredient of modern GUI-based programs. The key concepts related to 2D graphics include the rendering pipeline, the object space, the world space, the device space, coordinate systems, graphics primitives, geometric transformations, colors, clipping, composition rules, and other topics. Java 2D provides comprehensive support for 2D graphics.

This chapter covers the basic structures of Java 2D programs and the geometric-object models. Additional topics for 2D graphics and Java 2D programming will be discussed in the next two chapters. The rendering involves composing various objects through some relatively straightforward transformations.

Often the 2D world space is not even needed in order to explicitly model the relationships among the graphics objects. However, to achieve the clarity of system structures and to keep the analogy with 3D graphics, the notion of a virtual world space will be retained. Conceptually, a graphics object can be defined in its own object space and then placed in the 2D world space by an object transformation.

A 2D rendering takes a snapshot of the world and produces an image representing a particular view in a device space. Figure 2. The essential components of a 2D graphics system include the 2D object model to be rendered, the geometric transformations applied to the objects, and the rendering engine that creates a particular view of the virtual world on a display device. The basic steps for rendering graphics in a simple 2D graphics program can be outlined as follows: 1.

Construct the 2D objects. Apply transformations to the objects. Apply color and other rendering properties. Render the scene on a graphics device. The graphics objects in the model are two dimensional. Besides geometric objects constructed from basic primitives such as lines, polygons, and ellipses, the model may also include objects such as texts and images. The transformations involved in 2D graphics are usually affine transforms. The object transformations change the shapes and locations of the visual objects to which the transforms are applied.

The viewing transformations do not change the virtual world model, but they change the views of the entire scene on the world space. For example, in a virtual model with a circle and a triangle, a translation applied to the circle as an object transformation will move 2. A translation as a viewing transformation, on the other hand, will move the entire view.

In addition to the geometry, many other attributes will affect the rendering of a scene. Colors, transparency, textures, and line styles are examples of such attributes. A 2D graphics system will render a scene based on the geometry information, transformation, and a graphics context involving other attributes. In order to represent geometry precisely and efficiently in computers, coordinate systems are employed.

The most commonly used 2D coordinate system employs rectangular Cartesian coordinates, as illustrated in Figure 2. Two perpendicular axes are placed in the plane. Each axis is labeled with the set of real numbers. The horizontal axis is customarily called the x-axis and the vertical axis the y-axis. The intersection of the axes is identified with the number 0 on both axes and is called the origin.

Each point on the plane is associated with a pair of real numbers x, y known as the x-coordinate and the y-coordinate. The coordinates measure the horizontal and vertical position of the point relative to the axes. A 2D geometric object is a set of points in the plane. The number of points in the set that constitutes the geometric object is usually infinite. To effectively represent such an object, an equation is used to define the relation that the x- and y-coordinates of a point in the object must satisfy.

For example, a line see Figure 2. Instead of an equation relating x and y, a third variable, t, is introduced. Both x and y are expressed as functions of t. The ellipse shown in Figure 2. The collection of all points or coordinates is also known as a space. Three types of spaces are typically involved in a graphics system: object space, world space, and device space. Each space is usually characterized by its own coordinate system. Geometric objects in one space can be mapped to another by transformations.

An object coordinate system, also known as local or modeling coordinate system, is associated with the definition of a particular graphics object or primitive. In constructing such an object, it is usually convenient to choose a coordinate system that is natural to the object without concerning its final destination and appearance in the world space.

For example, when we define a circle primitive, we may choose to have the origin of the coordinate system at the center of the circle and simply define a unit circle a circle of radius 1. The circle can later be placed anywhere in the world space through a transformation called translation.

Its radius can be changed to any value by scaling. It can even be transformed to an ellipse by using a nonuniform scaling.

The world coordinate system, or user coordinate system, defines a common reference space for all the objects in a graphics model. It represents the virtual world shared by the modeling and rendering subsystems. Geometric objects are placed into this space through object transforms. The rendering system takes a snapshot of the space and produces a rendered image on an output device.

The origin is located at the upper left corner, and the positive direction of the y-axis is pointing downward. The coordinate values are usually integers only. This choice is obviously different from the usual mathematical representation, but it is more natural for most computer display devices.

With the available transformation facilities in a graphics system, it is easy to define a different world space that may be more appropriate for a particular application.

In early versions of Java, graphics drawings are obtained through the class java. The Graphics class contains basic methods for rendering graphics primitives and controlling the rendering modes. Java 2D uses the more extensive Graphics2D class for its graphics rendering. Graphics and Graphics2D classes are abstract, because their implementations will necessarily be platform dependent. Consequently, you cannot directly instantiate the Graphics2D class.

There are two ways to retrieve a Graphics2D object. It can be obtained as the parameter in the paintComponent method or by calling the getGraphics method. The standard approach to draw graphics in a JComponent object is to override the paintComponent method: void paintComponent Graphics g The parameter g is declared to be Graphics, but it is also a Graphics2D object.

It can be cast to a Graphics2D object. The paintComponent method is called automatically by Java Virtual Machine whenever the display needs to be redrawn, such as when the window is restored after it is minimized. Therefore the drawing made in the method will appear to be persistent. The custom code in the paintComponent method may be regarded implicitly as a model for the graphics system. Another way to obtain a reference to a Graphics2D object is to call the method getGraphics in the class Component.

However, usually the drawings obtained this way will not be persistent. In AWT, the Graphics class provides the methods to control all aspects of rendering. It contains methods to set colors and fonts, to translate the coordinates, to set XOR mode, and to draw primitives such as lines and ovals directly. Some of the methods in Graphics are listed below.

Graphics methods void setColor Color c void setFont Font f void setXORMode Color c void setPaintMode void translate int x, int y void drawLine int x1, int y1, int x2, int y2 void drawRect int x1, int y1, int width, int height void drawOval int x1, int y1, int width, int height void drawArc int x1, int y1, int width, int height, int start, int arc void drawRoundRect int x1, int y1, int width, int height, int arcW, int arcH void drawPolygon int[] xPoints, int[] yPoints, int nPoints void fillRect int x1, int y1, int width, int height void fillOval int x1, int y1, int width, int height void fillArc int x1, int y1, int width, int height, int start, int arc void fillRoundRect int x1, int y1, int width, int height, int arcW, int arcH void fillPolygon int[] xPoints, int[] yPoints, int nPoints void drawString String str, int x, int y There is no clear separation between the modeler and the renderer in AWT.

For example, a single method drawOval is responsible for both defining modeling an ellipse and drawing rendering the ellipse. Java 2D, on the other hand, is required to handle much more sophisticated graphics objects. Therefore, instead of bundling all rendering functions into a single class, the modeling and transformation features are implemented with additional classes separated from the Graphics2D class.

Graphics2D provides some generic methods such as draw Shape and fill Shape to render separately defined objects. The objects to be drawn are implemented as Shape objects. Similarly, the transformations can be constructed with the AffineTransform class.

Graphics2D provides the method setTransform AffineTransform to set the current transformation to the separately defined transformation object. A partial list of Graphics2D methods is given below.

For example, to draw an ellipse you will create an instance of Ellipse2D which implements the Shape interface and call the draw method to render it. By overriding the paintComponent method, custom painting is achieved. The associated Graphics2D object can be configured for proper settings such as colors, paints, strokes, and transformations. A graphical object is constructed as an instance of a class implementing the Shape interface.

It can be rendered through the Graphics2D object. Listing 2. It illustrates the basic structure of a Java 2D program and the use of the Graphics2D class. The program displays a transformed circle and a text string with a blue color, as shown in Figure 2. The program is written to run as both an applet and an application. The Hello2D class extends JApplet. A main method is also included that creates a JFrame window and adds an instance of Hello2D to the window.

The applet contains an instance of the Hello2DPanel. The main graphics functions are carried out inside the paintComponent method line The paintComponent method of the superclass is invoked first to handle the necessary cleanup operations. The parameter g is declared as Graphics, but it is actually a Graphics2D object in all versions of Java that include Java 2D.

The drawing color is then set to blue by calling the method setColor in the Graphics object. More sophisticated drawing attributes that exist in the Graphics2D class will be introduced later in this chapter. An ellipse is created using the Ellipse2D. Double class line The rotation is an object transformation applied only to the ellipse. The transformed shape is obtained by calling the createTransformedShape method of the AffineTransform object.

The viewing transformation consists of a translation by , and a scaling of factor 2. The transformation is achieved by directly calling the methods translate and scale in the Graphics2D object. Using the draw method the rotated ellipse is drawn to the screen line Conversely, graphical plots offer a useful tool for studying mathematical functions and equations.

Plotting an equation is a simple graphics application. A simple way to graph an equation is to generate a sequence of coordinates satisfying the equation and then plot the points. An equation of the 2. Certain equations can be expressed in the parametric form that is convenient for calculations. Its accessible approach and in- depth coverage features the high-level Java 2D and Java 3D APIs-offering an elegant and easy-to-understand presentation of 2D and 3D graphics without compromising the fundamentals of the subject.

Course Content Details: Chapter 1. Overview of Computer Graphics 1. Basic 3D Graphics 5. Graphics Contents 6. Geometric Transformation 7. Views 8. Lighting and Texturing 9. Behavior and Interaction Animation He holds a Ph. Liang holds a Ph. We're sorry! We don't recognize your username or password. Please try again. The work is protected by local and international copyright laws and is provided solely for the use of instructors in teaching their courses and assessing student learning.

You have successfully signed out and will be required to sign back in should you need to download more resources. Hong Zhang Y. Daniel Liang, Georgia Southern University. Clear, concise explanations of key concepts —Accompanied by complete examples. Enables students to comprehend abstract concepts with concrete examples. A collection of mathematical topics —Useful in graphics with easy-to-follow explanations.

Implementation of features not yet available ion Java —Such as 3D curves and surfaces. Table of Contents 1. Share a link to All Resources.



0コメント

  • 1000 / 1000