By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,648 Members | 1,528 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,648 IT Pros & Developers. It's quick & easy.

Drawing a dice and showing it... Help?

P: 1
So have this whole program that I need to do. What's required is that I draw a die using the user's input of die and location. Then show it. I have a main client "RollTheDie"
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.JFrame;
  2.  
  3. import java.awt.Color;
  4. import java.util.Scanner;
  5.  
  6. public class RollTheDie
  7. {
  8.     public static void main(String[] args)
  9.     {
  10.         final int WINDOW_HEIGHT = 350;
  11.         final int WINDOW_WIDTH = 300;
  12.  
  13.         System.out.println("Hi! Let's play dice!\n");
  14.  
  15.         Scanner kb = new Scanner( System.in );
  16.  
  17.         System.out.print("Enter the number on the face of the die:");
  18.         int num = kb.nextInt();
  19.  
  20.         System.out.print("Enter the location of the die:");
  21.         int x = kb.nextInt();
  22.         int y = kb.nextInt();
  23.  
  24.         System.out.println("I hope you had fun! Bye!");
  25.  
  26.         JFrame dieWindow = new JFrame();
  27.         dieWindow.setBackground(Color.gray);
  28.         dieWindow.setSize(WINDOW_HEIGHT, WINDOW_WIDTH);
  29.         dieWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30.         dieWindow.setTitle("Roll the Die");
  31.  
  32.         Die newDie = new Die(num,x,y);
  33.         dieWindow.add(newDie);
  34.         dieWindow.setVisible(true);
  35.  
  36.  
  37.     }
  38. }
  39.  
And The "Die" Class which should draw the die.
Expand|Select|Wrap|Line Numbers
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import javax.swing.JPanel;
  4.  
  5. public class Die extends JPanel
  6. {
  7.     /**
  8.      * 
  9.      */
  10.     private static final long serialVersionUID = 1L;
  11.     int xCoordinate;
  12.     int yCoordinate;
  13.     int num;
  14.  
  15.  
  16.     Graphics pen, pane;
  17.  
  18.     private static final int DIE_LENGTH = 60;        //Set's the size
  19.     private static final int DIE_WIDTH = 60;        //of the die
  20.     private static final int DIE_HEIGHT = 60;
  21.     private static final int DOT_LENGTH = 10;
  22.     private static final int DOT_WIDTH = 10;
  23.     private static final Color LINE_COLOR = Color.black;
  24.     private static final Color DIE_COLOR = Color.white;
  25.     private static final Color DOT_COLOR = Color.black;
  26.     private static final int DIE_VERT = 10;
  27.     private static final int DIE_HORI = 10;
  28.  
  29.  
  30.     public Die(int dieNum, int x, int y)
  31.     {
  32.         xCoordinate = x;
  33.         yCoordinate = y;
  34.  
  35.         num = dieNum;
  36.     }
  37.  
  38.     public void paintDie(Graphics pen)
  39.     {
  40.  
  41.         //Creates the square
  42.         pen.drawRect(getX(), getY(), DIE_WIDTH, DIE_HEIGHT);
  43.         pen.setColor(LINE_COLOR);
  44.         pen.fillRect(xCoordinate, yCoordinate, DIE_LENGTH, DIE_WIDTH);
  45.         if (num <= 0)
  46.             drawBlank();
  47.         else if (num == 1)
  48.             drawOne();
  49.         else if (num == 2)
  50.             drawTwo();
  51.         else if (num == 3)
  52.             drawThree();
  53.         else if (num == 4)
  54.             drawFour();
  55.         else if (num == 5)
  56.             drawFive();
  57.         else if (num == 6)
  58.             drawSix();
  59.  
  60.  
  61.  
  62.     }
  63.  
  64.     private void drawBlank ()
  65.     {
  66.         pane.setColor(DIE_COLOR);
  67.         pane.fillRect(xCoordinate, yCoordinate, DIE_LENGTH, DIE_WIDTH);
  68.  
  69.     }
  70.  
  71.     private void drawDot (int x, int y)
  72.     {
  73.         pane.setColor(DOT_COLOR);
  74.         pane.fillOval(x, y, DOT_LENGTH, DOT_WIDTH);
  75.  
  76.     }
  77.     private void drawOne ()
  78.     {
  79.  
  80.         drawBlank();
  81.         drawDot(xCoordinate, yCoordinate);
  82.  
  83.     }
  84.     private void drawTwo ()
  85.     {
  86.         drawBlank();
  87.         pane.fillOval((xCoordinate - DIE_HORI), (yCoordinate + DIE_VERT), DOT_LENGTH, DOT_WIDTH);
  88.         pane.fillOval((xCoordinate + DIE_HORI), (yCoordinate - DIE_VERT), DOT_LENGTH, DOT_WIDTH);
  89.  
  90.     }
  91.     private void drawThree ()
  92.     {
  93.         drawBlank();
  94.         drawOne();
  95.         drawTwo();
  96.  
  97.     }
  98.  
  99.     private void drawFour ()
  100.     {
  101.         drawBlank();
  102.         drawDot((xCoordinate - DIE_HORI),(yCoordinate + DIE_VERT));
  103.         drawDot((xCoordinate + DIE_HORI),(yCoordinate + DIE_VERT));
  104.         drawDot((xCoordinate - DIE_HORI),(yCoordinate - DIE_VERT));
  105.         drawDot((xCoordinate + DIE_HORI),(yCoordinate - DIE_VERT));
  106.  
  107.     }
  108.  
  109.     private void drawFive ()
  110.     {
  111.         drawBlank();
  112.         drawFour();
  113.         drawOne();
  114.  
  115.  
  116.     }
  117.  
  118.     private void drawSix()
  119.     {
  120.         drawBlank();
  121.         drawFour();
  122.         drawDot((xCoordinate - DIE_HORI), yCoordinate);
  123.         drawDot((xCoordinate + DIE_HORI) , yCoordinate);
  124.  
  125.     }
  126.  
  127. }
  128.  
I can't for the life of me figure out what is wrong.
Oct 25 '12 #1
Share this Question
Share on Google+
1 Reply


Rabbit
Expert Mod 10K+
P: 12,421
Actually, you know what's wrong and you need to tell us so that we can answer the real question. The real question being why is the code doing this wrong thing when I'm expecting it to do this other thing? But we can't answer the why until you tell us what's it doing wrong. What is it supposed to do instead? Are you getting error messages? What are those error messages?
Oct 25 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.