Documents

Graphics Class(2019)

Description
Description:
Categories
Published
of 4
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Share
Transcript
  Basic Graphics functions 1)   Line drawLine ( int  x0, int  y0, int  x1, int  y1)   Draw a straight line from pixel (x0, y0) to pixel (x1, y1), Example:  g2.drawLine(200, 200, 700, 500); 2)   Circle drawOval ( int  x, int  y, int  w, int  h) Draw an oval with left-top corner at (x,y) and right-bottom corner at (x+w,y+h). Example: g2.drawOval(250, 250, 50, 50); fillOval ( int  x, int  y, int  width, int  height) Fill an oval with left-top corner at (x,y) and right-bottom corner at (x+w-1,y+h-1). This means that fillOval(x,y,1,1) draws one pixel. Example: g2.fillOval(350, 350, 50, 50); 3)   Rectangle drawRect ( int  x, int  y, int  w, int  h) Draw the border of a rectangle with left-top corner at (x,y) and right-bottom corner at (x+w,y+h). drawRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight) Example:   g2.drawRect(10, 10, 80, 80); g2.drawRoundRect(300, 300, 100, 100, 50, 50); fillRect ( int  x, int  y, int  w, int  h) Fill the rectangle with left-top corner at (x,y) and bottom-right corner at (x+w-1,y+h-1). fillRoundRect(int x,int y,int width,int height,int arcWidth,int arcHeight) Example: g2.fillRect(250, 150, 80, 80); g2.fillRoundRect(350, 350, 100, 100, 50, 50); 4)   Arc/Curve drawArc ( int  x, int  y, int  w, int  h, int  srtang, int  ang) Draw the arc with left-top corner (x,y) and right-bottom corner at (x+w,y+h). The arc starts at srtang and sweeps through ang degrees (positive is counterclockwise). fillArc ( int  x, int  y, int  width, int  height, int  srtang, int ang) Fill the arc of an oval with left-top corner (x,y) and right-bottom corner at (x+w-1,y+h-1). The arc starts at srtang and sweeps through ang degrees (positive is counterclockwise). Example: g2.drawArc(200, 200, 100, 100, 150, 100); g2.fillArc(500, 200, 100, 100, 150, 100); 5)   String drawString (String str, int  x, int  y): Draw the first argument str horizontally starting with the baseline of the string at point (x,y). setFont ( new  Font(String name, int  style, int  size)): Set the font. Some choices for the name are Serif, SansSerif, and Monospaced.  The style can Font.BOLD, Font.ITALIC, Font.PLAIN. The size is the point size; 12 is a commonly used size. Example:   g2.drawString( hello world , 100, 100); g2.setFont(new Font( ALGERIAN ,Font.ITALIC,20)); g2.drawString( font changed , 200, 200); 6)   Color setColor(Color col): Set the pen colour for future drawing operations (drawing lines and filling regions). Example: g2.setColor(new Color(200,100,100)); Graphics2D Functions 1)   Lines Line2D.Double ( double  x0, double  y0, double  x1, double  y1):   Create a line from (x0,y0) to (x1,y1). Example: Line2D line1 = new Line2D.Double(0,0,100,100); Line2D line2 = new Line2D.Float(0.0F,50.0F,200.0F,150.0F); 2)   Circle Ellipse2D.Double ( double  x, double  y, double  w, double  h): Create an ellipse inside the rectangle with corner (x,y), width w, height h. Example: Ellipse2D elli = new Ellipse2D.Double(200,200,100,50); Ellipse2D elli1 = new Ellipse2D.Double(250,250,100,50); g2.draw(elli); g2.draw(elli1); g2.fill(elli1); 3)   Rectangle Rectangle2D.Double ( double  x, double  y, double  w, double  h): Create a rectangle with corner (x,y), width w, height h RoundRectangle2D.Double ( double  x, double  y, double  w, double  h,double r1,double r2): Create a rectangle with corner (x,y), width w, height h, horizontal and vertical radius of ellipse  part that cuts the corner Example: Rectangle2D rect = new Rectangle2D.Double(0,0,100,100); RoundRectangle2D roundrect = new RoundRectangle2D.Double(100,100,100,100,50,50); Rectangle2D rect 1= new Rectangle2D.Double(250,250,100,100); RoundRectangle2D roundrect1 = new RoundRectangle2D.Double(350,350,100,100,50,50); g2.draw(rect); g2.draw(roundrect);   g2.draw(rect1); g2.draw(roundrect); g2.fill(rect1); g2.fill(roundrect2); 4)   Arc/Curve Functions Arc2D.Double (double X, double Y, double Width, double Height, double Theta, double Delta, int Type) Create a arc with corner (x,y), width w, height h, The arc starts at theta and sweeps through delta, type says closer types of the curve Arc.OPEN, Arc,PIE, Arc.CHOD. Example: Arc2D ar1 = new Arc2D.Double(300,100,100,100,90,90,Arc2D.CHORD); Arc2D ar2 = new Arc2D.Double(500,300,100,100,90,90,Arc2D.OPEN); Arc2D ar3 = new Arc2D.Double(500,500,100,100,90,90,Arc2D.PIE); Arc2D ar4 = new Arc2D.Double(100,500,100,100,90,90,Arc2D.PIE); g2.draw(ar1); g2.draw(ar2); g2.draw(ar3); g2.fill(ar4); g2.draw(ar4); Path A path is a sequence of drawing instructions. It is considered a Shape in Graphics2D, just as Line2D is a Shape. You construct a GeneralPath and then use its drawing methods to add pieces to the path. GeneralPath path=new GeneralPath(); The drawing methods are (notice that they all use float  arguments, not double ): moveTo ( float  x, float  y): Move the current point to (x,y). lineTo ( float  x, float  y): Append a line from the current point to (x,y) to the current path. quadTo ( float  x1, float  y1, float  x2, float  y2): Append a quadratic Bézier with control points the current point, (x1,y1), and (x2,y2) to the current path. curveTo ( float  x1, float  y1, float  x2, float  y2, float  x3, float  y3): Append a cubic Bézier with control points the current point, (x1,y1), (x2,y2), and (x3,y3) to the current path. closePath (): Close the path with a straight line back to the last moveTo point.  Example: GeneralPath  pa= new  GeneralPath(); //Triangle using path  pa.moveTo(100, 100); g2.setColor( new  Color(255,0,0));  pa.lineTo(200, 300);  pa.lineTo(400, 300);  pa.closePath(); g2.draw( pa); GeneralPath ca= new  GeneralPath(); //Bezier Curves using four control points ca.moveTo(100, 100); g2.setColor( new  Color(255,0,0)); ca.curveTo(150,150, 250,100,300,150); ca.curveTo(370,200, 425,150,500,200); ca.curveTo(470,250, 550,350,475,420); ca.curveTo(400,350, 350,420,275,350); g2.draw(ca); //Bezier curve using three control points ca.moveTo(200, 100); g2.setColor( new  Color(0,255,0)); ca.quadTo(250, 50, 300, 100); ca.quadTo(350, 150, 400, 100); ca.quadTo(450, 50, 500, 100); ca.quadTo(550, 150, 600, 100); g2.draw(ca); Line patterns public BasicStroke(float penWidth) public BasicStroke(float penWidth, int capStyle, int joinStyle, float miterLimit, float[] dashPattern, float dashOffset) //Cap style - CAP_SQUARE, CAP_BUTT and CAP_ROUND. // join style - JOIN_MITER, JOIN_BEVEL and JOIN_ROUND. //Mitterlimit, dashoffest  –   where to  begin pattern Example: g2.setStroke(new BasicStroke(5.0F)); Line2D line1 = new Line2D.Double(0,0,100,100); g2.draw(line1); float[] pattern =new float[] { 1, 3, 5, 3 }; g2.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 0, pattern, 0)); Line2D line2 = new Line2D.Float(0.0F,50.0F,200.0F,150.0F); g2.draw(line2);

Poli

Sep 22, 2019
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks
SAVE OUR EARTH

We need your sign to support Project to invent "SMART AND CONTROLLABLE REFLECTIVE BALLOONS" to cover the Sun and Save Our Earth.

More details...

Sign Now!

We are very appreciated for your Prompt Action!

x