0

I have looked through Google searches and a few online tutorials and I am still stuck. It seems unfortunately I am stuck using AWT to do this assignment, seeing as Swing would be better. My assignment is to draw a die.

Currently I am stuck just trying to draw a line from corner of the the square to the edge of the die I am trying to draw. I have read that writing on top frames is ill-advised, but I am waiting on my instructor to let me know if I can just implement Swing components instead.

Currently, I can get the square to show, but I can't get the line to show after its supposedly drawn. I have read it is drawn, but hidden under another frame/object/component/something. However, how can I get the line show?

Once I can get the line to show, I can (hopefully) start drawing the die and the put the dots on it. I am only looking to get the lines to show, and not on how to do the assignment!

Any help is appreciated!

CSC 211
                                    Example #3

                        P r e t t y   a s   a   p i c t u r e
                        =====================================

    Purpose:    To demonstrate the graphic capability of Java.
*/

public class Example03
{
    //
    //  The main method is quite simple.
    //  We instantiate a (graphical) object, and we play with it.
    //
    public static void main(String[] args) 
    {

        System.out.println("Hi!"); //say hola

        Die myPicture = new Die(); //instantiate object Die

        myPicture.action(); //call type Die object myPicture's action method

        System.out.println("Bye!"); //say goodbye 
    }

}   // end Example03 class

Here is my Die class, it displays the die:

/*
    File:   Die.java

    Defines and implements the class for our "graphical" object.
*/

            //  To define a keyboard

import java.awt.*;                      //  AWT = "Abstract Window Toolkit"


public class Die extends Frame
{
    public final int WIDTH = 70;        //  Dimension of the rectangle
    public final int HEIGHT = 70;       //      to be drawn on the window
    private int xA = 200;               //  Coordinates of A (top left corner)
    private int yA = xA;                // trying to make the frame square 
    private int faceSide = 0;           // the die's face side 




    public Die() //public constructor Die to set initial stuff
    {
        setTitle("Let's play some dice!");  //  We set the characteristics of our
        setLocation(200, 200);          //      drawing window: the location,
        setSize(400, 400);              //      the size, and
        setBackground(Color.lightGray); //      the color of the background

        setVisible(true);               //  And we make it appear
    }

    //
    //  The action method reads the position of the picture from the keyboard and validates the face side 
    //
    public void action()
    {       
        BrainsOfTheOperation brains = new BrainsOfTheOperation(); //instantiate a new object of BrainsOfTheOperation

        brains.action(); //call object brain's, type of BrainsOfTheOperation, method's action

        xA = brains.returnXCoordinate(); //return object brain's  x coordinate
        yA = brains.returnYCoordinate(); //return object brain's  y coordinate
        faceSide = brains.returnFaceSide(); //return object brain's  dice face side position

        repaint();
    }

    //
    //  The only "graphical" method of the class is the paint method.
    //
    public void paint(Graphics pane)
    {
        pane.setColor(Color.cyan);      //  We use black paint to label
        pane.drawString("A", xA - 15, yA + 5);  //      the 2 opposite corners
        pane.drawString("B", 175 + 5, 175 + 5); //      of our rectangle

        pane.setColor(Color.blue);      //  Gray is darker than light gray
        pane.drawRect(175, 175, WIDTH , HEIGHT);    //  This is for the rectangle
//      drawBlank(pane);
    }
    private void drawBlank (Graphics pane)
    {

        pane.setColor(Color.cyan);      //  We use black paint to label
        pane.drawString("A", xA - 15, yA + 5);  //      the 2 opposite corners
//      pane.drawString("B", 175 + 5, 175 + 5); //      of our rectangle

        pane.setColor(Color.blue);      //  Gray is darker than light gray
        pane.drawRect(175, 175, WIDTH , HEIGHT);    //  This is for the rectangle


    }
    private void drawDot (Graphics pane)
    {

    }   
    private void drawOne (Graphics pane)
    {
    }
    private void drawTwo (Graphics pane)
    {
    }
    private void drawThree (Graphics pane)
    {
    }
    private void drawFour (Graphics pane)
    {
    }
    private void drawFive (Graphics pane)
    {
    }
    private void drawSix (Graphics pane)
    {
    }

}   // end class Die

And finally, my BrainsOfTheOperation class that validates an user's input and then asks for coordinates:

import java.util.Scanner;


public class BrainsOfTheOperation  
{
    public int xA, yA;                       //coordinates of where the dice will play
    private int faceSide = 0;                //what side the dice is showing
    private boolean faceSideNotValid = true; //used in a while loop to ensure a correct side is chosen

    public BrainsOfTheOperation() //public constructor
    {
        //left blank
    }

    public void action() //action method to ask for a face side, valid it, and then ask for the dice's coordinates on a frame
    {
        Scanner keyboard = new Scanner(System.in); //   Instantiating a keyboard scanner


    while ( faceSideNotValid )
    {
        System.out.print("Enter the number on the face of the die: ");
        faceSide = keyboard.nextInt(); //take the next integer

        testIfValid(); //make sure its valid: if faceSide >= to 1 and faceSide <= 6, return false, to break out of while loop
    }   
        System.out.print("Enter the location of the die: ");
        xA = keyboard.nextInt();        //  Determines the upper left corner of
        yA = keyboard.nextInt();        //      the square AKA die  

    }

    private void testIfValid() //declare method testIfValid to test if faceSide integer is a valid number for a die
    {
        if ( faceSide >= 1 && faceSide <= 6 )
        {
            faceSideNotValid = false; //set the faceSideNotValid to false to end the while loop
        }
        else //otherwise, leave the boolean faceSideNotValid true as they haven't entered a correct number
        {
            System.out.println("Number entered invalid please try again!");
            faceSideNotValid = true;
        }

    }

    public int returnXCoordinate() //returns the die's x Coordinate
    {
        return xA;
    }
    public int returnYCoordinate()//returns the die's y Coordinate
    {
        return yA;
    }
    public int returnFaceSide()//returns the die's face side (location)
    {
        return faceSide;
    }

}//end class BrainsOfTheOperation 
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sujkaa
  • 1
  • 1
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). The 3 source files seen above are *almost* an MCVE, which means the 3 classes need to be merged into a single source file by demoting the two classes with no `main(String[])` method to default access and pasting them in at the bottom of the main class. – Andrew Thompson Jun 07 '15 at 06:47
  • BTW - can you add (links to) images of a) what you see now. b) what you expect to see. I have run the code and am still not clear on what it is that is expected from the code. *"I have read that writing on top frames is ill-advised, but I am waiting on my instructor to let me know if I can just implement Swing components instead."* Thos two things together make no sense. In **either** Swing or AWT we can paint to a top level container (but should not do that). – Andrew Thompson Jun 07 '15 at 07:03
  • Thank you for assisting @AndrewThompson with trying to ensure I get can a wider audience. I found my problem, and it was because I didn't supply Graphic's class drawLine with both coordinates. I only give it the start position of the line and not where the line would end up. I do truly appreciate what you asked me to clarify though. – Sujkaa Jun 10 '15 at 01:49

0 Answers0