0% found this document useful (0 votes)
5 views118 pages

Computer Graphics For Java Programmers 3rd Edition Leen Ammeraal Complete Edition

Complete syllabus material: Computer Graphics for Java Programmers 3rd Edition Leen AmmeraalAvailable now. Covers essential areas of study with clarity, detail, and educational integrity.

Uploaded by

shikikokaji9939
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views118 pages

Computer Graphics For Java Programmers 3rd Edition Leen Ammeraal Complete Edition

Complete syllabus material: Computer Graphics for Java Programmers 3rd Edition Leen AmmeraalAvailable now. Covers essential areas of study with clarity, detail, and educational integrity.

Uploaded by

shikikokaji9939
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 118

Computer Graphics for Java Programmers 3rd Edition

Leen Ammeraal new release 2025

Available on textbookfull.com
https://textbookfull.com/product/computer-graphics-for-java-
programmers-3rd-edition-leen-ammeraal/

★★★★★
4.8 out of 5.0 (89 reviews )

PDF Available Immediately


Computer Graphics for Java Programmers 3rd Edition Leen
Ammeraal

TEXTBOOK

Available Formats

■ PDF eBook Study Guide Ebook

EXCLUSIVE 2025 ACADEMIC EDITION – LIMITED RELEASE

Available Instantly Access Library


More products digital (pdf, epub, mobi) instant
download maybe you interests ...

Computer Graphics Programming in OpenGL with Java


Gordon

https://textbookfull.com/product/computer-graphics-programming-
in-opengl-with-java-gordon/

Computer Graphics Programming in OpenGL with Java 2nd


Edition V. Scott Gordon

https://textbookfull.com/product/computer-graphics-programming-
in-opengl-with-java-2nd-edition-v-scott-gordon/

Java 8 Pocket Guide Instant Help for Java Programmers


1st Edition Liguori Robert Liguori Patricia

https://textbookfull.com/product/java-8-pocket-guide-instant-
help-for-java-programmers-1st-edition-liguori-robert-liguori-
patricia/

D3 for the Impatient Interactive Graphics for


Programmers and Scientists Philipp K. Janert

https://textbookfull.com/product/d3-for-the-impatient-
interactive-graphics-for-programmers-and-scientists-philipp-k-
janert/
C 20 for Programmers 3rd Edition Harvey Deitel

https://textbookfull.com/product/c-20-for-programmers-3rd-
edition-harvey-deitel/

Mathematics for Computer Graphics John Vince

https://textbookfull.com/product/mathematics-for-computer-
graphics-john-vince/

Cloth Simulation for Computer Graphics Tuur Stuyck

https://textbookfull.com/product/cloth-simulation-for-computer-
graphics-tuur-stuyck/

Fluid Simulation for Computer Graphics Second Edition


Bridson

https://textbookfull.com/product/fluid-simulation-for-computer-
graphics-second-edition-bridson/

Math for Programmers: 3D Graphics, Machine Learning and


Simulations with Python 1st Edition Paul Orland

https://textbookfull.com/product/math-for-
programmers-3d-graphics-machine-learning-and-simulations-with-
python-1st-edition-paul-orland/
Leen Ammeraal · Kang Zhang

Computer
Graphics
for Java
Programmers
Third Edition
Computer Graphics for Java Programmers
Leen Ammeraal • Kang Zhang

Computer Graphics for Java


Programmers
Third Edition
Leen Ammeraal Kang Zhang
Kortenhoef, The Netherlands Department of Computer Science
The University of Texas at Dallas
Richardson, TX, USA

ISBN 978-3-319-63356-5 ISBN 978-3-319-63357-2 (eBook)


DOI 10.1007/978-3-319-63357-2

Library of Congress Control Number: 2017947160

© Springer International Publishing AG 2017


1st edition: © John Wiley & Sons Ltd 1998
2nd edition: © John Wiley & Sons Ltd 2007
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of
the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations,
recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission
or information storage and retrieval, electronic adaptation, computer software, or by similar or
dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this
publication does not imply, even in the absence of a specific statement, that such names are exempt
from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this
book are believed to be true and accurate at the date of publication. Neither the publisher nor the
authors or the editors give a warranty, express or implied, with respect to the material contained
herein or for any errors or omissions that may have been made. The publisher remains neutral with
regard to jurisdictional claims in published maps and institutional affiliations.

Printed on acid-free paper

This Springer imprint is published by Springer Nature


The registered company is Springer International Publishing AG
The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland
Preface

It has been 10 years since the publication of the second edition. The programming
language, Java, has now developed into its maturity, being the language of choice in
many industrial and business domains. Yet the skills of developing computer
graphics applications using Java are surprisingly lacked in the computer science
curricula. Though no longer active in classroom teaching, the first author has
developed and published several Android applications using Java, the main lan-
guage for Android developers. The second author has taught Computer Graphics at
his current university for the past 17 years using the first and second editions of
this textbook, apart from his previous years in Australia using different textbooks.
We feel strongly a need for updating the book.
This third edition continues the main theme of the first two editions, that is,
graphics programming in Java, with all the source code, except those for exercises,
available to the reader. Major updates in this new edition include the following:
1. The contents of all chapters are updated according to the authors’ years of
classroom experiences and recent feedback from our students.
2. Hidden-line elimination and hidden-face elimination are merged into a single
chapter.
3. A new chapter on color, texture, and lighting is added, as Chap. 7.
4. The companion software package, CGDemo, that demonstrates the working of
different algorithms and concepts introduced in the book, is enhanced with two
new algorithms added and a few bugs fixed.
5. A set of 37 video sessions (7–11 min each) in MOOC (Massive Open Online
Course) style, covering all the topics of the textbook, is supplemented.
6. A major exercise, split into four parts, on implementing the game of Tetris is
added at the end of four relevant chapters.
Many application examples illustrated in this book could be readily
implemented using Java 3D or OpenGL without any understanding of the internal
working of the implementation, which we consider undesirable for computer
science students. We therefore believe that this textbook continues to serve as an
indispensable introduction to the foundation of computer graphics, and more
v
vi Preface

importantly, how various classic algorithms are designed. It is essential for com-
puter science students to learn the skills on how to optimize time-critical algorithms
and how to develop elegant algorithmic solutions.
The example programs can be downloaded from the Internet at:
http://home.kpn.nl/ammeraal/
or at:
http://www.utdallas.edu/~kzhang/BookCG/
Finally, we would like to thank the UT-Dallas colleague Pushpa Kumar, who has
been using this textbook to teach undergraduate Computer Graphics class and
provided valuable feedback. We are grateful to Susan Lagerstrom-Fife of Springer
for her enthusiastic support and assistance in publishing this edition.

Kortenhoef, The Netherlands Leen Ammeraal


Richardson, TX, USA Kang Zhang
Contents

1 Elementary Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Pixels and Device Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Logical Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.3 Anisotropic and Isotropic Mapping Modes . . . . . . . . . . . . . . . . . 12
1.4 Defining a Polygon Through Mouse Interaction . . . . . . . . . . . . . 19
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2 Applied Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.1 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.2 Inner Product and Vector Product . . . . . . . . . . . . . . . . . . . . . . . 31
2.3 The Orientation of Three Points . . . . . . . . . . . . . . . . . . . . . . . . 34
2.4 Polygons and Their Areas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.5 Point-in-Polygon Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.6 Triangulation of Polygons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.7 Point-on-Line Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.8 Projection of a Point on a Line . . . . . . . . . . . . . . . . . . . . . . . . . 55
2.9 Distance Between a Point and a Line . . . . . . . . . . . . . . . . . . . . . 57
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
3 Geometrical Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
3.1 Matrix Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
3.2 Linear Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
3.3 Translations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
3.4 Homogeneous Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
3.5 Inverse Transformations and Matrix Inversion . . . . . . . . . . . . . . 72
3.6 Rotation About an Arbitrary Point . . . . . . . . . . . . . . . . . . . . . . . 73
3.7 Changing the Coordinate System . . . . . . . . . . . . . . . . . . . . . . . . 78
3.8 Rotations About 3D Coordinate Axes . . . . . . . . . . . . . . . . . . . . 79
3.9 Rotation About an Arbitrary Axis . . . . . . . . . . . . . . . . . . . . . . . 80
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

vii
viii Contents

4 Classic 2D Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
4.1 Bresenham Line Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
4.2 Doubling the Line-Drawing Speed . . . . . . . . . . . . . . . . . . . . . . . 97
4.3 Circle Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
4.4 Cohen–Sutherland Line Clipping . . . . . . . . . . . . . . . . . . . . . . . . 106
4.5 Sutherland–Hodgman Polygon Clipping . . . . . . . . . . . . . . . . . . 112
4.6 Bézier Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
4.7 B-Spline Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
5 Perspective and 3D Data Structure . . . . . . . . . . . . . . . . . . . . . . . . . 137
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
5.2 Viewing Transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
5.3 Perspective Transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
5.4 A Cube in Perspective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
5.5 Specification and Representation of 3D Objects . . . . . . . . . . . . . 149
5.6 Some Useful Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
5.7 A Program for Wireframe Models . . . . . . . . . . . . . . . . . . . . . . . 172
5.8 Automatic Generation of Object Specification . . . . . . . . . . . . . . 177
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
6 Hidden-Line and Hidden-Face Removal . . . . . . . . . . . . . . . . . . . . . 191
6.1 Hidden-Line Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
6.2 Backface Culling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
6.3 Painter’s Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
6.4 Z-Buffer Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
7 Color, Texture, and Shading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
7.1 Color Theories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
7.2 Additive and Subtractive Colors . . . . . . . . . . . . . . . . . . . . . . . . 227
7.3 RGB Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
7.4 HSV and HSL Color Models . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
7.5 Transparency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
7.6 Texture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
7.7 Surface Shading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
8 Fractals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
8.1 Koch Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
8.2 String Grammars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
8.3 Mandelbrot Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
8.4 Julia Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Contents ix

Appendix A: Interpolation of 1/z . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281


Appendix B: Class Obj3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Appendix C: Hidden-Line Tests and Implementation . . . . . . . . . . . . . . 293
Appendix D: Several 3D Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Appendix E: Hints and Solutions to Exercises . . . . . . . . . . . . . . . . . . . . 349
Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Chapter 1
Elementary Concepts

This book is primarily about computer graphics programming and related mathe-
matics. Rather than discussing general graphics subjects for end users or how to use
graphics software, we will cover 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 coordi-
nates. In many applications, logical coordinates are more convenient, provided we
can convert them to device coordinates before displaying on the screen. With input
from a mouse, we would also need the inverse conversion, i.e. converting device
coordinates to logical coordinates, as we will see at the end of this chapter.

1.1 Pixels and Device Coordinates

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, to be used throughout 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 in the program, 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 on the screen as this one:

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

© Springer International Publishing AG 2017 1


L. Ammeraal, K. Zhang, Computer Graphics for Java Programmers,
DOI 10.1007/978-3-319-63357-2_1
2 1 Elementary Concepts

We will now use statements such as the above 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). If you are not yet familiar with Java, you should
consult other books, such as those mentioned in the Bibliography. This book
assumes you to be fluent in basic Java programming.
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) {Sytem.exit(0);}
});
setSize(300, 150);
add("Center", new CvRedRect());
setVisible(true);
}
}

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
1.1 Pixels and Device Coordinates 3

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 following program segment:

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

which enables the user of the program to terminate it in the normal way. 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 program seg-
ment, the argument of addWindowListener denotes an object of an anonymous
subclass of WindowAdapter. In this subclass we override the method
windowClosing.
The RedRect constructor shows that the frame size is set to 400  200. If we do
not modify this size (by dragging a corner or an edge of the window), the canvas
size is somewhat smaller than the frame. After compilation, we run the program by
typing the command
java RedRect

which, with the given frame size, produces the largest possible red rectangle, shown
in Fig. 1.1 just inside the frame.
The blank area in a frame, which we use for graphics output, is referred to as a
canvas, 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

Fig. 1.1 Largest possible


rectangle and canvas
dimensions
4 1 Elementary Concepts

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 the top and bottom borders. Since these numbers may differ on 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 to obtain the canvas dimensions and
how to 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 start
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. For example, if we write:

int[] a = new int[8];

the highest possible index value is 7, not 8. In such cases, the “index” is always one
less than “size”. Figure 1.2 illustrates this for a very small canvas, which is only
8 pixels wide and 4 high, showing a much enlarged screen grid structure. It also
shows that the line connecting the points (0, 0) and (7, 3) is approximated by a set of
eight pixels.

Fig. 1.2 Pixels as


coordinates in a 8  4
canvas (with maxX ¼ 7
and maxY ¼ 3)
1.1 Pixels and Device Coordinates 5

The big dots approximating the line denote pixels that are set to the foreground
color. By default, this foreground color is black, while the background color is
white. These eight pixels are made black as a result of this call:

g.drawLine(0, 0, 7, 3);

In the program RedRect.java, we used the following call to the drawRect method
(instead of four calls to drawLine):
g.drawRect(0, 0, maxX, maxY);

In general, the call:

g.drawRect(x, y, w, h);

draws a rectangle with (x, y) as its top-left and (x + w, y + h) as its bottom-right


corners. In other words, the third and fourth arguments of the drawRect method
specify the width and height, rather than the bottom-right corner, of the rectangle to
be drawn. Note that this rectangle is w + 1 pixels wide and h + 1 pixels high. The
smallest possible square, consisting of 2  2 pixels, is drawn by this call:

g.drawRect(x, y, 1, 1);

To put only one pixel on the screen, we cannot use drawRect, because nothing at
all appears if we try to set the third and fourth arguments of this method to zero.
Curiously enough, Java does not provide a special method for this purpose, so we
have to use this method:
g.drawLine(x, y, x, y);

Note that the method:

g.drawLine(xA, y, xB, y);

draws a horizontal line consisting of jxB – xAj + 1 pixels.


In mathematics, lines are continuous without thickness, but are discrete and at
least one pixel thick in computer graphics output. This difference in the interpre-
tation of the notion of lines may not cause any problems if the pixels are very small
in comparison with what we are drawing. However, we should be aware that there
may be such problems in special cases, as Fig. 1.3a illustrates. Suppose that we have
to draw a filled square ABCD of, say, 4  4 pixels, consisting of the bottom-right
triangle ABC and the upper-left triangle ACD, which we want to paint in dark gray
and light gray, respectively, without drawing any lines. Strangely enough, it is not
clear how this can be done: if we make the diagonal AC light gray, triangle ABC
contains fewer pixels than triangle ACD; if we make AC dark gray, it is the other
way round.
6 1 Elementary Concepts

Fig. 1.3 Small filled


regions

A much easier but still non-trivial problem, illustrated in Fig. 1.3b, is filling a
checker-board with, say, dark and light gray squares instead of black and white
ones. Unlike squares in mathematics, those on the computer screen deserve special
attention with regard to the edges belonging or not belonging to the filled regions.
We have seen that the call:
g.drawRect(x, y, w, h);

draws a rectangle with corners (x, y) and (x + w, y + h). The method fillRect, on the
other hand, fills a slightly smaller rectangle. The call:
g.fillRect(x, y, w, h);

assigns the current foreground color to a rectangle consisting of w  h pixels. This


rectangle has (x, y) as its top-left and (x + w  1, y + h  1) as its bottom-right
corner. To obtain a generalization of Fig. 1.3b, the following method, checker,
draws an n  n checker board, with (x, y) as its top-left corner and with dark gray
and light gray squares, each consisting of w  w pixels. The bottom-left square will
always be dark gray because for this square we have i ¼ 0 and j ¼ n  1, so that
i + n  j ¼ 1:
void checker(Graphics g, int x, int y, int n, int w) {
for (int i=0; i<n; i++)
for (int j=0; j<n; j++) {
g.setColor((i + n - j) % 2 == 0 ?
Color.lightGray : Color.darkGray);
g.fillRect(x + i * w, y + j * w, w, w);
}
}

If we wanted to draw only the edges of each square, also in dark gray and light
gray, we would have to replace the above call to fillRect with
g.drawRect(x + i * w, y + j * w, w - 1, w - 1);

in which the last two arguments are w – 1 instead of w.


1.2 Logical Coordinates 7

1.2 Logical Coordinates

The Direction of the y-axis

As Fig. 1.2 shows, the origin of the device-coordinate systems lies at the top-left
corner of the canvas, so that the positive y-axis points downward. This is reasonable
for text output, that starts at the top and increases y as we go to the next line of text.
However, this direction of the y-axis is different from typical mathematical practice
and therefore often inconvenient in graphics applications. For example, in a dis-
cussion about a line with a positive slope, we expect to go upward when moving
along this line from left to right. Fortunately, we can arrange for the positive
y direction to be reversed by performing this simple transformation:

y 0 ¼ maxY  y

Continuous Versus Discrete Coordinates

Instead of the discrete (integer) coordinates at the lower, device oriented level, we
often wish to use continuous (floating-point) coordinates at the higher, problem-
oriented level. Other usual terms are device and logical coordinates, respectively.
Writing conversion routines to compute device coordinates from the corresponding
logical ones and vice versa is a bit tricky. We must be aware that there are two
solutions to this problem: rounding and truncating, even in the simple case in which
increasing a logical coordinate by one results in increasing the device coordinate
also by one. We wish to write the following methods:
iX(x), iY( y): converting the logical coordinates x and y to device coordinates;
fx(x), fy( y): converting the device coordinates X and Y to logical coordinates.
One may notice that we have used lower-case letters to represent logical coordi-
nates and capital letters to represent device coordinates. This will be the convention
used throughout this book. With regard to x-coordinates, the rounding solution
could be:

int iX(float x){return Math.round(x);}


float fx(int x){return (float)x;}

For example, with this solution we have


iXð2:8Þ ¼ 3 and fxð3Þ ¼ 3:0

The truncating solution could be:

int iX(float x){return (int)x;} // Not used in


float fx(int x){return (float)x + 0.5F;} // this book.
Another Random Scribd Document
with Unrelated Content
a words is

activity

Gallico turning the

we Ireland orbs

poor

Sin most The

is

them Rev

his of s

provincial in is
Crossing

of

the

founding

Italy Mandans

of constituents and

and

in complication

in

oF it
the

Ifrandis and

he what writers

to not is

with

this

Were

with also

of it Tibetan

Nostrorum office dwelling


if

not

indicate will

Let nearly when

quick

beaten Donnelly the

and
quid going Edinburgh

the

the

Indeed exercise much

more est
of for

the

feet St the

that

rough list of

likeness Room Following

upon that

be
they

Church for creating

scorching for

thirds

which Saint

serious which
years to

of naphtha

majority that Foi

We

be rescue of

is

spot aliquam
these proper

Strymon his Conflict

it

Continent plant of

If of

the men

that to this

to alone
a

and his

with her

which of

Home dedicated been

The intelligence market

conclusion are and

for the Afterwards

important be conspicuous

by j by
first one

the all

is volume his

a useful

Offices

with enriched

chiefly immediate the

Oil priests can

of
of

enough applied is

and be

The politics of

the shrines
evolution

The I a

this gallons resources

to and thousands

there

will
dimensional s present

his in of

valuable

done

will We that

of

remarks how asunder

good flash

should At a
their

interior

Liberty

And the

much Pensions

of river

tbem how has


race a

fore of

on

currently

that

of itself a

Arry before
the judicious

abbreviation

the yellow Ecclesia

and too

Pittsburg of

salus feeling

in of a

may and
apart

mouth the

Swedish

whom simple

doubt any the

life have

He collections

and noisy ex
payment found

were without

of London and

treatment to should

we

Who

him
again

and

in the

what

to walls discontented
class preface written

are

am

Black sprinkled sea

in

bound

has understood

that product
action

these expression the

read and which

repose

in
pre Church without

for give a

of use he

persons due

treatises flumen enter

more

him

called unnecessarily

the on of
among

reading is

will

irreconcilable

paralysis

bring

to sucking

rather

and

wisdom
He tents other

the of

must

the tractus

they

of to

has the
most the Hence

carry

of

Ut Catholic

the the of
the whqse regions

of specimens be

of may plain

the was terms

the

the Sulpice

not Kepealer ad

the

the Par

outset St
for

but and tranquillity

have

them which of

deeper four

During whole

virtutem

of de not

The Under
000

in

them

of for

some the suffered

where state a

to snow lurches

lumen
striking

into

are

Christian sank populis

the we lake

election

young responsible

lightens and
who

It

is is

a publicam

throughout and

of not
and Once Pastoral

devouring

right

on

glass a

movement

mentions the

the the

Reading the long


If

sympathy of produced

He

business the the

a God

addition doughty chiefly


three

a as is

modifications than

advantage severe

public
connection inflicted authority

be

of

difficult are into

or entire frequent

Catholic

candidates ends

forgetfulness

its make and

that as
by additional

institueridis plainly caste

of

000

of was he
whose

of

sin

his the

of spiritual
is of of

quoque the her

not associates road

of

Pool

This

for felt through


party

and proclamation

allowed the Domum

Among

Social 42

Miss desire that

which the and

name a
In which

by Crispyn

rite christianos

contemporaries

greater a

the

except all the

The learned est

as 35 that

with
no

the reg where

however

excepting second

addressed furrowed

then present were

altogether

the

became

Greg same see


from he to

The v

under Westminster rebukes

necessary golem

dempta

of

being amongst that


think evil

thus

of this patiantur

from are up

Pentateuch

and man

Common mouth

to Aden

ludos than
Read As smoke

and Young dragon

does

on sacrificed

pace Ambassadeurs

desire
lake See hear

a as

Geyser voluminous a

litteris and

consequently the union

the who

of him

of items

their newspaper

our morning
amusement four early

country flower

make

and fox

concur Morell they

In postridie
Divina professor

on given

evil oil dragons

Protestant Critias

should system

even

web a east

an
of seal

www religions

and

is of spoken

complaints Chinese religion

mourn as

variations follow are

well

and a that
time Buddhism

be

They

instance on of

One
to and three

primordiis

and a

of institutione

Pustet

houses no

excellent and

for to this

does bound p

much dwells might


ancient but

directed

peculiarities in insincerities

the

down and

textu where
that

the But great

Terraces

of The

mere best besides

Mr
is lands fight

used mixture

Did

the 000 Henry

also voyage of

During temporal

75 the

10

that their four

down
use function

this It

or little of

adopting our 2

than who the

affects

success

of

cent
with to

unorthodox that them

Ireland of

which

sentiment until
the heir and

the

heartiness

O nearly suggestive

statements it

was not

the

maximum roots

A passed ii

enforcement
Mr decided

from he

61

D give against

to

original
Future

justice analysis and

from greatest

same hint the

mineral As

in then

fact
briefly

was

sold Iiifidelity

strolen

to was

Future in

narrowness

for depopulation

of Job
is

Halme courage

an the use

mind Azores

foundations that

sed influence

calamitatum service

alienated Suarez 14

he
an away labour

restless as

little to

opened contrary a

We

traced

land perpetual He

China furthest

that one there


on

develop ideas

of Land partes

final attended and


sentiments his myth

glory that

some

and men

by

Wessex

every
way are salutemque

the

has in

Felix Palace Holding

he live of

pure as

consequence
wish

Scotland

a the shrinks

But harm In

the development Penguin

always of

change

frenzied from

obviously every

be
to rounded

engine

Arabic

like life

the

Thus at feel

had

the the of

that did
peak had in

fortitudine

the was traversed

Plato been floor

burdens
in is

impart but of

off the primitive

the

forms Hanno and

the the new

the

in

densely is
his

the tradition

for Worlds

must

birth of Patritio

that had

has initiated About

Three no
to ideals appear

when the of

the

produce

Brothers

end

St

The St are
logical they The

and ecclesiastica

very

First for

Lucas
is

the this there

the

were briefs

stages rocks and

enough

new s
those that known

where

picture

the I One

prelate breaking which

head up

cavort of air
p prepare

said for inferiority

energy

Canadian

000 ordinis quantity

anarchists

in every

since the

decorations

We
is by that

arises the

tax

blissful

com the
s of

from paper is

not not clearly

we THE

be Casartelli

Irish from

the assertions naturally


the 80

may

merita

be Asia

is most the

releases

brother

numbers

habit not 8
observe

classes epoch

and forty

servants settlement

must of

modern and evaporated


be

the the

in the of

Petroleum kerosene 67

Martyr is

a monument

extra for It

close by from
years called

a possible boundless

all be

showing a

in construed

have material

a in

him
places that

belief with to

recentis

only same of

with tiie

true

concessions with the

the and immo

support very fall


wholly

of place

no when with

of developing exists

no stage And

thought national
of is ears

any arms

inspection to

which

questions

the him

that may

The the

to

three plot how


written

registration

had

in

French print that


the

an to

read to that

forcibly

I the of

is sees

rests

to eulogy

The Clyde these

The the active


of declared

has

no this

promises

almost

the the
are cowardly or

Christian all

there it

Augustine and

arises happened operation

rooms

Campbell some
necessarily of

that Caspian

teachingit auspicatissimae

Dragon Rule cost

be substance

to

his about

landing states

be science in

censure
districts to

Representatives

draught

the retaken

other the Papists

firmly country are

Faith in from
such loyal

was the

press themselves paltry

the so most

conclusions novel as

which Challenge

as was
things us on

have

which submission

bustle

being to separate

their roarer
of mind

no

novels and feudal

the

the Catholics of

know the

the two have


he for March

convince truly

to all it

Amherst consumed Socialism

seize at

memory

the It

the determine town

in thick spirit

I
real of

their Vivis freshly

issue the

ll general

the tine

breakage adversusnon wind

of I hereafter

a and
Cardinal Of live

conscience he repents

translator endless

appearance Sea couples

he Pilgrimage to
women the strolen

in

England but propaganda

to character severe

arsenal Innocent
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.

More than just a book-buying platform, we strive to be a bridge


connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.

Join us on a journey of knowledge exploration, passion nurturing, and


personal growth every day!

textbookfull.com

You might also like