1 / 94

Chapter 4: Graphics Programming with Java 2D and Java 3D

Chapter 4: Graphics Programming with Java 2D and Java 3D. Outline

Download Presentation

Chapter 4: Graphics Programming with Java 2D and Java 3D

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4: Graphics Programming with Java 2D and Java 3D Outline 4.1 Introduction4.2 Coordinates, Graphics Contexts and Graphics Objects4.3 Java 2D API 4.3.1 Java 2D Shapes 4.3.2 Java 2D Image Processing4.4 Java 3D API 4.4.1 Obtaining and Installing the Java 3D API 4.4.2 Java 3D Scenes 4.4.3 A Java 3D Example4.5 A Java 3D Case Study: A 3D Game with Custom Behaviors

  2. 4.1 Introduction • Java 2D • APIs for 2D graphics • Drawing • Coloring • Image manipulation • Java 3D • APIs for 3D graphics • 3D object manipulation • Lighting • Texture mapping

  3. 4.2   Coordinates, Graphics Contexts and Graphics Objects • Java 2D coordinate system • Scheme for identifying every point on screen • Upper left corner of GUI component has coordinates (0,0) • Java graphics context • Enables drawing on screen • e.g., font and color manipulation • Graphics object manages a graphics context • abstract class for portability

  4. 4.2   Coordinates, Graphics Contexts and Graphics Objects (cont.) Fig. 4.1 Java coordinate system. Units are measured in pixels.

  5. 4.3 Java 2D API • Java 2D API • Provides advanced 2D graphics capabilities • Part of the Java 2 Platform, Standard Edition • Includes features for processing line art, text and images • Class java.awt.Graphics2D • Enables drawing with Java 2D API • Offers three graphics primitives: images, text and shapes • java.awt.Graphics subclass • Contains seven state attributes • Clipping, compositing, font, paint, rendering hints, stroke and transforms

  6. 4.3 Java 2D API (cont.)

  7. 4.3 Java 2D API (cont.)

  8. 4.3.1 Java 2D Shapes • Java 2D shape classes • Package java.awt.geom • Ellipse2D.Double • Line2D.Double • Rectangle2D.Double • RoundRectangle2D.Double • Arc2D.Double

  9. Class Shapes demonstrates several Java 2D shapes and rendering attributes Cast Graphics object to Graphics2D object to allow access to Java 2D features GradientPaint displays a gradient color Draw a filled ellipse 1 // Shapes.java 2 // Shapes demonstrates some Java 2D shapes. 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 import java.awt.geom.*; 8 import java.awt.image.*; 9 10 // Java extension packages 11 import javax.swing.*; 12 13 publicclass Shapes extends JFrame { 14 15 // constructor method 16 public Shapes() 17 { 18 super( "Drawing 2D shapes" ); 19 } 20 21 // draw shapes using Java 2D API 22 publicvoid paint( Graphics g ) 23 { 24 // call superclass' paint method 25 super.paint( g ); 26 27 // get Graphics 2D by casting g to Graphics2D 28 Graphics2D graphics2D = ( Graphics2D ) g; 29 30 // draw 2D ellipse filled with blue-yellow gradient 31 graphics2D.setPaint( new GradientPaint 32 ( 5, 30, Color.blue, 35, 100, Color.yellow, true ) ); 33 graphics2D.fill( new Ellipse2D.Double( 5, 30, 65, 100 ) ); 34 Fig. 4.4 Demonstrating some Java 2D shapes(part 1).Line 13Line 28Lines 31-32Lines 33

  10. Draw hollow rectangle with thick red border Create a Buffered-Image, which will serve as a pattern for a rounded rectangle Use the Buffered-Image to create the pattern-filled rounded rectangle Draw hollow arc with thick white border 35 // draw 2D rectangle in red 36 graphics2D.setPaint( Color.red ); 37 graphics2D.setStroke( new BasicStroke( 10.0f ) ); 38 graphics2D.draw( 39 new Rectangle2D.Double( 80, 30, 65, 100 ) ); 40 41 // draw 2D rounded rectangle with BufferedImage background 42 BufferedImage bufferedImage = new BufferedImage( 43 10, 10, BufferedImage.TYPE_INT_RGB ); 44 45 Graphics2D graphics = bufferedImage.createGraphics(); 46 graphics.setColor( Color.yellow ); // draw in yellow 47 graphics.fillRect( 0, 0, 10, 10 ); // draw filled rectangle 48 graphics.setColor( Color.black ); // draw in black 49 graphics.drawRect( 1, 1, 6, 6 ); // draw rectangle 50 graphics.setColor( Color.blue ); // draw in blue 51 graphics.fillRect( 1, 1, 3, 3 ); // draw filled rectangle 52 graphics.setColor( Color.red ); // draw in red 53 graphics.fillRect( 4, 4, 3, 3 ); // draw filled rectangle 54 55 // paint buffImage into graphics context of JFrame 56 graphics2D.setPaint( new TexturePaint( 57 bufferedImage, new Rectangle( 10, 10 ) ) ); 58 graphics2D.fill( new RoundRectangle2D.Double( 59 155, 30, 75, 100, 50, 50 ) ); 60 61 // draw 2D pie-shaped arc in white 62 graphics2D.setPaint( Color.white ); 63 graphics2D.setStroke( new BasicStroke( 6.0f ) ); 64 graphics2D.draw( new Arc2D.Double( 65 240, 30, 75, 100, 0, 270, Arc2D.PIE ) ); 66 67 // draw 2D lines in green and yellow 68 graphics2D.setPaint( Color.green ); 69 graphics2D.draw( new Line2D.Double( 395, 30, 320, 150 ) ); Fig. 4.4 Demonstrating some Java 2D shapes(part 2).Lines 36-39Lines 42-53Lines 56-59Lines 62-65

  11. Draw yellow dashed line 70 71 float dashes[] = { 10, 2 }; 72 73 graphics2D.setPaint( Color.yellow ); 74 graphics2D.setStroke( new BasicStroke( 75 4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 76 10, dashes, 0 ) ); 77 graphics2D.draw( new Line2D.Double( 320, 30, 395, 150 ) ); 78 79 } // end method paint 80 81 // start application 82 publicstaticvoid main( String args[] ) 83 { 84 Shapes application = new Shapes(); 85 application.setDefaultCloseOperation( 86 JFrame.EXIT_ON_CLOSE ); 87 88 application.setSize( 425, 160 ); 89 application.setVisible( true ); 90 } 91 } Fig. 4.4 Demonstrating some Java 2D shapes(part 3).Lines 73-77

  12. Class Shapes2 demonstrates using a general path – a shape constructed from lines and complex curves – to draw a start Define two int array representing the x- and y-coordinates of the points in the star 1 // Shapes2.java 2 // Shapes2 demonstrates a general path. 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 import java.awt.geom.*; 8 9 // Java extension packages 10 import javax.swing.*; 11 12 publicclass Shapes2 extends JFrame { 13 14 // set window's title bar String and background color 15 16 public Shapes2() 17 { 18 super( "Drawing 2D Shapes" ); 19 20 getContentPane().setBackground( Color.gray ); 21 } 22 23 // draw general paths 24 publicvoid paint( Graphics g ) 25 { 26 // call superclass' paint method 27 super.paint( g ); 28 29 int xPoints[] = 30 { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 }; 31 int yPoints[] = 32 { 0, 36, 36, 54, 96, 72, 96, 54, 36, 36 }; 33 34 Graphics2D graphics2D = ( Graphics2D ) g; 35 Fig. 4.5 Demonstrating Java 2D paths (part 1).Line 12Lines 29-32

  13. Method moveTo specifies the first point in the star Method lineTo draws a line to the next point in the star Draw several stars 36 // create a star from a series of points 37 GeneralPath star = new GeneralPath(); 38 39 // set the initial coordinate of the General Path 40 star.moveTo( xPoints[ 0 ], yPoints[ 0 ] ); 41 42 // create the star--this does not draw the star 43 for ( int count = 1; count < xPoints.length; count++ ) 44 star.lineTo( xPoints[ count ], yPoints[ count ] ); 45 46 // close the shape 47 star.closePath(); 48 49 // translate the origin to (200, 200) 50 graphics2D.translate( 200, 200 ); 51 52 // rotate around origin and draw stars in random colors 53 for ( int count = 1; count <= 20; count++ ) { 54 55 // rotate coordinate system 56 graphics2D.rotate( Math.PI / 10.0 ); 57 58 // set random drawing color 59 graphics2D.setColor( new Color( 60 ( int ) ( Math.random() * 256 ), 61 ( int ) ( Math.random() * 256 ), 62 ( int ) ( Math.random() * 256 ) ) ); 63 64 // draw filled star 65 graphics2D.fill( star ); 66 } 67 68 } // end method paint 69 70 // execute application Fig. 4.5 Demonstrating Java 2D paths (part 2).Line 40Lines 43-44Lines 53-65

  14. 71 publicstaticvoid main( String args[] ) 72 { 73 Shapes2 application = new Shapes2(); 74 application.setDefaultCloseOperation( 75 JFrame.EXIT_ON_CLOSE ); 76 application.setSize( 400, 400 ); 77 application.setVisible( true ); 78 } 79 } Fig. 4.5 Demonstrating Java 2D paths (part 3).

  15. 4.3.2 Java 2D Image Processing • Image processing • Manipulation of digital images by applying filters • Filters – mathematical operations that change images • Compression filters • Reduce digital image’s memory usage • Measurement filters • Collect data from digital images • Enhancement filters • Alter certain physical aspects of an image • Java 2D image processing • Use java.awt.BufferedImage

  16. Constructor takes a URL that specifies the file containing the image to filter Load image from URL into memory 1 // ImagePanel.java 2 // ImagePanel contains an image for display. The image is 3 // converted to a BufferedImage for filtering purposes. 4 package com.deitel.advjhtp1.java2d; 5 6 // Java core packages 7 import java.awt.*; 8 import java.awt.event.*; 9 import java.awt.image.*; 10 import java.net.*; 11 12 // Java extension packages 13 import javax.swing.*; 14 import javax.swing.event.*; 15 16 publicclass ImagePanel extends JPanel { 17 18 private BufferedImage displayImage; // filtered image 19 private BufferedImage originalImage; // original image 20 private Image image; // image to load 21 22 // ImagePanel constructor 23 public ImagePanel( URL imageURL ) 24 { 25 image = 26 Toolkit.getDefaultToolkit().createImage( imageURL ); 27 28 // create MediaTracker for image 29 MediaTracker mediaTracker = new MediaTracker( this ); 30 mediaTracker.addImage( image, 0 ); 31 32 // wait for Image to load 33 try { 34 mediaTracker.waitForAll(); 35 } Fig. 4.6 Class ImagePanel allows for displaying and filtering Buffered-Images (part 1).Line 23Line 34

  17. Create BufferedImage to store the original (unaltered) image BufferedImagedisplayImage is the image that will be modified Apply filter to displayImage Replaces displayImage with original BufferedImage 36 37 // exit program on error 38 catch ( InterruptedException interruptedException ) { 39 interruptedException.printStackTrace(); 40 } 41 42 // create BufferedImages from Image 43 originalImage = new BufferedImage( image.getWidth( null ), 44 image.getHeight( null ), BufferedImage.TYPE_INT_RGB ); 45 46 displayImage = originalImage; 47 48 // get BufferedImage’s graphics context 49 Graphics2D graphics = displayImage.createGraphics(); 50 graphics.drawImage( image, null, null ); 51 52 } // end ImagePanel constructor 53 54 // apply Java2DImageFilter to Image 55 publicvoid applyFilter( Java2DImageFilter filter ) 56 { 57 // process Image using Java2DImageFilter 58 displayImage = filter.processImage( displayImage ); 59 repaint(); 60 } 61 62 // set Image to originalImage 63 publicvoid displayOriginalImage() 64 { 65 displayImage = new BufferedImage( image.getWidth( null ), 66 image.getHeight( null ), BufferedImage.TYPE_INT_RGB ); 67 68 Graphics2D graphics = displayImage.createGraphics(); 69 graphics.drawImage( originalImage, null, null ); 70 repaint(); Fig. 4.6 Class ImagePanel allows for displaying and filtering Buffered-Images (part 2).Lines 43-44Line 46Lines 55-60Lines 65-66

  18. Draw displayImage on screen 71 } 72 73 // draw ImagePanel 74 publicvoid paintComponent( Graphics g ) 75 { 76 super.paintComponent( g ); 77 Graphics2D graphics = ( Graphics2D ) g; 78 graphics.drawImage( displayImage, 0, 0, null ); 79 } 80 81 // get preferred ImagePanel size 82 public Dimension getPreferredSize() 83 { 84 returnnew Dimension( displayImage.getWidth(), 85 displayImage.getHeight() ); 86 } 87 88 // get minimum ImagePanel size 89 public Dimension getMinimumSize() 90 { 91 return getPreferredSize(); 92 } 93 } Fig. 4.6 Class ImagePanel allows for displaying and filtering Buffered-Images (part 3).Line 78

  19. Method processImage accepts a BufferedImage to filter and returns the filtered BufferedImage 1 // Java2DImageFilter.java 2 // Java2DImageFilter is an interface that defines method 3 // processImage for applying a filter to an Image. 4 package com.deitel.advjhtp1.java2d; 5 6 // Java core packages 7 import java.awt.*; 8 import java.awt.image.*; 9 10 publicinterface Java2DImageFilter { 11 12 // apply filter to Image 13 public BufferedImage processImage( BufferedImage image ); 14 } Fig. 4.6 Java2D-ImageFilter interface for creating Java 2D image filters.Line 13

  20. 4.3.2 Java 2D Image Processing (cont.)

  21. Use LookupOp (an array indexed by source pixel color values) to invert colors 1 // InvertFilter.java 2 // InvertFilter, which implements Java2DImageFilter, inverts a 3 // BufferedImage's RGB color values. 4 package com.deitel.advjhtp1.java2d; 5 6 // Java core packages 7 import java.awt.image.*; 8 9 publicclass InvertFilter implements Java2DImageFilter { 10 11 // apply color inversion filter to BufferedImage 12 public BufferedImage processImage( BufferedImage image ) 13 { 14 // create 256 color array and invert colors 15 byte[] invertArray = new byte[ 256 ]; 16 17 for ( int counter = 0; counter < 256; counter++ ) 18 invertArray[ counter ] = ( byte )( 255 - counter ); 19 20 // create filter to invert colors 21 BufferedImageOp invertFilter = new LookupOp( 22 new ByteLookupTable( 0, invertArray ), null ); 23 24 // apply filter to displayImage 25 return invertFilter.filter( image, null ); 26 27 } // end method processImage 28 } Fig. 4.9 InvertFilter inverts colors in a BufferedImage.Lines 21-22

  22. Use ConvolveOp (which combines the colors of a source pixel and its surrounding neighbors) to sharpen edges 1 // SharpenFilter.java 2 // SharpenFilter, which implements Java2DImageFilter, sharpens 3 // the edges in a BufferedImage. 4 package com.deitel.advjhtp1.java2d; 5 6 // Java core packages 7 import java.awt.image.*; 8 9 publicclass SharpenFilter implements Java2DImageFilter { 10 11 // apply edge-sharpening filter to BufferedImage 12 public BufferedImage processImage( BufferedImage image ) 13 { 14 // array used to detect edges in image 15 float[] sharpenMatrix = { 16 0.0f, -1.0f, 0.0f, 17 -1.0f, 5.0f, -1.0f, 18 0.0f, -1.0f, 0.0f }; 19 20 // create filter to sharpen edges 21 BufferedImageOp sharpenFilter = 22 new ConvolveOp( new Kernel( 3, 3, sharpenMatrix ), 23 ConvolveOp.EDGE_NO_OP, null ); 24 25 // apply sharpenFilter to displayImage 26 return sharpenFilter.filter( image, null ); 27 28 } // end method processImage 29 } Fig. 4.10 SharpenFilter sharpens edges in a BufferedImage.Lines 21-22

  23. Use ConvolveOp to blur edges 1 // BlurFilter.java 2 // Blurfilter blurs a BufferedImage. 3 package com.deitel.advjhtp1.java2d; 4 5 // Java core packages 6 import java.awt.image.*; 7 8 publicclass BlurFilter implements Java2DImageFilter { 9 10 // apply blurring filter to BufferedImage 11 public BufferedImage processImage( BufferedImage image ) 12 { 13 // array used to blur BufferedImage 14 float[] blurMatrix = { 15 1.0f / 9.0f, 1.0f / 9.0f, 1.0f / 9.0f, 16 1.0f / 9.0f, 1.0f / 9.0f, 1.0f / 9.0f, 17 1.0f / 9.0f, 1.0f / 9.0f, 1.0f / 9.0f }; 18 19 // create ConvolveOp for blurring BufferedImage 20 BufferedImageOp blurFilter = new ConvolveOp( 21 new Kernel( 3, 3, blurMatrix ) ); 22 23 // apply blurFilter to BufferedImage 24 return blurFilter.filter( image, null ); 25 26 } // end method processImage 27 } Fig. 4.11 BlurFilter blurs the colors in a BufferedImage.Lines 20-21

  24. Use BandCombineOp (which operates on the color bands of a Raster) to alter colors Raster organizes and stores the samples that determine the pixel colors in the BufferedImage 1 // ColorFilter.java 2 // ColorFilter is an Java2DImageFilter that alters the RGB 3 // color bands in a BufferedImage. 4 package com.deitel.advjhtp1.java2d; 5 6 // Java core packages 7 import java.awt.image.*; 8 9 publicclass ColorFilter implements Java2DImageFilter { 10 11 // apply color-change filter to BufferedImage 12 public BufferedImage processImage( BufferedImage image ) 13 { 14 // create array used to change RGB color bands 15 float[][] colorMatrix = { 16 { 1.0f, 0.0f, 0.0f }, 17 { 0.5f, 1.0f, 0.5f }, 18 { 0.2f, 0.4f, 0.6f } }; 19 20 // create filter to change colors 21 BandCombineOp changeColors = 22 new BandCombineOp( colorMatrix, null ); 23 24 // create source and display Rasters 25 Raster sourceRaster = image.getRaster(); 26 27 WritableRaster displayRaster = 28 sourceRaster.createCompatibleWritableRaster(); 29 30 // filter Rasters with changeColors filter 31 changeColors.filter( sourceRaster, displayRaster ); 32 33 // create new BufferedImage from display Raster 34 returnnew BufferedImage( image.getColorModel(), 35 displayRaster, true, null ); Fig. 4.12 ColorFilter changes the colors in a BufferedImage (part 1).Lines 21-22Line 25

  25. 36 37 } // end method processImage 38 } Fig. 4.12 ColorFilter changes the colors in a BufferedImage (part 2).

  26. Class Java2DExample provides a user interface for applying Java2DImageFilters to ImagePanels Declared a set of Java2DImageFilters 1 // Java2DExample.java 2 // Java2DExample is an application that applies filters to an 3 // image using Java 2D. 4 package com.deitel.advjhtp1.java2d; 5 6 // Java core packages 7 import java.awt.*; 8 import java.awt.event.*; 9 import java.awt.image.*; 10 import java.lang.*; 11 import java.net.*; 12 13 // Java extension packages 14 import javax.swing.*; 15 import javax.swing.event.*; 16 17 publicclass Java2DExample extends JFrame { 18 19 private JMenu filterMenu; 20 private ImagePanel imagePanel; 21 22 // image filters 23 private Java2DImageFilter invertFilter; 24 private Java2DImageFilter sharpenFilter; 25 private Java2DImageFilter blurFilter; 26 private Java2DImageFilter colorFilter; 27 28 // initialize JMenuItems 29 public Java2DExample() 30 { 31 super( "Java 2D Image Processing Demo" ); 32 33 // create Java2DImageFilters 34 blurFilter = new BlurFilter(); 35 sharpenFilter = new SharpenFilter(); Fig. 4.13 Java 2D image-processing application GUI (part 1).Line 17Lines 23-26

  27. Provide mechanism for displaying the original (unaltered) image 36 invertFilter = new InvertFilter(); 37 colorFilter = new ColorFilter(); 38 39 // initialize ImagePanel 40 imagePanel = new ImagePanel( 41 Java2DExample.class.getResource( "images/ajhtp.png" ) ); 42 43 // create JMenuBar 44 JMenuBar menuBar = new JMenuBar(); 45 setJMenuBar( menuBar ); 46 47 // create JMenu 48 filterMenu = new JMenu( "Image Filters" ); 49 filterMenu.setMnemonic( 'I' ); 50 51 // create JMenuItem for displaying original Image 52 JMenuItem originalMenuItem = 53 new JMenuItem( "Display Original" ); 54 originalMenuItem.setMnemonic( 'O' ); 55 56 originalMenuItem.addActionListener( 57 new ActionListener() { 58 59 // show original Image 60 publicvoid actionPerformed( ActionEvent action ) 61 { 62 imagePanel.displayOriginalImage(); 63 } 64 65 } // end anonymous inner class 66 ); 67 68 // create JMenuItems for Java2DImageFilters 69 JMenuItem invertMenuItem = createMenuItem( 70 "Invert", 'I', invertFilter ); Fig. 4.13 Java 2D image-processing application GUI (part 2).Lines 60-63

  28. Create menus for applying filters to ImagePanel 71 JMenuItem sharpenMenuItem = createMenuItem( 72 "Sharpen", 'S', sharpenFilter ); 73 JMenuItem blurMenuItem = createMenuItem( 74 "Blur", 'B', blurFilter ); 75 JMenuItem changeColorsMenuItem = createMenuItem( 76 "Change Colors", 'C', colorFilter ); 77 78 // add JMenuItems to JMenu 79 filterMenu.add( originalMenuItem ); 80 filterMenu.add( invertMenuItem ); 81 filterMenu.add( sharpenMenuItem ); 82 filterMenu.add( blurMenuItem ); 83 filterMenu.add( changeColorsMenuItem ); 84 85 // add JMenu to JMenuBar 86 menuBar.add( filterMenu ); 87 88 getContentPane().add( imagePanel, BorderLayout.CENTER ); 89 90 } // end Java2DExample constructor 91 92 // create JMenuItem and ActionListener for given filter 93 public JMenuItem createMenuItem( String menuItemName, 94 char mnemonic, final Java2DImageFilter filter ) 95 { 96 // create JMenuItem 97 JMenuItem menuItem = new JMenuItem( menuItemName ); 98 99 // set Mnemonic 100 menuItem.setMnemonic( mnemonic ); 101 102 menuItem.addActionListener( 103 new ActionListener() { 104 105 // apply Java2DImageFilter when MenuItem accessed Fig. 4.13 Java 2D image-processing application GUI (part 3).Lines 79-83

  29. Apply Java2DImageFilter to ImagePanel 106 publicvoid actionPerformed( ActionEvent action ) 107 { 108 imagePanel.applyFilter( filter ); 109 } 110 111 } // end anonymous inner class 112 ); 113 114 return menuItem; 115 116 } // end method createMenuItem 117 118 // start program 119 publicstaticvoid main( String args[] ) 120 { 121 Java2DExample application = new Java2DExample(); 122 application.setDefaultCloseOperation( EXIT_ON_CLOSE ); 123 application.pack(); 124 application.setVisible( true ); 125 } 126 } Fig. 4.13 Java 2D image-processing application GUI (part 4).Lines 106-109

  30. Fig. 4.13 Java 2D image-processing application GUI (part 5).Program output

  31. 4.4 Java 3D API • Java 3d • Offers several features for developing 3D applications: • Behavior • Fog • Geometry • Light • Sound • Texture

  32. 4.4.1 Obtaining and Installing the Java 3D API • Java 3D API requirements • J2SDK • OpenGL or Direct3D • Java extension and utility packages • Not integrated in core Java 2 Platform

  33. 4.4.2 Java 3D Scenes • Scenes • Pictures rendered with Java 3D • Also called a virtual universe • Class VirtualUniverse • Described by scene graphs • Hierarchical structures specifying attributes of 3D environment • Contain branch graphs

  34. 4.4.2 Java 3D Scenes (cont.)

  35. 4.4.3 A Java 3D Example • Interactive Java 3D scene • Demonstrate mouse behaviors • Using mouse to rotate, scale and translate 3D shapes

  36. Class Java3DWorld creates Java 3D environment using geometry, transforms and lighting Import Java 3D utility packages that simplify scene creation Canvas3D is a java.awt.Canvas subclass used for 3D rendering 1 // Java3DWorld.java 2 // Java3DWorld is a Java 3D Graphics display environment 3 // that creates a SimpleUniverse and provides capabilities for 4 // allowing a user to control lighting, motion, and texture 5 // of the 3D scene. 6 package com.deitel.advjhtp1.java3d; 7 8 // Java core packages 9 import java.awt.event.*; 10 import java.awt.*; 11 import java.net.*; 12 13 // Java extension packages 14 import javax.swing.event.*; 15 import javax.media.j3d.*; 16 import javax.vecmath.*; 17 18 // Java 3D utility packages 19 import com.sun.j3d.utils.universe.*; 20 import com.sun.j3d.utils.image.*; 21 import com.sun.j3d.utils.geometry.*; 22 import com.sun.j3d.utils.behaviors.mouse.*; 23 24 publicclass Java3DWorld extends Canvas3D { 25 26 private Appearance appearance; // 3D object's appearance 27 private Box shape; // 3D object to manipulate 28 private Color3f lightColor; // Light color 29 private Light ambientLight; // ambient scene lighting 30 private Light directionalLight; //directional light 31 private Material material; // 3D objects color object 32 private SimpleUniverse simpleUniverse; // 3D scene environment 33 private TextureLoader textureLoader; // 3D object's texture 34 Fig. 4.15 Creating a Java 3D SimpleUniverse with content (part 1).Line 1Lines 19-22Line 24

  37. SimpleUniverse creates a Java 3D scene and encapsulates all objects in virtual universe and viewing platform Configure viewing distance (length between viewer and canvas) for 3D scene BranchGroup is the root node of a scene graph in a Java 3D scene 35 // holds 3D transformation information 36 private TransformGroup transformGroup; 37 38 private String imageName; // texture image file name 39 40 // Java3DWorld constructor 41 public Java3DWorld( String imageFileName ) 42 { 43 super( SimpleUniverse.getPreferredConfiguration() ); 44 45 imageName = imageFileName; 46 47 // create SimpleUniverse (3D Graphics environment) 48 simpleUniverse = new SimpleUniverse( this ); 49 50 // set default view point and direction 51 ViewingPlatform viewPlatform = 52 simpleUniverse.getViewingPlatform(); 53 54 viewPlatform.setNominalViewingTransform(); 55 56 // create 3D scene 57 BranchGroup branchGroup = createScene(); 58 59 // attach BranchGroup to SimpleUniverse 60 simpleUniverse.addBranchGraph( branchGroup ); 61 62 } // end Java3DWorld constructor 63 64 // create 3D scene 65 public BranchGroup createScene() 66 { 67 BranchGroup scene = new BranchGroup(); 68 69 // initialize TransformGroup Fig. 4.15 Creating a Java 3D SimpleUniverse with content (part 2).Line 48Lines 51-54Line 67

  38. TransformGroup specifies transformational behavior, such as rotation, scaling and translation Capability bits are integer flags that specify whether a given object should allow its properties to be read or written during execution BoundingSphere creates bounding volume, which specifies the volume in which Lights and Behaviors affect geometry in the scene Load texture for texture mapping 70 transformGroup = new TransformGroup(); 71 72 // set TransformGroup's READ and WRITE permission 73 transformGroup.setCapability( 74 TransformGroup.ALLOW_TRANSFORM_READ ); 75 76 transformGroup.setCapability( 77 TransformGroup.ALLOW_TRANSFORM_WRITE ); 78 79 // add TransformGroup to BranchGroup 80 scene.addChild( transformGroup ); 81 82 // create BoundingSphere 83 BoundingSphere bounds = new BoundingSphere( 84 new Point3d( 0.0f, 0.0f, 0.0f ), 100.0 ); 85 86 appearance = new Appearance(); // create object appearance 87 material = new Material(); // create texture matieral 88 appearance.setMaterial( material ); 89 90 String rgb = new String( "RGB" ); 91 92 // load texture for scene object 93 textureLoader = new TextureLoader( 94 Java3DWorld.class.getResource( imageName ), rgb, this ); 95 96 // set capability bits for enabling texture 97 textureLoader.getTexture().setCapability( 98 Texture.ALLOW_ENABLE_WRITE ); 99 100 // initial texture will not show 101 textureLoader.getTexture().setEnable( false ); 102 103 // set object's texture 104 appearance.setTexture( textureLoader.getTexture() ); Fig. 4.15 Creating a Java 3D SimpleUniverse with content (part 3).Line 70Lines 73-77Lines 83-84Lines 93-94

  39. Create Box to rotate, scale and translate AmbientLight is a uniform light source that illuminates all objects within its boundary DirectionalLight is a light source that travels between two points 105106 // create object geometry 107 Box shape = new Box( 0.3f, 0.3f, 0.3f, 108 Box.GENERATE_NORMALS | Box.GENERATE_TEXTURE_COORDS, 109 appearance ); 110 111 // add geometry to TransformGroup 112 transformGroup.addChild( shape ); 113 114 // initialize Ambient lighting 115 ambientLight = new AmbientLight(); 116 ambientLight.setInfluencingBounds( bounds ); 117 118 // initialize directionalLight 119 directionalLight = new DirectionalLight(); 120 121 lightColor = new Color3f(); // initialize light color 122 123 // set initial DirectionalLight color 124 directionalLight.setColor( lightColor ); 125 126 // set capability bits to allow DirectionalLight's 127 // Color and Direction to be changed 128 directionalLight.setCapability( 129 DirectionalLight.ALLOW_DIRECTION_WRITE ); 130 131 directionalLight.setCapability( 132 DirectionalLight.ALLOW_DIRECTION_READ ); 133 134 directionalLight.setCapability( 135 DirectionalLight.ALLOW_COLOR_WRITE ); 136 137 directionalLight.setCapability( 138 DirectionalLight.ALLOW_COLOR_READ ); 139 Fig. 4.15 Creating a Java 3D SimpleUniverse with content (part 4).Lines 107-109Lines 115-116Line 119

  40. Create behavior that controls rotation of the Box via the mouse Create behavior that controls translation of the Box via the mouse Create behavior that controls scalability of the Box via the mouse Compiling a BranchGroup informs the Java 3D engine to optimize rendering the scene using the capability bits set by the developer 140 directionalLight.setInfluencingBounds( bounds ); 141 142 // add light nodes to BranchGroup 143 scene.addChild( ambientLight ); 144 scene.addChild( directionalLight ); 145 146 // initialize rotation behavior 147 MouseRotate rotateBehavior = new MouseRotate(); 148 rotateBehavior.setTransformGroup( transformGroup ); 149 rotateBehavior.setSchedulingBounds( bounds ); 150 151 // initialize translation behavior 152 MouseTranslate translateBehavior = new MouseTranslate(); 153 translateBehavior.setTransformGroup( transformGroup ); 154 translateBehavior.setSchedulingBounds( 155 new BoundingBox( new Point3d( -1.0f, -1.0f, -1.0f ), 156 new Point3d( 1.0f, 1.0f, 1.0f ) ) ); 157 158 // initialize scaling behavior 159 MouseZoom scaleBehavior = new MouseZoom(); 160 scaleBehavior.setTransformGroup( transformGroup ); 161 scaleBehavior.setSchedulingBounds( bounds ); 162 163 // add behaviors to BranchGroup 164 scene.addChild( scaleBehavior ); 165 scene.addChild( rotateBehavior ); 166 scene.addChild( translateBehavior ); 167 168 scene.compile(); 169 170 return scene; 171 172 } // end method createScene 173 Fig. 4.15 Creating a Java 3D SimpleUniverse with content (part 5).Lines 147-149Lines 152-156Lines 159-161Line 168

  41. 174 // change DirectionLight color 175 publicvoid changeColor( Color color ) 176 { 177 lightColor.set( color ); 178 directionalLight.setColor( lightColor ); 179 } 180 181 // change geometry surface to textured image or material color 182 publicvoid updateTexture( boolean textureValue ) 183 { 184 textureLoader.getTexture().setEnable( textureValue ); 185 } 186 187 // change image used for texture 188 publicvoid setImageName( String imageFileName ) 189 { 190 imageName = imageFileName; 191 } 192 193 // get image file name 194 public String getImageName() 195 { 196 return imageName; 197 } 198 199 // return preferred dimensions of Container 200 public Dimension getPreferredSize() 201 { 202 returnnew Dimension( 500, 500 ); 203 } 204 205 // return minimum size of Container 206 public Dimension getMinimumSize() 207 { Fig. 4.15 Creating a Java 3D SimpleUniverse with content (part 6).

  42. 208 return getPreferredSize(); 209 } 210 } Fig. 4.15 Creating a Java 3D SimpleUniverse with content (part 7).

  43. 4.4.3 A Java 3D Example (cont.) Fig. 4.16 Demonstrating MouseRotate behavior.

  44. 4.4.3 A Java 3D Example (cont.) Fig. 4.17 Demonstrating MouseTranslate behavior.

  45. 4.4.3 A Java 3D Example (cont.) Fig. 4.18 Demonstrating MouseZoom behavior.

  46. 4.4.3 A Java 3D Example (cont.) Fig. 4.19 Demonstrating changing color in Java 3D.

  47. 4.4.3 A Java 3D Example (cont.) Fig. 4.20 Demonstrating texture mapping in Java 3D.

  48. Declare three JSliders to change the color (red, green and blue) of the light that hits the 3D shape Declare JCheckBox to enable texture mapping 1 // ControlPanel.java 2 // ControlPanel is a JPanel that contains Swing controls 3 // for manipulating a Java3DWorld. 4 package com.deitel.advjhtp1.java3d; 5 6 // Java core packages 7 import java.awt.*; 8 import java.awt.event.*; 9 10 // Java extension packages 11 import javax.swing.*; 12 import javax.swing.border.*; 13 import javax.swing.event.*; 14 15 publicclass ControlPanel extends JPanel { 16 17 // JSliders control lighting color 18 private JSlider redSlider, greenSlider, blueSlider; 19 20 // JCheckbox turns on texture mapping 21 private JCheckBox textureCheckBox; 22 23 // graphics display environment 24 private Java3DWorld java3DWorld; 25 26 // ControlPanel constructor 27 public ControlPanel( Java3DWorld tempJ3DWorld ) 28 { 29 java3DWorld = tempJ3DWorld; 30 31 // assemble instruction panel 32 JPanel instructionPanel = new JPanel(); 33 34 TitledBorder titledBorder = 35 new TitledBorder( "Transformation Instructions" ); Fig. 4.21 ControlPanel provides Swing controls for Java3DWorld(part 1).Line 18Line 21

  49. Declare JLabels that include program instructions 36 37 titledBorder.setTitleJustification( TitledBorder.CENTER ); 38 instructionPanel.setBorder( titledBorder ); 39 40 JLabel rotationInstructions = 41 new JLabel( "Rotation - Left Mouse Button", 42 SwingConstants.CENTER ); 43 44 JLabel translationInstructions = 45 new JLabel( "Translation - Right Mouse Button", 46 SwingConstants.CENTER ); 47 48 JLabel scalingInstructions = 49 new JLabel( "Scale - Alt + Left Mouse Button", 50 SwingConstants.CENTER ); 51 52 // add instruction JLabels to JPanel 53 instructionPanel.add( rotationInstructions ); 54 instructionPanel.add( translationInstructions ); 55 instructionPanel.add( scalingInstructions ); 56 57 // assemble texture mapping control panel 58 JPanel texturePanel = new JPanel(); 59 60 TitledBorder textureBorder = 61 new TitledBorder( "Texture Controls" ); 62 63 textureBorder.setTitleJustification( TitledBorder.CENTER ); 64 texturePanel.setBorder( textureBorder ); 65 66 textureCheckBox = new JCheckBox( 67 "Apply Texture Map to Image" ); 68 69 texturePanel.add( textureCheckBox ); 70 Fig. 4.21 ControlPanel provides Swing controls for Java3DWorld(part 2).Lines 53-55

  50. When user selects JCheckBox, enable texture mapping 71 // create ItemListener for JCheckBox 72 textureCheckBox.addItemListener( 73 new ItemListener() { 74 75 // invoked when checkbox selected/deselected 76 publicvoid itemStateChanged( ItemEvent event ) 77 { 78 if( event.getStateChange() == ItemEvent.SELECTED ) 79 Java3DWorld.updateTexture( true ); 80 else 81 Java3DWorld.updateTexture( false ); 82 } 83 84 } // end anonymous inner class 85 ); 86 87 // create JPanel with instructionPanel and texturePanel 88 JPanel topPanel = new JPanel( 89 new GridLayout( 2, 1, 0, 20 ) ); 90 91 topPanel.add( instructionPanel ); 92 topPanel.add( texturePanel ); 93 94 // assemble lighting color control panel 95 JPanel colorPanel = new JPanel( 96 new FlowLayout( FlowLayout.LEFT, 15, 15 ) ); 97 98 TitledBorder colorBorder = 99 new TitledBorder( "Direct Lighting Color Controls" ); 100 101 colorBorder.setTitleJustification( TitledBorder.CENTER ); 102 colorPanel.setBorder( colorBorder ); 103 104 JLabel redLabel = new JLabel( "R" ); 105 JLabel greenLabel = new JLabel( "G" ); Fig. 4.21 ControlPanel provides Swing controls for Java3DWorld(part 3).Lines 72-85

More Related