Advertisement
Luninariel

289 - Volatile Image Renderer

Apr 8th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.86 KB | None | 0 0
  1. //
  2. //  VolatileImageRenderer.java
  3. //  SwingEasel
  4. //
  5. //  Created by Philip Rhodes on 8/20/05.
  6. //  Copyright 2005 Philip J Rhodes. All rights reserved.
  7. //
  8.  
  9. import java.awt.*;
  10. import java.awt.image.*;
  11.  
  12. /** This abstract class provides a VolatileImage for double buffering. Although
  13.  *  Swing provides double buffering by default, using a VolatileImage allows
  14.  *  the Java Virtual Machine (JVM) to take advantage of hardware acceleration.
  15.  *  Even if a particular JVM doesn't do this, or if there is no 3D graphics card
  16.  *  installed, there may still be an advantage to using an image, because each
  17.  *  drawing operation may be slightly faster.
  18.  */
  19. public abstract class VolatileImageRenderer extends ImageRenderer{
  20.  
  21.    /** Create a VolatileImageRenderer to render the shapes in the World w.*/
  22.    VolatileImageRenderer(World w, int width, int height, boolean showFPS){
  23.      
  24.       super(w, width, height, showFPS);
  25.    }
  26.    
  27.    /** Creates the Volatile image and associated Graphics object. The image
  28.     *  takes its dimensions from the size of the JPanel. (Remember that this
  29.     *  class is a child of JPanel.  IMPORTANT NOTE: The JPanel must be visible
  30.     *  when this method is called. So, call setVisible(true) BEFORE calling
  31.     *  init(). Even if you do this, there may be a race condition if your JVM
  32.     *  uses a separate thread to schedule repaints. Because of this, the init()
  33.     *  method will loop until it succeeds in creating a volatile image.
  34.     */
  35.    public void init(){
  36.  
  37.       do {
  38.      
  39.          this.backingImage = createVolatileImage(this.imageWidth, this.imageHeight);
  40.          
  41.       } while(this.backingImage == null);
  42.    
  43.       this.imageGraphics = this.backingImage.getGraphics();
  44.    }
  45.  
  46.  
  47.    public void render(){
  48.      
  49.       if(this.imageGraphics == null){
  50.          
  51.          this.init();
  52.       }
  53.      
  54.    }
  55.  
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement