A summary of java.awt.Graphics methods for drawing shapes, text, ... Other classes define more advanced graphics, eg, javax.swing.Graphics2D. Related classes: Fonts, Images - ImageIcon
java.awt.Graphics Class - Basic drawing methods
Draw on a JComponent or JPanel by overriding paintComponent(). Assume g is a Graphics object, and all variables are type int unless otherwise declared. Angles (int startAngle, arcAngle) are in degrees counterclockwise from 3 o'clock. These methods use (x,y) at the top, left, corner and a width and height of the bounding box (except drawString() and drawLine()).
g.drawLine(x1, y1, x2, y2); |
java.awt.Color Class
| Predefined colors (lowercase without underscores for pre-Java 1.4) | Color.BLACK, Color.WHITE, Color.DARK_GRAY, Color.GRAY, Color.LIGHT_GRAY, Color.BLUE, Color.CYAN, Color.GREEN, Color.RED, Color.MAGENTA, Color.PINK, Color.ORANGE, Color.YELLOW, Color.BLUE, Color.CYAN |
| Creating a color | c = new Color(int r, int g, int b); // creates a new color with RGB values (each 0-255) Example: Color mediumBlue = new Color(128, 128, 255); |
Drawing an image using ImageIcon
Use java.awt.ImageIcon to load and draw JPEG, GIF, or PNG images.
java.awt.Polygon
Straight-sided shapes (eg, triangles) can be created with Polygon class and the Graphics drawPolygon or fillPolygon methods. Add each vertex as an (x, y) pair.
Polygon poly = new Polygon(); // declare and create
poly.addPoint(x, y); // add points to polygon
. . .
g.drawPolygon(poly);
The polygon coordinates can be translated with:
poly.translate(deltaX, deltaY);
There is also a Polygon constructor which takes arrays of points:
Polygon p = new Polygon(int[] xPoints, int[] yPoints, int nPoints);
No comments:
Post a Comment