Programmer-books

Transcription

www.allitebooks.com

JavaFX Special Effects Taking Java RIA to the Extreme with Animation,Multimedia, and Game Elements Lucas L. Jordanwww.allitebooks.com

JavaFX Special Effects: Taking Java RIA to the Extreme with Animation, Multimedia, and Game ElementsCopyright 2009 by Lucas L. JordanAll rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronicor mechanical, including photocopying, recording, or by any information storage or retrieval system, without theprior written permission of the copyright owner and the publisher.ISBN-13 (pbk): 978-1-4302-2623-9ISBN-13 (electronic): 978-1-4302-2624-6Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of atrademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, withno intention of infringement of the trademark.President and Publisher: Paul ManningLead Editor: Tom WelshTechnical Reviewer: Sten AndersonEditorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick,Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann,Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom WelshCoordinating Editor: Debra KellyCopy Editors: Sharon Terdeman and Vanessa PorterCompositor: LaurelTechIndexer: BIM Indexing & Proofreading ServicesArtist: April MilneCover Designer: Kurt KramesDistributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York,NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visithttp://www.springeronline.com.For information on translations, please e-mail info@apress.com, or visit http://www.apress.com.Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBookversions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales.The information in this book is distributed on an “as is” basis, without warranty. Although every precaution hasbeen taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person orentity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the informationcontained in this work.The source code for this book is available to readers at http://www.apress.com.www.allitebooks.com

Dedicated to my wife, Debra. I love you, and I love our future together.www.allitebooks.com

CONTENTSContents at a Glance Chapter 1: JavaFX Design Considerations.1 Chapter 2: Effect: Particle Systems.23 Chapter 3: Effect: Visual Transitions .49 Chapter 4: Effect: Animated Lighting.71 Chapter 5: Effect: Advanced Interpolators .87 Chapter 6: Effect: Physics .113 Chapter 7: Effect: Animated Image Sequences .137 Chapter 8: Effect: Animated Gradients .153 Chapter 9: Effect: Audio Visualizer.177 Chapter 10: Effects: Physics and Particles .201 Chapter 11: Pulling It All Together: Clown Cannon .215 Index.241vwww.allitebooks.com

CONTENTSContentsContents at a Glance.vContents .viForeword .xiiAbout the Author .xiiiAbout the Technical Reviewer . xivAcknowledgments . xvIntroduction. xvi Chapter 1: JavaFX Design Considerations.1Graphics and Animations in Applications .1Controls and Layout .1Graphics .5Animations .8Designers and Developers .9Workflow.9Showing off the Platform .12Setting Realistic Expectations.12JavaFX .13Scene Graph.13viwww.allitebooks.com

CONTENTSDesigner Tools .14Deployment.18NetBeans.18Open Source Tools.20GIMP.20ImageMagick .21Blender.21Inkscape.21Audacity .21Summary .21 Chapter 2: Effect: Particle Systems.23Basic Principles .23Visual Density.25Particle Appearance and Behavior.25Animation Implementation.25Example 1: Core Classes.25Example 2: Adding Some Controls.29Example 3: Transparency .35Example 4: Blend Mode .38Example 5: Nonuniform Nodes .40Example 6: Direction.44Example 7: Nonlinear Paths.46Further Considerations .47Summary .48 Chapter 3: Effect: Visual Transitions .49Getting Started.49viiwww.allitebooks.com

CONTENTSExample 1: Fade Replace.53Example 2: Slide Replace .56Example 3: Flip Replace.58Example 4: Wipe Replace .63Example 5: Burn Replace.66Further Considerations .69Summary .69 Chapter 4: Effect: Animated Lighting.71Lighting Basics .71Animating Light.73Distant Light Example .75Point Light Example .77Spot Light Example .80Light and Shadow Example.83Further Considerations.86Summary .86 Chapter 5: Effect: Advanced Interpolators .87The Basics .87Visualizing Interpolators.88Built-In Interpolators .91Custom Interpolators .94Extending Interpolator.95Quadratic Interpolator .96Cubic Interpolator.97Polynomial Interpolator .99Windup-Overshoot Interpolator.101viiiwww.allitebooks.com

CONTENTSStep Interpolator .103Transition Example .108Fade Transition .110Slide Transition .110Flip Transition .111Further Considerations.111Summary .112 Chapter 6: Effect: Physics .113Simulation.113Third-Party Implementation.114Simple Example .115Pendulum Example .122Teeter Totter Example.126Transition Example .128Further Considerations .135Summary .135 Chapter 7: Effect: Animated Image Sequences .137Creating Images.137Implementation.143Further Considerations .151Summary .152 Chapter 8: Effect: Animated Gradients .153The Basics .153Paint Types s.com

CONTENTSSimple Color Example .159Simple Linear Gradient.160Simple Radial .163Multi-Colored Linear .165Animated Stops.166Animate Opacity and Stops.169Progress Bar .172Summary .175 Chapter 9: Effect: Audio Visualizer.177What Is an Audio Visualizer?.177Audio and the JVM.179Audio and JavaFX.179Java Sound .180JavaFX and Java .186Audio Visualizations.188Controlling the Audio.190Bars.192Disco .194Wave .198Summary .200 Chapter 10: Effects: Physics and Particles .201Particles as Bodies .201Emitters as Bodies.209Summary .214 Chapter 11: Pulling It All Together: Clown Cannon .215Design Phase .215xwww.allitebooks.com

CONTENTSGame Design.215Graphic Design.217Implementation.222Game Life Cycle .223Round Life Cycle .230Summary .240 Index.241xi

INTRODUCTIONForewordJavaFX. Oh boy, another new technology purporting to be the long-lost missing link between your mathfueled, unshaven, back-end developers, and latte-sipping, Mac-using, front-end designers.We’ve heard all this before: Flash, Flex, Silverlight, AJAX, and heck, even GWT have all surfaced inthe past few years—all attempting to solve the problems of delivering rich interfaces across devices asdiverse as television set-top boxes and mobile phones. But none of these has captured the broadaudience that Java serves. Much as Java functioned as the stepping-stone toward computer operatingsystem agnosticism, JavaFX may well be the springboard for device agnosticism. The idea thatapplications no longer must be coupled to a particular device is, frankly, transformative, and worthy ofserious consideration.This book, written by my friend, colleague, and neighbor, Lucas Jordan, provides an outstandingrun-through of most of the compelling reasons to consider JavaFX.By focusing on rich graphical tools that assist in the creation of deeply immersive applicationexperiences, Lucas taps into the essence of what makes JavaFX an attractive technology. Whether thesubject is JavaFX’s outstanding designer-developer workflow, unique declarative syntax, first-classsupport of Java technologies, rich graphics API, or innovative effects tools, Lucas shows you everythingyou need in a manner that is straightforward and compelling.If you are considering using JavaFX in the near future, read this book. It will provide you witheverything you wanted to know about JavaFX’s ability to produce rich, immersive interfaces.If you haven’t yet thought about using JavaFX, read this book—it just may change your mind.Ryan DonahueManager of Information SystemsGeorge Eastman House, International Museum of Photography and Filmxii

About the Author Lucas L. Jordan started his love of computers with a Commodore 64 and BASICin the 1980s. This love survived the DOS and Pascal years to blossom into asuccessful career as a Java developer. He is currently working on back-end systemsfor EffectiveUI, and he hopes one day to make it as an independent gamedeveloper. Keep your eye on claywaregames.com.Lucas also enjoys organic food and swimming whenever the western New Yorkweather permits.xiii

INTRODUCTIONAbout the Technical Reviewer Sten Anderson has been working with Java since the late ’90s and is currently aSenior Consultant for the software consultancy Citytech in Chicago. Sten blogsabout Java, JavaFX, Groovy, and any number of other things athttp://blogs.citytechinc.com/sanderson/.xiv

AcknowledgmentsFirst, I would like to acknowledge the people who encouraged me to work with computers: HaroldShermer (grandfather), Yvonne Jordan (mother), Len Wilcox, Matthew Godleski, David Eck, and BillSimons.Second, I would like to acknowledge EffectiveUI, my current employer, for providing me with theopportunity to meet the Java community face to face at JavaONE 2009.Third, I would like to thank Apress and all the hard work everyone put in to get me through my firstbook.Lastly, I would like to thank all of the people who are still my friend after me telling them, “Sorry Ican’t, I have to finish my book.”Oh, and Jim Weaver for putting me in touch with Apress in the first place.xv

IntroductionGoal of This BookThis book is intended to show off some interesting visual effects that can be implemented in JavaFX. Thechapters explore particle effects, realistic animations driven by physics, and a number of othertechniques. The first set of chapters look at each effect in isolation, and then the last chapters look atsome of these effects in combination. The book concludes by presenting an entire sample application.The book's goal is to provide a developer with a number of new tools that can be used to spruce upan old application or used in a new application. Given the newness of JavaFX at the time this book waswritten, I suspect that most developers are in the evaluation phase, exploring whether JavaFX is aworthwhile technology for some new project. I hope some of the effects in this book will convince youthat JavaFX has a lot of promise.While always keeping the needs of developers in mind, I will also spend some time looking at whyapplications require graphics at all, and how leveraging experts in visual design and user experience willgreatly improve the end result.This book was written by first messing around with JavaFX code to produce something interesting,then writing a little about it, and finally cleaning up the source code with the help of my technicalreviewer. I bring this up to point out that the accompanying source code is really the heart of this book; itdrove the content of these chapters. For the book to achieve its goal, I believe it is best consumed withthe code handy. Go fire up NetBeans and run the example code as you get to each chapter, I think it willadd a lot. Besides, this book is in black and white, while many of the examples are rich in color.Who This Book Is ForThis book is not an introduction to JavaFX. I assume you have played around with JavaFX already andhopefully have written an application or two. JavaFX is different enough from Java and other commonlanguages that I urge anyone interested in this book to spend some time with JavaFX first. Just becomingfamiliar with the numerous curly and square brackets that get littered throughout JavaFX source code isa good start. This will help you to understand the concepts being presented, instead of fighting withsyntax. JavaFX code can be surprisingly short and powerful, but some of that brevity comes with a cost inclarity or obviousness.Additionally I expect you, the reader, to have some experience with computer graphics, such ascoordinate systems, polygons, transformations, colors and gradients, and trigonometry. If you feel thatbrushing up on any of these topics is worth your while, I would like to recommend the online course"Computer Sc-Computer Graphics" from the Indian Institute of Technology Madras (IIT Madras) byxvi

INTRODUCTIONprofessor Dr. Sukhendu Das. The class is presented on YouTube at the following URL:http://www.youtube.com/view play list?p 338D19C40D6D1732If you are a JavaFX expert already and just want to get to the effects, jump to Chapter 2 – but comeback and read the section Designers and Developers in Chapter 1 sometime.Who This book Is ByI think it is worth giving a little bit of background on my development history. My hope is that by sharingwhere I come from as a developer, you will be able to better understand my opinions and my interest inJavaFX.Like so many of us, I started playing with computers at a young age. My early, pre-career computerprogramming was mostly in BASIC and Pascal. Using these tools, I tried in vain to reproduce a numberof video games I was very fond of. I had neither the time nor the experience to create anything of any realvalue, but I learned a lot about 2D graphics in the process.In college I was introduced to Java and fell in love with the language and with Swing. When Igraduated I got a number of contract jobs writing Swing applications. This was a great experience, sinceI had to learn the hard way about things such as testing code and the truth about cross-platformdeployment, and, most important, it taught me about how users use applications.I remember presenting a new feature to a client; I think it was a tool that laid out printed labels. Myclient sat down at my app and starting clicking randomly; he had no idea how to use the “clever”interface I had devised. I tried to explain how some combination of mouse gestures and key pressesproduced the desired results. He didn’t get it and I rewrote it. This showed me the value of usingstandard things such as buttons, sliders, and wizards to meet the expectation of the user. This is not tosay there is no place for innovation, of course there is; computer interfaces have been evolving sincethey were invented, but the changes come in small steps.Later, a small company that specializes in Rich Internet Applications (RIAs) hired me. This was myfirst experience working with user experience designers who have made it their career to understandhow people use applications, and to create interfaces that meet those expectations. Most of thesedesigners don’t know how to code; they create PDF documents explaining the interface and a developerimplements it. There is an example of one of these wire frame documents in Chapter 2 in the sectionDesigners and Developers, where I explain how they are used.Working at a company that put design first showed me the importance of having these specialistsinvolved in a project. Many developers and graphical designers use the term “developer ugly” to refer tothe quick and dirty graphics that developers create while in the process of getting the app to work.Developer ugly can also apply to user experience decisions as well. I don’t intend to say that alldevelopers lack the skills or the design sense to create nice-looking images or elegant UIs. I am pointingout that when you have your head down in the code, worrying about thread synchronization orsomething, it is not the best time to create an icon for a button. Just as graphic designers have all day toiterate over their creations, a user experience designer has all day to contemplate the UI. So I say, letspecialists do their job. And of course, great projects take shape when all of the specialists work together.So now my day job is implementing designs by other people, and at night I continue to try andcreate simple but fun video games. It is this endless tinkering that led me to JavaFX and to write thisbook. So while I try to address design issues as they come up in this book, my primary concern is gettingit to work. I hope that the techniques in

CONTENTS v Contents at a Glance Chapter 1: JavaFX Design Considerations.