⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 weatherchart.java

📁 Java经典例程 从外国一大学计算机教授出版物下载的代码 经典
💻 JAVA
字号:
import java.io.*;
import javagently.*;
import java.awt.*;
import java.awt.event.*;
import myutilities.*; // for the Stats class

class WeatherChart extends Frame {

    /* The Weather Charting program    by J M Bishop Dec 1997
     *                                 Java 1.1
     *                                 updated May 2000
     * Draws a histogram of monthly rainfall
     * from data taken over a few years.
     * The data must be in the form:
     * year followed by the 12 rainfall figures for
     * the months of that year.
     * Illustrates simple graphics.
     * Uses the Stats class from myutilities.
     */

    int        base = 1950;
    int        startYear, endYear, nYears = 0;
    double[][] rainTable = new double[12][70];
    String     months [] = {"Jan","Feb","Mar","Apr","May","Jun",
                            "Jul","Aug","Sep","Oct","Nov","Dec"};

    WeatherChart () throws IOException {
      getData ();
      setTitle("Weather Chart");
      setSize(400,350);
      setVisible(true);
      addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
        }
      });
    }

    public void paint (Graphics g) {
      int x = 50;
      int y = 300;
      int width = 20;
      int gap = 5;
   // the axes
      g.drawLine (x,y,x+12*(width+gap),y);
      g.drawLine (x,y,x,30);
   // labelling the axes
      for (int m = 0; m < 12; m++)
        g.drawString(months[m],m*(width+gap)+gap+x,y+20);
      for (int i = 0; i <y; i+=100)
        g.drawString(String.valueOf(i),20,y-i);
   // the title
      Font heading = new Font("SansSerif",Font.BOLD,14);
      g.setFont(heading);
      g.drawString("Savanna Rainfall Chart",120,40);
      g.setColor(Color.cyan);
   // the bars
      for (int month = 0; month < 12; month++) {
        int a = (int) Stats.mean
                (rainTable[month], nYears)*10;
        g.fillRect(month*(width+gap)+gap+x, y-a,width,a);
      }
    }

  void getData () throws IOException {
     Stream fin = new Stream ("rain.dat", Stream.READ);

     int actualYear = 0;       /* e.g. 1997 */
     int yearIndex = 0;        /* e.g. 0 */
     try {
       while (true) {
         actualYear = fin.readInt();
         if (yearIndex == 0)
            startYear = actualYear;
         for (int m = 0; m < 12; m++)
            rainTable[m][yearIndex] = fin.readDouble();
         yearIndex++;
       }
     } catch (EOFException e) {
    /* Pick up the last year of data read in. */
     endYear = actualYear;
     nYears = endYear-startYear+1;
   }
 }

 public static void main(String[] args) throws IOException {
   new WeatherChart ();
 }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -