Drawing a dice and showing it... Help?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Catching360
    New Member
    • Oct 2012
    • 1

    #1

    Drawing a dice and showing it... Help?

    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 "
    Code:
    import javax.swing.JFrame;
    
    import java.awt.Color;
    import java.util.Scanner;
    
    public class RollTheDie
    {
    	public static void main(String[] args)
    	{
    		final int WINDOW_HEIGHT = 350;
    		final int WINDOW_WIDTH = 300;
    
    		System.out.println("Hi! Let's play dice!\n");
    
    		Scanner kb = new Scanner( System.in );
    
    		System.out.print("Enter the number on the face of the die:");
    		int num = kb.nextInt();
    
    		System.out.print("Enter the location of the die:");
    		int x = kb.nextInt();
    		int y = kb.nextInt();
    
    		System.out.println("I hope you had fun! Bye!");
    
    		JFrame dieWindow = new JFrame();
    		dieWindow.setBackground(Color.gray);
    		dieWindow.setSize(WINDOW_HEIGHT, WINDOW_WIDTH);
    		dieWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		dieWindow.setTitle("Roll the Die");
    
    		Die newDie = new Die(num,x,y);
    		dieWindow.add(newDie);
    		dieWindow.setVisible(true);
    
    
    	}
    }
    And The "Die" Class which should draw the die.
    Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    
    public class Die extends JPanel
    {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	int xCoordinate;
    	int yCoordinate;
    	int num;
    	
    
    	Graphics pen, pane;
    
    	private static final int DIE_LENGTH = 60;		//Set's the size
    	private static final int DIE_WIDTH = 60;		//of the die
    	private static final int DIE_HEIGHT = 60;
    	private static final int DOT_LENGTH = 10;
    	private static final int DOT_WIDTH = 10;
    	private static final Color LINE_COLOR = Color.black;
    	private static final Color DIE_COLOR = Color.white;
    	private static final Color DOT_COLOR = Color.black;
    	private static final int DIE_VERT = 10;
    	private static final int DIE_HORI = 10;
    
    
    	public Die(int dieNum, int x, int y)
    	{
    		xCoordinate = x;
    		yCoordinate = y;
    
    		num = dieNum;
    	}
    
    	public void paintDie(Graphics pen)
    	{
    
    		//Creates the square
    		pen.drawRect(getX(), getY(), DIE_WIDTH, DIE_HEIGHT);
    		pen.setColor(LINE_COLOR);
    		pen.fillRect(xCoordinate, yCoordinate, DIE_LENGTH, DIE_WIDTH);
    		if (num <= 0)
    			drawBlank();
    		else if (num == 1)
    			drawOne();
    		else if (num == 2)
    			drawTwo();
    		else if (num == 3)
    			drawThree();
    		else if (num == 4)
    			drawFour();
    		else if (num == 5)
    			drawFive();
    		else if (num == 6)
    			drawSix();
    		
    
    
    	}
    
    	private void drawBlank ()
    	{
    		pane.setColor(DIE_COLOR);
    		pane.fillRect(xCoordinate, yCoordinate, DIE_LENGTH, DIE_WIDTH);
    		
    	}
    
    	private void drawDot (int x, int y)
    	{
    		pane.setColor(DOT_COLOR);
    		pane.fillOval(x, y, DOT_LENGTH, DOT_WIDTH);
    		
    	}
    	private void drawOne ()
    	{
    
    		drawBlank();
    		drawDot(xCoordinate, yCoordinate);
    		
    	}
    	private void drawTwo ()
    	{
    		drawBlank();
    		pane.fillOval((xCoordinate - DIE_HORI), (yCoordinate + DIE_VERT), DOT_LENGTH, DOT_WIDTH);
    		pane.fillOval((xCoordinate + DIE_HORI), (yCoordinate - DIE_VERT), DOT_LENGTH, DOT_WIDTH);
    		
    	}
    	private void drawThree ()
    	{
    		drawBlank();
    		drawOne();
    		drawTwo();
    		
    	}
    
    	private void drawFour ()
    	{
    		drawBlank();
    		drawDot((xCoordinate - DIE_HORI),(yCoordinate + DIE_VERT));
    		drawDot((xCoordinate + DIE_HORI),(yCoordinate + DIE_VERT));
    		drawDot((xCoordinate - DIE_HORI),(yCoordinate - DIE_VERT));
    		drawDot((xCoordinate + DIE_HORI),(yCoordinate - DIE_VERT));
    		
    	}
    
    	private void drawFive ()
    	{
    		drawBlank();
    		drawFour();
    		drawOne();
    		
    
    	}
    
    	private void drawSix()
    	{
    		drawBlank();
    		drawFour();
    		drawDot((xCoordinate - DIE_HORI), yCoordinate);
    		drawDot((xCoordinate + DIE_HORI) , yCoordinate);
    		
    	}
    
    }
    I can't for the life of me figure out what is wrong.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    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?

    Comment

    Working...