用java图形制作条形图

时间:2018-03-30 07:52:51

标签: java swing graphic

我试图从输入生成条形图 我想让我的代码像这样出来

enter image description here

这是我的代码,但我有一些错误

我在标签上有一些错误,说应该制作方法标签并在主

你能帮我解决这个问题

吗?
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Label;

public class baronly 
{
   //Establish the placement of the fields for user entry
   // get some error in this 
   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 -- Bar 


   //Establish variables for this program
   int numberOfScores = 5;
   int Xleft = 100;
   int Xright = 300;
   int Ytop = 100;
   int Ybottom = 250; // y value entries can be up to 150
   int BarWidth = 10;

   int totalX , totalY;
   int[ ] scores;
   char graphChoice;

   //Constructor to establish the initial values in the program
   public baronly()
   {
      scores = new int[numberOfScores];
      for (int i = 0; i < scores.length; i++)
         scores[ i ] = 0;
      totalX = Xright - Xleft + 1;
      totalY = Ybottom - Ytop + 1;

   }

   //Allow for the menu choices
   //Remember, we only have one choice right now


   public void paint (Graphics g)
   {
      getInputData();
      g.setColor (Color.red); //set color of the graph
      g.drawString("Java Grades",170,290);  //title
      drawBarGraph(g);
   }

   //Get input from the screen
   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 bar graph
   public void drawBarGraph(Graphics g)
   {
      drawAxes(g);
      int i, x, y, height, largestNumber, xIncrement, yIncrement;

   //Compute the x and y increments
   largestNumber = findLargest (scores);
   xIncrement = totalX /numberOfScores;
   if (largestNumber ==0)
      yIncrement = 0;
   else
      yIncrement = totalY / largestNumber;

     //Draw the bars
     for(i=0; i < numberOfScores; i++)
     {
        x = getXCoordinate(i+1, xIncrement);
        y = getYCoordinate(scores[i], yIncrement);
        x = x - BarWidth / 2;
        height = Ybottom - y + 1;
        g.fillRect(x, y, BarWidth, height);
     }

     //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 i;
     int loc = 0;
     for(i=1; i<a.length; i++)
         if(a[i]>a[loc])
            loc = i;
     return a[loc];
   }

  //Main
  public static void main(String[ ] args)
  {
     baronly frm = new baronly();
     frm.setSize(400,300);
     frm.setVisible(true);
  }
} 

该错误给出了我应该在标签中给出的方法是什么意思

0 个答案:

没有答案