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

📄 insertc.java

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

/* Adds components to the North region
 * of a border layout, and a canvas in
 * the Center.  Handles the same event as
 * Example 8.12, and no more yet.
 */

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

public class InsertC 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;
  private CheckboxGroup acquire = new CheckboxGroup();
  private Checkbox random = new Checkbox("Random",false,acquire);
  private Checkbox manual = new Checkbox("Manual",false,acquire);
  private Label size = new Label("Size");
  private TextField getSize = new TextField("10",5);
  private Button sort = new Button("Sort");
  private Label enter = new Label("Enter");

  public void init() {
    setLayout(new BorderLayout());
    Panel p = new Panel();  
    Panel p1 = new Panel();
    p1.setLayout(new GridLayout(2,1));
    p1.add(random);
    p1.add(manual);
    p.add(p1);
    Panel p2 = new Panel();
    p2.setLayout(new GridLayout(2,1));
    p2.add(size);
    p2.add(getSize);
    p.add(p2);
    p.add(sort);
    Panel p4 = new Panel();
    p4.setLayout(new GridLayout(2,1));
    p4.add(enter);
    p4.add(number);
    p.add(p4);
    add(p,"North"); 
    canvas.setBackground(Color.pink);
    add(canvas, "Center");
    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++) {
          if (i%2==0)g.setColor(Color.green);
          else       g.setColor(Color.blue);  
          g.fillRect(i*width,CHART_SIZE-item[i],width,item[i]);
        }
      }
    }
  }
} 
       

⌨️ 快捷键说明

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