java drawing methods

Views:
 
Category: Education
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

METHODS IN THE GRAPHICS CLASS : 

METHODS IN THE GRAPHICS CLASS K.SUBBIAH@SURESH, II M.C.A. M.S.UNIVERSITY. SEMINAR BY,

Agenda : 

Agenda @ Graphics class @ The coordinate system for Java graphics. @ The use of color. @ Drawing shapes such as line, oval, rectangle, polygon. @ Example programs(Ref. programming with java by C. Muthu) @ Conclusion.

The Graphics Class : 

The Graphics Class @ An object of the Graphics class represents a particular drawing surface @ It defines a graphics context in which drawn shapes will be rendered @ The Graphics class contains methods for drawing various shapes and controlling visual aspects like font and color @ An applet has a graphics context, which is automatically passed to the paint method when it is called

The Coordinate System : 

The Coordinate System @ A simple two-dimensional coordinate system exists for each graphics context (or drawing surface) @ Each point on the coordinate system represents a single pixel. @ The top left corner of the area is coordinate <0,0>. @ A drawing surface has a particular width and height. @ Anything drawn outside of that area will be clipped automatically.

Drawing methods : 

Drawing methods @ The Graphics class contains methods for drawing several specific shapes: lines, ovals, rectangles, arcs and polygons. @ Most shapes can be drawn filled or unfilled @ A line, drawn with the drawLine method, is always one pixel wide and cannot be filled. drawLine(int start X,int start Y, int end X, end Y);

oval : 

oval @ An oval is defined by its bounding rectangle width height @ The methods that draw an oval take four parameters, all integers: drawOval(x, y, width, height) fillOval(x, y, width, height)

Example : 

Example import java.applet.Applet; import java.awt.*; public class Ovals extends Applet { public void paint (Graphics s) { s.drawOval (20, 20, 30, 50); s.drawOval (70, 40, 60, 10); s.drawOval (150, 30, 30, 30); // a circle s.fillOval (30, 100, 50, 30); s.drawRect (100, 100, 50, 30); // bounding rectangle s.fillOval (100, 100, 50, 30); } // method paint } // class Ovals /*<APPLET CODE="Ovals.class" WIDTH=300 HEIGHT=150> </APPLET>*/

Rectangle : 

Rectangle @ Rectangles can be drawn filled or unfilled. with squared or rounded corners. with a slight three-dimensional effect or not. @ The primary parameters for all rectangle drawing methods define the upper left corner of the rectangle and its width and height. @ See Rectangles.java

Example : 

Example import java.applet.Applet; import java.awt.*; public class Rectangles extends Applet { public void paint (Graphics s) { setBackground (Color.white); setForeground (Color.blue); // Rectangles s.drawRect (20, 20, 30, 50); s.drawRect (70, 40, 60, 10); s.drawRect (150, 30, 30, 30); // a square s.fillRect (200, 20, 50, 30); s.fillRect (270, 20, 20, 40); } }

arc : 

arc @ An arc is defined as a segment of an oval. @ The first four parameters to the arc drawing methods define the bounding rectangle of the oval. @ The other two parameters define the start angle and the arc angle. @ The start angle indicates where the arc begins and the arc angle determines how far the arc sweeps across its defining oval.

Arc : 

Arc @ An arc angle can also be positive or negative. @ A positive arc angle sweeps counterclockwise, and a negative arc angle sweeps clockwise. @ Therefore, the same arc can be specified using four different combinations of start and arc angles. @ Arcs can also be filled or unfilled.

Example : 

Example APPLET TO DRAW AN ARC import java.applet.*; import java.awt.*; /*<applet code="drawarcdemo.class" width=800 height=600> </applet> */ public class drawarcdemo extends Applet { public void paint(Graphics s) { s.setColor(Color.blue); s.drawArc(120,20,560,560,0,180); } }

OUTPUT : 

OUTPUT

Example : 

Example APPLET TO DRAW AN ARC import java.applet.*; import java.awt.*; /*<applet code=“fillarcdemo.class" width=800 height=600> </applet> */ public class drawarcdemo extends Applet { public void paint(Graphics s) { s.setColor(Color.blue); s.fillArc(120,20,560,560,0,180); } }

OUTPUT : 

OUTPUT

Polygon : 

Polygon @ A polygon is a multisided figure defined by a series of ordered points @ Line segments connecting the points form the polygon @ The points are defined by corresponding arrays of x and y coordinate values, and can already be incorporated into an object of the Polygon class @ Polygons are closed, forming a line segment from the last point back to the first

Example : 

Example import java.applet.Applet; import java.awt.*; public class Polygon extends Applet { int n=5; int[] xdata = {110, 110, 115, 120, 150}; int[] ydata = {10, 40, 30, 50, 15}; public void paint (Graphics s) { s.setcolor(color.blue); s.drawpolygon(xdata, ydata, n); } // method paint } // class Polylines

Example : 

Example // APPLET TO DRAW A TRAFFIC SIGNAL import java.awt.*; import java.awt.Graphics; import java.applet.Applet; /*<applet code="trafficsignal" width=600 height=500> </applet>*/ public class trafficsignal extends Applet { public void paint(Graphics g) { int n=4; int x[]={160,160,300,300}; int y[]={20,450,450,20}; g.setColor(Color.black); g.fillPolygon(x,y,n); g.setColor(Color.red); g.fillOval(180,50,100,100);

Example : 

Example //TRAFFIC SIGNAL CONTINUES g.setColor(Color.yellow); g.drawString("STOP",210,105); g.setColor(Color.orange); g.fillOval(180,175,100,100); g.setColor(Color.white); g.drawString("LISTEN",210,230); g.setColor(Color.green); g.drawOval(180,300,100,100); g.fillOval(180,300,100,100); g.setColor(Color.black); g.drawString("PROCEED",200,355); g.setColor(Color.black); int a[]={225,225,250,250}; int b[]={450,575,575,450}; g.fillPolygon(a,b,n); } }

Example : 

Example // APPLET TO DRAW A BAR CHART import java.awt.*; import java.io.*; import java.awt.Graphics; import java.applet.Applet; import java.lang.*; /* <applet code="barchart" width=300 height=250> </applet>*/ public class barchart extends Applet { int n,i;

Example : 

Example // BAR CHART CONTINUES public void paint(Graphics g) { n=10; int valu[]={110,150,100,170,110,150,100,170,110,150}; String label[]={"1991","1992","1993","1994","1995","1996","1997","1998","1999","2000"}; g.setColor(Color.red); for(i=0;i<n;i++) { g.drawString(label[i],20,i*50+30); g.fillRect(50,i*50+10,valu[i],40); } } }

OUTPUT : 

OUTPUT

Example : 

Example APPLET TO DRAW PIE CHART import java.awt.*; import java.awt.Graphics; import java.applet.Applet; /*<applet code="piedigram" width=600 height=500> </applet> */ public class piedigram extends Applet { public void paint(Graphics g) { int x[]={25,50,20,5};

Example : 

Example pie chart continues Color color[]={Color.gray,Color.green,Color.red,Color.black}; int sd=0; for(int i=0;i<4;i++) { int dd=(int)(3.6*x[i]); g.setColor(color[i]); g.fillArc(120,10,540,540,sd,dd); sd=sd+dd; } } }

OUTPUT : 

OUTPUT

Font : 

Font @ Each computer system supports a specific set of fonts @ A font defines the look of each character when it is printed or drawn @ The Font class provides methods for specifying fonts in a Java program @ The setFont method defines the current font for a program

Font : 

Font @ A font is defined using the Font class constructor and a combination of: font name font style: plain, bold, italic, or bold+italic font size, in points @ Constants are defined in the Font class to specify the font style

Example : 

Example import java.applet.Applet; import java.awt.*; public class Entropy extends Applet { private String quote = “JVM creates an instance for a APPLET class."; public void paint (Graphics s) { s.setFont (new Font ("TimesRoman", Font.PLAIN, 12)); s.drawString (quote, 10,20); s.setFont (new Font ("TimesRoman", Font.BOLD, 14)); s.drawString (quote, 10,40); s.setFont (new Font ("Helvetica", Font.ITALIC, 16)); s.drawString (quote, 10,60); s.setFont (new Font ("Helvetica", Font.PLAIN, 18)); s.drawString (quote, 10,80); } // method paint } // class Entropy

summary : 

summary Graphic class represents a particular drawing surface and contains methods for drawing shapes on it. Shapes: ovals, rectangles, lines, polygons, polylines string Any portion drawn outside of the drawing surface will not be displayed. Color class defines several common colors.

References : 

References @ Programming with JAVA by C.MUTHU @ www.Google.com (web page) @ www.youtube.com(web page)