Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /*
- * Interface.java
- *
- * Created on Nov 18, 2010, 9:46:05 PM
- */
- package dice;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- /**
- *
- * @author phani
- */
- public class Interface extends javax.swing.JFrame {
- BufferedImage dice1I;
- BufferedImage dice2I;
- Graphics dice1G;
- Graphics dice2G;
- Dices dice;
- public Interface() {
- initComponents();
- //init display
- dice1I = new BufferedImage(dice1.getWidth(), dice1.getHeight(), BufferedImage.TYPE_INT_RGB);
- dice2I = new BufferedImage(dice2.getWidth(), dice2.getHeight(), BufferedImage.TYPE_INT_RGB);
- dice1G = dice1I.createGraphics();
- dice2G = dice2I.createGraphics();
- dice = new Dices();
- drawDice();
- }
- private void updateImages() {
- dice1.setImage(dice1I);
- dice2.setImage(dice2I);
- }
- private void drawDice() {
- int num1 = dice.getDice1();
- int num2 = dice.getDice2();
- dice1G.setColor(Color.white);
- dice2G.setColor(Color.white);
- dice1G.fillRect(0, 0, 1000, 1000);
- dice2G.fillRect(0, 0, 1000, 1000);
- dice1G.setColor(Color.black);
- dice2G.setColor(Color.black);
- if (num1 == 1 || num1 == 5 || num1 == 3) {
- dice1G.fillOval((dice1I.getWidth() / 2) - 6, (dice1I.getHeight() / 2) - 6, 12, 12);
- }
- if (num2 == 1 || num2 == 5 || num2 == 3) {
- dice2G.fillOval((dice2I.getWidth() / 2) - 6, (dice2I.getHeight() / 2) - 6, 12, 12);
- }
- if (num1 == 4 || num1 == 6 || num1 == 5) {
- dice1G.fillOval((dice1I.getWidth() / 5) - 6, (dice1I.getHeight() / 5) - 6, 12, 12);
- }
- if (num2 == 4 || num2 == 6 || num2 == 5) {
- dice2G.fillOval((dice2I.getWidth() / 5) - 6, (dice2I.getHeight() / 5) - 6, 12, 12);
- }
- if (num1 == 6) {
- dice1G.fillOval((dice1I.getWidth() / 5) - 6, (dice1I.getHeight() / 2) - 6, 12, 12);
- }
- if (num2 == 6) {
- dice2G.fillOval((dice2I.getWidth() / 5) - 6, (dice2I.getHeight() / 2) - 6, 12, 12);
- }
- if (num1 == 4 || num1 == 6 || num1 == 5 || num1 == 3 || num1 == 2) {
- dice1G.fillOval((dice1I.getWidth() / 5) - 6, dice1I.getHeight() - (dice1I.getHeight() / 5) - 6, 12, 12);
- }
- if (num2 == 4 || num2 == 6 || num2 == 5 || num2 == 3 || num2 == 2) {
- dice2G.fillOval((dice2I.getWidth() / 5) - 6, dice2I.getHeight() - (dice2I.getHeight() / 5) - 6, 12, 12);
- }
- if (num1 == 4 || num1 == 6 || num1 == 5 || num1 == 3 || num1 == 2) {
- dice1G.fillOval(dice1I.getWidth() - (dice1I.getWidth() / 5) - 6, (dice1I.getHeight() / 5) - 6, 12, 12);
- }
- if (num2 == 4 || num2 == 6 || num2 == 5 || num2 == 3 || num2 == 2) {
- dice2G.fillOval(dice1I.getWidth() - (dice2I.getWidth() / 5) - 6, (dice2I.getHeight() / 5) - 6, 12, 12);
- }
- if (num1 == 6) {
- dice1G.fillOval(dice1I.getWidth() - (dice1I.getWidth() / 5) - 6, (dice1I.getHeight() / 2) - 6, 12, 12);
- }
- if (num2 == 6) {
- dice2G.fillOval(dice1I.getWidth() - (dice2I.getWidth() / 5) - 6, (dice2I.getHeight() / 2) - 6, 12, 12);
- }
- if (num1 == 6 || num1 == 4 || num1 == 5) {
- dice1G.fillOval(dice1I.getWidth() - (dice1I.getWidth() / 5) - 6, dice1I.getHeight() - (dice1I.getHeight() / 5) - 6, 12, 12);
- }
- if (num2 == 6 || num2 == 4 || num2 == 5) {
- dice2G.fillOval(dice1I.getWidth() - (dice2I.getWidth() / 5) - 6, dice1I.getHeight() - (dice2I.getHeight() / 5) - 6, 12, 12);
- }
- updateImages();
- }
- /** 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() {
- dice1 = new imagepanel.ImagePanel();
- dice2 = new imagepanel.ImagePanel();
- progress = new javax.swing.JProgressBar();
- rollDiceBtn = new javax.swing.JButton();
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- tdice1 = new javax.swing.JTextField();
- tdice2 = new javax.swing.JTextField();
- jButton1 = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- dice1.setBackground(new java.awt.Color(1, 1, 1));
- javax.swing.GroupLayout dice1Layout = new javax.swing.GroupLayout(dice1);
- dice1.setLayout(dice1Layout);
- dice1Layout.setHorizontalGroup(
- dice1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 100, Short.MAX_VALUE)
- );
- dice1Layout.setVerticalGroup(
- dice1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 100, Short.MAX_VALUE)
- );
- dice2.setBackground(new java.awt.Color(11, 11, 11));
- javax.swing.GroupLayout dice2Layout = new javax.swing.GroupLayout(dice2);
- dice2.setLayout(dice2Layout);
- dice2Layout.setHorizontalGroup(
- dice2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 100, Short.MAX_VALUE)
- );
- dice2Layout.setVerticalGroup(
- dice2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 100, Short.MAX_VALUE)
- );
- progress.setString("");
- progress.setStringPainted(true);
- rollDiceBtn.setText("Roll Dice");
- rollDiceBtn.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- rollDiceBtnActionPerformed(evt);
- }
- });
- jLabel1.setText("Test Dice 1:");
- jLabel2.setText("Test Dice 2:");
- tdice1.setText("1");
- tdice2.setText("1");
- jButton1.setText("Look for Tests");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(progress, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE)
- .addGroup(layout.createSequentialGroup()
- .addComponent(dice1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(dice2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel2)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(tdice2, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE))
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel1)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(tdice1, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE))
- .addGroup(layout.createSequentialGroup()
- .addComponent(rollDiceBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)))))
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(dice2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(dice1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(rollDiceBtn)
- .addComponent(jButton1))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel1)
- .addComponent(tdice1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel2)
- .addComponent(tdice2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap())
- );
- pack();
- }// </editor-fold>
- private void rollDiceBtnActionPerformed(java.awt.event.ActionEvent evt) {
- dice.rollDice();
- drawDice();
- progress.setString("Rolled a " + dice.getDice1() + " and a " + dice.getDice2());
- }
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- Runnable find = new Runnable() {
- public void run() {
- int tries = 0;
- int tried = 0;
- int test1;
- int test2;
- int count = 0;
- try {
- test1 = Integer.parseInt(tdice1.getText());
- test2 = Integer.parseInt(tdice2.getText());
- } catch (Exception e) {
- test1 = 1;
- test2 = 1;
- tdice1.setText("1");
- tdice2.setText("2");
- }
- progress.setMaximum(1000);
- for (int i = 0; i < 1000; i++) {
- progress.setValue(i);
- progress.setString("Rolling..."+i+"/1000");
- count = 0;
- boolean found = false;
- while (!found) {
- dice.rollDice();
- drawDice(); // comment out for faster execution
- count++;
- if (dice.getDice1() == test1 && dice.getDice2() == test2) {
- found = true;
- tried++;
- tries += count;
- }
- }
- }
- progress.setString("Found test case in an average " + tries/tried + " tries in "+tried+" trials");
- }
- };
- new Thread(find).start();
- }
- /**
- * @param args the command line arguments
- */
- // Variables declaration - do not modify
- private imagepanel.ImagePanel dice1;
- private imagepanel.ImagePanel dice2;
- private javax.swing.JButton jButton1;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JProgressBar progress;
- private javax.swing.JButton rollDiceBtn;
- private javax.swing.JTextField tdice1;
- private javax.swing.JTextField tdice2;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement