far_light

Window.java

Nov 15th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. package com.vladislav;
  2.  
  3. // Import libraries for create UI and event
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7.  
  8. // Extends JFrame, contain game's field and realized listen keyboard
  9. public class Window extends JFrame {
  10.     Field field;
  11.  
  12.     // Listen keyboard and move cup
  13.     private class Key implements KeyListener {
  14.         public void keyPressed(KeyEvent e) {
  15.             int key = e.getKeyCode();
  16.  
  17.             if (key == 27) {
  18.                 // Close window
  19.                 System.exit(0);
  20.             } else if (key == 37) {
  21.                 // Left
  22.                 if (field.x - 30 > -48)
  23.                     field.x -= 30;
  24.                 else
  25.                     field.x = 752;
  26.             } else if (key == 39) {
  27.                 // Right
  28.                 if (field.x + 30 < 752)
  29.                     field.x += 30;
  30.                 else
  31.                     field.x = -48;
  32.             }
  33.         }
  34.  
  35.         public void keyReleased(KeyEvent e) {
  36.  
  37.         }
  38.  
  39.         public void keyTyped(KeyEvent e) {
  40.  
  41.         }
  42.     }
  43.  
  44.     public Window(int hard) {
  45.         // Create field with hard
  46.         field = new Field(hard);
  47.  
  48.         // Add listener and set focus
  49.         addKeyListener(new Key());
  50.         setFocusable(true);
  51.  
  52.         // Add field - main body for drawing
  53.         Container container = getContentPane();
  54.         container.add(field);
  55.  
  56.         // Set window (bounds, title, visible)
  57.         setBounds(100, 100, 800, 600);
  58.         setTitle("Java Application");
  59.         setVisible(true);
  60.     }
  61. }
Add Comment
Please, Sign In to add comment