Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package storkfrogfly;
- import animal.Animal;
- import animal.Frog;
- import animal.Stork;
- import animal.Fly;
- import animal.Position;
- import java.util.ArrayList;
- import areas.Field;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Insets;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.Icon;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- public class MainWindow extends javax.swing.JFrame {
- private final static int HEIGHT;
- private final static int WIDTH;
- private final static Position STARTING_POS_OF_STORK;
- private final static Position STARTING_POS_OF_FROG;
- private final static Position STARTING_POS_OF_FLY;
- static{
- HEIGHT = 9;
- WIDTH = 9;
- STARTING_POS_OF_STORK = new Position(0, 0);
- STARTING_POS_OF_FROG = new Position(HEIGHT-1, WIDTH-1);
- STARTING_POS_OF_FLY = new Position(HEIGHT/2, WIDTH/2);
- }
- public MainWindow() {
- }
- public static JLabel[][] addComponentsToPane(Container pane) {
- pane.setLayout(null);
- JLabel[][] labels = new JLabel[HEIGHT][WIDTH];
- for (int i = 0; i < HEIGHT; i++) {
- for (int j = 0; j < WIDTH; j++) {
- JLabel label = new JLabel("hali");
- labels[i][j] = label;
- pane.add(label);
- }
- }
- Insets insets = pane.getInsets();
- int y = 0;
- for (int i = 0; i < HEIGHT; i++) {
- int x = 0;
- for (int j = 0; j < WIDTH; j++) {
- Dimension size = labels[i][j].getPreferredSize();
- labels[i][j].setBounds(x + insets.left, y + insets.top, 50, 50);
- x += 50;
- }
- y += 50;
- }
- return labels;
- }
- private static void createAndShowGUI() {
- //Create and set up the window.
- JFrame frame = new JFrame("MainWindow");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //Set up the content pane.
- JLabel[][] labels = addComponentsToPane(frame.getContentPane());
- //Size and display the window.
- Insets insets = frame.getInsets();
- frame.setSize((50*WIDTH+10) + insets.left,
- (50*HEIGHT+40) + insets.top);
- frame.setVisible(true);
- try {
- playGame(labels);
- } catch (InterruptedException ex) {
- Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static void playGame(JLabel[][] labels) throws InterruptedException{
- new Thread(new Runnable(){
- @Override
- public void run() {
- ArrayList<Animal> animals = new ArrayList<>();
- Animal a = new Fly("L", STARTING_POS_OF_FLY);
- animals.add(a);
- a = new Frog("B", STARTING_POS_OF_FROG);
- animals.add(a);
- a = new Stork("G", STARTING_POS_OF_STORK);
- animals.add(a);
- Icon storkIcon = new ImageIcon("stork.jpg");
- Icon bloodyStorkIcon = new ImageIcon("bloodystork.jpg");
- Icon frogIcon = new ImageIcon("frog.jpg");
- Icon flyIcon = new ImageIcon("fly.jpg");
- Icon grassIcon = new ImageIcon("grass.jpg");
- Field fieldInstance = new Field(animals, HEIGHT, WIDTH);
- while(fieldInstance.moveTo()){
- try {
- Thread.sleep(400);
- } catch (InterruptedException ex) {
- Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
- }
- for (int i = 0; i < HEIGHT; i++) {
- for (int j = 0; j < WIDTH; j++) {
- if (fieldInstance.getField()[i][j] instanceof Stork){
- labels[i][j].setIcon(storkIcon);
- } else if (fieldInstance.getField()[i][j] instanceof Frog){
- labels[i][j].setIcon(frogIcon);
- } else if (fieldInstance.getField()[i][j] instanceof Fly){
- labels[i][j].setIcon(flyIcon);
- } else {
- labels[i][j].setIcon(grassIcon);
- }
- }
- }
- }
- for (int i = 0; i < HEIGHT; i++) {
- for (int j = 0; j < WIDTH; j++) {
- if (fieldInstance.getField()[i][j] instanceof Stork){
- labels[i][j].setIcon(bloodyStorkIcon);
- } else if (fieldInstance.getField()[i][j] instanceof Frog){
- labels[i][j].setIcon(frogIcon);
- } else if (fieldInstance.getField()[i][j] instanceof Fly){
- labels[i][j].setIcon(flyIcon);
- } else {
- labels[i][j].setIcon(grassIcon);
- }
- }
- }
- }
- }).start();
- }
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setPreferredSize(new java.awt.Dimension(0, 0));
- setResizable(false);
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 468, Short.MAX_VALUE)
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 336, Short.MAX_VALUE)
- );
- pack();
- }// </editor-fold>
- public static void main(String args[]) {
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- createAndShowGUI();
- }
- });
- }
- // Variables declaration - do not modify
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement