Facebook
From Ungracious Butterfly, 5 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 241
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. public class GameButton extends JButton implements ActionListener{
  7.     ImageIcon allay, enemy;
  8.     static int turn = 0;
  9.     private boolean set = false;
  10.     private Point coordinates;
  11.     public GameButton(int boardSize, Point coordinates) {
  12. //        allay = new ImageIcon("src/img/catBG.png");
  13. //        enemy = new ImageIcon("src/img/dogBG.png");
  14.         this.coordinates = coordinates;
  15.         allay = new ImageIcon("src/img/batman.png");
  16.         enemy = new ImageIcon("src/img/joker.png");
  17.         this.setBackground(Color.black);
  18.         if(boardSize <= 15 && boardSize >= 0) {
  19.             this.setMinimumSize(new Dimension(10, 10));
  20.             this.setPreferredSize(new Dimension(50, 50));
  21.             this.setMaximumSize(new Dimension(200, 200));
  22.         } else if(boardSize > 15 && boardSize <= 30) {
  23.             this.setPreferredSize(new Dimension(25, 25));
  24.         } else {
  25.             this.setPreferredSize(new Dimension(15, 15));
  26.         }
  27.         this.addActionListener(this);
  28.     }
  29.  
  30.     @Override
  31.     public void actionPerformed(ActionEvent e) {
  32.  
  33.         if(!set) {
  34.             turn++;
  35.             switch (turn%2) {
  36.                 case 0:
  37.                     setIcon(allay);
  38.                     set = true;
  39.                     break;
  40.                 case 1:
  41.                     setIcon(enemy);
  42.                     set = true;
  43.                     break;
  44.             }
  45.         }
  46.     }
  47.  
  48.     public boolean isSet() {
  49.         return set;
  50.     }
  51.  
  52.     public Point getCoordinates() {
  53.         return coordinates;
  54.     }
  55. }
  56.