Advertisement
iAndr0idOs

Game.java

Jul 10th, 2011
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  1. package clientApplet;
  2.  
  3. import java.awt.AlphaComposite;
  4. import java.awt.Color;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. import javax.swing.JPanel;
  13. import javax.swing.JScrollPane;
  14. import javax.swing.JTextArea;
  15. import javax.swing.JTextField;
  16. import javax.swing.SwingUtilities;
  17.  
  18. public class Game extends JPanel
  19. {
  20.  
  21.     private static final long serialVersionUID = 1L;
  22.    
  23.     public static int fps = 60;
  24.    
  25.     KeyboardEvents keyEvents;
  26.    
  27.     public static JTextField chatBox;
  28.     public static JTextArea chatRecords;
  29.    
  30.     public Main main;
  31.    
  32.     public static List<Integer> keys;
  33.     public static List<Integer> keysPrev;
  34.    
  35.    
  36.     public Game(Main main) {
  37.         this.main = main;
  38.        
  39.         keyEvents = main.keyEvents;
  40.        
  41.         setFocusable(true);
  42.         addKeyListener(keyEvents);
  43.        
  44.         setLayout(null);
  45.        
  46.         setBounds(0,0,900,600);
  47.         setBackground(Color.WHITE);
  48.        
  49.         (new Thread(new Runnable() {
  50.             public void run() {
  51.                 runThread();
  52.             }
  53.         })).start();
  54.     }
  55.    
  56.     public void runThread() {
  57.         try {
  58.             fStart();
  59.             while(true) {
  60.                
  61.                 keys = arr2List(keyEvents.getKeys());
  62.                 fUpdate();
  63.                 keysPrev = keys;
  64.                
  65.                 repaint();
  66.                
  67.                 Thread.sleep(1000/fps);
  68.             }
  69.         } catch(Exception ex) {
  70.             ex.printStackTrace();
  71.         }
  72.     }
  73.    
  74.     public void paintComponent(final Graphics g) {
  75.         super.paintComponent(g);
  76.         fDraw(g);
  77.     }
  78.    
  79.     public static String chatString = "";
  80.    
  81.     public void fStart() {
  82.         SwingUtilities.invokeLater(new Runnable() {
  83.             public void run() {
  84.                 createGUI();
  85.             }
  86.         });
  87.     }
  88.    
  89.     public void createGUI() {
  90.         //chatBox
  91.         chatBox = new JTextField();
  92.         chatBox.setBounds(6,580,230,20);
  93.         chatBox.setOpaque(false);
  94.         chatBox.setBorder(null);
  95.         chatBox.addActionListener(new ActionListener() {
  96.             public void actionPerformed(ActionEvent e) {
  97.                 //If chatBox is Empty
  98.                 if(chatBox.getText().isEmpty()) {
  99.                     //Hide it
  100.                     chatBox.setVisible(false);
  101.                 }else {
  102.                     //Send the message and clear it
  103.                     chatBox.setText("");
  104.                 }
  105.             }
  106.         });
  107.         add(chatBox);
  108.                
  109.         //chatRecords
  110.         chatRecords = new JTextArea("HI\nHI");
  111.         chatRecords.setEditable(true);
  112.         chatRecords.setOpaque(false);
  113.         JScrollPane chatScroll = new JScrollPane(chatRecords);
  114.         chatRecords.setBounds(0,360,240,240);
  115.         chatScroll.setOpaque(false);
  116.         chatScroll.setBorder(null);
  117.         add(chatRecords);
  118.     }
  119.    
  120.     public void fDraw(Graphics g) {
  121.         Graphics2D g2D = (Graphics2D) g;
  122.         //If chatBox is Visible
  123.         if(chatBox.isVisible()) {
  124.             //Chat Records
  125.             g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .4f));
  126.             g2D.setColor(Color.GRAY);
  127.             g2D.fillRoundRect(0,360,240,240,10,10);
  128.             //Outline of Chat Area
  129.             g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
  130.             g2D.setColor(Color.BLACK);
  131.             g2D.drawRoundRect(0,360,240,239,10,10);
  132.             //Chat Box
  133.             g2D.setColor(Color.WHITE);
  134.             g2D.fillRoundRect(1,580,239,25,10,10);
  135.             g2D.setColor(Color.BLACK);
  136.             g2D.drawRoundRect(0,580,240,25,10,10);
  137.         }else
  138.         {
  139.             //Chat Records
  140.             g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .4f));
  141.             g2D.setColor(Color.GRAY);
  142.             g2D.fillRect(0,480,240,120);
  143.             //Outline of Chat Area
  144.             g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
  145.             g2D.setColor(Color.BLACK);
  146.             g2D.drawRect(0,480,240,120);
  147.         }
  148.     }
  149.    
  150.     public void fUpdate() {
  151.         //If Enter Key is Pressed
  152.         if(keyPressed(10)) {
  153.             //If chatBox is hidden
  154.             if(!chatBox.isVisible()) {
  155.                 //Show it and give it focus
  156.                 chatBox.setVisible(true);
  157.                 chatBox.requestFocus();
  158.             }
  159.         }
  160.     }
  161.    
  162.     //---KeyEvents---
  163.     public boolean keyPressed(int key) {
  164.         return (keys.contains(key) && !keysPrev.contains(key));
  165.     }
  166.    
  167.     public boolean keyDown(int key) {
  168.         return keys.contains(key);
  169.     }
  170.    
  171.     public boolean keyReleased(int key) {
  172.         return (!keys.contains(key) && keysPrev.contains(key));
  173.     }
  174.    
  175.     //---Misc Functions---
  176.     public List<Integer> arr2List(Integer[] array) {
  177.         List<Integer> objList = new ArrayList<Integer>();
  178.         for (int index = 0; index < array.length; index++)
  179.         {
  180.             objList.add(array[index]);
  181.         }
  182.         return objList;
  183.     }
  184.    
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement