CSCI 2170 Lecture 6: Graphics with DrawingPanel

19 views2 pages
11 Oct 2019
School
Course
Professor
AP CS Name: ________________________
Notes: Graphics with DrawingPanel
Objects
Constructing (creating) an object
Type objectName = new Type(parameters);
Calling an object’s method
objectName.methodName(parameters);
DrawingPanel & Graphics Objects
Required Library for DrawingPanel and Graphics Objects
import java.awt.*; // Place imports above the class header
Object Creations
DrawingPanel panelName = new DrawingPanel(width, height); // Creates a DrawingPanel
Graphics graphicsName = panelName.getGraphics(); // Creates panelName’s Graphics object
Color colorName = new Color(red, green, blue); // Creates a color with RGB values
Polygon polygonName = new Polygon(); // Creates a polygon
Key DrawingPanel Methods
panelName.setBackground(colorName); // sets the background color of the panel
panelName.clear(); // Erases any shapes that are drawn on the drawing panel.
panelName.setWidth(width); // Changes the drawing panel's width
panelName.setHeight(height); // Changes the drawing panel's height
panelName.setSize(width, height); // Changes the drawing panel's width & height
panelName.save(filename); // Saves the image on the panel to the given filename
panelName.sleep(ms); // Pauses the drawing for the given number of milliseconds
Key Graphics Methods
graphicName.drawLine(x1, y1, x2, y2); // Draws a line from points 1 to 2
graphicName.drawOval(x, y, width, height); // Draws an Ovals outline
graphicName.drawRect(x, y, width, height); // Draws an Rectangle’s outline
graphicName.drawString(text, x, y); // Draws out the text string
graphicName.fillOval(x, y, width, height); // Draws a filled Oval
graphicName.fillRect(x, y, width, height); // Draws a filled Rectangle
graphicName.setColor(Color); // Sets the color for drawing
graphicName.fillPolygon(polygonName); // Fills the Polygon with the current color
Key Color Values
Color.CONSTANT_NAME // Preset color options are: BLACK, BLUE, CYAN, DARK_GRAY, GRAY,
// GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE, YELLOW
Key Polygon Method
polygonName.addPoint(x, y); // Adds a point at x, y to the Polygon named polygonName
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Get OneClass Grade+

Unlimited access to all notes and study guides.

Grade+All Inclusive
$10 USD/m
You will be charged $120 USD upfront and auto renewed at the end of each cycle. You may cancel anytime under Payment Settings. For more information, see our Terms and Privacy.
Payments are encrypted using 256-bit SSL. Powered by Stripe.