Posts

Showing posts from November, 2016

Program to demonstrate "HashMap" class in Java's Collection Framework

The  Map  interface maps unique keys to values. A key is an object that you use to retrieve a value at a later date. Given a key and a value, you can store the value in a Map object. After the value is stored, you can retrieve it by using its key. It stores value in pairs i.e key and value we can store data using put( ) method and fetch specific data using get( ) method we can print all data using entrySet( ) method. It has following methods: void clear( ) :  Removes all key/value pairs from the invoking map. boolean containsKey(Object k) :  Returns true if the invoking map contains k as a key. Otherwise, returns false. boolean containsValue(Object v) :  Returns true if the map contains v as a value. Otherwise, returns false.

Program to demonstrate "LinkedList" class in Java's Collection Framework

The  LinkedList  class extends  AbstractSequentialList  and implements the  List  interface. It provides a linked-list data structure. Following are the constructors supported by the LinkedList class. LinkedList( ) This constructor builds an empty linked list. LinkedList(Collection c) This constructor builds a linked list that is initialized with the elements of the collection c. LinkedList defines following methods: void add(int index, Object element) :  Inserts the specified element at the specified position index in the list. boolean add(Object o) :  Appends the specified element to the end of the list.

Program to demonstrate "Queue" class in Java's Collection Framework

The  Queue  interface order elements in a FIFO (first-in-first-out) manner. The  PriorityQueue  class has following methods: boolean add(E e) :  Inserts the specified element into the priority queue. void clear() :  Removes all of the elements from the priority queue. boolean contains(Object o) :  Returns true if the queue contains the specified element. boolean remove(Object o) :  Removes a single instance of the specified element from the queue, if it is present. int size() :  Returns the number of elements in the collection. PROGRAM import java.util.PriorityQueue; import java.util.Queue; class QueueDemo { public static void main(String[] args) { Queue<String> p=new PriorityQueue<String>(); p.add("Apple"); p.add("PineApple"); p.add("Orange"); p.add("Banana"); p.add("Raddish");

Program to demonstrate "Stack" class in Java's Collection Framework

The  Stack  class implements a LIFO (Last In First Out) stack of elements. For example, stack of coins. When you add a new element, it gets stacked on top of the others. When you pull an element off the stack, it removes the element at top. Hence it is called as LIFO (Last In First Out) or FILO (First In Last Out). Stack  is a subclass of Vector that implements a standard last-in, first-out stack. Stack only defines the default constructor, which creates an empty stack. Stack includes all the methods defined by Vector and adds several of its own. Stack class has following methods: boolean empty( ) :  Tests if this stack is empty. Returns true if the stack is empty, and returns false if the stack contains elements. Object peek( ) :  Returns the element on the top of the stack, but does not remove it.

Program to read records from file "stud.dat" and calculate total and percentage

Here, we read the details of students from file and display the total marks and percentage of each student on console. The  DataInputStream  class is used in the context of DataOutputStream and can be used to read primitives. Following is the constructor to create an InputStream: DataInputStream in = DataInputStream (InputStream in); PROGRAM import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; class ReadRecordFromFile { public static void main(String[] args) { try { File f=new File("stud.dat"); FileInputStream fos=new FileInputStream(f); DataInputStream dos=new DataInputStream(fos); int roll_no,m1,m2,tot; float per; String name;

Program to store the entered details (records) in a file named "stud.dat"

In this program, we have read the details of students like roll no, name, marks1, marks2 and store them in a file using  DataOutputStream  class. The  DataOutputStream  stream lets you write the primitives to an output source. Following is the constructor to create a DataOutputStream. DataOutputStream out = DataOutputStream( OutputStream out); PROGRAM import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.util.Scanner; class StoreRecordInFile { public static void main(String[] args) { try { File f=new File("stud.dat"); FileOutputStream fos=new FileOutputStream(f); DataOutputStream dos=new DataOutputStream(fos);

Program to copy contents of one file into other file

This program copied the contents of one file into other file suing  FileInputStream  and  FileOutputStream  classes. The name of files for reading and writing are passed through command line arguments. PROGRAM import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; class CopyFileContentsInOtherFile { public static void main(String[] args) { try { File f1=new File(args[0]); File f2=new File(args[1]);

Program to show date and time using only Date class methods

The  java.util.Date  class represents date and time in java. It provides constructors and methods to deal with date and time in java. The java.util.Date class implements Serializable, Cloneable and Comparable  interface. The constructors of java.util.Date class are given below: Date( ) :  Creates a date object representing current date and time. Date(long milliseconds) :  Creates a date object for the given milliseconds since January 1, 1970, 00:00:00 GMT. Some of the methods of Data class are: long getTime( ) :  Returns the time represented by this date object.

Program to demonstrate use of ArrayList class

Arraylist  is a class which implements  List  interface. It is widely used because of the functionality and flexibility it offers. Java ArrayList class uses a dynamic array for storing the elements. The issue with arrays is that they are of fixed length so if it is full we cannot add any more elements to it, likewise if there are number of elements gets removed from it the memory consumption would be the same as it doesn’t shrink. On the other  ArrayList  can dynamically grow and shrink as per the need. Apart from these benefits ArrayList class enables us to use predefined methods of it which makes our task easy. This program shows some of the methods of  ArrayList  class. I'm going to add String elements so I made it of string type as given below: ArrayList<String> al = new ArrayList<String>( );

Implement a program to count number of lines and words in the file

Using a random access file, we can read from a file as well as write to the file. Reading and writing using the file input and output streams are a sequential process. Using a random access file, we can read or write at any position within the file. An object of the RandomAccessFile class can do the random file access. We can read/write bytes and all primitive types values to a file. RandomAccessFile can work with strings using its readUTF() and writeUTF() methods. We create an instance of the RandomAccessFile class by specifying the file name and the access mode. RandomAccessFile raf = new RandomAccessFile("randomtest.txt", "rw"); A random access file has a file pointer that moves forward when we read data from it or write data to it. We can get the value of file pointer by using its getFilePointer( ) method. When we create an object of the RandomAccessFile class, the file pointer is set to zero. We can set the file pointer at a specific locat

Program to write data into file using Character Stream

Character Streams are specially designed to read and write data from and to the Streams of Characters. We require this specialized Stream because of different file encoding systems. We should not use Byte Streams for Reading and Writing character files. In Character Streams all the classes are the descendents of 2 abstract classes: Reader for reading operation. Writer for writing operation. The 2 most important methods in all the classes that are used for I/O operations are: public int read( ) throws IOException : It reads a character from stream. public int write(int i) throws IOException : It writes the Character into the Stream. Data into 16 lower bits are converted into characters and higher 16 bits are ignored..

Program to read the contents of file using Character Stream

Character Streams  are specially designed to read and write data from and to the Streams of Characters. We require this specialized Stream because of different file encoding systems. We should not use  Byte Streams  for Reading and Writing character files. In Character Streams all the classes are the descendents of 2 abstract classes: Reader  for reading operation. Writer  for writing operation.

Program for reading bytes from file using FileInputStream (Byte Stream)

In Java,  FileInputStream  is a bytes stream class that’s used to read bytes from file. The following program will use FileInputStream to read a file named 'test.txt' and display its content to console. The FileInputStream class is present in  java.io  package. To read the contents from a file you have to follow below stpes: Create an object of  FileInputStream  by passing filename in constructor call. This statement throws  FileNotFoundException  so you have to surround this statement with try-catch block. To read bytes from stream use,  int read( )  method of FileInputStream class. This method reads a byte from stream. This method returns next byte of data from file or -1 if the end of the file is reached. Read method throws IOException in case of any IO errors. To close the FileInputStream, use  void close( )  method of FileInputStream class. This also throws IOException. The following program will the read the contents from file which is passed from command line

Program to draw a different shapes in Applet window

Image
This program will display the different shapes in Applet window. PROGRAM import java.awt.event.*; import java.applet.Applet; import java.awt.Graphics; public class GraphicsExcercise extends Applet { public void paint(Graphics g) { // For rounded rectangle and filled oval g.drawRoundRect(20,20,140,100,10,10); g.fillOval(65,43,50,50); // For triangle g.drawLine(200,20,200,120); g.drawLine(200,20,300,120); g.drawLine(300,120,200,120); // For smiley g.drawOval(20,150,150,150); // For head g.fillOval(50,190,15,15); // Left Eye g.fillOval(120,190,15,15); // Right Eye int x[] = {95,85,106,95}; int y[] = {215,234,234,215}; g.drawPolygon(x, y, 4); // Nose g.drawArc(55,225,78,50,0,-180); // Smile g.drawLine(50,256,60,246); g.drawLine(128,245,139,256); // For diamond int x1[] = {203,252,301,252,203}; int y1[] = {225,176,225,274,225}; g.fillPolygon(x1, y1, 4); } } /* <applet code="GraphicsExcercise.class" width

Program to draw a text at random location in random color

Image
This program will display the text at random location with random colours.

Program to demonstrate use of Thread in an applet to show multithreading in Java

Image
This program makes the use of Runnable interface in Applet class to show the mutithreading in Java. The output of this program is that the string "MutiThreading In Java" will move from top to bottom with a delay of 50ms. PROGRAM import java.applet.Applet; import java.awt.*; public class AnimationUsingThread extends Applet implements Runnable { Image img; Thread t; Graphics g,g1; public void init() { img = createImage(400,400); g = getGraphics(); g1 = img.getGraphics(); t = new Thread(this); t.start(); } public void run() { Font f = new Font("Cambria",Font.BOLD,16); g1.setFont(f); for(int i=1;i<=190;i+=3) drawme("MultiThreading In Java",20,i); } void drawme(String s,int x,int y) { g1.setColor(Color.black); g1.fillRect(0,0,400,400); g1.setColor(Color.white); g1.drawString(s,x,y); g.drawImage(img,0,0,this); try { Thread.sleep(50); }catch(Exception ee){ } } } /* <applet code="AnimationUsin

Program to display table of 1 to 10 on applet

Image
This Java example displays the table of 1 to 10 numbers on applet. PROGRAM import java.applet.Applet; import java.awt.*; public class Table1To10Applet extends Applet { public void paint(Graphics g) { setBackground(Color.cyan); setForeground(Color.red); Font f=new Font("Cambria",Font.BOLD,16); g.setFont(f); int x=20,y=20; for(int i=1;i<=10;i++) { for(int j=1;j<=10;j++) { int k=i*j; g.drawString(k+"",x,y); x=x+30; } x=20; y=y+30; } } } /* <applet class="" width="350" height="350"> </applet> */ OUTPUT C:\>javac Table1To10Applet.java C:\>appletviewer Table1To10Applet.java

Program to display message "Hello World!" in different colors in applet

Image
This Java example displays the message "Hello World!" in different colours in applet. The  java.awt.Color  class is used to get pre-defined colors in Java. We have created an array of objects of  Color  class which contains the pre-defined colours in Java. PROGRAM import java.applet.Applet; import java.awt.*; public class PrintHelloWorldInDifferentColors extends Applet { public void paint(Graphics g) { Color clr[ ] = {Color.black, Color.red, Color.yellow, Color.blue, Color.cyan, Color.gray, Color.green, Color.magenta, Color.pink, Color.orange }; for(int i=0; i<clr.length; i++) { g.setColor(clr[i]); g.drawString("Hello World!", 10, 25*(i+1)); } } } /* <applet code="PrintHelloWorldInDifferentColors.class" width="200" height="300"> </applet> */ OUTPUT C:\>javac PrintHelloWorldInDifferentColors.java C:\>appletviewer PrintHelloWorldInDifferentColors.java

Program to draw Smiley in Applet

Image
This Java example shows how to draw smiley in an applet window using Java Applet class methods. PROGRAM import java.applet.Applet; import java.awt.*; public class SmileyExc extends Applet { public void paint(Graphics g) { g.setColor(Color.yellow); g.fillOval(20,20,150,150); // For face g.setColor(Color.black); g.fillOval(50,60,15,25); // Left Eye g.fillOval(120,60,15,25); // Right Eye int x[] = {95,85,106,95}; int y[] = {85,104,104,85}; g.drawPolygon(x, y, 4); // Nose g.drawArc(55,95,78,50,0,-180); // Smile g.drawLine(50,126,60,116); // Smile arc1 g.drawLine(128,115,139,126); // Smile arc2 } } /* <applet code="SmileyExc.class" width="200" height="200"> </applet> */ OUTPUT C:\>javac SmileyExc.java C:\>appletviewer SmileyExc.java

Program to draw different shapes on Applet

Image
This program draws different shapes on applet. PROGRAM import java.awt.*; // Importing awt package import java.applet.*; // Importing applet package public class Shapes extends Applet { public void paint(Graphics g) { g.setFont(new Font("Cambria", Font.BOLD,15)); g.drawString("Different Shapes", 15, 15); g.drawLine(10,20,50,60); g.drawRect(10,70,40,40); g.setColor(Color.RED); g.fillOval(60,20,30,90); g.fillArc(60,135,80,40,180,180); g.fillRoundRect(20,120,60,30,5,5); } } /* <applet code="Shapes" width=200 height=200> </applet> */ OUTPUT C:\>javac Shapes.java C:\>appletviewer Shapes.java

Program to demonstrate some of font class methods

Image
This program demonstrates some important methods of Java's  Font  class. The  Font  class represents fonts, which are used to render text in a visible way. It has following constructors: Font (Font font) Font (String name, int style, int size) The first constructor creates a new Font from the specified font. And the second constructor creates a new Font from the specified name, style and point size. Some of the methods of Font class are given below: Font getFont( ) :  It returns the reference of current font. String getFamily( ) :  It returns the family name of the Font. String getFontName( ) :  It returns the font face name of the Font. String getName( ) :  It returns the logical name of the Font. int getSize( ) :  It returns the point size of the Font, rounded to an integer. int getStyle( ) :  It returns the style of the Font. boolean isBold( ) :  It indicates whether or not the Font object's style is BOLD. boolean isItalic( ) :  It indicates whether or not th

Program to design an applet which display a triangle filled with red colour and a message as "The triangle" in blue below it

Image
A simple program which display a triangle using  fillPolygon( )  method with a caption below it. Here, xPoints[ ] array holds the x coordinates and yPoints[ ] array holds the y coordinates of polygon. The  fillPolygon( )  method fills a closed polygon defined by arrays of x and y coordinates. The syntax for fillPolygon( ) is given below: void fillPolygon (int[ ] xPoints, int[ ] yPoints, int nPoints) PROGRAM import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; public class FilledTriangleApplet extends Applet { public void paint(Graphics g) { int xPoints[] = {110,180,30,110}; int yPoints[] = {30,100,100,30}; g.setColor(Color.RED); g.fillPolygon(xPoints, yPoints, 4); g.setColor(Color.BLUE); g.drawString("The triangle", 80, 140); } } /* <applet code="FilledTriangleApplet.class" width=250 height=250> </applet> */ OUTPUT C:\>javac FilledTriangleApplet.java C:\>appletviewer FilledTriang

Program to design an applet which displays three circles one below the other and fill them red, green and yellow color respectively

Image
Given program will draw three circle one below other with colours red, green and blue respectively. To fill circle with respective colours first we have to set the colour using  setColor(Color c)  method of  Graphics  class and then use the  fillOval( )  method. PROGRAM import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; public class ThreeCircleApplet extends Applet { public void paint(Graphics g) { // First circle filled with RED color g.setColor(Color.RED); g.fillOval(50, 50, 50, 50); // Second circle filled with GREEN color g.setColor(Color.GREEN); g.fillOval(50, 100, 50, 50); // Third circle filled with YELLOW color g.setColor(Color.YELLOW); g.fillOval(50, 150, 50, 50); } } /* <applet code="ThreeCircleApplet" width=200 height=250> </applet> */ OUTPUT C:\>javac ThreeCircleApplet.java C:\>appletviewer ThreeCircleApplet.java

Program to create applet to draw Bar Chart

Image
Here, we are displaying bar chart on applet window. The values for labels, columns are passed from applet's <PARAM> tag and stored in applet's  init( )  method with the help of arrays. In <APPLET> tag first four parameters are for column values, next four parameters are for column labels and the last parameter is for the number of columns in bar chart. PROGRAM import java.awt.*; import java.applet.*; public class BarChart extends Applet { int n=0; String label[]; int value[]; public void init() { setBackground(Color.pink); try { int n = Integer.parseInt(getParameter("Columns")); label = new String[n]; value = new int[n]; label[0] = getParameter("label1"); label[1] = getParameter("label2"); label[2] = getParameter("label3"); label[3] = getParameter("label4"); value[0] = Integer.parseInt(getParameter("c1")); value[1] = Integer.parseInt(getParameter(&quo

Program to display rolling banner using repaint( ) method of an Applet

Image
This is a very interesting applet program in which we are displaying a rolling banner like marquee tag in HTML. The banner message is passed in applet's <PARAM> tag. PROGRAM import java.awt.*; import java.applet.*; /* <applet code="ParamBanner" width=300 height=50> <param name="message" value="Java makes the Web move!"> </applet> */ public class ParamBanner extends Applet implements Runnable { String msg; Thread t = null; int state; boolean stopFlag; // Set colors and initialize thread. public void init() { setBackground(Color.cyan); setForeground(Color.red); } // Start thread public void start() { msg = getParameter("message"); if(msg == null) msg = "Message not found."; msg = " " + msg; t = new Thread(this); stopFlag = false; t.start(); } // Entry point for the thread that runs the banner. public void run() { char ch; // Display banner for( ; ; ) { t

Program to display numbers from 1 to 10 on Applet such that each number will be display after delay of 100 ms

Image
This makes the use of  Thread 's  sleep( )  method which causes currently executing thread to sleep for given number of milliseconds. In output it will print the numbers from 1 to 10 with a delay of 100ms. PROGRAM import java.awt.*; // import awt package import java.applet.*; // import applet package public class Print1To10Num extends Applet { public void paint(Graphics g) { for(int i=1;i<=10;i++) { g.drawString(i+"",20,10*i); // display message on applet try { Thread.sleep(100); } catch(InterruptedException e){} } } } /* <applet code=Print1To10Num.class height=300 width=250> </applet>*/ OUTPUT C:\>javac Print1To10Num.java C:\>appletviewer Print1To10Num.java

Program to use control loops in Applets

Image
Here, we have shown four circle one below other using "for" loop with two different colors. The methods used are  drawOval( ) ,  fillOval( )  and  setColor( ) . Details of these methods are given in previous programs. PROGRAM import java.awt.*; import java.applet.*; public class ControlLoopApplet extends Applet { public void paint(Graphics g) { for(int i=1;i<=4;i++) { if(i%2==0) { g.fillOval(90,i*50+10,50,50); g.setColor(Color.black); } else { g.drawOval(90,i*50+10,50,50); g.setColor(Color.red); } } } } /* <applet code=ControlLoopApplet width=300 height=300> </applet> */ OUTPUT C:\>javac ControlLoopApplet.java C:\>appletviewer ControlLoopApplet.java

Program to set background and foreground color of an Applet

Image
To set the background and foreground color of an applet  setBackground(Color c)  and  setForeground(Color c)  methods are used respectively. You have to use these methods in applet's  init( )  method. The output of program is shown in OUTPUT section. PROGRAM import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; public class SetBackColor extends Applet { public void init() { setBackground(Color.cyan); setForeground(Color.red); } public void paint(Graphics g) { g.drawString("Hello Java",50,50); } } /* <applet code="SetBackColor" width=200 height=200> </applet> */ OUTPUT C:\>javac SetBackColor.java C:\>appletviewer SetBackColor.java

Program to create an applet to accept a username as parameter and display number of characters.

Image
In this program, we have to get the username from Applet's <PARAM> tag and display the length of the string. The value of parameter can be get using  getParameter( )  method of applet. The  length( ) function is used to find length of given string. PROGRAM import java.applet.Applet; import java.awt.Graphics; public class CharCountParam extends Applet{ String uname="", msg; int length; public void init() { uname = getParameter("Username"); length = uname.length(); uname = "Hello " + uname; msg = "Length = " + length; } public void paint(Graphics g) { g.drawString(uname,50,50); g.drawString(msg, 50, 90); } } /* <applet code="CharCountParam" width=200 height=200> <param name="Username" value="RAHUL"> </applet> */ OUTPUT   C:\>javac CharCountParam.java  C:\>appletviewer CharCountParam.java

Program to draw three concentric circles with three colors pink, red and green

Image
In this program, we have to draw three concentric circles with three colours pink, red and green as shown in output. The  setFont( )  method is used to set the font in that you have to pass the  Font  class's object. You have to pass font name, font style and font size in Font's constructor like: g.setFont(new Font("Times New Roman", Font.BOLD|Font.ITALIC, 14)); To change the current color use the  setColor( )  method of  Graphics  class as shown below. There are some pre-defined colors in Java like RED, GREEN, WHITE, BLUE, CYAN, BLACK, PINK, etc. g.setColor(Color.pink); To use different color you have to pass the RGB values in  Color 's class constructor as given below. g.setColor(new Color(23,123,101)); PROGRAM // Draw Concentric Circles. import java.awt.*; import java.applet.*; public class ConcentricCircles2 extends Applet { String str = "Concentric Circles"; public void paint(Graphics g) { g.setColor(Color.pink); g.draw

Program to draw Concentric Circles on Applet

Image
In this program, we have to draw four concentric circles as shown in output. The  setFont( )  method is used to set the font in that you have to pass the  Font  class's object. You have to pass font name, font style and font size in Font's constructor like: g.setFont(new Font("Times New Roman", Font.BOLD|Font.ITALIC, 14)); PROGRAM import java.awt.*; import java.applet.*; public class ConcentricCircles extends Applet { public void paint(Graphics g) { g.setFont(new Font("Times New Roman",Font.BOLD|Font.ITALIC,14)); g.drawString("Concentric Circles",30,30); int i=65,j=65; while(i>=30) { g.drawOval(i,i,j,j); i = i - 10; j = j + 20; } } } /* <applet code=ConcentricCircles width=200 height=200> </applet> */ OUTPUT C:\>javac ConcentricCircles.java C:\>appletviewer ConcentricCircles.java

Program to draw Polygons on Applet

Image
This program demonstrates how to draw and fill polygons. The  drawPolygon( )  method draws a closed polygon defined by arrays of x and y coordinates. Each pair of (x, y) coordinates defines a point. The drawPolygon method's third parameter,  nPoints , is the number of points in the polygon and should equal the number of pairs in the  xPoints  and  yPoints  arrays. The syntax for drawPolygon( ) is given below: void drawPolygon (int[ ] xPoints, int[ ] yPoints, int nPoints) The  fillPolygon( )  method Fills a closed polygon defined by arrays of x and y coordinates. The syntax for fillPolygon( ) is given below: void fillPolygon (int[ ] xPoints, int[ ] yPoints, int nPoints) PROGRAM // Draw and fill polygons import java.awt.Graphics; public class DrawFillPolygon extends java.applet.Applet { int xCoords[] = { 50, 200, 300, 150, 50, 50 }; int yCoords[] = { 100, 0, 50, 300, 200, 100 }; int xFillCoords[] = { 450, 600, 700, 550, 450, 450 }; public void paint(Graphics g)

Program to draw Arcs on Applet

Image
This program demonstrates how to draw and fill arcs. The  drawArc( )  method draws the outline of a circular or elliptical arc covering the specified rectangle. Angles are interpreted such that 0 degrees is at the 3 o'clock position. Arc angle is also known as  sweep angle . A positive value of angle indicates a  counter-clockwise  rotation while a negative value indicates a  clockwise  rotation. The syntax for drawArc( ) is given below: void drawArc (int x, int y, int width, int height, int startAngle, int arcAngle) The  fillArc( )  method fills a circular or elliptical arc covering the specified rectangle. Angles are interpreted such that 0 degrees is at the 3 o'clock position. A positive value of angle indicates a  counter-clockwise  rotation while a negative value indicates a  clockwise  rotation. The syntax for fillArc( ) is given below: void fillArc (int x, int y, int width, int height, int startAngle, int arcAngle) PROGRAM // Draw Arcs import java.awt.*

Program to draw Ellipses on Applet

Image
This program demonstrates the methods of Graphics class. To change the current color use the  setColor( )  method which has some predefined colors like white, yellow, black, red, green, cyan, etc. For example, to change current color to "red" use the following statement: g.setColor(Color.red); The  drawOval( )  method draws the outline of an oval. The result is a circle or ellipse that fits within the rectangle specified by the x, y, width, and height arguments. The syntax for drawOval( ) is given below: void drawOval (int x, int y, int width, int height) The  fillOval( )  method fills an oval bounded by the specified rectangle with the current color. The rectangle is filled using the graphics context's current color. The syntax for fillOval( ) is given below: void fillOval (int x, int y, int width, int height) PROGRAM // Draw Ellipses import java.awt.*; import java.applet.*; public class DrawEllipses extends Applet { public void paint(Graphics g

Program to draw Rectangles on Applet

Image
This program demonstrates the methods of Graphics class. The  drawRect( )  method draws the outline of the specified rectangle. The rectangle is drawn using the graphics context's current color. The syntax for drawRect( ) is given below: void drawRect (int x, int y, int width, int height) The  fillRect( )  method fills the specified rectangle. The rectangle is filled using the graphics context's current color. The syntax for fillRect( ) is given below: void fillRect (int x, int y, int width, int height) The  drawRoundRect( )  method draws an outlined round-cornered rectangle using the graphics context's current color. The syntax for drawRoundRect is given below: void drawRoundRect (int x, int y, int width, int height, int arcWidth, int arcHeight) The  fillRoundRect( )  method fills the specified rounded corner rectangle with the current color. The syntax for fillRoundRect( ) is given below: void fillRoundRect (int x, int y, int width, int heigh