/* Copyright 1996 Matthew Freedman and University of Washington mattf@cac.washington.edu This program may be freely used, modified, or redistributed for any non-commercial purpose as long as credit is given. This file contains the ColorControllers and PaletteController classes. */ import java.awt.*; /* This class provides and applet for holding the controller devices for Number of Colors and the Palette. setParams(NinaParams p): Set the interface components to match the parameter values. getParams(NinaParams p): Set the parameter values to macth the interface components. */ public class ColorControllers extends GenericController { CountSMController color_ctrl; PaletteController palette_ctrl; public void init() { FlowLayout layout = new FlowLayout(FlowLayout.CENTER, 50, 0); String[] color_max_choices = {"4", "8", "16"}; setLayout(layout); add(color_ctrl = new CountSMController(this, "Number of Colors", color_max_choices, "00")); add(palette_ctrl = new PaletteController()); validate(); initted = true; /* If somebody tried to setParams() during initialization, we have to re-do now */ if (params != null) setParams(params); } public void setParams(NinaParams p) { boolean force; force = super.preSetParams(p); /* If we have not finished initializing, don't want to do anything */ if (!initted) return; color_ctrl.setValues(force, params.num_colors, params.ran_colors, params.max_colors); palette_ctrl.setValues(force, params.ran_palette, params.palette, params.max_colors); } public void getParams(NinaParams p) { /* Number of colors controller */ params.num_colors = color_ctrl.value(); params.ran_colors = color_ctrl.is_ran(); params.max_colors = color_ctrl.max(); params.ran_palette = palette_ctrl.is_ran(); palette_ctrl.get_palette(params.palette); } /* Called back by the controllers when a signficant change to the user-interface state has been made. Of course we only have one actual Controller */ void interfaceChanged(Controller ctrl) { /* Changing number of colors is not particularly interesting right now, but if max has changed we need to adjust palette appearance */ palette_ctrl.setValues(false, palette_ctrl.is_ran(), null, color_ctrl.max()); } } /* This class provides a user-interface for display/inchoosing a color palette. It has public routines for setting and retrieving the palette. */ class PaletteController extends Panel { static final int MAX_COLORS = 16; Button[] buttons = new Button[MAX_COLORS]; Choice select_type; Button regen_btn; boolean random; private Color[] palette = new Color[MAX_COLORS]; private ColorRandomizer crandy = new ColorRandomizer(); private int palette_size; PaletteController() { int i; GridLayout grid = new GridLayout(3, 1, 0, 0); Panel first_row, second_row, third_row; setLayout(grid); Font big_font = new Font("Helvetica", Font.BOLD, 14); first_row = new Panel(); first_row.setFont(new Font("Helvetica", Font.BOLD, 24)); first_row.setLayout(new GridLayout(1, 8, 0, 0)); for (i = 0; i < MAX_COLORS/2; i++) { buttons[i] = new Button(" "); first_row.add(buttons[i]); } add(first_row); second_row = new Panel(); second_row.setFont(new Font("Helvetica", Font.BOLD, 24)); second_row.setLayout(new GridLayout(1, 8, 0, 0)); for (i = MAX_COLORS/2; i < MAX_COLORS; i++) { buttons[i] = new Button(" "); second_row.add(buttons[i]); } add(second_row); third_row = new Panel(); third_row.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0)); select_type = new Choice(); select_type.addItem("Random Palette"); select_type.addItem("Selected Palette"); third_row.add(select_type); regen_btn = new Button("New Palette"); third_row.add(regen_btn); regen_btn.setFont(big_font); add(third_row); } /* Change the background color of the buttons to those specified in the array, plus set random-choice. If palette is null, we just adjust which colors from our current palette are already displayed */ void setValues(boolean force, boolean random, Color[] palette, int num) { int i; Color bg = getBackground(); if (force || (random != this.random)) { this.random = random; select_type.show(false); if (random) select_type.select("Random Palette"); else select_type.select("Selected Palette"); select_type.show(true); /* "New palette" button only meaningful with Selected */ regen_btn.show(!random); } palette_size = num; if (palette == null) palette = this.palette; for (i = 0; i < palette_size; i++) if (buttons[i].getBackground() != palette[i]) { buttons[i].setBackground(palette[i]); this.palette[i] = palette[i]; } /* We always get an entire palette full, but only display palette_size of them */ for (; i < MAX_COLORS; i++) { buttons[i].setBackground(bg); this.palette[i] = palette[i]; } } /* Return whether or not we are currently random */ boolean is_ran() { return(random); } /* Return the current palette */ void get_palette(Color[] palette) { System.arraycopy(this.palette, 0, palette, 0, MAX_COLORS); } /* Called by pop-up color picker */ void set_color(Color color, int which) { buttons[which].setBackground(color); palette[which] = color; } /* Event Handler */ public boolean handleEvent(Event e) { int i; Color c; Color bg = getBackground(); /* Handle change of random/selected state */ if ((e.target == select_type) && (e.id == e.ACTION_EVENT)) { random = ((String) e.arg).equals("Random Palette"); regen_btn.show(!random); } else if ((e.target == regen_btn) && (e.id == e.ACTION_EVENT)) { for (i = 0; i < palette_size; i++) { c = crandy.getOne(false); buttons[i].setBackground(c); palette[i] = c; } for (; i < MAX_COLORS; i++) { c = crandy.getOne(false); buttons[i].setBackground(bg); palette[i] = c; } } else if (e.id == e.ACTION_EVENT) { /* Clicked on an individual button. At some point we should really launch a color-picker dialog, but ain't got time for that now */ for (i = 0; i < palette_size; i++) { if (e.target == buttons[i]) { ColorFrame cframe = new ColorFrame("Pick a Color", buttons[i].getBackground(), this, i); /* If the user changes a color, switch to selected palette mode */ if (random) { random = false; select_type.show(false); select_type.select("Selected Palette"); select_type.show(true); regen_btn.show(true); } break; } } } return(false); } }