Advertisement
Guest User

CanvasTest

a guest
Jan 6th, 2013
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. /*
  2.  * This work is licensed under a Creative Commons Attribution 3.0 Unported License, available
  3.  * at http://creativecommons.org/licenses/by/3.0/
  4.  *
  5.  * Please attribute by not editing the @author lines in any location on the source code.
  6.  * Last modified 1/6/2013
  7.  */
  8. import java.awt.Canvas;
  9. import java.awt.Color;
  10. import java.awt.Graphics;
  11. import java.awt.image.BufferStrategy;
  12.  
  13. /**
  14.  * A basic canvas class with a double buffering strategy
  15.  * @author /u/Tjstretchalot
  16.  */
  17. public class CanvasTest extends Canvas {
  18.    
  19.     /**
  20.      * Creates the canvas
  21.      * @param doubleBuf double buffering enabled
  22.      */
  23.     public CanvasTest() {
  24.        
  25.     }
  26.    
  27.     public void setDoubleBuffered(boolean doubleBuf) {
  28.         if(doubleBuf) {
  29.             createBufferStrategy(2);
  30.         }else {
  31.             createBufferStrategy(1);
  32.         }
  33.     }
  34.    
  35.     /**
  36.      * Draw a red rectangle
  37.      * @param g the graphics
  38.      */
  39.     public void paint(Graphics g) {
  40.         g.setColor(Color.RED);
  41.         g.fillRect(100, 100, 100, 100);
  42.     }
  43.  
  44.     public void render() {
  45.         BufferStrategy bs= getBufferStrategy();
  46.         Graphics g = bs.getDrawGraphics();
  47.         paint(g);
  48.         g.dispose();
  49.         bs.show();
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement