import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.MemoryImageSource;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
//****************************************************************************
//
//
//
//****************************************************************************
class Constants
{
public static final double FIXED_TIME_STEP = 1.0 / 60.0;
public final static int SCREEN_WIDTH = 640;
public final static int SCREEN_HEIGHT = 480;
}
//****************************************************************************
//
//
//
//****************************************************************************
class Keyboard implements KeyListener
{
private static Keyboard instance = new Keyboard();
private boolean[] keysPressed = new boolean[256];
private boolean[] keysReleased = new boolean[256];
private boolean anyKeyPressed = false;
private Keyboard()
{
}
public static Keyboard getInstance()
{
return instance;
}
@Override
public void keyPressed( KeyEvent e )
{
int keyId = (e.getKeyCode() & 255);
keysPressed[keyId] = true;
keysReleased[keyId] = false;
anyKeyPressed = true;
}
@Override
public void keyReleased( KeyEvent e )
{
int keyId = (e.getKeyCode() & 255);
keysPressed[keyId] = false;
keysReleased[keyId] = true;
anyKeyPressed = false;
}
@Override
public void keyTyped( KeyEvent e )
{
}
public boolean isKeyPressed( int key )
{
return keysPressed[key];
}
public boolean isKeyReleased( int key )
{
return keysReleased[key];
}
public boolean isAnyKeyPressed()
{
return anyKeyPressed;
}
public void resetKeys()
{
for( int i = 0; i < 256; i++ )
{
keysReleased[i]= false;
}
}
}
//****************************************************************************
//
//
//
//****************************************************************************
class Game
{
private double accumulator = 0;
private int fps = 0;
private int framesPerSecond = 0;
private double previousTime = 0;
private double oldTime = 0;
private double secondsElapsed = 0;
int[] pixels = new int[ Constants.SCREEN_WIDTH * Constants.SCREEN_HEIGHT ];
MemoryImageSource renderBufferSource = new MemoryImageSource( Constants.SCREEN_WIDTH,
Constants.SCREEN_HEIGHT,
pixels,
0,
Constants.SCREEN_WIDTH );
Image renderBuffer;
public Game()
{
}
public void init( BufferStrategy strategy, Screen screen )
{
Graphics2D g2D = (Graphics2D) strategy.getDrawGraphics();
g2D.setColor(Color.BLACK);
g2D.fillRect(0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
Toolkit.getDefaultToolkit().sync();
strategy.show();
g2D.dispose();
strategy.show();
for( int y = 0; y < Constants.SCREEN_HEIGHT; y++ )
{
for( int x = 0; x < Constants.SCREEN_WIDTH; x++ )
{
pixels[x + y * Constants.SCREEN_WIDTH] = (x*x) ^ (y*y);
}
}
renderBufferSource.setAnimated( true );
renderBuffer = Toolkit.getDefaultToolkit().createImage(renderBufferSource);
renderBufferSource.newPixels();
}
private void update()
{
Keyboard.getInstance().resetKeys();
}
private void render( Graphics2D g2D, Screen screen )
{
for( int y = 0; y < Constants.SCREEN_HEIGHT; y++ )
{
for( int x = 0; x < Constants.SCREEN_WIDTH; x++ )
{
pixels[x + y * Constants.SCREEN_WIDTH] = (x*x*x) ^ (y*y*y);
}
}
renderBufferSource.newPixels();
g2D.drawImage( renderBuffer,
0,
0,
screen );
}
public void run( BufferStrategy strategy, Screen screen )
{
double dt = getDeltaTime(getSystemTime());
init( strategy, screen );
while (true)
{
dt = getDeltaTime(getSystemTime());
if( dt > Constants.FIXED_TIME_STEP ) dt = Constants.FIXED_TIME_STEP;
accumulator += dt;
secondsElapsed += dt;
while( accumulator >= Constants.FIXED_TIME_STEP )
{
accumulator -= Constants.FIXED_TIME_STEP;
update();
}
Graphics2D g2D = (Graphics2D) strategy.getDrawGraphics();
g2D.setColor(Color.BLACK);
g2D.fillRect(0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
render( g2D, screen );
g2D.dispose();
strategy.show();
Toolkit.getDefaultToolkit().sync();
if( Keyboard.getInstance().isKeyPressed( KeyEvent.VK_ESCAPE ) )
{
System.exit(0);
}
try
{
Thread.sleep(1);
}
catch (InterruptedException e)
{
System.out.println("interrupted");
}
}
}
public Keyboard getKeyHandler()
{
return Keyboard.getInstance();
}
public double getSystemTime()
{
return System.nanoTime() / 1000000000.0;
}
public double getDeltaTime( double timerInSeconds )
{
double currentTime = timerInSeconds;
double elapsedTime = currentTime - oldTime;
oldTime = currentTime;
framesPerSecond++;
if ((currentTime - previousTime) > 1.0)
{
previousTime = currentTime;
fps = framesPerSecond;
framesPerSecond = 0;
}
return elapsedTime;
}
} // end class
//****************************************************************************
//
//
//
//****************************************************************************
class Screen extends Canvas
{
private static final long serialVersionUID = 1L;
private BufferStrategy strategy;
Game engine = new Game();
public Screen()
{
JFrame container = new JFrame("Java2D GFX Framework by Relminator");
JPanel panel = (JPanel) container.getContentPane();
panel.setPreferredSize(new Dimension(Constants.SCREEN_WIDTH,
Constants.SCREEN_HEIGHT));
panel.setLayout(null);
setBounds(0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
panel.add(this);
setIgnoreRepaint(true);
container.pack();
container.setLocationRelativeTo(null);
container.setResizable(false);
container.setVisible(true);
container.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
addKeyListener( engine.getKeyHandler() );
requestFocus();
setFocusable(true);
createBufferStrategy(2);
strategy = getBufferStrategy();
}
public void playGame()
{
engine.run( strategy, this );
}
}
//****************************************************************************
//
// Main Entry Point
//
//****************************************************************************
public class XorTexture
{
public static void main(String[] args)
{
Screen screen = new Screen();
screen.playGame();
}
}