Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.85 KB | None | 0 0
  1. package storkfrogfly;
  2.  
  3. import animal.Animal;
  4. import animal.Frog;
  5. import animal.Stork;
  6. import animal.Fly;
  7. import animal.Position;
  8. import java.util.ArrayList;
  9. import areas.Field;
  10. import java.awt.Container;
  11. import java.awt.Dimension;
  12. import java.awt.Insets;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15. import javax.swing.Icon;
  16. import javax.swing.ImageIcon;
  17. import javax.swing.JFrame;
  18. import javax.swing.JLabel;
  19.  
  20. public class MainWindow extends javax.swing.JFrame {
  21.    
  22.     private final static int HEIGHT;
  23.     private final static int WIDTH;
  24.     private final static Position STARTING_POS_OF_STORK;
  25.     private final static Position STARTING_POS_OF_FROG;
  26.     private final static Position STARTING_POS_OF_FLY;
  27.    
  28.     static{
  29.         HEIGHT = 9;
  30.         WIDTH = 9;
  31.         STARTING_POS_OF_STORK = new Position(0, 0);
  32.         STARTING_POS_OF_FROG = new Position(HEIGHT-1, WIDTH-1);
  33.         STARTING_POS_OF_FLY = new Position(HEIGHT/2, WIDTH/2);
  34.     }
  35.  
  36.     public MainWindow() {
  37.     }
  38.    
  39.     public static JLabel[][] addComponentsToPane(Container pane) {
  40.         pane.setLayout(null);
  41.         JLabel[][] labels = new JLabel[HEIGHT][WIDTH];
  42.         for (int i = 0; i < HEIGHT; i++) {
  43.             for (int j = 0; j < WIDTH; j++) {
  44.                 JLabel label = new JLabel("hali");
  45.                 labels[i][j] = label;
  46.                 pane.add(label);
  47.             }
  48.         }
  49.         Insets insets = pane.getInsets();
  50.         int y = 0;
  51.         for (int i = 0; i < HEIGHT; i++) {
  52.             int x = 0;
  53.             for (int j = 0; j < WIDTH; j++) {
  54.                 Dimension size = labels[i][j].getPreferredSize();
  55.                 labels[i][j].setBounds(x + insets.left, y + insets.top, 50, 50);
  56.                 x += 50;
  57.             }
  58.             y += 50;
  59.         }
  60.         return labels;
  61.     }
  62.    
  63.     private static void createAndShowGUI() {
  64.         //Create and set up the window.
  65.         JFrame frame = new JFrame("MainWindow");
  66.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  67.  
  68.         //Set up the content pane.
  69.         JLabel[][] labels = addComponentsToPane(frame.getContentPane());
  70.  
  71.         //Size and display the window.
  72.         Insets insets = frame.getInsets();
  73.         frame.setSize((50*WIDTH+10) + insets.left,
  74.                       (50*HEIGHT+40) + insets.top);
  75.         frame.setVisible(true);
  76.         try {
  77.             playGame(labels);
  78.         } catch (InterruptedException ex) {
  79.             Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
  80.         }
  81.     }
  82.    
  83.     public static void playGame(JLabel[][] labels) throws InterruptedException{
  84.         new Thread(new Runnable(){
  85.             @Override
  86.             public void run() {
  87.                 ArrayList<Animal> animals = new ArrayList<>();
  88.                 Animal a = new Fly("L", STARTING_POS_OF_FLY);
  89.                 animals.add(a);
  90.                 a = new Frog("B", STARTING_POS_OF_FROG);
  91.                 animals.add(a);
  92.                 a = new Stork("G", STARTING_POS_OF_STORK);
  93.                 animals.add(a);
  94.                 Icon storkIcon = new ImageIcon("stork.jpg");
  95.                 Icon bloodyStorkIcon = new ImageIcon("bloodystork.jpg");
  96.                 Icon frogIcon = new ImageIcon("frog.jpg");
  97.                 Icon flyIcon = new ImageIcon("fly.jpg");
  98.                 Icon grassIcon = new ImageIcon("grass.jpg");
  99.                 Field fieldInstance = new Field(animals, HEIGHT, WIDTH);
  100.                 while(fieldInstance.moveTo()){
  101.                     try {
  102.                         Thread.sleep(400);
  103.                     } catch (InterruptedException ex) {
  104.                         Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
  105.                     }
  106.                     for (int i = 0; i < HEIGHT; i++) {
  107.                         for (int j = 0; j < WIDTH; j++) {
  108.                             if (fieldInstance.getField()[i][j] instanceof Stork){
  109.                                 labels[i][j].setIcon(storkIcon);
  110.                             } else if (fieldInstance.getField()[i][j] instanceof Frog){
  111.                                 labels[i][j].setIcon(frogIcon);
  112.                             } else if (fieldInstance.getField()[i][j] instanceof Fly){
  113.                                 labels[i][j].setIcon(flyIcon);
  114.                             } else {
  115.                                 labels[i][j].setIcon(grassIcon);
  116.                             }
  117.                         }
  118.                     }
  119.                 }
  120.                 for (int i = 0; i < HEIGHT; i++) {
  121.                         for (int j = 0; j < WIDTH; j++) {
  122.                             if (fieldInstance.getField()[i][j] instanceof Stork){
  123.                                 labels[i][j].setIcon(bloodyStorkIcon);
  124.                             } else if (fieldInstance.getField()[i][j] instanceof Frog){
  125.                                 labels[i][j].setIcon(frogIcon);
  126.                             } else if (fieldInstance.getField()[i][j] instanceof Fly){
  127.                                 labels[i][j].setIcon(flyIcon);
  128.                             } else {
  129.                                 labels[i][j].setIcon(grassIcon);
  130.                             }
  131.                         }
  132.                     }
  133.             }
  134.         }).start();
  135.     }
  136.  
  137.     @SuppressWarnings("unchecked")
  138.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  139.     private void initComponents() {
  140.  
  141.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  142.         setPreferredSize(new java.awt.Dimension(0, 0));
  143.         setResizable(false);
  144.  
  145.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  146.         getContentPane().setLayout(layout);
  147.         layout.setHorizontalGroup(
  148.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  149.             .addGap(0, 468, Short.MAX_VALUE)
  150.         );
  151.         layout.setVerticalGroup(
  152.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  153.             .addGap(0, 336, Short.MAX_VALUE)
  154.         );
  155.  
  156.         pack();
  157.     }// </editor-fold>                        
  158.  
  159.     public static void main(String args[]) {
  160.        
  161.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  162.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  163.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  164.          */
  165.         try {
  166.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  167.                 if ("Nimbus".equals(info.getName())) {
  168.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  169.                     break;
  170.                 }
  171.             }
  172.         } catch (ClassNotFoundException ex) {
  173.             java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  174.         } catch (InstantiationException ex) {
  175.             java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  176.         } catch (IllegalAccessException ex) {
  177.             java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  178.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  179.             java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  180.         }
  181.         //</editor-fold>
  182.        
  183.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  184.             public void run() {
  185.                 createAndShowGUI();
  186.             }
  187.         });
  188.     }
  189.  
  190.     // Variables declaration - do not modify                    
  191.     // End of variables declaration                  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement