Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.maineri.mainepix;
- import java.awt.*;
- import java.awt.image.BufferStrategy;
- import java.awt.image.BufferedImage;
- import java.awt.event.*;
- import java.io.File;
- import javax.imageio.ImageIO;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- public class GamePanel extends Canvas
- implements Runnable, KeyListener{
- private static final long serialVersionUID = -2342164197569319147L;
- // game thread
- private Thread thread;
- private boolean running;
- private int FPS = 60;
- private long targetTime = 1000 / FPS;
- // image
- private BufferedImage image;
- private Graphics2D g;
- // game state manager
- private GameStateManager gsm;
- public GamePanel() {
- super();
- setFocusable(true);
- requestFocus();
- }
- public void addNotify() {
- super.addNotify();
- if(thread == null) {
- thread = new Thread(this, "Display");
- addKeyListener(this);
- thread.start();
- }
- }
- public static class HandlerClass implements MouseListener, MouseMotionListener {
- public void mouseDragged(MouseEvent me) {
- }
- public void mouseMoved(MouseEvent me) {
- }
- public void mouseClicked(MouseEvent me) {
- System.exit(0);
- }
- public void mouseEntered(MouseEvent me) {
- }
- public void mouseExited(MouseEvent me) {
- System.exit(0);
- }
- public void mousePressed(MouseEvent me) {
- System.exit(0);
- }
- public void mouseReleased(MouseEvent me) {
- System.exit(0);
- }
- }
- static BufferedImage logBox;
- private void init() {
- JPanel jp = new JPanel();
- JLabel jl = new JLabel();
- HandlerClass handler = new HandlerClass();
- jl.addMouseListener(handler);
- jl.addMouseMotionListener(handler);
- jp.addMouseListener(handler);
- jp.addMouseMotionListener(handler);
- Game gam = new Game();
- gam.frame.addMouseListener(handler);
- gam.frame.addMouseMotionListener(handler);
- try {
- logBox = ImageIO.read(new File(getClass().getResource("LogginBox.png").toURI()));
- }catch(Exception E) {
- E.printStackTrace();
- }
- running = true;
- gsm = new GameStateManager();
- }
- public void run() {
- init();
- long start;
- long elapsed;
- long wait;
- // game loop
- while(running) {
- start = System.nanoTime();
- update();
- draw();
- elapsed = System.nanoTime() - start;
- wait = targetTime - elapsed / 1000000;
- if(wait < 0) wait = 5;
- try {
- Thread.sleep(wait);
- }
- catch(Exception e) {
- e.printStackTrace();
- }
- System.out.println(FPS);
- }
- }
- private void update() {
- gsm.update();
- }
- private void draw() {
- BufferStrategy bs = getBufferStrategy();
- if(bs == null) {
- createBufferStrategy(3);
- return;
- }
- Font font1 = new Font("Serif", Font.BOLD, 90);
- Font font2 = new Font("Serif", Font.BOLD, 12);
- Graphics g2 = (Graphics2D) bs.getDrawGraphics();
- if(gsm.currentState == 1) {
- //GAMESTATE
- }
- if(gsm.currentState == 0) {
- //MENUSTATE
- g2.setColor(Color.green);
- g2.fillRect(0, 0, 600, 400);
- g2.setColor(Color.gray);
- g2.setFont(font2);
- g2.drawString("Copyright Maineri ©", 2, 10);
- }
- /*if(MenuState == true) {
- }
- if(MenuState == true) {
- }
- if(MenuState == true) {
- }
- */
- g2.dispose();
- bs.show();
- }
- public void keyTyped(KeyEvent key) {}
- public void keyPressed(KeyEvent key) {
- gsm.keyPressed(key.getKeyCode());
- }
- public void keyReleased(KeyEvent key) {
- gsm.keyReleased(key.getKeyCode());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement