finasl Flashcards
(69 cards)
JFrame
A window used for drawing in Java.
JComponent Subclass
A class that extends JComponent. This class acts like the canvas where programmatic drawing is done. It is where the paintComponent method is overridden. Requires importing java.awt., java.awt.geom., and javax.swing.*.
paintComponent(Graphics g)
A protected method of the JComponent class that you override to put your drawing code inside. It is automatically called when the component is added to the frame. A Graphics object is automatically passed to it.
Graphics object
Responsible for tasks such as setting the colour, drawing lines, and filling shapes.
Graphics2D object
An enhanced version of the Graphics object, obtained by casting the Graphics object. It allows for features like adding antialiasing to smoothen shape edges. This class is part of the awt package.
java.awt.geom
A Java package containing classes for creating shapes and paths, including Ellipse2D.Double, Line2D.Double, Path2D.Double, and Rectangle2D.Double.
Rectangle2D.Double
A class from java.awt.geom used to create rectangles. Its constructor takes four arguments: x and y for the shape’s location coordinates, and w and h for its dimensions (width and height). double values are valid arguments.
x and y coordinates
Represent the location of a shape. The point x=0 and y=0 is located at the top left corner of the JComponent’s container.
setColor(Color c)
A method of Graphics2D used to set the colour for subsequent drawing operations. The Color constructor accepts three int arguments (0 to 255) for RGB values. Alternatively, Color constants like Color.BLUE can be used.
fill(Shape s)
A method of Graphics2D used to fill a shape with the most recently set Color. Used for shapes like rectangles and circles.
draw(Shape s)
A method of Graphics2D used to draw the outline of a shape. Used for lines.
Order of drawing
The order in which shapes are drawn determines their layering. Shapes drawn first will appear behind succeeding shapes, which is more apparent if shapes overlap. Each succeeding shape appears higher in the stack.
Antialiasing
A technique used to smoothen the edges of shapes, especially curves, by blending the colours of the edges with the background. It is added by creating a RenderingHints object and passing it to the Graphics2D object using setRenderingHints(RenderingHints rh). This should be set before drawing the shapes.
Decluttering paintComponent
The practice of separating drawing elements into different classes, each with a draw method that accepts a Graphics2D object, to reduce the number of lines in the main paintComponent method and make it easier to maintain.
Reusability
Making drawing classes more flexible so that one class can be used to draw different variations of an object (e.g., clouds of different sizes, colours, and positions). This is achieved by adding instance fields (like x, y, size, colour) and a constructor to initialize them, then using these fields instead of hard coded values in the drawing method.
Path2D.Double
A class used to create geometric paths.
moveTo(x,y)
A method of Path2D.Double used to specify the starting point for a path. It moves the “pen” to the given coordinates. Can also be called between calls to lineTo to start a new segment at a different point.
lineTo(x,y)
A method of Path2D.Double used to create a path segment from the previous point to the specified coordinates. Multiple calls can be made to create more segments. Segments are not visible until fill or draw is called on the Graphics2D object.
curveTo(x1, y1, x2, y2, x3, y3)
A method used to create curved segments in a path using Bézier control points. It requires six arguments: (x1,y1) and (x2,y2) for the coordinates of the first and second Bézier control points, respectively, and (x3,y3) for the segment’s end point. The starting coordinate is determined by the end point of the previous segment.
Bézier control points (x1,y1, x2,y2)
Points used to create Bézier handles.
Bézier handles
Formed by connecting the segment’s starting point to the first Bézier control point and the second Bézier control point to the segment’s ending point. They act like levers that control the direction of the curve.
Transformations
Functions available in the Graphics2D class to apply changes like translation (adjusting coordinates), scaling, and rotation.
Translation
A transformation that moves the origin point (x:0, y:0) of the coordinate space. It is achieved using the translate(x,y) method of Graphics2D. Subsequent calls concatenate the translation values. To reset, you need to translate by the negative counterparts of the accumulated values.
translate(x,y)
A method of Graphics2D that moves the origin point of the coordinate space by the specified x and y values. It does not change the coordinates of the drawing object itself.