Class StdDraw 类 StdDraw

  • All Implemented Interfaces:
    所有已实现的接口:
    ActionListener, KeyListener, MouseListener, MouseMotionListener, EventListener

    public final class StdDraw
    extends Object
    implements ActionListener, MouseListener, MouseMotionListener, KeyListener
    The StdDraw class provides static methods for creating drawings with your programs. It uses a simple graphics model that allows you to create drawings consisting of points, lines, squares, circles, and other geometric shapes in a window on your computer and to save the drawings to a file.
    StdDraw 类提供了用于在程序中创建绘图的静态方法。它使用了一个简单的图形模型,允许您在计算机窗口中创建由点、线、正方形、圆等几何形状组成的绘图,并将这些绘图保存到文件中。

    Standard drawing also includes facilities for text, color, pictures, and animation, along with user interaction via the keyboard and mouse.
    标准绘图还包括文本、颜色、图片和动画的功能,以及通过键盘和鼠标进行用户交互。

    Getting started. To use this class, you must have StdDraw.class in your Java classpath. If you used our autoinstaller, you should be all set. Otherwise, either download stdlib.jar and add to your Java classpath or download StdDraw.java and put a copy in your working directory.
    开始了。要使用这个类,你必须在你的 Java 类路径中有 StdDraw.class 。如果你使用我们的自动安装程序,你应该已经准备就绪了。否则,要么下载 stdlib.jar 并将其添加到你的 Java 类路径中,要么下载 StdDraw.java 并将一份副本放在你的工作目录中。

    Now, cut-and-paste the following short program into your editor:
    现在,将下面的简短程序剪切粘贴到你的编辑器中:

       public class TestStdDraw {
           public static void main(String[] args) {
               StdDraw.setPenRadius(0.05);
               StdDraw.setPenColor(StdDraw.BLUE);
               StdDraw.point(0.5, 0.5);
               StdDraw.setPenColor(StdDraw.MAGENTA);
               StdDraw.line(0.2, 0.2, 0.8, 0.2);
           }
       }
      
    If you compile and execute the program, you should see a window appear with a thick magenta line and a blue point. This program illustrates the two main types of methods in standard drawing—methods that draw geometric shapes and methods that control drawing parameters.
    如果您编译并执行程序,您应该会看到一个窗口出现,窗口上有一条粗粉红色的线和一个蓝点。这个程序展示了标准绘图中的两种主要类型的方法——绘制几何图形的方法和控制绘图参数的方法。

    The methods StdDraw.line() and StdDraw.point() draw lines and points; the methods StdDraw.setPenRadius() and StdDraw.setPenColor() control the line thickness and color.
    方法 StdDraw.line()StdDraw.point() 用于绘制线条和点;方法 StdDraw.setPenRadius()StdDraw.setPenColor() 用于控制线条的粗细和颜色。

    Points and lines. You can draw points and line segments with the following methods:
    点和线。您可以通过以下方法绘制点和线段:

    The x- and y-coordinates must be in the drawing area (between 0 and 1 and by default) or the points and lines will not be visible.
    x 和 y 坐标必须在绘图区域内(默认为 0 到 1 之间),否则点和线将不可见。

    Squares, circles, rectangles, and ellipses. You can draw squares, circles, rectangles, and ellipses using the following methods:
    正方形、圆形、矩形和椭圆。您可以使用以下方法绘制正方形、圆形、矩形和椭圆:

    All of these methods take as arguments the location and size of the shape. The location is always specified by the x- and y-coordinates of its center. The size of a circle is specified by its radius and the size of an ellipse is specified by the lengths of its semi-major and semi-minor axes. The size of a square or rectangle is specified by its half-width or half-height.
    所有这些方法都以形状的位置和大小作为参数。位置总是由其中心的 x 和 y 坐标指定。圆的大小由其半径指定,椭圆的大小由其半长轴和半短轴的长度指定。正方形或矩形的大小由其半宽度或半高度指定。

    The convention for drawing squares and rectangles is parallel to those for drawing circles and ellipses, but may be unexpected to the uninitiated.
    绘制正方形和矩形的惯例与绘制圆和椭圆的惯例相似,但对于未接触过的人来说可能是意外的。

    The methods above trace outlines of the given shapes. The following methods draw filled versions:
    上述方法描绘了给定形状的轮廓。以下方法绘制填充版本:

    Circular arcs. You can draw circular arcs with the following method:
    圆弧。您可以使用以下方法绘制圆弧:

    The arc is from the circle centered at (x, y) of the specified radius. The arc extends from angle1 to angle2. By convention, the angles are polar (counterclockwise angle from the x-axis) and represented in degrees. For example, StdDraw.arc(0.0, 0.0, 1.0, 0, 90) draws the arc of the unit circle from 3 o'clock (0 degrees) to 12 o'clock (90 degrees).
    弧是从以(x,y)为中心的指定半径的圆弧。该弧从 angle1 延伸到 angle2。按照约定,角度是极坐标角度(逆时针角度从 x 轴开始计算),并用度数表示。例如, StdDraw.arc(0.0, 0.0, 1.0, 0, 90) 绘制了单位圆的圆弧,从 3 点钟方向(0 度)到 12 点钟方向(90 度)。

    Polygons. You can draw polygons with the following methods:
    多边形。您可以使用以下方法绘制多边形:

    The points in the polygon are (x[i], y[i]). For example, the following code fragment draws a filled diamond with vertices (0.1, 0.2), (0.2, 0.3), (0.3, 0.2), and (0.2, 0.1):
    多边形中的点为( x[i]y[i] )。例如,以下代码片段绘制了一个填充的菱形,其顶点为(0.1,0.2)、(0.2,0.3)、(0.3,0.2)和(0.2,0.1):

       double[] x = { 0.1, 0.2, 0.3, 0.2 };
       double[] y = { 0.2, 0.3, 0.2, 0.1 };
       StdDraw.filledPolygon(x, y);
      

    Pen size. The pen is circular, so that when you set the pen radius to r and draw a point, you get a circle of radius r. Also, lines are of thickness 2r and have rounded ends. The default pen radius is 0.002 and is not affected by coordinate scaling.
    笔的大小。笔是圆形的,因此当您将笔半径设置为 r 并绘制一个点时,您得到一个半径为 r 的圆。此外,线条的厚度为 2r,并且具有圆形端头。默认的笔半径为 0.002,不受坐标缩放的影响。

    This default pen radius is about 1/500 the width of the default canvas, so that if you draw 200 points equally spaced along a horizontal or vertical line, you will be able to see individual circles, but if you draw 250 such points, the result will look like a line.
    默认的笔半径约为默认画布宽度的 1/500,因此如果您在水平或垂直线条上均匀绘制 200 个点,您将能够看到单个圆圈,但如果您绘制 250 个这样的点,结果看起来将像一条线。

    For example, StdDraw.setPenRadius(0.01) makes the thickness of the lines and the size of the points to be five times the 0.002 default. To draw points with the minimum possible radius (one pixel on typical displays), set the pen radius to 0.0.
    例如, StdDraw.setPenRadius(0.01) 将线条的厚度和点的大小设为默认值 0.002 的五倍。要绘制具有最小可能半径(典型显示器上的一个像素)的点,请将笔的半径设置为 0.0。

    Pen color. All geometric shapes (such as points, lines, and circles) are drawn using the current pen color. By default, it is black. You can change the pen color with the following methods:
    笔颜色。所有几何形状(如点、线和圆)都使用当前的笔颜色绘制。默认情况下为黑色。您可以通过以下方法更改笔颜色:

    The first method allows you to specify colors using the RGB color system. This color picker is a convenient way to find a desired color.
    第一种方法允许您使用 RGB 颜色系统指定颜色。这个颜色选择器是一种方便的方法来找到所需的颜色。

    The second method allows you to specify colors using the Color data type, which is defined in Java's java.awt package. Standard drawing defines a number of predefined colors including BLACK, WHITE, RED, GREEN, and BLUE. For example, StdDraw.setPenColor(StdDraw.RED) sets the pen color to red.
    第二种方法允许您使用 Color 数据类型指定颜色,该数据类型在 Java 的 java.awt 包中定义。标准绘图定义了多种预定义颜色,包括 BLACKWHITEREDGREENBLUE 。例如, StdDraw.setPenColor(StdDraw.RED) 将画笔颜色设置为红色。

    Window title. By default, the standard drawing window title is "Standard Draw". You can change the title with the following method:
    窗口标题。默认情况下,标准绘图窗口的标题是"标准绘图"。您可以使用以下方法更改标题:

    This sets the standard drawing window title to the specified string.
    将标准绘图窗口标题设置为指定的字符串。

    Canvas size. By default, all drawing takes places in a 512-by-512 canvas. The canvas does not include the window title or window border. You can change the size of the canvas with the following method:
    画布大小。默认情况下,所有绘图都在一个 512 乘 512 的画布上进行。画布不包括窗口标题或窗口边框。您可以使用以下方法更改画布的大小:

    This sets the canvas size to be width-by-height pixels. It also clears the current drawing using the default background color (white). Ordinarily, this method is called only once, at the very beginning of a program. For example, StdDraw.setCanvasSize(800, 800) sets the canvas size to be 800-by-800 pixels.
    这将画布大小设置为宽度乘以高度的像素。它还使用默认背景色(白色)清除当前的绘图。通常,此方法仅在程序的最开始时调用一次。例如, StdDraw.setCanvasSize(800, 800) 将画布大小设置为 800 x 800 像素。

    Canvas scale and coordinate system. By default, all drawing takes places in the unit square, with (0, 0) at lower left and (1, 1) at upper right. You can change the default coordinate system with the following methods:
    画布比例和坐标系。默认情况下,所有绘图都在单位正方形中进行,其中左下角为(0,0),右上角为(1,1)。您可以使用以下方法更改默认坐标系:

    The arguments are the coordinates of the minimum and maximum x- or y-coordinates that will appear in the canvas. For example, if you wish to use the default coordinate system but leave a small margin, you can call StdDraw.setScale(-.05, 1.05).
    参数是将出现在画布中的最小和最大 x 或 y 坐标的坐标。 例如,如果您希望使用默认坐标系但保留一小部分空白,则可以调用 StdDraw.setScale(-.05, 1.05)

    These methods change the coordinate system for subsequent drawing commands; they do not affect previous drawings. These methods do not change the canvas size; so, if the x- and y-scales are different, squares will become rectangles and circles will become ellipses.
    这些方法更改了后续绘图命令的坐标系统;它们不会影响先前的绘图。这些方法不会改变画布大小;因此,如果 x 轴和 y 轴的刻度不同,正方形将变为矩形,圆形将变为椭圆。

    Text. You can use the following methods to annotate your drawings with text:
    文本。您可以使用以下方法为您的绘图添加文本注释:

    The first two methods write the specified text in the current font, centered at (x, y). The second method allows you to rotate the text. The last two methods either left- or right-align the text at (x, y).
    前两种方法在当前位置 (x, y) 以当前字体写入指定文本,文本居中。第二种方法允许您旋转文本。最后两种方法则将文本左对齐或右对齐于 (x, y)。

    The default font is a Sans Serif font with point size 16. You can use the following method to change the font:
    默认字体为无衬线字体,字号为 16。您可以使用以下方法更改字体:

    To specify the font, you use the Font data type, which is defined in Java's java.awt package. This allows you to choose the face, size, and style of the font. For example, the following code fragment sets the font to Arial Bold, 60 point. The import statement allows you to refer to Font directly, without needing the fully qualified name java.awt.Font.
    为了指定字体,您使用在 Java 的 java.awt 包中定义的 Font 数据类型。这允许您选择字体的样式、大小和风格。例如,以下代码片段将字体设置为 Arial Bold,60 磅。 import 语句允许您直接引用 Font ,而无需使用完全限定的名称 java.awt.Font

       import java.awt.Font;
       ...
       Font font = new Font("Arial", Font.BOLD, 60);
       StdDraw.setFont(font);
       StdDraw.text(0.5, 0.5, "Hello, World");
      

    Images. You can use the following methods to add images to your drawings:
    图片。您可以使用以下方法将图像添加到您的绘图中:

    These methods draw the specified image, centered at (x, y). The image must be in a supported file format (typically JPEG, PNG, GIF, TIFF, and BMP). The image will display at its native size, independent of the coordinate system.
    这些方法在坐标(x,y)处居中绘制指定的图像。图像必须在支持的文件格式中(通常为 JPEG、PNG、GIF、TIFF 和 BMP)。图像将以其固有大小显示,与坐标系独立。

    Optionally, you can rotate the image a specified number of degrees counterclockwise or rescale it to fit snugly inside a bounding box.
    可选择地,您可以将图像逆时针旋转指定角度,或者重新调整大小使其完全适应边界框内。

    Saving to a file. You can save your image to a file using the File → Save menu option. You can also save a file programmatically using the following method:
    保存到文件。您可以使用“文件”→“保存”菜单选项将图像保存到文件。您还可以使用以下方法以编程方式保存文件:

    You can save the drawing to a file in a supported file format (typically JPEG, PNG, GIF, TIFF, and BMP).
    您可以将绘图保存到支持的文件格式(通常为 JPEG、PNG、GIF、TIFF 和 BMP 格式)的文件中。

    File formats. The StdDraw class supports reading and writing images to any of the file formats supported by javax.imageio (typically JPEG, PNG, GIF, TIFF, and BMP). The file extensions corresponding to JPEG, PNG, GIF, TIFF, and BMP, are .jpg, .png, .gif, .tif, and .bmp, respectively.
    文件格式。 StdDraw 类支持将图像读取和写入到 javax.imageio 支持的任何文件格式(通常是 JPEG、PNG、GIF、TIFF 和 BMP)。对应于 JPEG、PNG、GIF、TIFF 和 BMP 的文件扩展名分别是 .jpg.png.gif.tif.bmp

    We recommend using PNG for drawing that consist solely of geometric shapes and JPEG for drawings that contains pictures. The JPEG file format does not support transparent backgrounds.
    我们建议对仅包含几何形状的图形使用 PNG,对包含图片的图形使用 JPEG。JPEG 文件格式不支持透明背景。

    Clearing the canvas. To clear the entire drawing canvas, you can use the following methods:
    清除画布。要清除整个绘图画布,您可以使用以下方法:

    The first method clears the canvas to the default background color (white); the second method allows you to specify the background color. For example, StdDraw.clear(StdDraw.LIGHT_GRAY) clears the canvas to a shade of gray. To make the background transparent, call StdDraw.clear(StdDraw.TRANSPARENT).
    第一种方法将画布清空为默认背景色(白色);第二种方法允许您指定背景色。例如, StdDraw.clear(StdDraw.LIGHT_GRAY) 将画布清空为一种灰色阴影。要使背景透明,请调用 StdDraw.clear(StdDraw.TRANSPARENT)

    Computer animations and double buffering. Double buffering is one of the most powerful features of standard drawing, enabling computer animations. The following methods control the way in which objects are drawn:
    计算机动画与双缓冲。双缓冲是标准绘图中最强大的功能之一,使计算机动画成为可能。以下方法控制对象的绘制方式:

    By default, double buffering is disabled, which means that as soon as you call a drawing method—such as point() or line()—the results appear on the screen.
    默认情况下,双缓冲被禁用,这意味着当你调用绘图方法时——比如 point()line() ——结果会立即在屏幕上显示。

    When double buffering is enabled by calling enableDoubleBuffering(), all drawing takes place on the offscreen canvas. The offscreen canvas is not displayed. Only when you call show() does your drawing get copied from the offscreen canvas to the onscreen canvas, where it is displayed in the standard drawing window.
    当通过调用 enableDoubleBuffering() 启用双缓冲时,所有绘画都在离屏画布上进行。离屏画布不会显示。只有当您调用 show() 时,您的绘画才会从离屏画布复制到屏幕画布上,在标准绘画窗口中显示出来。

    You can think of double buffering as collecting all of the lines, points, shapes, and text that you tell it to draw, and then drawing them all simultaneously, upon request.
    您可以将双缓冲视为收集您指示它绘制的所有线条、点、形状和文本,然后在请求时同时绘制它们。

    The most important use of double buffering is to produce computer animations, creating the illusion of motion by rapidly displaying static drawings. To produce an animation, repeat the following four steps:
    双缓冲的最重要用途是生成计算机动画,通过快速显示静态图来制造运动的视觉错觉。要生成动画,请重复以下四个步骤:

    • Clear the offscreen canvas.
      清除屏幕外的画布。
    • Draw objects on the offscreen canvas.
      在离屏画布上绘制对象。
    • Copy the offscreen canvas to the onscreen canvas.
      将离屏画布复制到屏幕画布上。
    • Wait for a short while.
      稍等片刻。

    The clear(), show(), and pause(int t) methods support the first, third, and fourth of these steps, respectively.
    clear()show()pause(int t) 方法分别支持这些步骤中的第一、第三和第四步。

    For example, this code fragment animates two balls moving in a circle.
    例如,此代码片段演示了两个小球在圆周运动的动画。

       StdDraw.setScale(-2.0, +2.0);
       StdDraw.enableDoubleBuffering();
    
       for (double t = 0.0; true; t += 0.02) {
           double x = Math.sin(t);
           double y = Math.cos(t);
           StdDraw.clear();
           StdDraw.filledCircle(x, y, 0.1);
           StdDraw.filledCircle(-x, -y, 0.1);
           StdDraw.show();
           StdDraw.pause(20);
       }
      
    Without double buffering, the balls would flicker as they move.
    没有双缓冲,球在移动时会闪烁。

    Keyboard and mouse inputs. Standard drawing has very basic support for keyboard and mouse input. It is much less powerful than most user interface libraries provide, but also much simpler. You can use the following methods to intercept mouse events:
    键盘和鼠标输入。 标准绘图对键盘和鼠标输入有非常基本的支持。 它比大多数用户界面库提供的功能要弱,但也简单得多。 您可以使用以下方法拦截鼠标事件:

    The first method tells you whether a mouse button is currently being pressed. The last two methods tells you the x- and y-coordinates of the mouse's current position, using the same coordinate system as the canvas (the unit square, by default). You should use these methods in an animation loop that waits a short while before trying to poll the mouse for its current state.
    第一种方法告诉你鼠标按钮是否当前被按下。最后两种方法告诉你鼠标当前位置的 x 和 y 坐标,使用与画布相同的坐标系统(默认情况下为单位正方形)。你应该在一个动画循环中使用这些方法,该循环在尝试查询鼠标的当前状态之前等待一段时间。

    You can use the following methods to intercept keyboard events:
    您可以使用以下方法拦截键盘事件:

    If the user types lots of keys, they will be saved in a list until you process them. The first method tells you whether the user has typed a key (that your program has not yet processed).
    如果用户按下了大量按键,它们会被保存在一个列表中直到你处理它们。第一个方法告诉你用户是否已经输入了一个尚未被你的程序处理过的按键。

    The second method returns the next key that the user typed (that your program has not yet processed) and removes it from the list of saved keystrokes. The third method tells you whether a key is currently being pressed.
    第二种方法返回用户输入的下一个键(您的程序尚未处理)并将其从保存的按键列表中移除。第三种方法告诉您当前是否有键被按下。

    Accessing control parameters. You can use the following methods to access the current pen color, pen radius, and font:
    访问控制参数。您可以使用以下方法访问当前笔颜色、笔半径和字体:

    These methods are useful when you want to temporarily change a control parameter and, later, reset it back to its original value.
    这些方法在您想临时改变控制参数,并在之后将其重置回原始值时非常有用。

    Corner cases. Here are some corner cases.
    边缘情况。这里有一些边缘情况。

    • Drawing an object outside (or partly outside) the canvas is permitted. However, only the part of the object that appears inside the canvas will be visible.
      在画布之外(或部分在画布之外)绘制物体是允许的。然而,只有物体出现在画布内部的部分才会可见。
    • Due to floating-point issues, an object drawn with an x- or y-coordinate that is way outside the canvas (such as the line segment from (0.5, –10^308) to (0.5, 10^308) may not be visible even in the part of the canvas where it should be.
      由于浮点数问题,使用 x 或 y 坐标远离画布边界的对象(例如线段从(0.5, –10^308)到(0.5, 10^308))可能即使在其应该出现的画布部分也不可见。
    • Any method that is passed a null argument will throw an IllegalArgumentException.
      任何传递了 null 参数的方法都会抛出一个 IllegalArgumentException 异常。
    • Any method that is passed a Double.NaN, Double.POSITIVE_INFINITY, or Double.NEGATIVE_INFINITY argument will throw an IllegalArgumentException.
      任何传入 Double.NaNDouble.POSITIVE_INFINITYDouble.NEGATIVE_INFINITY 参数的方法都会抛出 IllegalArgumentException 异常。

    Performance tricks. Standard drawing is capable of drawing large amounts of data. Here are a few tricks and tips:
    性能技巧。标准绘图可以绘制大量数据。以下是一些技巧和建议:

    • Use double buffering for static drawing with a large number of objects. That is, call enableDoubleBuffering() before the sequence of drawing commands and call show() afterwards. Incrementally displaying a complex drawing while it is being created can be intolerably inefficient on many computer systems.
      对于绘制大量对象的静态绘图,使用双缓冲。也就是说,在一系列绘图命令之前调用 enableDoubleBuffering() ,在之后调用 show() 。在创建复杂绘图时逐步显示可能在许多计算机系统上效率低下。
    • When drawing computer animations, call show() only once per frame, not after drawing each individual object.
      在绘制计算机动画时,每帧只调用一次 show() ,而不是在绘制每个单独对象后调用。
    • If you call picture() multiple times with the same filename, Java will cache the image, so you do not incur the cost of reading from a file each time.
      如果您使用相同的文件名多次调用 picture() ,Java 会缓存图像,因此您每次读取文件时都不需要承担成本。

    Known bugs and issues.
    已知的错误和问题。

    • The picture() methods may not draw the portion of the image that is inside the canvas if the center point (x, y) is outside the canvas. This bug appears only on some systems.
      使用 picture() 方法可能不会绘制图像在画布内部的部分,如果中心点(x,y)在画布外部。这个错误只会出现在一些系统上。

    Reference. For additional documentation, see Section 1.5 of Computer Science: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.
    参考。有关更多文档,请参阅 Robert Sedgewick 和 Kevin Wayne 的《计算机科学:一种跨学科方法》第 1.5 节。

    Author: 作者:
    Robert Sedgewick, Kevin Wayne
    罗伯特·塞奇威克,凯文·韦恩
    • Field Summary 领域总结

      Fields 领域 
      Modifier and Type 修改器和类型 Field 领域 Description 描述
      static Color AQUA
      The color aqua (0, 255, 255).
      青绿色 (0, 255, 255)。
      static Color BLACK
      The color black (0, 0, 0).
      颜色黑色(0,0,0)。
      static Color BLUE
      The color blue (0, 0, 255).
      蓝色(0, 0, 255)。
      static Color BOOK_BLUE
      The shade of blue used in Introduction to Programming in Java.
      《Java 编程入门》中使用的蓝色阴影。
      static Color BOOK_LIGHT_BLUE
      The shade of light blue used in Introduction to Programming in Java.
      在《Java 编程导论》中使用的浅蓝色。
      static Color BOOK_RED
      The shade of red used in Algorithms, 4th edition.
      《算法(第 4 版)》中使用的红色阴影。
      static Color CYAN
      The color cyan (0, 255, 255).
      青色 (0, 255, 255)。
      static Color DARK_GRAY
      The color dark gray (64, 64, 64).
      颜色深灰(64, 64, 64)。
      static Color FUSCIA
      The color fuscia (255, 0, 255).
      紫红色(255, 0, 255)。
      static Color GRAY
      The color gray (128, 128, 128).
      灰色(128, 128, 128)。
      static Color GREEN
      The color green (0, 128, 0).
      绿色 (0, 128, 0)。
      static Color LIGHT_GRAY
      The color light gray (192, 192, 192).
      浅灰色(192,192,192)。
      static Color LIME
      The color lime (0, 255, 0).
      颜色酸橙 (0, 255, 0)。
      static Color MAGENTA
      The color magenta (255, 0, 255).
      品红色 (255, 0, 255)。
      static Color MAROON
      The color maroon (128, 0, 0).
      深褐色(128, 0, 0)。
      static Color NAVY
      The color navy (0, 0, 128).
      海军蓝(0, 0, 128)。
      static Color OLIVE
      The color olive (128, 128, 0).
      橄榄色(128, 128, 0)。
      static Color ORANGE
      The color orange (255, 200, 0).
      橙色(255,200,0)。
      static Color PINK
      The color pink (255, 175, 175).
      颜色粉红色(255, 175, 175)。
      static Color PRINCETON_ORANGE
      The shade of orange used in Princeton University's identity.
      普林斯顿大学标识中使用的橙色阴影。
      static Color PURPLE
      The color purple (128, 0, 128).
      紫色(128,0,128)。
      static Color RED
      The color red (255, 0, 0).
      红色 (255, 0, 0)。
      static Color SILVER
      The color silver (192, 192, 192).
      银色 (192, 192, 192)。
      static Color TEAL
      The color teal (0, 128, 128).
      青绿色(0,128,128)。
      static Color TRANSPARENT
      A 100% transparent color, for a transparent background.
      100% 透明的颜色,用于透明背景。
      static Color WHITE
      The color white (255, 255, 255).
      颜色白色(255,255,255)。
      static Color YELLOW
      The color yellow (255, 255, 0).
      黄色(255,255,0)。
    • Method Summary 方法总结

      All Methods 所有方法 Static Methods 静态方法 Instance Methods 实例方法 Concrete Methods 混凝土方法 
      Modifier and Type 修饰符和类型 Method 方法 Description 描述
      void actionPerformed​(ActionEvent event)
      This method cannot be called directly.
      这种方法不能被直接调用。
      static void arc​(double x, double y, double radius, double angle1, double angle2)
      Draws a circular arc of the specified radius, centered at (x, y), from angle1 to angle2 (in degrees).
      以指定的半径绘制一个圆弧,以角度 1 到角度 2 为中心(x,y)。
      static void circle​(double x, double y, double radius)
      Draws a circle of the specified radius, centered at (x, y).
      以指定半径,在(x,y)为中心画一个圆。
      static void clear()
      Clears the screen using the default background color (white).
      使用默认背景颜色(白色)清除屏幕。
      static void clear​(Color color)
      Clears the screen using the specified background color.
      使用指定的背景颜色清除屏幕。
      static void close()
      Closes the standard drawing window.
      关闭标准绘图窗口。
      static void disableDoubleBuffering()
      Disables double buffering.
      禁用双缓冲。
      static void ellipse​(double x, double y, double semiMajorAxis, double semiMinorAxis)
      Draws an ellipse with the specified semimajor and semiminor axes, centered at (x, y).
      用指定的半长轴和半短轴,在点 (x, y) 处绘制一个椭圆。
      static void enableDoubleBuffering()
      Enables double buffering.
      启用双缓冲。
      static void filledCircle​(double x, double y, double radius)
      Draws a filled circle of the specified radius, centered at (x, y).
      以指定半径在点(x,y)处居中绘制一个填充的圆。
      static void filledEllipse​(double x, double y, double semiMajorAxis, double semiMinorAxis)
      Draws a filled ellipse with the specified semimajor and semiminor axes, centered at (x, y).
      以指定的半长轴和半短轴在点 (x, y) 处居中绘制填充椭圆。
      static void filledPolygon​(double[] x, double[] y)
      Draws a filled polygon with the vertices (x0, y0), (x1, y1), ..., (xn–1, yn–1).
      绘制一个填充多边形,其顶点为 (x0, y0), (x1, y1), …, (xn–1, yn–1)。
      static void filledRectangle​(double x, double y, double halfWidth, double halfHeight)
      Draws a filled rectangle of the specified size, centered at (x, y).
      绘制指定尺寸的填充矩形,在坐标(x, y)处居中。
      static void filledSquare​(double x, double y, double halfLength)
      Draws a filled square of the specified size, centered at (x, y).
      以(x, y)为中心,绘制指定大小的填充正方形。
      static Color getBackgroundColor()
      Returns the current background color.
      返回当前背景颜色。
      static Font getFont()
      Returns the current font.
      返回当前字体。
      static Color getPenColor()
      Returns the current pen color.
      返回当前笔的颜色。
      static double getPenRadius()
      Returns the current pen radius.
      返回当前笔的半径。
      static boolean hasNextKeyTyped()
      Returns true if the user has typed a key (that has not yet been processed).
      如果用户已经输入了一个尚未处理的按键,则返回 true。
      static boolean isKeyPressed​(int keycode)
      Returns true if the given key is being pressed.
      如果按下给定的键,则返回 true。
      static boolean isMousePressed()
      Returns true if the mouse is being pressed.
      如果鼠标按下,则返回 true。
      void keyPressed​(KeyEvent event)
      This method cannot be called directly.
      该方法不能被直接调用。
      void keyReleased​(KeyEvent event)
      This method cannot be called directly.
      这个方法不能直接调用。
      void keyTyped​(KeyEvent event)
      This method cannot be called directly.
      该方法无法被直接调用。
      static void line​(double x0, double y0, double x1, double y1)
      Draws a line segment between (x0, y0) and (x1, y1).
      在(x0,y0)和(x1,y1)之间画一条线段。
      static void main​(String[] args)
      Test client. 测试客户端。
      void mouseClicked​(MouseEvent event)
      This method cannot be called directly.
      该方法无法直接调用。
      void mouseDragged​(MouseEvent event)
      This method cannot be called directly.
      此方法不能被直接调用。
      void mouseEntered​(MouseEvent event)
      This method cannot be called directly.
      该方法不能直接调用。
      void mouseExited​(MouseEvent event)
      This method cannot be called directly.
      这种方法不能以直接方式调用。
      void mouseMoved​(MouseEvent event)
      This method cannot be called directly.
      此方法不能直接调用。
      void mousePressed​(MouseEvent event)
      This method cannot be called directly.
      这种方法不能直接调用。
      void mouseReleased​(MouseEvent event)
      This method cannot be called directly.
      不能直接调用这种方法。
      static double mouseX()
      Returns the x-coordinate of the mouse.
      返回鼠标的 x 坐标。
      static double mouseY()
      Returns the y-coordinate of the mouse.
      返回鼠标的纵坐标。
      static char nextKeyTyped()
      Returns the next key that was typed by the user (that your program has not already processed).
      返回用户键入的下一个键(您的程序尚未处理的键)。
      static void pause​(int t)
      Pauses for t milliseconds.
      暂停 t 毫秒。
      static void picture​(double x, double y, String filename)
      Draws the specified image centered at (x, y).
      在 (x, y) 处绘制指定的图像并使其居中。
      static void picture​(double x, double y, String filename, double degrees)
      Draws the specified image centered at (x, y), rotated given number of degrees.
      在 (x, y) 处绘制指定图像,按给定的角度旋转。
      static void picture​(double x, double y, String filename, double scaledWidth, double scaledHeight)
      Draws the specified image centered at (x, y), rescaled to the specified bounding box.
      在(x, y)处绘制指定图像,将其重新缩放到指定的边界框中央。
      static void picture​(double x, double y, String filename, double scaledWidth, double scaledHeight, double degrees)
      Draws the specified image centered at (x, y), rotated given number of degrees, and rescaled to the specified bounding box.
      在 (x, y) 处绘制指定的图像,按照给定的角度旋转,并调整为指定的边界框大小。
      static void point​(double x, double y)
      Draws a point centered at (x, y).
      在(x, y)处绘制一个点。
      static void polygon​(double[] x, double[] y)
      Draws a polygon with the vertices (x0, y0), (x1, y1), ..., (xn–1, yn–1).
      绘制一个顶点为 (x0, y0), (x1, y1), ..., (xn–1, yn–1) 的多边形。
      static void rectangle​(double x, double y, double halfWidth, double halfHeight)
      Draws a rectangle of the specified size, centered at (x, y).
      绘制一个指定大小的矩形,中心位于 (x, y)。
      static void save​(String filename)
      Saves the drawing to a file in a supported file format (typically JPEG, PNG, GIF, TIFF, and BMP).
      将图形保存为支持的文件格式(通常为 JPEG、PNG、GIF、TIFF 和 BMP)的文件。
      static void setCanvasSize()
      Sets the canvas (drawing area) to be 512-by-512 pixels.
      将画布(绘图区域)设置为 512x512 像素。
      static void setCanvasSize​(int canvasWidth, int canvasHeight)
      Sets the canvas (drawing area) to be width-by-height pixels.
      将画布(绘图区域)设置为宽乘高像素。
      static void setFont()
      Sets the font to the default font (sans serif, 16 point).
      将字体设置为默认字体(无衬线,16 号字体)。
      static void setFont​(Font font)
      Sets the font to the specified value.
      将字体设置为指定的值。
      static void setPenColor()
      Sets the pen color to the default color (black).
      将画笔颜色设置为默认颜色(黑色)。
      static void setPenColor​(int red, int green, int blue)
      Sets the pen color to the specified RGB color.
      设置画笔颜色为指定的 RGB 颜色。
      static void setPenColor​(Color color)
      Sets the pen color to the specified color.
      将笔的颜色设置为指定的颜色。
      static void setPenRadius()
      Sets the pen size to the default size (0.002).
      将笔的大小设置为默认大小(0.002)。
      static void setPenRadius​(double radius)
      Sets the radius of the pen to the specified size.
      设置笔的半径为指定大小。
      static void setScale()
      Sets both the x-scale and y-scale to the default range (between 0.0 and 1.0).
      将 x 轴和 y 轴的比例都设置为默认范围(0.0 到 1.0 之间)。
      static void setScale​(double min, double max)
      Sets both the x-scale and y-scale to the (same) specified range.
      将 x 轴和 y 轴的比例尺都设置为指定范围(相同)。
      static void setTitle​(String title)
      Sets the title of the standard drawing window to the specified string.
      将标准绘图窗口的标题设置为指定的字符串。
      static void setVisible​(boolean isVisible)
      Makes the drawing window visible or invisible.
      使绘图窗口可见或不可见。
      static void setXscale()
      Sets the x-scale to the default range (between 0.0 and 1.0).
      将 x 轴的比例设置为默认范围(从 0.0 到 1.0 之间)。
      static void setXscale​(double min, double max)
      Sets the x-scale to the specified range.
      将 x 轴刻度设置为指定范围。
      static void setYscale()
      Sets the y-scale to the default range (between 0.0 and 1.0).
      将 y 轴的比例设置为默认范围(在 0.0 到 1.0 之间)。
      static void setYscale​(double min, double max)
      Sets the y-scale to the specified range.
      设置 y 轴比例尺的指定范围。
      static void show()
      Copies offscreen buffer to onscreen buffer.
      将离屏缓冲区的内容复制到屏幕缓冲区。
      static void square​(double x, double y, double halfLength)
      Draws a square of the specified size, centered at (x, y).
      在指定大小处绘制一个以(x, y)为中心的正方形。
      static void text​(double x, double y, String text)
      Writes the given text string in the current font, centered at (x, y).
      在当前字体中以中心对齐的方式写入给定的文本字符串,位置为 (x, y)。
      static void text​(double x, double y, String text, double degrees)
      Writes the given text string in the current font, centered at (x, y) and rotated by the specified number of degrees.
      以当前字体在 (x, y) 处将给定文本字符串写入,居中并按指定角度旋转。
      static void textLeft​(double x, double y, String text)
      Writes the given text string in the current font, left-aligned at (x, y).
      在当前字体中,将给定的文本字符串以左对齐的方式写在 (x, y) 处。
      static void textRight​(double x, double y, String text)
      Writes the given text string in the current font, right-aligned at (x, y).
      在当前字体中将给定的文本字符串写入,右对齐于(x,y)位置。
    • Field Detail 字段详细信息

      • AQUA 水产

        public static final Color AQUA
        The color aqua (0, 255, 255).
        颜色青色 (0, 255, 255)。
      • BLACK 黑色

        public static final Color BLACK
        The color black (0, 0, 0).
        颜色黑(0, 0, 0)。
      • BLUE 蓝色

        public static final Color BLUE
        The color blue (0, 0, 255).
        蓝色(0, 0, 255)。
      • CYAN 青色

        public static final Color CYAN
        The color cyan (0, 255, 255).
        青色(0, 255, 255)。
      • FUSCIA FUSCIA 紫红色

        public static final Color FUSCIA
        The color fuscia (255, 0, 255).
        颜色品红(255, 0, 255)。
      • DARK_GRAY 暗灰色

        public static final Color DARK_GRAY
        The color dark gray (64, 64, 64).
        深灰色(64, 64, 64)。
      • GRAY 灰色

        public static final Color GRAY
        The color gray (128, 128, 128).
        灰色 (128, 128, 128)。
      • GREEN 绿色

        public static final Color GREEN
        The color green (0, 128, 0).
        颜色绿色 (0, 128, 0)。
      • LIGHT_GRAY 浅灰

        public static final Color LIGHT_GRAY
        The color light gray (192, 192, 192).
        颜色为浅灰色(192, 192, 192)。
      • LIME

        public static final Color LIME
        The color lime (0, 255, 0).
        颜色酸橙(0,255,0)。
      • MAGENTA 洋红

        public static final Color MAGENTA
        The color magenta (255, 0, 255).
        品红色(255, 0, 255)。
      • MAROON 栗色

        public static final Color MAROON
        The color maroon (128, 0, 0).
        深红色(128,0,0)。
      • NAVY 海军

        public static final Color NAVY
        The color navy (0, 0, 128).
        海军蓝色 (0, 0, 128)。
      • OLIVE 橄榄

        public static final Color OLIVE
        The color olive (128, 128, 0).
        橄榄色 (128, 128, 0).
      • ORANGE 橙子

        public static final Color ORANGE
        The color orange (255, 200, 0).
        橙色(255, 200, 0)。
      • PINK 粉色

        public static final Color PINK
        The color pink (255, 175, 175).
        粉色(255,175,175)。
      • PURPLE 紫色

        public static final Color PURPLE
        The color purple (128, 0, 128).
        紫色 (128, 0, 128)。
      • RED 红色

        public static final Color RED
        The color red (255, 0, 0).
        颜色红色(255, 0, 0)。
      • SILVER 

        public static final Color SILVER
        The color silver (192, 192, 192).
        银色(192, 192, 192)。
      • TEAL TEAL 蓝绿色

        public static final Color TEAL
        The color teal (0, 128, 128).
        蓝绿色 (0, 128, 128)。
      • WHITE 

        public static final Color WHITE
        The color white (255, 255, 255).
        颜色白色(255, 255, 255).
      • YELLOW 黄色

        public static final Color YELLOW
        The color yellow (255, 255, 0).
        颜色黄色(255,255,0)。
      • TRANSPARENT 透明

        public static final Color TRANSPARENT
        A 100% transparent color, for a transparent background.
        100%透明的颜色,用于透明背景。
      • BOOK_BLUE 书籍蓝

        public static final Color BOOK_BLUE
        The shade of blue used in Introduction to Programming in Java. It is Pantone 300U. The RGB values are approximately (9, 90, 166).
        在 Java 编程导论中使用的蓝色调色板为 Pantone 300U。其 RGB 值约为(9, 90, 166)。
      • BOOK_LIGHT_BLUE 书籍_浅蓝色

        public static final Color BOOK_LIGHT_BLUE
        The shade of light blue used in Introduction to Programming in Java. The RGB values are approximately (103, 198, 243).
        在《Java 编程入门》中使用的浅蓝色。其 RGB 值大约为(103,198,243)。
      • BOOK_RED 书籍_红色

        public static final Color BOOK_RED
        The shade of red used in Algorithms, 4th edition. It is Pantone 1805U. The RGB values are approximately (150, 35, 31).
        《算法》第四版中使用的红色阴影。它是潘通 1805U。RGB 值大约为(150,35,31)。
      • PRINCETON_ORANGE 普林斯顿橙色

        public static final Color PRINCETON_ORANGE
        The shade of orange used in Princeton University's identity. It is PMS 158. The RGB values are approximately (245, 128, 37).
        普林斯顿大学标识中使用的橙色色调是 PMS 158。RGB 数值大约为(245,128,37)。
    • Method Detail 方法细节

      • setVisible

        public static void setVisible​(boolean isVisible)
        Makes the drawing window visible or invisible.
        使绘图窗口可见或不可见。
        Parameters: 参数:
        isVisible - if true, makes the drawing window visible, otherwise hides the drawing window.
        isVisible - 如果 true ,则使绘图窗口可见,否则隐藏绘图窗口。
      • setCanvasSize

        public static void setCanvasSize()
        Sets the canvas (drawing area) to be 512-by-512 pixels. This also clears the current drawing using the default background color (white). Ordinarily, this method is called once, at the very beginning of a program.
        设置画布(绘图区域)大小为 512x512 像素。这也会使用默认的背景颜色(白色)清除当前的绘图。通常情况下,这个方法在程序开始的时候调用一次。
      • setCanvasSize 设置画布大小

        public static void setCanvasSize​(int canvasWidth,
                                         int canvasHeight)
        Sets the canvas (drawing area) to be width-by-height pixels. This also clears the current drawing using the default background color (white). Ordinarily, this method is called once, at the very beginning of a program.
        将画布(绘图区域)设置为宽度乘以高度的像素。这还使用默认背景色(白色)清除当前绘图。通常情况下,这个方法在程序开始时只调用一次。
        Parameters: 参数:
        canvasWidth - the width as a number of pixels
        canvasWidth - 宽度以像素为单位的数值
        canvasHeight - the height as a number of pixels
        canvasHeight - 以像素为单位的高度
        Throws: 抛出异常:
        IllegalArgumentException - unless both canvasWidth and canvasHeight are positive
        IllegalArgumentException - 除非 canvasWidthcanvasHeight 都是正数
      • close 关闭

        public static void close()
        Closes the standard drawing window. This allows the client program to terminate instead of requiring the user to close the standard drawing window manually. Drawing after calling this method will restore the previous window state.
        关闭标准绘图窗口。这样可以使客户端程序终止,而不需要用户手动关闭标准绘图窗口。调用此方法后继续绘图将恢复之前的窗口状态。
      • setTitle 标题

        public static void setTitle​(String title)
        Sets the title of the standard drawing window to the specified string.
        将标准绘图窗口的标题设置为指定的字符串。
        Parameters: 参数:
        title - the title  title - 标题
        Throws: 抛出:
        IllegalArgumentException - if title is null
        IllegalArgumentException - 如果 titlenull
      • setXscale

        public static void setXscale()
        Sets the x-scale to the default range (between 0.0 and 1.0).
        将 x 轴刻度设置为默认范围(在 0.0 和 1.0 之间)。
      • setYscale setYscale 函数

        public static void setYscale()
        Sets the y-scale to the default range (between 0.0 and 1.0).
        将 y 轴的比例尺设置为默认范围(介于 0.0 和 1.0 之间)。
      • setScale setScale 设定比例

        public static void setScale()
        Sets both the x-scale and y-scale to the default range (between 0.0 and 1.0).
        将 x 轴和 y 轴的比例尺设置为默认范围(从 0.0 到 1.0 之间)。
      • setXscale

        public static void setXscale​(double min,
                                     double max)
        Sets the x-scale to the specified range.
        设置 x 轴比例尺到指定范围。
        Parameters: 参数:
        min - the minimum value of the x-scale
        min - x 轴比例尺的最小值
        max - the maximum value of the x-scale
        max - x 轴的最大值
        Throws: 抛出:
        IllegalArgumentException - if (max == min)  IllegalArgumentException - 如果 (max == min)
        IllegalArgumentException - if either min or max is either NaN or infinite
        IllegalArgumentException - 如果 min 或者 max 是 NaN 或无限大
      • setYscale setYscale 设置 Y 轴比例

        public static void setYscale​(double min,
                                     double max)
        Sets the y-scale to the specified range.
        将 y 轴比例尺设置为指定范围。
        Parameters: 参数:
        min - the minimum value of the y-scale
        min - y 轴刻度的最小值
        max - the maximum value of the y-scale
        max - y 轴的最大值
        Throws: 抛出异常:
        IllegalArgumentException - if (max == min) 如果 (max == min) ,则 IllegalArgumentException
        IllegalArgumentException - if either min or max is either NaN or infinite
        如果 minmax 是 NaN 或无穷大,则 IllegalArgumentException
      • setScale setScale 设置比例

        public static void setScale​(double min,
                                    double max)
        Sets both the x-scale and y-scale to the (same) specified range.
        将 x 轴和 y 轴的尺度都设置为(相同的)指定范围。
        Parameters: 参数:
        min - the minimum value of the x- and y-scales
        min - x 轴和 y 轴刻度的最小值
        max - the maximum value of the x- and y-scales
        max - x 和 y 轴的最大值
        Throws: 投掷:
        IllegalArgumentException - if (max == min)  IllegalArgumentException - 如果 (max == min)
        IllegalArgumentException - if either min or max is either NaN or infinite
        IllegalArgumentException - 如果 minmax 是 NaN 或无穷大
      • clear 明确

        public static void clear()
        Clears the screen using the default background color (white).
        使用默认背景颜色(白色)清除屏幕。
      • clear 明确

        public static void clear​(Color color)
        Clears the screen using the specified background color. To make the background transparent, use StdDraw.TRANSPARENT.
        使用指定的背景颜色清除屏幕。要使背景透明,请使用 StdDraw.TRANSPARENT
        Parameters: 参数:
        color - the color to make the background
        color - 使背景成为该颜色
        Throws: 抛出异常:
        IllegalArgumentException - if color is null
        IllegalArgumentException - 如果 colornull
      • getPenRadius 获取笔半径

        public static double getPenRadius()
        Returns the current pen radius.
        返回当前笔的半径。
        Returns: 返回值:
        the current value of the pen radius
        当前笔半径的值
      • setPenRadius

        public static void setPenRadius()
        Sets the pen size to the default size (0.002). The pen is circular, so that lines have rounded ends, and when you set the pen radius and draw a point, you get a circle of the specified radius. The pen radius is not affected by coordinate scaling.
        将笔尺寸设置为默认尺寸(0.002)。笔是圆形的,因此线条具有圆形末端,当您设置笔半径并绘制一个点时,您会得到一个指定半径的圆。笔半径不受坐标缩放的影响。
      • setPenRadius 设置画笔宽度

        public static void setPenRadius​(double radius)
        Sets the radius of the pen to the specified size. The pen is circular, so that lines have rounded ends, and when you set the pen radius and draw a point, you get a circle of the specified radius. The pen radius is not affected by coordinate scaling.
        将笔的半径设定为指定大小。 笔是圆形的,因此线条的端点是圆形的。 当您设置笔的半径并绘制一个点时,您将得到指定半径的圆。 笔的半径不受坐标缩放的影响。
        Parameters: 参数:
        radius - the radius of the pen
        radius - 笔的半径
        Throws: 投掷:
        IllegalArgumentException - if radius is negative, NaN, or infinite
        IllegalArgumentException - 如果 radius 是负数、NaN 或无穷大
      • getPenColor 获取笔颜色

        public static Color getPenColor()
        Returns the current pen color.
        返回当前笔的颜色。
        Returns: 返回值:
        the current pen color
        当前的笔颜色
      • getBackgroundColor 获取背景颜色

        public static Color getBackgroundColor()
        Returns the current background color.
        返回当前的背景颜色。
        Returns: 返回:
        the current background color
        当前的背景颜色
      • setPenColor 设置笔的颜色

        public static void setPenColor()
        Sets the pen color to the default color (black).
        将笔的颜色设置为默认颜色(黑色)。
      • setPenColor 设置笔颜色

        public static void setPenColor​(Color color)
        Sets the pen color to the specified color.
        将笔的颜色设置为指定的颜色。

        There are a number predefined pen colors, such as StdDraw.BLACK, StdDraw.WHITE, StdDraw.RED, StdDraw.GREEN, and StdDraw.BLUE.
        有一些预定义的笔颜色,例如 StdDraw.BLACKStdDraw.WHITEStdDraw.REDStdDraw.GREENStdDraw.BLUE

        Parameters: 参数:
        color - the color to make the pen
        color - 制作钢笔的颜色
        Throws: 投掷:
        IllegalArgumentException - if color is null
        IllegalArgumentException - 如果 colornull
      • setPenColor 设置笔颜色

        public static void setPenColor​(int red,
                                       int green,
                                       int blue)
        Sets the pen color to the specified RGB color.
        将笔的颜色设置为指定的 RGB 颜色。
        Parameters: 参数:
        red - the amount of red (between 0 and 255)
        red - 红色的数量(介于 0 和 255 之间)
        green - the amount of green (between 0 and 255)
        green - 绿色的数量(在 0 到 255 之间)
        blue - the amount of blue (between 0 and 255)
        blue - 蓝色的数量(介于 0 和 255 之间)
        Throws: 抛出异常:
        IllegalArgumentException - if red, green, or blue is outside its prescribed range
        如果 redgreen ,或 blue 超出其规定的范围 IllegalArgumentException
      • getFont 获取字体

        public static Font getFont()
        Returns the current font.
        返回当前字体。
        Returns: 返回:
        the current font 当前字体
      • setFont

        public static void setFont()
        Sets the font to the default font (sans serif, 16 point).
        将字体设置为默认字体(无衬线字体,16 磅)。
      • setFont

        public static void setFont​(Font font)
        Sets the font to the specified value.
        将字体设置为指定值。
        Parameters: 参数:
        font - the font  font - 字体
        Throws: 投掷:
        IllegalArgumentException - if font is null
        IllegalArgumentException - 如果 fontnull
      • line 线

        public static void line​(double x0,
                                double y0,
                                double x1,
                                double y1)
        Draws a line segment between (x0, y0) and (x1, y1).
        在 (x0, y0) 和 (x1, y1) 之间绘制一条线段。
        Parameters: 参数:
        x0 - the x-coordinate of one endpoint
        x0 - 一个端点的 x 坐标
        y0 - the y-coordinate of one endpoint
        y0 - 一个端点的 y 坐标
        x1 - the x-coordinate of the other endpoint
        x1 - 另一个端点的 x 坐标
        y1 - the y-coordinate of the other endpoint
        y1 - 另一个端点的 y 坐标
        Throws: 抛出异常:
        IllegalArgumentException - if any coordinate is either NaN or infinite
        IllegalArgumentException - 如果任何坐标是 NaN 或无穷
      • point 

        public static void point​(double x,
                                 double y)
        Draws a point centered at (x, y). The point is a filled circle whose radius is equal to the pen radius. To draw a single-pixel point, first set the pen radius to 0.
        在(x, y)处绘制一个中心点。该点是一个填充的圆,其半径等于笔的半径。要绘制一个单像素点,首先将笔的半径设置为 0。
        Parameters: 参数:
        x - the x-coordinate of the point
        x - 该点的 x 坐标
        y - the y-coordinate of the point
        y - 该点的 y 坐标
        Throws: 抛出:
        IllegalArgumentException - if either x or y is either NaN or infinite
        如果 xy 是 NaN 或无限,则 IllegalArgumentException
      • circle 圆形

        public static void circle​(double x,
                                  double y,
                                  double radius)
        Draws a circle of the specified radius, centered at (x, y).
        在指定半径的情况下绘制以(x, y)为中心的圆。
        Parameters: 参数:
        x - the x-coordinate of the center of the circle
        x - 圆心的 x 坐标
        y - the y-coordinate of the center of the circle
        y - 圆心的纵坐标
        radius - the radius of the circle
        radius - 圆的半径
        Throws: 掷出:
        IllegalArgumentException - if radius is negative
        IllegalArgumentException - 如果 radius 是负数
        IllegalArgumentException - if any argument is either NaN or infinite
        如果任何一个参数是 NaN 或无穷大
      • filledCircle 填充圆

        public static void filledCircle​(double x,
                                        double y,
                                        double radius)
        Draws a filled circle of the specified radius, centered at (x, y).
        在(x, y)处以指定半径绘制一个填充的圆。
        Parameters: 参数:
        x - the x-coordinate of the center of the circle
        x - 圆的中心点的 x 坐标
        y - the y-coordinate of the center of the circle
        y - 圆心的 y 坐标
        radius - the radius of the circle
        radius - 圆的半径
        Throws: 扔:
        IllegalArgumentException - if radius is negative
        IllegalArgumentException - 如果 radius 为负数
        IllegalArgumentException - if any argument is either NaN or infinite
        如果任何参数是 NaN 或无穷大
      • ellipse 椭圆

        public static void ellipse​(double x,
                                   double y,
                                   double semiMajorAxis,
                                   double semiMinorAxis)
        Draws an ellipse with the specified semimajor and semiminor axes, centered at (x, y).
        绘制一个以 (x, y)为中心,具有指定的半长轴和半短轴的椭圆。
        Parameters: 参数:
        x - the x-coordinate of the center of the ellipse
        x - 椭圆的中心的 x 坐标
        y - the y-coordinate of the center of the ellipse
        y - 椭圆中心的 y 坐标
        semiMajorAxis - is the semimajor axis of the ellipse
        semiMajorAxis - 是椭圆的半长轴
        semiMinorAxis - is the semiminor axis of the ellipse
        semiMinorAxis - 是椭圆的半短轴
        Throws: 投掷:
        IllegalArgumentException - if either semiMajorAxis or semiMinorAxis is negative
        如果 semiMajorAxis 或者 semiMinorAxis 为负数,则结果为 IllegalArgumentException
        IllegalArgumentException - if any argument is either NaN or infinite
        IllegalArgumentException - 如果任何一个参数是 NaN 或无穷大
      • filledEllipse 填充椭圆

        public static void filledEllipse​(double x,
                                         double y,
                                         double semiMajorAxis,
                                         double semiMinorAxis)
        Draws a filled ellipse with the specified semimajor and semiminor axes, centered at (x, y).
        绘制一个充填的椭圆,指定半长轴和半短轴,以 (x, y) 为中心。
        Parameters: 参数:
        x - the x-coordinate of the center of the ellipse
        x - 椭圆中心的 x 坐标
        y - the y-coordinate of the center of the ellipse
        y - 椭圆中心的 y 坐标
        semiMajorAxis - is the semimajor axis of the ellipse
        semiMajorAxis - 是椭圆的半长轴
        semiMinorAxis - is the semiminor axis of the ellipse
        semiMinorAxis - 是椭圆的半短轴
        Throws: 投掷:
        IllegalArgumentException - if either semiMajorAxis or semiMinorAxis is negative
        如果 semiMajorAxis 或者 semiMinorAxis 为负数, 则 IllegalArgumentException -
        IllegalArgumentException - if any argument is either NaN or infinite
        如果任何参数为 NaN 或无穷大
      • arc 弧形

        public static void arc​(double x,
                               double y,
                               double radius,
                               double angle1,
                               double angle2)
        Draws a circular arc of the specified radius, centered at (x, y), from angle1 to angle2 (in degrees).
        从角度 1 绘制一个以(x, y)为中心,半径为指定半径的圆弧,到角度 2(以度为单位)。
        Parameters: 参数:
        x - the x-coordinate of the center of the circle
        x - 圆心的 x 坐标
        y - the y-coordinate of the center of the circle
        y - 圆心的纵坐标
        radius - the radius of the circle
        radius - 圆的半径
        angle1 - the starting angle. 0 would mean an arc beginning at 3 o'clock.
        angle1 - 起始角度。0 表示从 3 点钟开始的弧。
        angle2 - the angle at the end of the arc. For example, if you want a 90 degree arc, then angle2 should be angle1 + 90.
        angle2 - 弧线末端的角度。例如,如果您想要一个 90 度的弧线,那么角度 2 应为角度 1 + 90。
        Throws: 投掷:
        IllegalArgumentException - if radius is negative
        IllegalArgumentException - 如果 radius 为负
        IllegalArgumentException - if any argument is either NaN or infinite
        IllegalArgumentException - 如果任何参数是 NaN 或者无穷大
      • square 正方形

        public static void square​(double x,
                                  double y,
                                  double halfLength)
        Draws a square of the specified size, centered at (x, y).
        绘制一个指定大小的正方形,中心位于 (x, y)。
        Parameters: 参数:
        x - the x-coordinate of the center of the square
        x - 正方形中心的 x 坐标
        y - the y-coordinate of the center of the square
        y - 正方形中心的 y 坐标
        halfLength - one half the length of any side of the square
        halfLength - 正方形任一边长度的一半
        Throws: 抛出异常:
        IllegalArgumentException - if halfLength is negative
        IllegalArgumentException - 如果 halfLength 是负数
        IllegalArgumentException - if any argument is either NaN or infinite
        IllegalArgumentException - 如果任何参数是 NaN 或无穷大
      • filledSquare 填充方块

        public static void filledSquare​(double x,
                                        double y,
                                        double halfLength)
        Draws a filled square of the specified size, centered at (x, y).
        在指定的位置(x, y)绘制一个填充的正方形,大小为指定大小。
        Parameters: 参数:
        x - the x-coordinate of the center of the square
        x - 正方形中心的 x 坐标
        y - the y-coordinate of the center of the square
        y - 正方形中心的 y 坐标
        halfLength - one half the length of any side of the square
        halfLength - 正方形任意一边长度的一半
        Throws: 投掷:
        IllegalArgumentException - if halfLength is negative
        IllegalArgumentException - 如果 halfLength 是负数
        IllegalArgumentException - if any argument is either NaN or infinite
        若任何一个参数是 NaN 或无穷大,则为 IllegalArgumentException
      • rectangle 矩形

        public static void rectangle​(double x,
                                     double y,
                                     double halfWidth,
                                     double halfHeight)
        Draws a rectangle of the specified size, centered at (x, y).
        绘制一个指定大小的矩形,中心位于 (x, y)。
        Parameters: 参数:
        x - the x-coordinate of the center of the rectangle
        x - 矩形中心的 x 坐标
        y - the y-coordinate of the center of the rectangle
        y - 矩形中心的 y 坐标
        halfWidth - one half the width of the rectangle
        halfWidth - 矩形宽度的一半
        halfHeight - one half the height of the rectangle
        halfHeight - 矩形高度的一半
        Throws: 扔出去的引用,
        IllegalArgumentException - if either halfWidth or halfHeight is negative
        IllegalArgumentException - 如果 halfWidthhalfHeight 为负数
        IllegalArgumentException - if any argument is either NaN or infinite
        IllegalArgumentException - 如果任何参数是 NaN 或无穷大
      • filledRectangle 填充矩形

        public static void filledRectangle​(double x,
                                           double y,
                                           double halfWidth,
                                           double halfHeight)
        Draws a filled rectangle of the specified size, centered at (x, y).
        绘制一个指定大小的填充矩形,中心位于 (x, y)。
        Parameters: 参数:
        x - the x-coordinate of the center of the rectangle
        x - 矩形中心点的 x 坐标
        y - the y-coordinate of the center of the rectangle
        y - 矩形中心的 y 坐标
        halfWidth - one half the width of the rectangle
        halfWidth - 矩形宽度的一半
        halfHeight - one half the height of the rectangle
        halfHeight - 矩形的一半高度
        Throws: 抛出异常:
        IllegalArgumentException - if either halfWidth or halfHeight is negative
        如果 halfWidthhalfHeight 为负,则 IllegalArgumentException
        IllegalArgumentException - if any argument is either NaN or infinite
        如果任何参数是 NaN 或无穷大
      • polygon 多边形

        public static void polygon​(double[] x,
                                   double[] y)
        Draws a polygon with the vertices (x0, y0), (x1, y1), ..., (xn–1, yn–1).
        绘制具有顶点(x0,y0),(x1,y1),...,(xn-1,yn-1)的多边形。
        Parameters: 参数:
        x - an array of all the x-coordinates of the polygon
        x - 多边形所有 x 坐标的数组
        y - an array of all the y-coordinates of the polygon
        y - 多边形所有 y 坐标的数组
        Throws: 抛出:
        IllegalArgumentException - unless x[] and y[] are of the same length
        IllegalArgumentException - 除非 x[]y[] 长度相同
        IllegalArgumentException - if any coordinate is either NaN or infinite
        如果任何坐标是 NaN 或无穷大,则 IllegalArgumentException
        IllegalArgumentException - if either x[] or y[] is null
        IllegalArgumentException - 如果 x[]y[]null
      • filledPolygon 填充多边形

        public static void filledPolygon​(double[] x,
                                         double[] y)
        Draws a filled polygon with the vertices (x0, y0), (x1, y1), ..., (xn–1, yn–1).
        绘制一个填充多边形,顶点为 (x0, y0), (x1, y1), ..., (xn–1, yn–1)。
        Parameters: 参数:
        x - an array of all the x-coordinates of the polygon
        x - 多边形所有 x 坐标的数组
        y - an array of all the y-coordinates of the polygon
        y - 多边形所有 y 坐标的数组
        Throws: 投掷:
        IllegalArgumentException - unless x[] and y[] are of the same length
        IllegalArgumentException - 除非 x[]y[] 的长度相同
        IllegalArgumentException - if any coordinate is either NaN or infinite
        IllegalArgumentException - if either x[] or y[] is null
        IllegalArgumentException - 如果 x[]y[] 中的任一条件为 null
      • picture 图片

        public static void picture​(double x,
                                   double y,
                                   String filename)
        Draws the specified image centered at (x, y). The supported image formats are typically JPEG, PNG, GIF, TIFF, and BMP. As an optimization, the picture is cached, so there is no performance penalty for redrawing the same image multiple times (e.g., in an animation).
        绘制指定图像以 (x, y) 为中心。支持的图像格式通常是 JPEG、PNG、GIF、TIFF 和 BMP。作为优化,图片被缓存,因此多次重绘相同的图像(例如在动画中)不会有性能损失。

        However, if you change the picture file after drawing it, subsequent calls will draw the original picture.
        然而,如果在绘制后更改图片文件,则后续调用将绘制原始图片。
        Parameters: 参数:
        x - the center x-coordinate of the image
        x - 图像的中心 x 坐标
        y - the center y-coordinate of the image
        y - 图像的中心 y 坐标
        filename - the name of the image/picture, e.g., "ball.gif"
        filename - 图像/图片的名称,例如,“ball.gif”
        Throws: 投掷:
        IllegalArgumentException - if the image filename is invalid
        IllegalArgumentException - 如果图像文件名无效
        IllegalArgumentException - if either x or y is either NaN or infinite
        IllegalArgumentException - 如果 xy 中有一个是 NaN 或无穷大
      • picture 图片

        public static void picture​(double x,
                                   double y,
                                   String filename,
                                   double degrees)
        Draws the specified image centered at (x, y), rotated given number of degrees. The supported image formats are typically JPEG, PNG, GIF, TIFF, and BMP.
        在指定位置(x, y)以给定角度旋转的方式绘制图像。支持的图像格式通常为 JPEG、PNG、GIF、TIFF 和 BMP。
        Parameters: 参数:
        x - the center x-coordinate of the image
        x - 图像的中心 x 坐标
        y - the center y-coordinate of the image
        y - 图像的中心 y 坐标
        filename - the name of the image/picture, e.g., "ball.gif"
        filename - 图像/图片的名称,例如 "ball.gif"
        degrees - is the number of degrees to rotate counterclockwise
        degrees - 是逆时针旋转的度数
        Throws: 投掷:
        IllegalArgumentException - if the image filename is invalid
        IllegalArgumentException - 如果图像文件名无效
        IllegalArgumentException - if x, y, degrees is NaN or infinite
        IllegalArgumentException - 如果 x , y , degrees 是 NaN 或无穷大
        IllegalArgumentException - if filename is null
        IllegalArgumentException - 如果 filenamenull
      • picture 图片

        public static void picture​(double x,
                                   double y,
                                   String filename,
                                   double scaledWidth,
                                   double scaledHeight)
        Draws the specified image centered at (x, y), rescaled to the specified bounding box. The supported image formats are typically JPEG, PNG, GIF, TIFF, and BMP.
        以(x, y)为中心绘制指定的图像,按照指定的边界框进行重新缩放。通常支持的图像格式包括 JPEG、PNG、GIF、TIFF 和 BMP。
        Parameters: 参数:
        x - the center x-coordinate of the image
        x - 图像的中心 x 坐标
        y - the center y-coordinate of the image
        y - 图像的中心 y 坐标
        filename - the name of the image/picture, e.g., "ball.gif"
        filename - 图像/图片的名称,例如 “ball.gif”
        scaledWidth - the width of the scaled image (in screen coordinates)
        scaledWidth - 缩放图像的宽度(以屏幕坐标为单位)
        scaledHeight - the height of the scaled image (in screen coordinates)
        scaledHeight - 缩放图像的高度(以屏幕坐标表示)
        Throws: 投掷:
        IllegalArgumentException - if either scaledWidth or scaledHeight is negative
        IllegalArgumentException - 如果 scaledWidthscaledHeight 为负数
        IllegalArgumentException - if the image filename is invalid
        IllegalArgumentException - 如果图像文件名无效
        IllegalArgumentException - if x or y is either NaN or infinite
        IllegalArgumentException - 如果 xy 为 NaN 或无穷大
        IllegalArgumentException - if filename is null
        IllegalArgumentException - 如果 filenamenull
      • picture 照片

        public static void picture​(double x,
                                   double y,
                                   String filename,
                                   double scaledWidth,
                                   double scaledHeight,
                                   double degrees)
        Draws the specified image centered at (x, y), rotated given number of degrees, and rescaled to the specified bounding box. The supported image formats are typically JPEG, PNG, GIF, TIFF, and BMP.
        在指定的边界框内,以 (x, y) 为中心绘制指定图像,旋转指定的角度,并重新缩放。支持的图像格式通常包括 JPEG、PNG、GIF、TIFF 和 BMP。
        Parameters: 参数:
        x - the center x-coordinate of the image
        x - 图像的中心 x 坐标
        y - the center y-coordinate of the image
        y - 图像的中心 y 坐标
        filename - the name of the image/picture, e.g., "ball.gif"
        filename - 图像/图片的名称,例如,“ball.gif”
        scaledWidth - the width of the scaled image (in screen coordinates)
        scaledWidth - 缩放图像的宽度(以屏幕坐标计)
        scaledHeight - the height of the scaled image (in screen coordinates)
        scaledHeight - 缩放图像的高度(以屏幕坐标表示)
        degrees - is the number of degrees to rotate counterclockwise
        degrees - 是逆时针旋转的度数
        Throws: 投掷:
        IllegalArgumentException - if either scaledWidth or scaledHeight is negative
        IllegalArgumentException - 如果 scaledWidthscaledHeight 之一为负数
        IllegalArgumentException - if the image filename is invalid
        IllegalArgumentException - 如果图像文件名无效
      • text I'm sorry, but it seems like you forgot to provide the source text for translation. Please provide the text you would like me to translate into Simplified Chinese

        public static void text​(double x,
                                double y,
                                String text)
        Writes the given text string in the current font, centered at (x, y).
        在当前字体中,将给定文本字符串写入,居中于 (x, y)。
        Parameters: 参数:
        x - the center x-coordinate of the text
        x - 文本的中心 x 坐标
        y - the center y-coordinate of the text
        y - 文本的中心 y 坐标
        text - the text to write
        text - 要写的文本
        Throws: 抛出异常:
        IllegalArgumentException - if text is null
        IllegalArgumentException - 如果 textnull
        IllegalArgumentException - if x or y is either NaN or infinite
        IllegalArgumentException - 如果 xy 是 NaN 或无穷大
      • text

        public static void text​(double x,
                                double y,
                                String text,
                                double degrees)
        Writes the given text string in the current font, centered at (x, y) and rotated by the specified number of degrees.
        在当前字体中,将给定文本字符串以指定的度数旋转,居中于 (x, y) 位置。
        Parameters: 参数:
        x - the center x-coordinate of the text
        x - 文本的中心 x 坐标
        y - the center y-coordinate of the text
        y - 文本的中心 y 坐标
        text - the text to write
        text - 要写的文本
        degrees - is the number of degrees to rotate counterclockwise
        degrees - 是逆时针旋转的角度数
        Throws: 投掷:
        IllegalArgumentException - if text is null
        IllegalArgumentException - 如果 textnull
        IllegalArgumentException - if x, y, or degrees is either NaN or infinite
        IllegalArgumentException - 如果 xydegrees 其中任意一个是 NaN 或无穷大
      • textLeft 文本左

        public static void textLeft​(double x,
                                    double y,
                                    String text)
        Writes the given text string in the current font, left-aligned at (x, y).
        在当前字体中将给定文本字符串写入,左对齐于(x,y)。
        Parameters: 参数:
        x - the x-coordinate of the text
        x - 文本的 x 坐标
        y - the y-coordinate of the text
        y - 文本的 y 坐标
        text - the text  text - 该文本
        Throws: 投掷:
        IllegalArgumentException - if text is null
        IllegalArgumentException - 如果 textnull
        IllegalArgumentException - if x or y is either NaN or infinite
        IllegalArgumentException - 如果 x 或者 y 是 NaN 或无穷大
      • textRight 文字正确

        public static void textRight​(double x,
                                     double y,
                                     String text)
        Writes the given text string in the current font, right-aligned at (x, y).
        在当前字体下,将给定文本字符串写入,右对齐于 (x, y)。
        Parameters: 参数:
        x - the x-coordinate of the text
        x - 文本的 x 坐标
        y - the y-coordinate of the text
        y - 文本的 y 坐标
        text - the text to write
        text - 写作的文本
        Throws: 投掷:
        IllegalArgumentException - if text is null
        IllegalArgumentException - 如果 textnull
        IllegalArgumentException - if x or y is either NaN or infinite
        IllegalArgumentException - 如果 x 或者 y 是 NaN 或无穷大
      • pause 暂停

        public static void pause​(int t)
        Pauses for t milliseconds. This method is intended to support computer animations.
        暂停 t 毫秒。此方法旨在支持计算机动画。
        Parameters: 参数:
        t - number of milliseconds
        t - 毫秒数
        Throws: 投掷:
        IllegalArgumentException - if t is negative
        IllegalArgumentException - 如果 t 为负数
      • show 展示

        public static void show()
        Copies offscreen buffer to onscreen buffer. There is no reason to call this method unless double buffering is enabled.
        将离屏缓冲区的内容复制到屏幕缓冲区。除非启用了双缓冲,否则没有理由调用此方法。
      • enableDoubleBuffering enableDoubleBuffering 启用双缓冲

        public static void enableDoubleBuffering()
        Enables double buffering. All subsequent calls to drawing methods such as line(), circle(), and square() will be deferred until the next call to show(). Useful for animations.
        启用双缓冲。所有后续的绘图方法调用,例如 line()circle()square() ,将在下一个 show() 调用之前被延迟。对动画非常有用。
      • disableDoubleBuffering 禁用双重缓冲

        public static void disableDoubleBuffering()
        Disables double buffering. All subsequent calls to drawing methods such as line(), circle(), and square() will be displayed on screen when called. This is the default.
        禁用双缓冲。所有后续对绘图方法的调用,例如 line()circle()square() ,将在调用时显示在屏幕上。这是默认设置。
      • isMousePressed

        public static boolean isMousePressed()
        Returns true if the mouse is being pressed.
        如果鼠标正在被按下,则返回真。
        Returns: 返回:
        true if the mouse is being pressed; false otherwise
        如果鼠标正在被按下,则 true ;否则 false
      • mouseX 鼠标 X

        public static double mouseX()
        Returns the x-coordinate of the mouse.
        返回鼠标的 x 坐标。
        Returns: 返回:
        the x-coordinate of the mouse
        鼠标的 x 坐标
      • mouseY 鼠标

        public static double mouseY()
        Returns the y-coordinate of the mouse.
        返回鼠标的 y 坐标。
        Returns: 返回:
        y-coordinate of the mouse
        鼠标的 y 坐标
      • hasNextKeyTyped

        public static boolean hasNextKeyTyped()
        Returns true if the user has typed a key (that has not yet been processed).
        如果用户已经输入了一个尚未被处理的键,则返回 true。
        Returns: 返回:
        true if the user has typed a key (that has not yet been processed by nextKeyTyped(); false otherwise
        如果用户已输入一个键(尚未被 nextKeyTyped() 处理的键;否则 false
      • nextKeyTyped

        public static char nextKeyTyped()
        Returns the next key that was typed by the user (that your program has not already processed). This method should be preceded by a call to hasNextKeyTyped() to ensure that there is a next key to process. This method returns a Unicode character corresponding to the key typed (such as 'a' or 'A'). It cannot identify action keys (such as F1 and arrow keys) or modifier keys (such as control).
        返回用户输入的下一个键(该键尚未被您的程序处理)。此方法应在调用 hasNextKeyTyped() 之前,以确保有下一个键可供处理。此方法返回与输入的键对应的 Unicode 字符(例如 'a''A' )。它无法识别动作键(如 F1 键和方向键)或修饰键(如 Ctrl 键)。
        Returns: 返回值:
        the next key typed by the user (that your program has not already processed).
        用户输入的下一个按键(您的程序尚未处理的)。
        Throws: 抛出:
        NoSuchElementException - if there is no remaining key
        如果没有剩余的密钥 NoSuchElementException
      • isKeyPressed isKeyPressed 按键是否被按下

        public static boolean isKeyPressed​(int keycode)
        Returns true if the given key is being pressed.
        如果按下了给定的键,则返回 true。

        This method takes the keycode (corresponding to a physical key) as an argument. It can handle action keys (such as F1 and arrow keys) and modifier keys (such as shift and control). See KeyEvent for a description of key codes.
        该方法接受按键码(对应于物理按键)作为参数。它可以处理动作按键(如 F1 和箭头键)和修饰键(如 Shift 和 Control)。有关按键码的描述,请参见 KeyEvent

        Parameters: 参数:
        keycode - the key to check if it is being pressed
        keycode - 检查按键是否被按下的关键
        Returns: 返回结果:
        true if keycode is currently being pressed; false otherwise
        true 如果 keycode 正在被按下;否则 false
      • keyTyped 键入的字符

        public void keyTyped​(KeyEvent event)
        This method cannot be called directly.
        这种方法不能被直接调用。
        Specified by: 指定为:
        keyTyped in interface KeyListener 在接口 KeyListener 中的 keyTyped
      • main 主内容

        public static void main​(String[] args)
        Test client. 测试客户。
        Parameters: 参数:
        args - the command-line arguments
        args - 命令行参数