Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.97 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 = 5;
  30.         WIDTH = 5;
  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 < 5; i++) {
  43.             for (int j = 0; j < 5; 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 < 5; i++) {
  52.             int x = 0;
  53.             for (int j = 0; j < 5; 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(600 + insets.left + insets.right,
  74.                       500 + insets.top + insets.bottom);
  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.         ArrayList<Animal> animals = new ArrayList<>();
  85.         Animal a = new Fly("L", STARTING_POS_OF_FLY);
  86.         animals.add(a);
  87.         a = new Frog("B", STARTING_POS_OF_FROG);
  88.         animals.add(a);
  89.         a = new Stork("G", STARTING_POS_OF_STORK);
  90.         animals.add(a);
  91.         Icon storkIcon = new ImageIcon("stork.jpg");
  92.         Field fieldInstance = new Field(animals, HEIGHT, WIDTH);
  93.         while(fieldInstance.moveTo()){
  94.             Thread.sleep(50);
  95.             for (int i = 0; i < HEIGHT; i++) {
  96.                 for (int j = 0; j < WIDTH; j++) {
  97.                     if (fieldInstance.getField()[i][j] instanceof Stork){
  98.                         labels[i][j].setIcon(storkIcon);
  99.                     } else {
  100.                         labels[i][j].setIcon(null);
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.     }
  106.  
  107.     @SuppressWarnings("unchecked")
  108.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  109.     private void initComponents() {
  110.  
  111.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  112.  
  113.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  114.         getContentPane().setLayout(layout);
  115.         layout.setHorizontalGroup(
  116.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  117.             .addGap(0, 468, Short.MAX_VALUE)
  118.         );
  119.         layout.setVerticalGroup(
  120.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  121.             .addGap(0, 336, Short.MAX_VALUE)
  122.         );
  123.  
  124.         pack();
  125.     }// </editor-fold>                        
  126.  
  127.     public static void main(String args[]) {
  128.        
  129.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  130.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  131.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  132.          */
  133.         try {
  134.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  135.                 if ("Nimbus".equals(info.getName())) {
  136.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  137.                     break;
  138.                 }
  139.             }
  140.         } catch (ClassNotFoundException ex) {
  141.             java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  142.         } catch (InstantiationException ex) {
  143.             java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  144.         } catch (IllegalAccessException ex) {
  145.             java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  146.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  147.             java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  148.         }
  149.         //</editor-fold>
  150.        
  151.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  152.             public void run() {
  153.                 createAndShowGUI();
  154.             }
  155.         });
  156.     }
  157.  
  158.     // Variables declaration - do not modify                    
  159.     // End of variables declaration                  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement