Program to create applet to draw Bar Chart

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("c2"));
   value[2] = Integer.parseInt(getParameter("c3"));
   value[3] = Integer.parseInt(getParameter("c4"));
  }
  catch(NumberFormatException e){}
 }
 public void paint(Graphics g)
 {
  for(int i=0;i<4;i++) {
   g.setColor(Color.black);
   g.drawString(label[i],20,i*50+30);
   g.setColor(Color.red);
   g.fillRect(50,i*50+10,value[i],40);
  }
 }
}

/* <applet code=BarChart width=400 height=400>
 <param name=c1 value=110>
 <param name=c2 value=150>
 <param name=c3 value=100>
 <param name=c4 value=170>
 <param name=label1 value=1991>
 <param name=label2 value=1992>
 <param name=label3 value=1993>
 <param name=label4 value=1994>
 <param name=Columns value=4>
</applet>
*/

OUTPUT

C:\>javac BarChart.java
C:\>appletviewer BarChart.java



Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative