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

📄 insertb.java

📁 Java 入门书的源码
💻 JAVA
字号:
//Copyright (c) 1998, Arthur Gittleman
//This example is provided WITHOUT ANY WARRANTY either expressed or implied.

/* Add an insertNext method to insert
 * each value in order as soon as the 
 * user enters it
 */

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class InsertB extends Applet implements ActionListener{
  public static int ITEM_SIZE = 10;
  public static int CHART_SIZE 	= 100;
  private TextField number = new TextField(5);
  private DrawOn canvas = new DrawOn();
  private int [] item = new int[ITEM_SIZE];
  private int count = 0;

  public void init() { 
    add(number); 
    canvas.setSize(CHART_SIZE,CHART_SIZE);
    canvas.setBackground(Color.pink);
    add(canvas);
    number.addActionListener(this);
  }
  public void actionPerformed(ActionEvent event) {
    item[count] = new Integer(number.getText()).intValue();
    number.setText("");
    insertNext(item,count++);
    canvas.repaint();
  }   
  public void insertNext(int [] data, int size) {
      int current = data[size]; 
      int j = 0;
      while (current > data[j]) j++;
      for (int k=size; k>j; k--)
        data[k] = data[k-1];
      data[j] = current; 
  }
  class DrawOn extends Canvas {
    public void paint(Graphics g) {
      if (count > 0){
        int width = CHART_SIZE/count;
        for (int i=0; i<count; i++)
          g.fillRect(i*width,CHART_SIZE-item[i],width,item[i]);
      }
    }
  }
} 
       

⌨️ 快捷键说明

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