Simonefleek / Zybooks

Random Number
0 stars 0 forks source link

16.1 Basic graphics #27

Open Simonefleek opened 6 months ago

Simonefleek commented 6 months ago

Java supports a set of objects for developing graphical applications. A graphical application is a program that displays drawings and other graphical objects. Graphical applications display their contents inside a window called a frame using a JFrame object. The following program shows how to create and configure a JFrame object to display an empty application window.

Simonefleek commented 6 months ago

Figure 16.1.1: Creating a JFrame object for a graphical application. import javax.swing.JFrame;

public class EmptyFrame { public static void main(String[] args) {

  // Construct the JFrame object
  JFrame appFrame = new JFrame();

  // Set the frame's width (400) and height (250) in pixels
  appFrame.setSize(400, 250);

  // Set the frame's title
  appFrame.setTitle("An Empty Frame");

  // Set the program to exit when the user
  // closes the frame
  appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  // Make the frame visible to the user
  appFrame.setVisible(true);

} }

Feedback? Figure 16.1.2: Screenshot of empty application window. Screenshot of an application's empty frame.

Feedback? Constructing a JFrame object does not immediately display a frame. The program uses the methods supported by the JFrame object to configure and display the frame as follows:

Set the frame's size by calling the setSize() method with arguments for the width and height, as in appFrame.setSize(400, 250). Forgetting to set the frame's size results in a frame too small to see. Set the frame's title by calling the setTitle() method with a String as the argument. Alternatively, the frame's title can be provided as an argument to JFrame's constructor as in JFrame appFrame = new JFrame("An Empty Frame"). Set the frame's closing operation by calling the setDefaultCloseOperation() method, as in appFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE). This statement configures the program to exit when the user closes the frame. Make the frame visible to the user by calling the setVisible() method with a boolean argument of true.

Simonefleek commented 6 months ago

The programmer needs to know the positioning coordinate system in order to draw shapes in the intended location. As the following figure illustrates, the top-left corner of a JComponent corresponds to coordinates (0, 0). The x-coordinate increases horizontally to the right and the y-coordinate increases vertically downward.

Simonefleek commented 6 months ago

The programmer needs to know the positioning coordinate system in order to draw shapes in the intended location. As the following figure illustrates, the top-left corner of a JComponent corresponds to coordinates (0, 0). The x-coordinate increases horizontally to the right and the y-coordinate increases vertically downward.

Figure 16.1.6: Graphics coordinate system.

image

Simonefleek commented 6 months ago

Which code segment (type the letter) performs the described operation? Assume each code segment is written within the paintComponent() method of an extended JComponent class, and that the Graphics2D object is called graphicsObj.

Rectangle rectangle = new Rectangle(0, 0, 150, 100);
Color color = new Color(0, 255, 0); graphicsObj.setColor(color); graphicsObj.fill(rectangle); Rectangle rectangle = new Rectangle(0, 100, 200, 200); Color color = new Color(255, 0, 0); graphicsObj.setColor(color); graphicsObj.fill(rectangle); Rectangle rectangle = new Rectangle(0, 100, 50, 150); Color color = new Color(255, 0, 255); graphicsObj.setColor(color); graphicsObj.draw(rectangle); 1) Draws a filled in red square.

Simonefleek commented 6 months ago

16.1.3: Drawing colored rectangles.

Which code segment (type the letter) performs the described operation? Assume each code segment is written within the paintComponent() method of an extended JComponent class, and that the Graphics2D object is called graphicsObj.

A. Rectangle rectangle = new Rectangle(0, 0, 150, 100);
Color color = new Color(0, 255, 0); graphicsObj.setColor(color); graphicsObj.fill(rectangle); Rectangle rectangle = new Rectangle(0, 100, 200, 200); B. Color color = new Color(255, 0, 0); graphicsObj.setColor(color); graphicsObj.fill(rectangle); Rectangle rectangle = new Rectangle(0, 100, 50, 150); C. Color color = new Color(255, 0, 255); graphicsObj.setColor(color); graphicsObj.draw(rectangle);

B. Draws a filled in red square.

2) Draws the outline of a purple rectangle 50 pixels wide and 150 pixels in height.

C.

Draws a rectangle whose top-left corner is located at the origin of the coordinate system.

A.

Draws a green, filled in rectangle.

A.