![]() |
![]() |
Intro. to Graphics class | |
Drawing - Lines , Rectangles | |
Drawing - Circles and Ellipse | |
Drawing - Arcs | |
Drawing - Polygons | |
Drawing - Bar Charts |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
Java applets can be designed to display bar charts, which are used in analysis of data.
We can draw a Bar chart using any of the following method. The best way we draw using Rectangle
In this topic we will see by using Rectangle
Java graphics class have fillRect() method to draw filled rectangle on window.
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); } } }
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 |
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); } }