line graph in java, graphics in java or java graph programming
Demo Line Graph
//Display a line graph representing the number of A's, B's, C's, D's and F's
//on the Java Programming Final Examination
import java.awt.*;
import BreezyGUI.*; public class lineonly extends GBFrame { //Establish the placement of the fields for user entry Label LabelA = addLabel("A", 1,1,1,1); IntegerField FieldA = addIntegerField(0,1,2,1,1); Label LabelB = addLabel("B", 1,3,1,1); IntegerField FieldB= addIntegerField(0,1,4,1,1); Label LabelC = addLabel("C", 1,5,1,1); IntegerField FieldC = addIntegerField(0,1,6,1,1); Label LabelD = addLabel("D", 1,7,1,1); IntegerField FieldD = addIntegerField(0,1,8,1,1); Label LabelF = addLabel("F", 1,9,1,1); IntegerField FieldF = addIntegerField(0,1,10,1,1); //Establish a drop down menu for the drawing //Right now we only have one choice -- Line MenuItem lineChoice = addMenuItem("Graph", "Line"); //Establish variables for this program int numberOfScores = 5; int Xleft = 100; int Xright = 300; int Ytop = 100; // The y-value entries can be up to 250-100=150. int Ybottom = 250; int totalX, totalY; int[ ] scores; char graphChoice; //Constructor to establish the initial values in the program public lineonly ( ) { scores = new int[numberOfScores]; for (int i = 0; i < scores.length; i++) scores[ i ] = 0; totalX = Xright - Xleft + 1; totalY = Ybottom - Ytop + 1; graphChoice = 'L'; } //Allow for the menu choices //Remember, we only have one choice right now public void menuItemSelected(MenuItem item) { if (item == lineChoice) graphChoice = 'L'; repaint(); } //Establish the paint method public void paint (Graphics g) { getInputData(); g.setColor (Color.red); //set color of the graph drawLineGraph(g); g.drawString("Java Grades", 170, 290); // title } //Get input from user public void getInputData() { scores[0] = FieldA.getNumber(); scores[1] = FieldB.getNumber(); scores[2] = FieldC.getNumber(); scores[3] = FieldD.getNumber(); scores[4] = FieldF.getNumber(); } //Draw the line graph public void drawLineGraph(Graphics g) { drawAxes(g); int i, x1, y1, x2, y2, largestNumber, xIncrement, yIncrement; //Compute the x and y increments largestNumber = findLargest (scores); xIncrement = totalX / numberOfScores; if (largestNumber ==0) yIncrement = 0; else yIncrement = totalY / largestNumber; //Set the initial origin point x1 = Xleft; y1 = Ybottom; //Compute and plot the data points for(i=0; i < numberOfScores; i++) { x2 = getXCoordinate(i+1, xIncrement); y2 = getYCoordinate(scores[i], yIncrement); g.fillOval(x2, y2, 5, 5); g.drawLine(x1, y1, x2, y2); x1 = x2; y1 = y2; } //Label x - axes with grade choices String [ ] label = {"A", "B", "C", "D", "F"}; for (i=1; i<=numberOfScores; i++) g.drawString(label[i-1], 100+ i*xIncrement, 270); //Label y - axes with quantity of each grade int topy; if(largestNumber%10==0) topy=largestNumber; else topy = (largestNumber/10+1)*10; // i = i+5 controls y value label -- adjust for size of data for (i = 0; i <= topy; i = i+5) { g.drawString(String.valueOf(i), 70, Ybottom-i*yIncrement+5); } } //Draw the axes for the graph public void drawAxes (Graphics g) { g.drawLine(Xleft, Ytop, Xleft, Ybottom); g.drawLine(Xleft, Ybottom, Xright, Ybottom); } //Determining x coordinate public int getXCoordinate(int i, int xIncrement) { return Xleft + xIncrement *i; } //Determining y coordinate public int getYCoordinate(int numStudents, int yIncrement) { return Ybottom - yIncrement * numStudents; } //Finding the largest value in the array public int findLargest(int [ ] a) { int location = 0; for( int i = 1; i < a.length; i++) if(a[i] > a[location]) location = i; return a[location]; } //Main public static void main(String[ ] args) { Frame frm = new lineonly(); frm.setSize (400, 300); frm.setVisible (true); } } |
0 Comment:
Post a Comment