Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry facebook
  • Androidberry twitter
  • Androidberry google plus

Java Tutorials


Basic Introduction

Decision Making

Looping

Classes and Objects

Inheritance

Interface

Packages

Exception Handling

Multithreading

Java Applets

What is Applet?
Java Applet vs Application
Creating a Applet
Displaying Applet on Webpage
Applet Lifecycle
Applet Tag
Passing Parameters to Applet
Setting Color to applet
Update and Repaint method
Using Fonts in Applet

Graphics Programming

Intro. to Graphics class
Drawing - Lines , Rectangles
Drawing - Circles and Ellipse
Drawing - Arcs
Drawing - Polygons
Drawing - Bar Charts

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java Drawing Bar Charts


What is Bar Chart

  • A bar chart or bar graph is a chart or graph that presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent.
  • Bar graphs/charts provide a visual presentation of categorical data.
  • The bars can be plotted vertically or horizontally. A vertical bar chart is called a line graph.

Java applets can be designed to display bar charts, which are used in analysis of data.

Different ways to draw chart

We can draw a Bar chart using any of the following method. The best way we draw using Rectangle

  • Using Lines and Rectangles
  • Using Polygon shapes

In this topic we will see by using Rectangle


Drawing Bar chart using Rectangle

Java graphics class have fillRect() method to draw filled rectangle on window.

Example

import java.awt.*;
import java.applet.*;
/* <applet code="DrawBarChart.class" width=300 height=300>
</applet> */

public class DrawBarChart extends Applet
{	
	int values[]={100,150,110,170};
	
	public void paint(Graphics g)
	{
		for(int i=0;i &Lt 4;i++)
		{
			g.setColor(Color.blue);
			g.drawString(Integer.toString(values[i]),20, i*50+30);
			g.fillRect(50,i*50+10, values[i], 40);
		}
	}
}

Output

Java Bar Chart Example

Drawing Line Graphics

Applets can draw line graphs to show relationships graphically between two values.

Consider following table of x,y values.

X 0 60 120 180 240 300 360 400
Y 0 120 180 260 340 340 300 180

Program

import java.awt.*;
import java.applet.*;
/* <applet code="LineGraphics.class" width=600 height=600>
</applet> */

public class LineGraphics extends Applet
{	
	int x[]={0,60,120,180,240,300,360,400};
	int y[]={0,120,180,260,340,340,300,180};
	int points = x.length;
	
	public void paint(Graphics g)
	{
		g.setColor(Color.red);
		g.drawPolygon(x,y,points);
	}
}

Output

Java Line Graphics Example
<< Previous

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo