/**
*
* Copyright @German Kuznetsov
*/
package your.package;
import java.awt.Canvas;
import java.awt.Color;
import
java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
/**
*
* some code ripped from
*
http://www.gamedev.net/page/resources/_/technical/general-programming/java-games-active-rendering-r2418 */
public class Main {
private static JFrame frame; //Lets define some stuff we will be using
private static Canvas canvas;
private static BufferStrategy buffer;
public static final int WIDTH = 600; //Our window size
public static final int HEIGHT = 400;
public static int MouseX, MouseY; //These variables will hold values for the mouse.
public static boolean LeftMouseButtonDown, MouseWheelDown, RightMouseButtonDown;
public static final boolean[] keyDown = new boolean[65536]; //This list will hold the position of keys (up/down)
public static void main(String[] args) {
//create our window
frame = new JFrame();
frame.setIgnoreRepaint(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create our canvas to draw on
canvas = new Canvas();
canvas.setIgnoreRepaint(true);
canvas.setSize(WIDTH, HEIGHT);
//pack our canvas into our frame
frame.add(canvas);
frame.pack();
frame.setVisible(true);
//add a mouselistener to our window (mouse button pressed / released)
canvas.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
switch (e.getButton()) {
case 1:
LeftMouseButtonDown = true;
break;
case 2:
MouseWheelDown = true;
break;
case 3:
RightMouseButtonDown = true;
break;
}
}
public void mouseReleased(MouseEvent e) {
switch (e.getButton()) {
case 1:
LeftMouseButtonDown = true;
break;
case 2:
MouseWheelDown = true;
break;
case 3:
RightMouseButtonDown = true;
break;
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
});
// //add a mouse motion listener to our window (Mouse moved)
canvas.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
MouseX = e.getX();
MouseY = e.getY();
}
@Override
public void mouseMoved(MouseEvent e) {
MouseX = e.getX();
MouseY = e.getY();
}
});
//add Key Listener (Key pressed/ released)
canvas.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent ke) {
}
@Override
public void keyPressed(KeyEvent e) {
keyDown[e.getKeyCode()] = true;
}
@Override
public void keyReleased(KeyEvent e) {
keyDown[e.getKeyCode()] = false;
}
});
//buffer setup
canvas.createBufferStrategy(2);
buffer = canvas.getBufferStrategy();
//our game loop
Graphics graphics = null;
while (true) {
try {
update();
render(graphics);
if (!buffer.contentsLost()) {
buffer.show();
}
} finally {
if (graphics != null) {
graphics.dispose();
}
}
}
}
//RENDER YOUR STUFF HERE
public static void render(Graphics graphics) {
graphics = buffer.getDrawGraphics(); //Clears back buffer
graphics.setColor(Color.BLACK);
graphics.fillRect(0, 0, WIDTH, HEIGHT);
graphics.setColor(new Color(255, 0, 255));
graphics.fillRect(MouseX, MouseY, 5, 5);
}
public static void update() {
}
}