Question

In: Computer Science

How does one use the standard draw class?

How does one use the standard draw class?

Solutions

Expert Solution

The StdDraw class provides a basic capability 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. 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.

Now, type 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.

Points and lines. You can draw points and line segments with the following methods:

  • point(double x, double y)
  • line(double x1, double y1, double x2, double y2)

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.

Squares, circles, rectangles, and ellipses. You can draw squares, circles, rectangles, and ellipses using the following methods:

  • circle(double x, double y, double radius)
  • ellipse(double x, double y, double semiMajorAxis, double semiMinorAxis)
  • square(double x, double y, double halfLength)
  • rectangle(double x, double y, double halfWidth, double halfHeight)

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. 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:

  • filledCircle(double x, double y, double radius)
  • filledEllipse(double x, double y, double semiMajorAxis, double semiMinorAxis)
  • filledSquare(double x, double y, double radius)
  • filledRectangle(double x, double y, double halfWidth, double halfHeight)

Related Solutions

How does one declare a class variable in C#?
How does one declare a class variable in C#?
Draw a class diagram for your monopoly. You can use any visual tools to draw the...
Draw a class diagram for your monopoly. You can use any visual tools to draw the diagram and you need to utilize different relationships between classes, define the attributes and operations of classes. Nothing too complex please. Just the basics.
Use the UML tool to draw a UML class diagrambased on the descriptions provided below....
Use the UML tool to draw a UML class diagrambased on the descriptions provided below.The diagram should be drawn with a UML toolIt should include all the classes listed below and use appropriate arrows to identify the class relationshipsEach class should include all the described attributes and operations but nothing elseEach constructor and method should include the described parameters and return types - no more and no lessPlease do one of the following in JavaDescriptions of a PriceBecause of the...
The java.awt.Rectangle class of the standard Java library does not supply a method to compute the...
The java.awt.Rectangle class of the standard Java library does not supply a method to compute the area or perimeter of a rectangle. Provide a subclass BetterRectangle of the Rectangle class that has getPerimeter and getArea methods. Do not add any instance variables. In the constructor, call the setLocation and setSize methods of the Rectangle class. In Main class, provide 3 test cases that tests the methods that you supplied printing expected and actual results. The test cases are: 1) One...
USE C++ 1. Write a class that will represent a card in a standard deck of...
USE C++ 1. Write a class that will represent a card in a standard deck of playing cards. You will need to represent both the suit (clubs, diamonds, hearts or spades) as well as the rank (A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2) of each card. Note: Use enumeration instead of strings For example: Do something like this...        enum suits        {            CLUBS,            DIAMONDS,   ...
Draw one card with replacement from a well-shuffled standard poker deck until you draw a spade....
Draw one card with replacement from a well-shuffled standard poker deck until you draw a spade. Find the probability of each of the following: a. You draw only once. b. You draw at least once. c. You draw at least twice. d. You draw at least three times, but no more than five times.
What does it mean to say that we are going to use a sample to draw...
What does it mean to say that we are going to use a sample to draw an inference about a population? Why is a random sample so important for this process? If we wanted a random sample of students in the cafeteria, why couldn’t we just choose the students who order Diet Pepsi with their lunch? Comment on the statement, “A random sample is like a miniature population, whereas samples that are not random are likely to be biased.” Why...
A couple of questions that I want to understand better. How to draw a class diagram?...
A couple of questions that I want to understand better. How to draw a class diagram? What a clean object hierarchy diagram looks like? How to instantiate objects? What {cohesive, decoupled, information hiding, inheritance, and polymorphic} mean and why we strive to achieve them? When to implement a list with an array and when to implement it with a linked list When to use a list, stack, or queue?
Draw a sag curve. surveying class
Draw a sag curve. surveying class
Write one page how was the class Medsurge?
Write one page how was the class Medsurge?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT