Java drawString方法不显示文本
我已经浏览了谷歌搜索和一些在线教程,但我仍然被卡住了。似乎很不幸,我被困在使用AWT来完成这个任务,因为Swing会更好。我的任务是画一个骰子 目前,我只是想画一条线,从正方形的角到我想画的模具边缘。我已经读到在顶部框架上写作是不明智的,但我正在等待我的导师让我知道我是否可以只实现Swing组件 目前,我可以让正方形显示出来,但我不能在画完线后让线显示出来。我读过,它是画出来的,但隐藏在另一个框架/对象/组件/某物下。但是,我怎样才能获得在线节目 一旦我能画出线条,我就可以(希望)开始画模具,并在上面画上点。我只是想让台词显示出来,而不是如何做作业 感谢您的帮助Java drawString方法不显示文本,java,awt,drawstring,Java,Awt,Drawstring,我已经浏览了谷歌搜索和一些在线教程,但我仍然被卡住了。似乎很不幸,我被困在使用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
这是我的模具类,它显示模具:
/*
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
import java.util.Scanner;
公营部门运作
{
public int xA,yA;//掷骰子的位置坐标
private int faceSide=0;//骰子显示的是哪一边
私有布尔faceSideNotValid=true;//在while循环中使用,以确保选择了正确的边
public BrainsOfTheOperation()//公共构造函数
{
//留白
}
public void action()//操作方法,用于请求正面,使其有效,然后请求骰子在帧上的坐标
{
扫描器键盘=新扫描器(System.in);//实例化键盘扫描器
while(faceSideNotValid)
{
系统输出打印(“在模具表面输入编号:”);
faceSide=keyboard.nextInt();//取下一个整数
testIfValid();//确保其有效性:如果faceSide>=to 1和faceSide=1&&faceSide要更快获得更好的帮助,请发布一个(最小完整的可验证示例)或(简短、自包含、正确的示例)。上面看到的3个源文件几乎是一个MCVE,这意味着需要将这3个类合并为一个源文件,方法是将两个没有main(String[])
方法的类降级为默认访问,然后将它们粘贴到主类的底部。顺便问一下,您可以添加(链接到)吗a)您现在看到的图片。b)您期望看到的图片。我已经运行了代码,但仍然不清楚代码期望看到的是什么。“我已经读到在顶部框架上写作是不明智的,但我正在等待我的讲师告诉我是否可以改为实现Swing组件。”这两件事放在一起是没有意义的。无论是在Swing还是AWT中,我们都可以在顶级容器中进行绘制(但不应该这样做)。感谢您帮助@AndrewThompson努力确保我能获得更多的观众。我发现了我的问题,这是因为我没有为Graphic的课堂画线提供两个坐标。我只给出线的起始位置,而不是线的终点。我非常感谢您让我澄清的内容。