import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class CanvasSample extends Canvas {
public static final int FRAME_WIDTH = 800;
public static final int FRAME_HEIGHT = 600;
public static final int FRAME_X_ORIGIN = 50;
public static final int FRAME_Y_ORIGIN = 50;
private BufferedImage bi;
public BufferStrategy bs;
public float timeElapsed; // # of seconds between iterations of the game loop
public Ball ball;
public boolean up, down, left, right, endGame;
public CanvasSample() {
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setLocation(0, 0);
addKeyListener(new KeyStroke());
}
public static void main(String[] args) {
JFrame j = new JFrame();
Container contentPane = j.getContentPane();
j.setTitle("JButtonEvents");
j.setSize(FRAME_WIDTH, FRAME_HEIGHT);
j.setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
j.setResizable(false);
j.setVisible(true);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CanvasSample c = new CanvasSample();
contentPane.add(c);
c.init();
c.gameLoop();
}
public void init() {
createBufferStrategy(2);
bs = getBufferStrategy();
ball = new Ball(this);
try {
bi = ImageIO.read(new File("peg1.png"));
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "error");
}
}
public void gameLogic() {
ball.update();
}
public void gameLoop() {
long timeBefore = System.currentTimeMillis();
while (!endGame) {
timeElapsed = (System.currentTimeMillis() - timeBefore) / 1000.0f; // Divide to 1000 because you need to convert to seconds
timeBefore = System.currentTimeMillis();
gameLogic();
// put this at the top for clearing screen
Graphics g = bs.getDrawGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, FRAME_WIDTH, FRAME_HEIGHT);
g.setColor(Color.BLACK);
g.fillRect(0, 400 + bi.getHeight(), FRAME_WIDTH, 20);
g.drawImage(bi, (int) ball.x, (int) ball.y, bi.getWidth(), bi.getHeight(), null);
try {
Thread.sleep(10);
} catch (Exception e) {
} finally {
// put this at the bottom for showing
bs.show();
}
}
}
class KeyStroke implements KeyListener {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
endGame = true;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
up = true;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
down = true;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
left = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
right = true;
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
up = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
down = false;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
left = false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
right = false;
}
}
public void keyTyped(KeyEvent e) {
}
}
}
//================================================
//Put this in a separate class
public class Ball {
// Gravitational Acceleration(Pixels / Second ^ 2)
public final static float GRAVITY = 4f;
// Initial Velocity (Pixels / Second)
public final static float JUMP_VELOCITY = 2;
// Move Speed (Pixels / Second)
public final static float MOVE_SPEED = 100;
CanvasSample c;
float x, y; // Position
float vx, vy; // Velocity (Rate at which Position Changes)
public Ball(CanvasSample c) {
this.c = c;
x = 30;
y = 30;
vx = 0;
vy = 0;
}
public void update() {
if (c.up) {
vy = JUMP_VELOCITY;
}
if (c.left) {
x -= MOVE_SPEED * c.timeElapsed;
}
if (c.right) {
x += MOVE_SPEED * c.timeElapsed;
}
vy -= GRAVITY * c.timeElapsed; // FORCE OF GRAVITY
x += vx;
y -= vy; // This is negative because y-axis is inverted.
if (x < 0) x = 0;
if (x > CanvasSample.FRAME_WIDTH) x = 400;
if (y < 0) y = 0;
if (y > 400) y = 400;
}
}