Computer Graphics for Java Programmers, Second Edition byLeen AmmeraalandKang Zhang John Wiley & Sons 2007 (386 pages) ISBN:9780470031605 Covering elementary concepts in creating and manipulating 2D and 3D graphical objects, this book presents topics from classic graphics algorithms to perspective drawings and hidden-line elimination Table of Contents Computer Graphics for Java Programmers, Second Edition Preface Chapter 1 - Elementary Concepts Chapter 2 - Applied Geometry Chapter 3 - Geometrical Transformations Chapter 4 - Some Classic Algorithms Chapter 5 - Perspective Chapter 6 - Hidden-Line Elimination Chapter 7 - Hidden-Face Elimination Chapter 8 - Fractals Appendix A - Linear Interpolation of 1/z Appendix B - A Note on Event Handling Appendix C - File Obj3D.java Appendix D - Class CvHLines.java Appendix E - Some Applications Appendix F - Hints and Solutions to Exercises Bibliography Colour Plates Index List of Figures List of Code Examples Back Cover 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 selfstudy, it provides a perfect foundation for programming computer graphics using Java About the Authors 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 Computer Graphics for Java Programmers, Second Edition Leen Ammeraal Hogeschool Utrecht Kang Zhang University of Texas at Dallas John Wiley & Sons, Ltd © 2007 John Wiley & Sons Ltd The Atrium, Southern Gate, Chichester, West Sussex PO19 8SQ, England Telephone (+44) 1243 779777 First published 1998 (0-471-98142-7) Email (for orders and customer service enquiries): cs-books@wiley.co.uk Visit our Home Page on http://www.wiley.com All Rights Reserved No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except under the terms of the Copyright, Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London W1T 4LP, UK, without the permission in writing of the Publisher Requests to the Publisher should be addressed to the Permissions Department, John Wiley & Sons Ltd, The Atrium, Southern Gate, Chichester, West Sussex PO19 8SQ, England, or emailed to permreq@wiley.co.uk, or faxed to (+44) 1243 770620 Designations used by companies to distinguish their products are often claimed as trademarks All brand names and product names used in this book are trade names, service marks, trademarks or registered trademarks of their respective owners The Publisher is not associated with any product or vendor mentioned in this book This publication is designed to provide accurate and authoritative information in regard to the subject matter covered It is sold on the understanding that the Publisher is not engaged in rendering professional services If professional advice or other expert assistance is required, the services of a competent professional should be sought Other Wiley Editorial Offices John Wiley & Sons Inc., 111 River Street, Hoboken, NJ 07030, USA Jossey-Bass, 989 Market Street, San Francisco, CA 94103-1741, USA Wiley-VCH Verlag GmbH, Boschstr 12, D-69469 Weinheim, Germany John Wiley & Sons Australia Ltd, 42 McDougall Street, Milton, Queensland 4064, Australia John Wiley & Sons (Asia) Pte Ltd, 2 Clementi Loop #02-01, Jin Xing Distripark, Singapore 129809 John Wiley & Sons Canada Ltd, 6045 Freemont Blvd, Mississauga, ONT, L5R 4J3, Canada Wiley also publishes its books in a variety of electronic formats Library of Congress Cataloging-in-Publication Data Ammeraal, L (Leendert) Computer graphics for Java programmers / Leen Ammeraal – 2nd ed p cm Includes bibliographical references ISBN-13 978-0-470-03160-5 (alk paper) 0-470-03160-3 Computer graphics Java (Computer program language) I Title T385.A488 2007 006.6 63 – dc22 2006029327 British Library Cataloguing in Publication Data A catalogue record for this book is available from the British Library 13: 978-0-470-03160-5 (PB) ISBN-10: 0-470-03160-3 (PB) Preface During the eight years since the publication of the first edition of Computer Graphics for Java Programmers, the programming language Java has increasingly become the language of choice in many industrial and business domains Hence the skills for developing computer graphics applications using Java have been highly in demand but are surprisingly lacking in the computer science curricula Meanwhile, for the past five years the second author has been teaching Computer Graphics at his current university using the first edition of this textbook, and felt that there was a need to update the book We therefore decided to jointly write this second edition This edition continues the main theme of the first edition, that is, graphics programming in Java, with plenty of source code available to the reader The new edition has, however, been updated as follows: The contents of some chapters have been updated, as a result of the authors' years of classroom experience and recent feedback from our students An instructor's manual has been created to include lecture slides and answers to exercise questions Chapter 8 has been replaced by a new chapter covering the topic of fractals A beta version of a companion software package has been added, which demonstrates the working of different algorithms and concepts introduced in the book More illustrative examples have been included in several chapters and various minor errors in the first edition have been corrected Over the past few years, Java has evolved into more powerful programming environments The most notable development related to computer graphics is its support for 3D graphics Many application examples illustrated in this book could be readily implemented using Java 3D 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, on which many application program interfaces (APIs) and graphics libraries could be developed, and more importantly, how they are developed As in the first edition, the example programs can again be downloaded from the Internet at: http://home.planet.nl/~ammeraal or at: http://www.utdallas.edu/~kzhang/BookCG/ In writing this second edition, several people need to be acknowledged for their direct or indirect contributions We would first like to thank the UT-Dallas graduate students Bill Fahle, Andy Restrepo, Janis Schubert, and Subramanya Suresh, who contributed to different parts of the demonstration software In particular, we appreciate the great effort of Janis Schubert in developing and integrating different parts of the software while striving to maintain the same look and feel of the user interfaces Finally, we thank Jonathan Shipley of John Wiley and Sons for his enthusiastic support and assistance in publishing this edition Leen Ammeraal, The Netherlands l.ammeraal@hccnet.nl Kang Zhang, USA kzhang@utdallas.edu Chapter 1: 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 ycoordinates 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 Chapter 7: Hidden-Face Elimination Figure 7.1: Result of back-face culling Figure 7.2: Vector s, pointing to the sun, and normal vector n of a face Figure 7.3: Beams, each of which partly obscures another Figure 7.4: Program Painter.java applied to file letterL.dat Figure 7.5: Sphere and cone Figure 7.6: Triangle ABC and scan line LR Figure 7.7: Illustration of more efficient version Figure 7.8: Z-buffer algorithm applied to three beams Figure 7.9: Painter's algorithm failing Figure 7.10: Function of two variables Figure 7.11: Point P belonging to triangles of very different colors Figure 7.12: Geometrical interpretation of x, n and h, where h is negative Figure F.3: Object nearby: two inner faces visible Figure F.4: The same object far away: two inner faces invisible Figure 7.13: Two rotating cubes, generated by back-face culling Figure 7.14: Steps Chapter 8: Fractals Figure 8.1: Three generations of the Koch curve Figure 8.2: Koch snowflakes of generations 3, 5 and 7 Figure 8.3: Dragon curves: 1st, 2nd, 3rd, 4th, 5th and 11th generations Figure 8.4: Dragon curve of 8th generation, rounded corners Figure 8.5: Hilbert curve (5th generation) and Sierpinski arrowhead (7th generation) Figure 8.6: Second generation islands Figure 8.7: Examples of fractal trees: Tree1 (4th generation), Tree2 (5th generation) and Tree3 (4th generation) Figure 8.8: Mandelbrot set and magnified detail Figure 8.9: Cropping and zooming in Figure 8.10: Result of cropping and zooming in Figure 8.11: Julia set, obtained by using c = −0.76 + 0.084i Appendix A: Linear Interpolation of 1/z Figure A.1: Central projection of point P on screen Figure A.2: Linear interpolation applied to inverse values Appendix E: Some Applications Figure E.1: A tetrahedron: (a) inside a cube; (b) cube omitted; (c) after rotation Figure E.2: Octahedron Figure E.3: Icosahedron and dodecahedron as reciprocal polyhedra Figure E.4: Approximations of a sphere: (a) n = 6;(b) n = 30 Figure E.5: Dividing a triangle into four smaller ones Figure E.6: Icosahedron and three other polyhedra derived from it by program SphTria.java: (a) 20 faces (see Section E.1); (b) 80 faces; (c) 320 faces; (d) 1280 faces Figure E.7: Octahedron and three other polyhedra derived from it by program SphTria.java: (a) 8 faces (see Section E.1); (b) 32 faces; (c) 128 faces; (d) 512 faces Figure E.8: Torus (n = 30 and R = 2.5) Figure E.9: Basic circles of a torus Figure E.10: Vertex numbers of first beam Figure E.11: Spirals of beams Figure E.12: Surface of a function z = f (x, y) as specified by (E.2) and (E.3) Figure E.13: Dialog box for program Func.java Figure E.14: Two triangles with an invisible common edge forming a rectangle Appendix F: Hints and Solutions to Exercises Figure F.1: Dashed lines representing invisible edges Figure F.2: One of the four faces for the square rings of Figure 6.27 Figure F.3: Object nearby: two inner faces visible Figure F.4: The same object far away: two inner faces invisible Colour Plates Plate 1: Fillet or rounded corner, Exercise 2.8 Plate 2: Inscribed circle and three excircles of triangle, Exercise 2.9 Plate 3: Tree of Pythagoras, Exercise 2.10 Plate 4: Demonstration of Bresenham's algorithm, Exercise 4.6 Plate 5: Two colored cubes, captured when rotating independently, Exercise 7.3 Plate 6: Three beams partly hiding each other, see Sections 6.7, 7.3 and 7.4 Plate 7: Beams in a spiral, see Section E4, Appendix E Plate 8: An open book, see Exercise 6.5 Plate 9: Solid letter A as an object with a hole, see Section 6.4 Plate 10: A cube of cubes, Exercise 6.8 Plate 11: Dodecahedron, see Section E1, Appendix E Plate 12: Two tori, Exercise 6.9 Plate 13: A function of two variables, see Section E.5, Appendix E The expression 10 sin(pow(x*x + y*y, 0.5))/(1 + pow(x*x + y*y, 0.2)) was supplied to program Func.java, with ranges and step sizes for x and y specified as 15(0.25)15 Plate 14: Tree with green leaves, Exercise 8.4 Plate 15: Sample output of program MandelbrotZoom.java, see Section 8.4 Plate 16: Julia set, as discussed at the end of Section 8.4 List of Code Examples Chapter 1: Elementary Concepts EXERCISES Chapter 2: Applied Geometry EXERCISES Chapter 3: Geometrical Transformations EXERCISES Chapter 4: Some Classic Algorithms EXERCISES Chapter 5: Perspective EXERCISES Chapter 6: Hidden-Line Elimination EXERCISES Chapter 7: Hidden-Face Elimination EXERCISES Chapter 8: Fractals EXERCISES ... 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... 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 Computer Graphics for Java Programmers, ... Computer Graphics for Java Programmers, the programming language Java has increasingly become the language of choice in many industrial and business domains Hence the skills for developing computer graphics applications using Java have been highly in demand but are