Boucherwayne78

Untitled

Feb 28th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package rollem;
  7.  
  8. import javax.swing.JLabel;
  9. import java.util.Random;
  10. import javax.swing.*;
  11. import java.awt.event.*;
  12.  
  13. /**
  14.  *
  15.  * @author wayne
  16.  */
  17. public class DieLabel extends JLabel {
  18.    
  19.      private boolean isDieAnimated = true;
  20.     private final static int DELAY = 200;
  21.     private final static int FRAME_COUNT_MAX = 9;
  22.     private final static int FRAME_COUNT_MIN = 5;
  23.     private int frameCount = 0;
  24.     private int frameCountLimit = 0;
  25.     private Timer animationTimer;
  26.     private final static String IS_HELD_MSG = "held";
  27.     private final static String IS_USABLE_MSG = " ";
  28.    
  29.     private Random rand = new Random();
  30.     private int dieValue = 6;
  31.     private final int NUMBER_OF_SIDES = 6;
  32.     private ImageIcon[] dieImage = new ImageIcon[ NUMBER_OF_SIDES + 1 ];
  33.  
  34.     /**
  35.      * Creates new form DieLabel
  36.      */
  37.     public DieLabel() {
  38.         initComponents();
  39.                
  40.         for (int i = 1; i <= NUMBER_OF_SIDES; i++)
  41.         {
  42.             String filename = "/images/die" + i + ".gif";
  43.             dieImage[i] = new ImageIcon(this.getClass().getResource(filename));
  44.         }
  45.        
  46.         //set up our animation timer
  47.         animationTimer = new Timer( DELAY, new java.awt.event.ActionListener() {
  48.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  49.                 animationTimerActionPerformed(evt);
  50.             }
  51.         });
  52.        
  53.         //counters are used to control the number of times the timer repeats...
  54.         animationTimer.setRepeats(false);
  55.     }
  56.    
  57.     public int rollDie()
  58.     {
  59.        
  60.         // initialize the frame counting variables for this roll
  61.         frameCount = 0;
  62.         int       range = FRAME_COUNT_MAX - FRAME_COUNT_MIN + 1;
  63.         frameCountLimit = FRAME_COUNT_MIN + rand.nextInt(range);
  64.        
  65.         //"roll" the die and start the animation if we're to do so
  66.         dieValue = rand.nextInt( NUMBER_OF_SIDES ) + 1;
  67.         if   (isDieAnimated) {animationTimer.start();}
  68.         else                 {setIcon(dieImage[dieValue]); }
  69.        
  70.         return dieValue;
  71.     }
  72.        
  73.     public int getDieValue()
  74.     {
  75.         return dieValue;
  76.     }
  77.    
  78.     public boolean setDieValue( int value )
  79.     {
  80.         if (value > 0 && value <= NUMBER_OF_SIDES)
  81.         {
  82.             dieValue = value;
  83.             setIcon(dieImage[dieValue]);
  84.             return true;
  85.         }
  86.         return false;
  87.     }
  88.    
  89.     private void animationTimerActionPerformed( java.awt.event.ActionEvent evt) {
  90.         frameCount++;        //increment our animation frame counter
  91.        
  92.         //if we reached our limit, display the true value and restore mouse events;
  93.         // otherwise, show some random value...
  94.         if (frameCount < frameCountLimit ) {
  95.             int value = rand.nextInt(NUMBER_OF_SIDES) + 1;
  96.             this.setIcon(dieImage[value]);
  97.             animationTimer.start();
  98.         }
  99.         else {
  100.             this.setIcon(dieImage[dieValue]);
  101.         }
  102.     }
  103.  
  104.     public void setAnimation( boolean animationState ) {
  105.         isDieAnimated = animationState;
  106.     }
  107.  
  108.     public boolean isAnimated() {
  109.         return isDieAnimated;
  110.     }
  111.  
  112.     /**
  113.      * This method is called from within the constructor to initialize the form.
  114.      * WARNING: Do NOT modify this code. The content of this method is always
  115.      * regenerated by the Form Editor.
  116.      */
  117.     @SuppressWarnings("unchecked")
  118.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  119.     private void initComponents() {
  120.  
  121.         setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
  122.         setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  123.         setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/die6.gif"))); // NOI18N
  124.         setToolTipText("");
  125.         setVerticalAlignment(javax.swing.SwingConstants.TOP);
  126.         setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
  127.         setMaximumSize(new java.awt.Dimension(80, 100));
  128.         setMinimumSize(new java.awt.Dimension(80, 100));
  129.         setPreferredSize(new java.awt.Dimension(80, 100));
  130.         setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
  131.     }// </editor-fold>                        
  132.  
  133.  
  134.     // Variables declaration - do not modify                    
  135.     // End of variables declaration                  
  136. }
Add Comment
Please, Sign In to add comment