Computer Graphics for Java Programmer Second Edition - Softcover

Ammeraal, Leen

 
9780470031605: Computer Graphics for Java Programmer Second Edition

Inhaltsangabe

The book covers elementary concepts, from how to produce simple graphical objects using logical coordinates to producing filled regions. The book reinforces concepts with useful and simple examples, then progresses to applied geometry (vectors, polygons) and then onto how to perform rotations and other transformations of graphical objects.

In a logical progression of ideas, the reader is introduced to some of the classic graphics algorithms and finally to chapters which cover particular effects such as perspective drawings and hidden-face and hidden-line elimination.

The book also provides a host of ready-to-run programs and worked examples to illuminate general principles and geometric techniques for the creation of both 2D and 3D graphical objects.

Die Inhaltsangabe kann sich auf eine andere Ausgabe dieses Titels beziehen.

Über die Autorin bzw. den Autor

Leen Ammeraal is a retired lecturer of Hogeschool Utrecht, The Netherlands, where he was employed from 1977 to 1998. He has a degree (ir.) in mathematics at University of Technology Delft, The Netherlands. He worked as a programmer and mathematician at Akzo Research and Engineering, Arnhem, The Netherlands, from 1961 to 1972 and did research work on compilers from 1972 to 1977 at Mathematical Centre, Amsterdam. He wrote many books for Wiley (as well as for the Dutch publisher Academic Service). Some of his Wiley books have been translated into other languages (Japanese, Russian, Italian, French, German, Greek, Danish, Portuguese, Bulgarian).

Kang Zhang is a Professor in Computer Science and Director of Visual Computing Lab at the University of Texas at Dallas. He received his B.Eng. in Computer Engineering from the University of Electronic Science and Technology, China, in 1982; and Ph.D. from the University of Brighton, UK, in 1990. He held academic positions in the UK and Australia, prior to joining UTD. Zhang's current research interests are in the areas of visual languages, graphical visualization, and Web engineering; and has published over 130 papers in these areas. He has taught computer graphics and related subjects at both graduate and undergraduate levels for many years. Zhang was also an editor of two books on software visualization.

Von der hinteren Coverseite

A great many varied and interesting visual effects can be achieved with computer graphics, for which a fundamental understanding of the underlying mathematical concepts – and a knowledge of how they can be implemented in a particular programming language – is essential.

Computer Graphics for Java Programmers, 2nd edition covers elementary concepts in creating and manipulating 2D and 3D graphical objects, covering topics from classic graphics algorithms to perspective drawings and hidden-line elimination.

Completely revised and updated throughout, the second edition of this highly popular textbook contains a host of ready-to-run-programs and worked examples, illuminating general principles and geometric techniques. Ideal for classroom use or self-study, it provides a perfect foundation for programming computer graphics using Java.

Aus dem Klappentext

A great many varied and interesting visual effects can be achieved with computer graphics, for which a fundamental understanding of the underlying mathematical concepts – and a knowledge of how they can be implemented in a particular programming language – is essential.

Computer Graphics for Java Programmers, 2nd edition covers elementary concepts in creating and manipulating 2D and 3D graphical objects, covering topics from classic graphics algorithms to perspective drawings and hidden-line elimination. 

Completely revised and updated throughout, the second edition of this highly popular textbook contains a host of ready-to-run-programs and worked examples, illuminating general principles and geometric techniques. Ideal for classroom use or self-study, it provides a perfect foundation for programming computer graphics using Java. 

Auszug. © Genehmigter Nachdruck. Alle Rechte vorbehalten.

Computer Graphics for Java Programmers

By Leen Ammeraal Kang Zhang

John Wiley & Sons

Copyright © 2007 John Wiley & Sons, Ltd
All right reserved.

ISBN: 978-0-470-03160-5

Chapter One

Elementary Concepts

This book is primarily about graphics programming and mathematics. Rather than discussing general graphics subjects for end users or how to use graphics software, we will deal with more fundamental subjects, required for graphics programming. In this chapter, we will first understand and appreciate the nature of discreteness of displayed graphics on computer screens. We will then see that x- and y-coordinates need not necessarily be pixel numbers, also known as device coordinates. In many applications logical coordinates are more convenient, provided we can convert them to device coordinates. Especially with input from a mouse, we also need the inverse conversion, as we will see at the end of this chapter.

1.1 LINES, COORDINATES AND PIXELS

The most convenient way of specifying a line segment on a computer screen is by providing the coordinates of its two endpoints. In mathematics, coordinates are real numbers, but primitive line-drawing routines may require these to be integers. This is the case, for example, in the Java language, which we will use in this book. The Java Abstract Windows Toolkit (AWT) provides the class Graphics containing the method drawLine, which we use as follows to draw the line segment connecting A and B:

g.drawLine(xA, yA, xB, yB);

The graphics context g in front of the method is normally supplied as a parameter of the paint method we are using, and the four arguments of drawLine are integers, ranging from zero to some maximum value. The above call to drawLine produces exactly the same line as this one:

g.drawLine(xB, yB, xA, yA);

We will now use statements such as the above one in a complete Java program. Fortunately, you need not type these programs yourself, since they are available from the Internet, as specified in the Preface. It will also be necessary to install the Java Development Kit (JDK), which you can also download, using the following Web page:

http://java.sun.com/

If you are not yet familiar with Java, you should consult other books, such as some mentioned in the Bibliography, besides this one.

The following program draws the largest possible rectangle in a canvas. The color red is used to distinguish this rectangle from the frame border:

// RedRect.java: The largest possible rectangle in red. import java.awt.*; import java.awt.event.*;

public class RedRect extends Frame { public static void main(String args){new RedRect();}

RedRect() { super("RedRect"); addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e){System.exit(0);}}); setSize (200, 100); add("Center", new CvRedRect()); show(); } }

class CvRedRect extends Canvas { public void paint(Graphics g) { Dimension d = getSize(); int maxX = d.width - 1, maxY = d.height - 1; g.drawString("d.width = " + d.width, 10, 30); g.drawString("d.height = " + d.height, 10, 60); g.setColor(Color.red); g.drawRect(0, 0, maxX, maxY); } }

The call to drawRect almost at the end of this program has the same effect as these four lines:

g.drawLine(0, 0, maxX, 0); // Top edge g.drawLine(maxX, 0, maxX, maxY); // Right edge g.drawLine(maxX, maxY, 0, maxY); // Bottom edge g.drawLine(0, maxY, 0, 0); // Left edge

The program contains two classes:

RedRect: The class for the frame, also used to close the application.

CvRedRect: The class for the canvas, in which we display graphics output.

However, after compiling the program by entering the command

javac RedRect.java

we notice that three class files have been generated: RedRect.class, CvRedRect.class and RedRect$1. class. The third one is referred to as an anonymous class since it has no name in the program. It is produced by the two program lines

addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e){System.exit(0);}});

which enable the user of the program to terminate it in the normal way. We could have written the same program code as

addWindowListener ( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } );

to show more clearly the structure of this fragment. The argument of the method addWindowListener must be an object of a class that implements the interface WindowListener. This implies that this class must define seven methods, one of which is windowClosing. The base class WindowAdapter defines these seven methods as do-nothing functions. In the above fragment, the argument of addWindowListener denotes an object of an anonymous subclass of WindowAdapter. In this subclass we override the method windowClosing. A further discussion of this compact program code for event handling can be found in Appendix B.

The RedRect constructor shows that the frame size is set to 200 100. If we do not modify this size (by dragging a corner or an edge of the window), the canvas size is somewhat less. After compilation, we run the program by typing the command

java RedRect

which produces the output shown in Figure 1.1.

The blank area in a frame, which we use for graphics output, is referred to as a client rectangle in Microsoft Windows programming. We will consistently use a canvas for it, which is a subclass, such as CvRedRect in program RedRect.java, of the AWT class Canvas. If, instead, we displayed the output directly in the frame, we would have a problem with the coordinate system: its origin would be in the top-left corner of the frame; in other words, the x-coordinates increase from left to right and y-coordinates from top to bottom. Although there is a method getInsets to obtain the widths of all four borders of a frame so that we could compute the dimensions of the client rectangle ourselves, we prefer to use a canvas.

The tiny screen elements that we can assign a color are called pixels (short for picture elements), and the integer x- and y-values used for them are referred to as device coordinates. Although there are 200 pixels on a horizontal line in the entire frame, only 192 of these lie on the canvas, the remaining 8 being used for the left and right borders. On a vertical line, there are 100 pixels for the whole frame, but only 73 for the canvas. Apparently, the remaining 27 pixels are used for the title bar and for the top and bottom borders. Since these numbers may differ in different Java implementations and the user can change the window size, it is desirable that our program can determine the canvas dimensions. We do this by using the getSize method of the class Component, which is a superclass of Canvas. The following program lines in the paint method show how we obtain the canvas dimensions and how we interpret them:

Dimension d = getSize(); int maxX = d.width - 1, maxY = d.height - 1;

The getSize method of Component (a superclass of Canvas) supplies us with the numbers of pixels on horizontal and vertical lines of the canvas. Since we begin counting at zero, the highest pixel numbers, maxX and maxY, on these lines are one less than these numbers of pixels. Remember that this is similar with arrays in Java and C....

„Über diesen Titel“ kann sich auf eine andere Ausgabe dieses Titels beziehen.

Weitere beliebte Ausgaben desselben Titels

9788126512898: Computer Graphics for Java Programmers, 2ed

Vorgestellte Ausgabe

ISBN 10:  812651289X ISBN 13:  9788126512898
Softcover