Graphics2D IS311 Programming Concepts J เป่น subclass ของ Graphics เพิ้มความสามารถ ในการควบคูมรฺปทรงเรขาคณีตที้ซาบซ๊อนมากขึ๊น AVA เช้น – Draw lines of any thickness – Fill shapes with gradients and textures – Move, rotate, scale, and shear text and Abstract Window Toolkit graphics (part 2: Graphics2D ) – Composite overlapping text and graphics Shape Interface Drawing Shapes • To draw, use things that implement Shape such • Old AWT way (can still use) as: – draw Xxxx – Area – fill Xxxx – CubicCurve2D • OR - Create a Shape object and the draw – GeneralPath – Line2D or fill – Polygon – QuadCurve2D – Rectangle – RectangularShape • These use double's or float's for parameters
Package java.awt.geom Graphics2D object Classes and interfaces related to the definition of geometric primitives: • Used to draw Shape objects • Has rendering attributes: AffineTransform Arc2D Arc2D.Double Arc2D.Float Area CubicCurve2D – Pen Style (solid, dotted, dashed) CubicCurve2D.Double CubicCurve2D.Float Dimension2D – Fill Ellipse2D Ellipse2D.Double Ellipse2D.Float FlatteningPathIterator GeneralPath Line2D – Compositing (used when objects overlap) Line2D.Double Line2D.Float PathIterator Point2D Point2D.Double Point2D.Float – Transform (scale, rotate, translate, shear) QuadCurve2D QuadCurve2D.Double QuadCurve2D.Float – Clip area Rectangle2D Rectangle2D.Double Rectangle2D.Float RectangularShape RoundRectangle2D RoundRectangle2D.Double – Font RoundRectangle2D.Float Graphics2D – setting attributes Graphics2D Rendering Methods • draw • setStroke() pen style that is applied to the outline of a shape – renders outline of graphics primitive using stroke and • setPaint() fill style that is applied to a shape's interior paint attributes • fill • setComposite() compositing style that is used when rendered – fills shape with given paint attribute (color or pattern) objects overlap existing objects • drawString • setTransform() transform that is applied during rendering to – draw string with given font and paint attribute convert the rendered object from user space to device-space coordinates • drawImage • setClip() restricts rendering to the area within the outline of – draw an image the Shape • setFont() font used to convert text strings to glyphs
Graphics2D shape methods Shapes (in package java.awt.geom ) • public void draw (Shape s) • Arc2D.Double(double x, double y, double w, Draws the outline of the given shape using this double h, double start, Graphics2D 's current pen. double extent, int type) An arc, which is a portion of an ellipse. • public void fill (Shape s) Draws outline and fills inside of given shape. • Ellipse2D.Double(double x, double y, double w, double h) An ellipse, which is a generalization of a circle. • rotate , scale , translate , shear , transform Perform various coordinate transformations on the • Line2D.Double(double x1, double y1, double x2, Graphics2D context. double y2) Line2D.Double(Point p1, Point p2) A line between two points. Shapes Methods of to all shapes • public boolean contains(double x, double y) public boolean contains(Point2D p) Whether the given point is contained inside the bounds of this shape. • public Rectangle getBounds() • Rectangle2D.Double(double x, double y, A rectangle representing the bounding box around this shape. double w, double h) A rectangle, which is a generalization of a square. • public double getCenterX() / getCenterY() • public double getMinX() / getMinY() • RoundRectangle2D.Double( • public double getMaxX() / getMaxY() double x, double y, double w, Various corner or center coordinates within the shape. double h, double arcx, double arcy) A rounded rectangle. • public boolean intersects(double x, double y, double w, double h) • GeneralPath() • public boolean intersects(Rectangle2D r) A customizable polygon. Whether this shape touches the given rectangular region.
Creating Shapes Graphics2D draw and fill • Arguments to the draw and fill must implement the Shape interface public void paint(Graphics g) { • Graphics built using objects rather than Graphics2D g2d = (Graphics2D)g; double x = 50.0; instructions double y = 30.0; double diameter=150.0; Ellipse2D.Double circle = new Ellipse2D.double(x, y, diameter, diameter); g2d.fill(circle); } Arc2D – three different close types Rectangles, Ellipses and ARcs • Note that many Shape classes have inner constructors • Open • Use Rectangle2D inner classes to create: • Pie – Rectangle2D.Double – Retangle2D.Float • Chord • Ellipse2D : – Ellipse2D.Double – Ellipse2D.Float • Arc2D – Arc2D.Double – Arc2D.Float
Lines Bounds/Hit Testing • Create a line using Line2D classes inner • Shape has various contains() methods class constructors • Makes for easy user interaction – Line2D.Float() , etc. – Line2D.Double() , etc. public void mouseClicked(MouseEvent e) { • QuadCurve2D Point2D point = new Point2D.Double(e.getX(), e.getY()); – 2 end points and a control points if (shape.contains(point)) { • CubicCurve2D showMessage(“Hit me!”); } – 2 end points and 2 control points } Constructive Area Geometry Combining Shapes • Combine Shape objects using the Area class Constructive Area Geometry (CAG) is the process of creating new geometric shapes by performing boolean operations on existing ones. – Area implements Shape – can draw using In the Java 2D API a special type of Shape called an Area supports Graphics2D boolean operations. • Create: new Area() or new Area(Shape s) • Combine: – public void add(Area a) – public void intersect(Area a); – public void subtract(Area a); Subtraction Union (Add) – public void exclusiveOr(Area a); A pear shape from several ellipses Intersection Exclusive-or( XOR )
Painting Paint related Classes of AWT • Color • Paint attribute of Graphics2D – Solid color – Goes way beyond the old color attribute (still – Same constants as in AWT: Color.red , useable) Color.black , etc. or mix your own – getPaint , setPaint – Can be transparency – Used when fill method is called on a shape • GradientPaint – Arguments to setPaint must implement the – Gradient fill gradually combining two colors – Can create your own Paint interface • TexturePaint – Tiled image as a paint background Colors and paints Compositing • java.awt. Color • Process of putting two pictures together (a simple single-colored paint) – public Color(int r, int g, int b) • Every time object is drawn, source and – public Color(int r, int g, int b, int alpha) • a partially-transparent color (range 0-255, 0=transparent) destination combination process is – public Color brighter (), darker () – public static Color black , blue , cyan , darkGray , performed gray , green , lightGray , magenta , orange , pink , red , white , yellow • Usually use source over destination • java.awt. GradientPaint (smooth transition between 2 colors) – public GradientPaint(float x1, float y1, Color color1, float x2, float y2, Color color2) • java.awt.TexturePaint (use an image as a "paint" background)
Compositing Rules Transparency AlphaComposite supports most of the standard Porter-Duff • The AlphaComposite class encapsulates various compositing rules shown in the following table. compositing styles, which determine how overlapping objects are rendered. • An AlphaComposite can also have an alpha value that specifies the degree of transparency: The source pixels are Only the source pixels Only the source pixels alpha = 1.0 is totally opaque, alpha = 0.0 totally rendered over the in the overlapping area outside of the overlapping transparent (clear). destination pixels are rendered area are rendered • Assign transparency (alpha) values to drawing operations • Underlying graphics show through Call setComposite of Graphics2D object • If pixels in the source and If the alpha = 1.0, the If the alpha = 1.0, the • AlphaComposite.getInstance the destination overlap, pixels in the overlapping pixels in the overlapping only the source pixels area are unchanged; if area are cleared; if the outside of the overlapping the alpha is 0.0, pixels in alpha is 0.0, the pixels in area are rendered the overlapping area are the overlapping area are cleared. unchanged Using Local Fonts Printing All Font Names • Same logical fonts as in Java 1.1 GraphicsEnvironment env = – Serif, SansSerif, Monospaced, Dialog, DialogInput GraphicsEnvironment.getLocalGraphicsEnvironment(); • Also, arbitrary local fonts String[] fontNames = env.getAvailableFontFamilyName(); – Must lookup entire list System.out.println("Available Fonts:"); • May take awhile – Use GraphicsEnvironment methods for(int i=0; i<fontNames.length; i++) System.out.println(" " + fontNames[i]); • getAvailableFontFamilyNames • getAllFonts
Recommend
More recommend