1. import java.awt.Canvas;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.GraphicsConfiguration;
  7. import java.awt.GraphicsEnvironment;
  8. import java.awt.Image;
  9. import java.awt.Toolkit;
  10. import java.awt.Transparency;
  11. import java.awt.event.KeyEvent;
  12. import java.awt.event.KeyListener;
  13. import java.awt.event.WindowAdapter;
  14. import java.awt.event.WindowEvent;
  15. import java.awt.image.BufferStrategy;
  16. import java.awt.image.BufferedImage;
  17. import java.awt.image.MemoryImageSource;
  18. import java.net.URL;
  19.  
  20. import javax.swing.ImageIcon;
  21. import javax.swing.JFrame;
  22. import javax.swing.JPanel;
  23.  
  24.  
  25.  
  26.  
  27.  
  28. //****************************************************************************
  29. //
  30. //
  31. //
  32. //****************************************************************************
  33.  
  34. class Constants
  35. {
  36.  
  37.     public static final double FIXED_TIME_STEP = 1.0 / 60.0;
  38.  
  39.     public final static int SCREEN_WIDTH = 640;
  40.     public final static int SCREEN_HEIGHT = 480;
  41.  
  42. }
  43.  
  44.  
  45. //****************************************************************************
  46. //
  47. //
  48. //
  49. //****************************************************************************
  50.  
  51. class Keyboard implements KeyListener
  52. {
  53.     private static Keyboard instance = new Keyboard();
  54.  
  55.     private boolean[] keysPressed = new boolean[256];
  56.     private boolean[] keysReleased = new boolean[256];
  57.    
  58.     private boolean anyKeyPressed = false;
  59.    
  60.     private Keyboard()
  61.     {
  62.        
  63.     }
  64.    
  65.     public static Keyboard getInstance()
  66.     {
  67.         return instance;
  68.     }
  69.    
  70.     @Override
  71.     public void keyPressed( KeyEvent e )
  72.     {
  73.         int keyId = (e.getKeyCode() & 255);
  74.         keysPressed[keyId] = true;
  75.         keysReleased[keyId] = false;
  76.         anyKeyPressed = true;
  77.     }
  78.  
  79.     @Override
  80.     public void keyReleased( KeyEvent e )
  81.     {
  82.         int keyId = (e.getKeyCode() & 255);
  83.         keysPressed[keyId] = false;
  84.         keysReleased[keyId] = true;
  85.         anyKeyPressed = false;
  86.     }
  87.  
  88.     @Override
  89.     public void keyTyped( KeyEvent e )
  90.     {
  91.        
  92.     }
  93.    
  94.     public boolean isKeyPressed( int key )
  95.     {
  96.         return keysPressed[key];
  97.     }
  98.    
  99.     public boolean isKeyReleased( int key )
  100.     {
  101.         return keysReleased[key];
  102.     }
  103.    
  104.     public boolean isAnyKeyPressed()
  105.     {
  106.         return anyKeyPressed;
  107.     }
  108.    
  109.     public void resetKeys()
  110.     {
  111.         for( int i = 0; i < 256; i++ )
  112.         {
  113.             keysReleased[i]= false;
  114.         }
  115.     }
  116.    
  117.    
  118. }
  119.  
  120.  
  121. //****************************************************************************
  122. //
  123. //
  124. //
  125. //****************************************************************************
  126.  
  127. class Game
  128. {
  129.  
  130.     private double accumulator = 0;
  131.  
  132.     private int fps = 0;
  133.     private int framesPerSecond = 0;
  134.     private double previousTime = 0;
  135.     private double oldTime = 0;
  136.     private double secondsElapsed = 0;
  137.  
  138.     int[] pixels = new int[ Constants.SCREEN_WIDTH * Constants.SCREEN_HEIGHT ];
  139.     MemoryImageSource renderBufferSource = new MemoryImageSource( Constants.SCREEN_WIDTH,
  140.                                                                   Constants.SCREEN_HEIGHT,
  141.                                                                   pixels,
  142.                                                                   0,
  143.                                                                   Constants.SCREEN_WIDTH );
  144.  
  145.     Image renderBuffer;
  146.    
  147.     public Game()
  148.     {
  149.        
  150.     }
  151.  
  152.     public void init( BufferStrategy strategy, Screen screen )
  153.     {
  154.          
  155.         Graphics2D g2D = (Graphics2D) strategy.getDrawGraphics();
  156.         g2D.setColor(Color.BLACK);
  157.         g2D.fillRect(0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
  158.  
  159.         Toolkit.getDefaultToolkit().sync();
  160.  
  161.         strategy.show();
  162.  
  163.  
  164.         g2D.dispose();
  165.         strategy.show();
  166.        
  167.         for( int y = 0; y < Constants.SCREEN_HEIGHT; y++ )
  168.         {
  169.             for( int x = 0; x < Constants.SCREEN_WIDTH; x++ )
  170.             {
  171.                 pixels[x + y * Constants.SCREEN_WIDTH] = (x*x) ^ (y*y);
  172.             }
  173.            
  174.         }
  175.        
  176.        
  177.         renderBufferSource.setAnimated( true );
  178.         renderBuffer = Toolkit.getDefaultToolkit().createImage(renderBufferSource);
  179.  
  180.         renderBufferSource.newPixels();
  181.        
  182.     }
  183.    
  184.     private void update()
  185.     {
  186.  
  187.        
  188.         Keyboard.getInstance().resetKeys();
  189.        
  190.        
  191.     }
  192.    
  193.    
  194.     private void render( Graphics2D g2D, Screen screen )
  195.     {
  196.  
  197.         for( int y = 0; y < Constants.SCREEN_HEIGHT; y++ )
  198.         {
  199.             for( int x = 0; x < Constants.SCREEN_WIDTH; x++ )
  200.             {
  201.                 pixels[x + y * Constants.SCREEN_WIDTH] = (x*x*x) ^ (y*y*y);
  202.             }
  203.            
  204.         }
  205.        
  206.         renderBufferSource.newPixels();
  207.        
  208.         g2D.drawImage( renderBuffer,
  209.                        0,
  210.                        0,
  211.                        screen );
  212.  
  213.        
  214.     }
  215.  
  216.  
  217.     public void run( BufferStrategy strategy, Screen screen )
  218.     {
  219.  
  220.         double dt = getDeltaTime(getSystemTime());
  221.        
  222.         init( strategy, screen  );
  223.        
  224.         while (true)
  225.         {
  226.  
  227.             dt = getDeltaTime(getSystemTime());
  228.             if( dt > Constants.FIXED_TIME_STEP ) dt = Constants.FIXED_TIME_STEP;
  229.            
  230.             accumulator += dt;
  231.             secondsElapsed += dt;
  232.            
  233.             while( accumulator >= Constants.FIXED_TIME_STEP )
  234.             {
  235.  
  236.                 accumulator -= Constants.FIXED_TIME_STEP;
  237.                 update();
  238.                                
  239.             }
  240.  
  241.            
  242.             Graphics2D g2D = (Graphics2D) strategy.getDrawGraphics();
  243.             g2D.setColor(Color.BLACK);
  244.             g2D.fillRect(0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
  245.  
  246.             render( g2D, screen );
  247.             g2D.dispose();
  248.             strategy.show();
  249.             Toolkit.getDefaultToolkit().sync();
  250.            
  251.  
  252.             if( Keyboard.getInstance().isKeyPressed( KeyEvent.VK_ESCAPE ) )
  253.             {
  254.                 System.exit(0);
  255.             }
  256.            
  257.             try
  258.             {
  259.                 Thread.sleep(1);
  260.             }
  261.             catch (InterruptedException e)
  262.             {
  263.                 System.out.println("interrupted");
  264.             }
  265.  
  266.         }
  267.  
  268.     }
  269.  
  270.    
  271.     public Keyboard getKeyHandler()
  272.     {
  273.         return Keyboard.getInstance();
  274.     }
  275.    
  276.    
  277.     public double getSystemTime()
  278.     {
  279.         return System.nanoTime() / 1000000000.0;
  280.     }
  281.  
  282.     public double getDeltaTime( double timerInSeconds )
  283.     {
  284.  
  285.         double currentTime = timerInSeconds;
  286.         double elapsedTime = currentTime - oldTime;
  287.         oldTime = currentTime;
  288.  
  289.         framesPerSecond++;
  290.  
  291.         if ((currentTime - previousTime) > 1.0)
  292.         {
  293.             previousTime = currentTime;
  294.             fps = framesPerSecond;
  295.             framesPerSecond = 0;
  296.         }
  297.  
  298.         return elapsedTime;
  299.     }
  300.  
  301.        
  302. }  // end class
  303.  
  304.  
  305. //****************************************************************************
  306. //
  307. //
  308. //
  309. //****************************************************************************
  310.  
  311. class Screen extends Canvas
  312. {
  313.  
  314.     private static final long serialVersionUID = 1L;
  315.  
  316.     private BufferStrategy strategy;
  317.    
  318.     Game engine = new Game();
  319.    
  320.     public Screen()
  321.     {
  322.  
  323.         JFrame container = new JFrame("Java2D GFX Framework by Relminator");
  324.  
  325.         JPanel panel = (JPanel) container.getContentPane();
  326.         panel.setPreferredSize(new Dimension(Constants.SCREEN_WIDTH,
  327.                 Constants.SCREEN_HEIGHT));
  328.         panel.setLayout(null);
  329.  
  330.         setBounds(0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
  331.  
  332.         panel.add(this);
  333.  
  334.         setIgnoreRepaint(true);
  335.  
  336.         container.pack();
  337.         container.setLocationRelativeTo(null);
  338.         container.setResizable(false);
  339.         container.setVisible(true);
  340.  
  341.         container.addWindowListener(new WindowAdapter()
  342.         {
  343.             public void windowClosing(WindowEvent e)
  344.             {
  345.                 System.exit(0);
  346.             }
  347.         });
  348.  
  349.         addKeyListener( engine.getKeyHandler() );
  350.  
  351.         requestFocus();
  352.         setFocusable(true);
  353.        
  354.         createBufferStrategy(2);
  355.         strategy = getBufferStrategy();
  356.        
  357.        
  358.     }
  359.  
  360.     public void playGame()
  361.     {
  362.  
  363.         engine.run( strategy, this );
  364.        
  365.     }
  366.  
  367. }
  368.  
  369.  
  370. //****************************************************************************
  371. //
  372. // Main Entry Point
  373. //
  374. //****************************************************************************
  375.  
  376.  
  377. public class XorTexture
  378. {
  379.  
  380.     public static void main(String[] args)
  381.     {
  382.  
  383.         Screen screen = new Screen();
  384.         screen.playGame();
  385.    
  386.     }
  387.  
  388. }