Study Guides (390,000)
US (220,000)
MTSU (30)
CSCI (10)
Final

CSCI 2170 Lecture 3: Graphics with DrawingPanelExam


Department
Computer Science
Course Code
CSCI 2170
Professor
Liu Zhang
Study Guide
Final

This preview shows half of the first page. to view the full 2 pages of the document.
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
You're Reading a Preview

Unlock to view full version