Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package rollem;
- import javax.swing.JLabel;
- import java.util.Random;
- import javax.swing.*;
- import java.awt.event.*;
- /**
- *
- * @author wayne
- */
- public class DieLabel extends JLabel {
- private boolean isDieAnimated = true;
- private final static int DELAY = 200;
- private final static int FRAME_COUNT_MAX = 9;
- private final static int FRAME_COUNT_MIN = 5;
- private int frameCount = 0;
- private int frameCountLimit = 0;
- private Timer animationTimer;
- private final static String IS_HELD_MSG = "held";
- private final static String IS_USABLE_MSG = " ";
- private Random rand = new Random();
- private int dieValue = 6;
- private final int NUMBER_OF_SIDES = 6;
- private ImageIcon[] dieImage = new ImageIcon[ NUMBER_OF_SIDES + 1 ];
- /**
- * Creates new form DieLabel
- */
- public DieLabel() {
- initComponents();
- for (int i = 1; i <= NUMBER_OF_SIDES; i++)
- {
- String filename = "/images/die" + i + ".gif";
- dieImage[i] = new ImageIcon(this.getClass().getResource(filename));
- }
- //set up our animation timer
- animationTimer = new Timer( DELAY, new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- animationTimerActionPerformed(evt);
- }
- });
- //counters are used to control the number of times the timer repeats...
- animationTimer.setRepeats(false);
- }
- public int rollDie()
- {
- // initialize the frame counting variables for this roll
- frameCount = 0;
- int range = FRAME_COUNT_MAX - FRAME_COUNT_MIN + 1;
- frameCountLimit = FRAME_COUNT_MIN + rand.nextInt(range);
- //"roll" the die and start the animation if we're to do so
- dieValue = rand.nextInt( NUMBER_OF_SIDES ) + 1;
- if (isDieAnimated) {animationTimer.start();}
- else {setIcon(dieImage[dieValue]); }
- return dieValue;
- }
- public int getDieValue()
- {
- return dieValue;
- }
- public boolean setDieValue( int value )
- {
- if (value > 0 && value <= NUMBER_OF_SIDES)
- {
- dieValue = value;
- setIcon(dieImage[dieValue]);
- return true;
- }
- return false;
- }
- private void animationTimerActionPerformed( java.awt.event.ActionEvent evt) {
- frameCount++; //increment our animation frame counter
- //if we reached our limit, display the true value and restore mouse events;
- // otherwise, show some random value...
- if (frameCount < frameCountLimit ) {
- int value = rand.nextInt(NUMBER_OF_SIDES) + 1;
- this.setIcon(dieImage[value]);
- animationTimer.start();
- }
- else {
- this.setIcon(dieImage[dieValue]);
- }
- }
- public void setAnimation( boolean animationState ) {
- isDieAnimated = animationState;
- }
- public boolean isAnimated() {
- return isDieAnimated;
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
- setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
- setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/die6.gif"))); // NOI18N
- setToolTipText("");
- setVerticalAlignment(javax.swing.SwingConstants.TOP);
- setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
- setMaximumSize(new java.awt.Dimension(80, 100));
- setMinimumSize(new java.awt.Dimension(80, 100));
- setPreferredSize(new java.awt.Dimension(80, 100));
- setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
- }// </editor-fold>
- // Variables declaration - do not modify
- // End of variables declaration
- }
Add Comment
Please, Sign In to add comment