drawString方法不显示文本

时间:2015-06-05 21:02:45

标签: java awt drawstring

我查看了Google搜索和一些在线教程,但我仍然陷入困境。不幸的是,我很难使用AWT来完成这项任务,因为Swing会更好。我的任务是画一个骰子。

目前我只是试图从广场的角落到我想要绘制的模具边缘画一条线。我已经读到在顶部框架上写作是不明智的,但我正在等待我的导师让我知道我是否可以实现Swing组件。

目前,我可以拿到广场来展示,但是我猜不出它应该被画出来的线。我已经看过它被绘制,但隐藏在另一个框架/对象/组件/东西下。但是,我怎样才能获得线下节目?

一旦我能够显示线条,我可以(希望)开始绘制模具并将点放在上面。我只是想要显示线条,而不是如何进行分配!

任何帮助表示赞赏!

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

这是我的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

最后,我的BrainsOfTheOperation类验证了用户的输入,然后请求坐标:

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 

0 个答案:

没有答案