/* * @(#)BallSampler.java 0.0.0 99/07/05 * * Copyright (c) 1999 by Willie Wheeler. All rights reserved. */ package stats; import java.awt.*; import java.util.*; import javax.swing.*; /** * ... *

At some point we need to support arbitrarily many colors, instead * of supporting only five! * * @version 0.0.0 07/05/99 * @author Willie Wheeler */ public class BallSampler extends Sampler { static boolean debug = false; public static final int COLOR_COLUMN = 0; protected static String[] cats = new String[] { "Red", "Blue", "Green", "Yellow", "Gray" }; protected static Color[] catColors = new Color[] { Color.red, Color.blue, Color.green, Color.yellow, Color.gray }; protected int[] catPopFreqs; public BallSampler() { this(new int[] { 500, 500, 500, 500, 500 }); } public BallSampler(int[] catPopFreqs) { debug("", "Entered."); setPopulation(buildPopulation(catPopFreqs)); this.sampleSize = getPopulationSize() / 10; this.replace = true; updateUI(); } public BallSampler(int[] catPopFreqs, int sampleSize, boolean replace) { debug("", "Entered."); setPopulation(buildPopulation(catPopFreqs)); debug("", "pop=" + pop.getRowCount()); this.sampleSize = sampleSize; this.replace = replace; updateUI(); } protected DataTableModel buildPopulation(int[] catPopFreqs) { debug("buildPopulation()", "Entered."); Vector tableData = new Vector(); for (int i = 0; i < catPopFreqs.length; i++) { for (int j = 0; j < catPopFreqs[i]; j++) { Vector rowData = new Vector(1); rowData.add(new Integer(i)); tableData.add(rowData); } } Collections.shuffle(tableData); // Just for fun. Variable var = new TextVariable("Color", cats); Vector vars = new Vector(1); vars.add(var); // Return a population. return new DataTableModel(tableData, vars); } public void updateUI() { debug("updateUI()", "Entered."); setUI(UIManager.getUI(this)); } public String getUIClassID() { debug("getUIClassID()", "Entered."); return "stats.plaf.BallSamplerUI"; } public String[] getCategories() { debug("getCategories()", "Entered."); return cats; } public Color[] getCategoryColors() { debug("getCategoryColors()", "Entered."); return catColors; } public int[] getCatPopFreqs() { debug("getCatPopFreqs()", "Entered."); return catPopFreqs; } static void debug(String method, String msg) { if (debug) { System.err.println("BallSampler." + method + ": " + msg); } } }