s1ay3r44

Untitled

Oct 17th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.03 KB | None | 0 0
  1. /**
  2.  *
  3.  * Copyright @German Kuznetsov
  4.  */
  5.  
  6. package your.package;
  7.  
  8. import java.awt.Canvas;
  9. import java.awt.Color;
  10. import java.awt.Graphics;
  11. import java.awt.event.KeyEvent;
  12. import java.awt.event.KeyListener;
  13. import java.awt.event.MouseEvent;
  14. import java.awt.event.MouseListener;
  15. import java.awt.event.MouseMotionListener;
  16. import java.awt.image.BufferStrategy;
  17. import javax.swing.JFrame;
  18.  
  19. /**
  20.  *
  21.  * some code ripped from
  22.  * http://www.gamedev.net/page/resources/_/technical/general-programming/java-games-active-rendering-r2418
  23.  */
  24. public class Main {
  25.  
  26.     private static JFrame frame; //Lets define some stuff we will be using
  27.     private static Canvas canvas;
  28.     private static BufferStrategy buffer;
  29.  
  30.     public static final int WIDTH = 600; //Our window size
  31.     public static final int HEIGHT = 400;
  32.  
  33.     public static int MouseX, MouseY; //These variables will hold values for the mouse.
  34.     public static boolean LeftMouseButtonDown, MouseWheelDown, RightMouseButtonDown;
  35.  
  36.     public static final boolean[] keyDown = new boolean[65536]; //This list will hold the position of keys (up/down)
  37.  
  38.     public static void main(String[] args) {
  39.         //create our window
  40.         frame = new JFrame();
  41.         frame.setIgnoreRepaint(true);
  42.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43.  
  44.         //create our canvas to draw on
  45.         canvas = new Canvas();
  46.         canvas.setIgnoreRepaint(true);
  47.         canvas.setSize(WIDTH, HEIGHT);
  48.  
  49.         //pack our canvas into our frame
  50.         frame.add(canvas);
  51.         frame.pack();
  52.         frame.setVisible(true);
  53.  
  54.         //add a mouselistener to our window (mouse button pressed / released)
  55.         canvas.addMouseListener(new MouseListener() {
  56.  
  57.             public void mouseClicked(MouseEvent e) {
  58.             }
  59.  
  60.             public void mousePressed(MouseEvent e) {
  61.                 switch (e.getButton()) {
  62.                     case 1:
  63.                         leftMouseButtonDown = true;
  64.                         break;
  65.                     case 2:
  66.                         mouseWheelDown = true;
  67.                         break;
  68.                     case 3:
  69.                         rightMouseButtonDown = true;
  70.                         break;
  71.                 }
  72.             }
  73.  
  74.             public void mouseReleased(MouseEvent e) {
  75.                 switch (e.getButton()) {
  76.                     case 1:
  77.                         leftMouseButtonDown = true;
  78.                         break;
  79.                     case 2:
  80.                         mouseWheelDown = true;
  81.                         break;
  82.                     case 3:
  83.                         rightMouseButtonDown = true;
  84.                         break;
  85.                 }
  86.             }
  87.  
  88.             public void mouseEntered(MouseEvent e) {
  89.             }
  90.  
  91.             public void mouseExited(MouseEvent e) {
  92.             }
  93.  
  94.         });
  95.        // //add a mouse motion listener to our window (Mouse moved)
  96.         canvas.addMouseMotionListener(new MouseMotionListener() {
  97.  
  98.             @Override
  99.             public void mouseDragged(MouseEvent e) {
  100.                 MouseX = e.getX();
  101.                 MouseY = e.getY();
  102.             }
  103.  
  104.             @Override
  105.             public void mouseMoved(MouseEvent e) {
  106.                 MouseX = e.getX();
  107.                 MouseY = e.getY();
  108.             }
  109.         });
  110.  
  111.        //add Key Listener (Key pressed/ released)
  112.         canvas.addKeyListener(new KeyListener() {
  113.  
  114.             @Override
  115.             public void keyTyped(KeyEvent ke) {
  116.                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  117.             }
  118.  
  119.             @Override
  120.             public void keyPressed(KeyEvent e) {
  121.                 keyDown[e.getKeyCode()] = true;
  122.             }
  123.  
  124.             @Override
  125.             public void keyReleased(KeyEvent e) {
  126.                 keyDown[e.getKeyCode()] = false;
  127.             }
  128.         });
  129. //buffer setup
  130.         canvas.createBufferStrategy(2);
  131.         buffer = canvas.getBufferStrategy();
  132.  
  133.       //our game loop
  134.         Graphics graphics = null;
  135.         while (true) {
  136.             try {
  137.                 update();
  138.                 render(graphics);
  139.                 if (!buffer.contentsLost()) {
  140.                     buffer.show();
  141.                 }
  142.             } finally {
  143.                 if (graphics != null) {
  144.                     graphics.dispose();
  145.                 }
  146.  
  147.             }
  148.  
  149.         }
  150.     }
  151.  
  152.    //RENDER YOUR STUFF HERE
  153.     public static void render(Graphics graphics) {
  154.         graphics = buffer.getDrawGraphics(); //Clears back buffer
  155.  
  156.         graphics.setColor(Color.BLACK);
  157.         graphics.fillRect(0, 0, WIDTH, HEIGHT);
  158.         graphics.setColor(new Color(255, 0, 255));
  159.         graphics.fillRect(MouseX, MouseY, 5, 5);
  160.     }
  161.  
  162.     public static void update() {
  163.  
  164.     }
  165.  
  166.     private void setButton(int button, boolean b) {
  167.         switch (button) {
  168.             case 1:
  169.                 leftMouseButtonDown = b;
  170.                 break;
  171.             case 2:
  172.                 mouseWheelDown = b;
  173.                 break;
  174.             case 3:
  175.                 rightMouseButtonDown = b;
  176.                 break;
  177.         }
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment