qna Flashcards
(119 cards)
Question - What is an objective when learning graphics in Java
Answer - An objective is to implement simple drawing objects in Java.
Question - How do you create a basic JFrame for drawing in Java
Answer - You can create a JFrame instance, set its size, title, default close operation, and visibility. An example involves creating an integer w for width (e.g., 640) and h for height (e.g., 480), creating a JFrame f = new JFrame(), setting its size with f.setSize(w,h), setting the title with f.setTitle(“Drawing in Java”), setting the default close operation with f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE), and making it visible with f.setVisible(true). Remember to import javax.swing.*.
Question - Where are the lines for creating a JFrame typically placed, and why are they sometimes shown in the main method in these lessons
Answer - All the lines for creating a JFrame are usually placed in a separate class. To reduce the number of files used in the lesson, the JFrame is created in the main method instead.
Question - What is used as the canvas for programmatic drawing in Java graphics, and what is it like
Answer - You create a class that extends JComponent. This is where the programmatic drawing will be created. It is like the canvas that is drawn on, although the JComponent class is not exclusively just for drawing.
Question - What packages need to be imported when working with a JComponent subclass for drawing
Answer - You need to import java.awt.* for Color and Graphics, java.awt.geom.* for shapes and paths, and javax.swing.* for the JComponent.
Question - Why might a JComponent subclass constructor accept width and height parameters
Answer - The width (w) and height (h) parameters allow the JComponent to accept the width and height of the JFrame. This is not a requirement, but it can be useful for basing the position and size of the elements on the JFrame’s dimensions.
Question - What method in a JComponent subclass is used for drawing, when is it called, and what object is automatically passed to it
Answer - You must override the paintComponent(Graphics g) method of the JComponent class. This method automatically gets called when the component is added to the frame; there is no need to write a call to it anywhere in the code. A Graphics object automatically gets passed to it as well; there is no need to create one.
Question - What is the Graphics object responsible for
Answer - The Graphics object is responsible for tasks such as setting the color, drawing lines, and filling shapes.
Question - What is a Graphics2D object, how is it related to Graphics, and why might you use it
Answer - The Graphics2D object is an enhanced version of the Graphics object. It allows things like adding antialiasing to make the edges of shapes appear smoother.
Question - How do you obtain a Graphics2D object from the Graphics object passed to paintComponent
Answer - You can convert the Graphics object into a Graphics2D object using casting. The code is Graphics2D g2d = (Graphics2D) g;.
Question - What Java package contains classes used to create shapes and paths
Answer - The java.awt.geom package contains some classes that can be used to create shapes and paths.
Question - What are some examples of shape classes available in java.awt.geom
Answer - Some examples are Ellipse2D.Double, Line2D.Double, Path2D.Double, and Rectangle2D.Double.
Question - How do you create a rectangle using Rectangle2D.Double, and what do the constructor arguments represent
Answer - To create a rectangle, you use Rectangle2D.Double r = new Rectangle2D.Double(x,y,w,h);. The x and y are the shape’s location coordinates, and w and h are the shape’s dimensions (width and height). Double values for arguments are valid. For example, new Rectangle2D.Double(50,75,100,250) creates a rectangle originating from point x=50 and y=75, which is 100 pixels wide and 250 pixels high.
Question - Where is the origin point (x=0, y=0) located in a JComponent’s container
Answer - The point x=0 and y=0 is located at the top left corner of the JComponent’s container.
Question - How do you set the drawing color using a Graphics2D object, and how can you specify a color
Answer - You set the color using the setColor(Color c) method of Graphics2D. The Color constructor accepts three arguments, each an integer from 0 to 255, representing RGB (red, green, blue) values. Alternatively, you can use Color constants like Color.BLACK, Color.BLUE, or Color.MAGENTA.
Question - How do you fill a shape using a Graphics2D object
Answer - You use the fill(Shape s) method of Graphics2D to fill the shape with the most recently set Color.
Question - Where should the code for drawing shapes be placed within the JComponent subclass
Answer - Place all of the drawing code in the paintComponent(Graphics g) method.
Question - How is the drawing viewed after creating the JFrame and JComponent subclass
Answer - To view the drawing, you create an instance of the JComponent subclass and add it to the frame in the main method. For example, after creating the JFrame f, you would create DrawingComponent dc = new DrawingComponent(w,h); and then call f.add(dc); before making the frame visible.
Question - How can a solid background be added to the drawing, and how should its dimensions relate to the frame
Answer - One way is to draw a large rectangle that covers the whole area. Set the x and y values to 0 so that it starts at the upper-left corner of the container. Make its dimensions match the width and height of the frame, using the width and height fields created in the JComponent subclass.
Question - How do you draw a circle or oval using Ellipse2D.Double, what do the arguments represent, and what determines if it’s a circle or an oval
Answer - To draw a circle, you use Ellipse2D.Double e = new Ellipse2D.Double(x,y,width,height);. The first two values passed to the constructor are the x and y coordinates. The last two values are the width and height. Equal values for width and height create a circle; unequal values will create an oval. You use the g2d.fill(e) method to render it.
Question - How do you draw a line using Line2D.Double, what do the arguments represent, and what method is used to render it
Answer - To draw a line, you use Line2D.Double line = new Line2D.Double(x1,y1,x2,y2);. The first two values passed to the constructor are the x and y coordinates of the line segment’s starting point. The last two values are the x and y coordinates of the line segment’s ending point. Use the draw(Shape s) method for lines instead of fill(Shape s).
Question - How does the order of drawing commands affect the appearance of overlapping shapes
Answer - Based on the order of code in the paintComponent method, the first shape drawn will appear behind the succeeding shapes (more apparent if the shapes overlap). Each succeeding shape appears higher in the stack.
Question - What is antialiasing, and how is it implemented in Java graphics
Answer - Antialiasing will smoothen the edges of shapes (especially curves) by blending the colors of the edges with the background. To add antialiasing, create a RenderingHints object, for example RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);. Pass it to the Graphics2D object using its setRenderingHints(RenderingHints rh) method.
Question - When should you set the RenderingHints for antialiasing
Answer - Set the RenderingHints before drawing the shapes.