SHARE
TWEET

Dice class

a guest Dec 3rd, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package myPackage;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import javax.swing.JPanel;
  6.  
  7. public class Dice extends JPanel {
  8.     private static final long serialVersionUID = 1L;
  9.    
  10.     boolean topLeft;
  11.     boolean topMid;
  12.     boolean topRight;
  13.     boolean midLeft;
  14.     boolean midMid;
  15.     boolean midRight;
  16.     boolean bottomLeft;
  17.     boolean bottomMid;
  18.     boolean bottomRight;
  19.    
  20.  
  21.  
  22.  
  23.     public Dice(int value) {
  24.         Dice toRoll;
  25.        
  26.         switch (value) {
  27.             case 1:
  28.                 toRoll = new Dice(false, false, false, false, true, false, false, false, false);
  29.                 break;
  30.             case 2:
  31.                 System.out.println("Throwing 2");
  32.                 toRoll = new Dice(true, false, false, false, false, false, false, false, true);
  33.                 break;
  34.             default:
  35.                 System.out.println("Default.");
  36.                 break;
  37.         }
  38.     }
  39.    
  40.  
  41.     public Dice(boolean _topLeft, boolean _topMid, boolean _topRight, boolean _midLeft, boolean _midMid, boolean _midRight, boolean _bottomLeft, boolean _bottomMid, boolean _bottomRight) {
  42.         System.out.println("Creating dice");
  43.         topLeft = _topLeft;
  44.         topMid = _topMid;
  45.         topRight = _topRight;
  46.         midLeft = _midLeft;
  47.         midMid = _midMid;
  48.         midRight = _midRight;
  49.         bottomLeft = _bottomLeft;
  50.         bottomMid = _bottomMid;
  51.         bottomRight = _bottomRight;
  52.        
  53.         System.out.println("In Dice constructor, top left is " + topLeft);
  54.        
  55.         repaint();
  56.     }
  57.    
  58.     public void paintComponent(Graphics g)  {
  59.         super.paintComponent(g);
  60.        
  61.         System.out.println("In paintComponent, top left is " + topLeft);
  62.  
  63.         setLayout(null);
  64.        
  65.         int cubePosX = 200; //Top left of object on x axis
  66.         int cubePosY = 100; //Top left of object on y axis
  67.        
  68.         int cubeSizeX = 250; //Size of the object on x axis
  69.         int cubeSizeY = 250; //Size of the object on y axis
  70.        
  71.         g.setColor(Color.black);
  72.         g.drawRoundRect(cubePosX, cubePosY, cubeSizeX, cubeSizeY, 15, 15);
  73.        
  74.         int step = 75; //Units between dots
  75.         int dotSizeX = 25;
  76.         int dotSizeY = 25;
  77.        
  78.         int dotPosX = 0;
  79.         int dotPosY = 0;
  80.        
  81.         int xMiddle = (int)cubePosX + cubeSizeX / 2;
  82.         int yMiddle = (int)cubePosY + cubeSizeY / 2;
  83.        
  84.         int offsetX = (int)dotSizeX / -2;
  85.         int offsetY = (int)dotSizeY / -2;
  86.        
  87.  
  88.        
  89.         System.out.print("Drawing ");  
  90.         if(topLeft) {
  91.             System.out.print("tl ");
  92.             dotPosX = xMiddle - step + offsetX;
  93.             dotPosY = yMiddle - step + offsetY;
  94.            
  95.             g.fillOval(dotPosX, dotPosY, dotSizeX, dotSizeY);
  96.         }
  97.         if(topMid) {
  98.             System.out.print("tm ");
  99.             dotPosX = xMiddle + offsetX;
  100.             dotPosY = yMiddle - step + offsetY;
  101.  
  102.             g.fillOval(dotPosX, dotPosY, dotSizeX, dotSizeY);
  103.         }
  104.         if(topRight) {
  105.             System.out.print("tr ");
  106.             dotPosX = xMiddle + step + offsetX;
  107.             dotPosY = yMiddle - step + offsetY;
  108.  
  109.             g.fillOval(dotPosX, dotPosY, dotSizeX, dotSizeY);
  110.         }
  111.         if(midLeft) {
  112.             System.out.print("ml ");
  113.             dotPosX = xMiddle - step + offsetX;
  114.             dotPosY = yMiddle + offsetY;
  115.            
  116.             g.fillOval(dotPosX, dotPosY, dotSizeX, dotSizeY);
  117.         }
  118.         if(midMid) {
  119.             System.out.print("mm ");
  120.             dotPosX = xMiddle + offsetX;
  121.             dotPosY = yMiddle + offsetY;
  122.  
  123.             g.fillOval(dotPosX, dotPosY, dotSizeX, dotSizeY);
  124.         }
  125.         if(midRight) {
  126.             System.out.print("mr ");
  127.             dotPosX = xMiddle + step + offsetX;
  128.             dotPosY = yMiddle + offsetY;
  129.  
  130.             g.fillOval(dotPosX, dotPosY, dotSizeX, dotSizeY);
  131.         }
  132.         if(bottomLeft) {
  133.             System.out.print("bl ");
  134.             dotPosX = xMiddle - step + offsetX;
  135.             dotPosY = yMiddle + step + offsetY;
  136.            
  137.             g.fillOval(dotPosX, dotPosY, dotSizeX, dotSizeY);
  138.         }
  139.         if(bottomMid) {
  140.             System.out.print("bm ");
  141.             dotPosX = xMiddle + offsetX;
  142.             dotPosY = yMiddle + step + offsetY;
  143.  
  144.             g.fillOval(dotPosX, dotPosY, dotSizeX, dotSizeY);
  145.         }
  146.         if(bottomRight) {
  147.             System.out.print("br ");
  148.             dotPosX = xMiddle + step + offsetX;
  149.             dotPosY = yMiddle + step + offsetY;
  150.  
  151.             g.fillOval(dotPosX, dotPosY, dotSizeX, dotSizeY);
  152.         }
  153.     }
  154. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top