/* Name: Chris Johnson johnson@dcs.gla.ac.uk Last modified: 27/10/98 Description: ScrollCanvas implements a scrollable area that is painted with colours that reflect the settings on three scroll bars (one each for Red, Green and Blue values). Part of an applet to illustrate the use of scroll bars to mix proportyions of RGB colors. Based on a program in G.W. Rouse's An Introduction to Data Structures and Algorithms with Java, Prentice Hall, Hemel Hemstead, 1998. pp 120-126. */ import java.awt.*; class ScrollCanvas extends Canvas { private int startRed, startGreen, blue; private int selRed, selGreen; private ColorChooser parent; ScrollCanvas (ColorChooser owner) { super(); parent = owner; startRed = startGreen = blue = 0; // initially selected colour is selRed = selGreen = 0; // black (RGB=0) } public void setWindow(int offsetRed, int offsetGreen) { startRed = offsetRed; startGreen = offsetGreen; repaint(); } public void setBlue(int offsetBlue) { blue = offsetBlue; repaint(); } public int getRed() { return selRed; } public int getBlue() { return blue; } public int getGreen() { return selGreen; } public boolean mouseDown(Event event, int x, int y) { selRed = y + startRed; selGreen = x + startGreen; parent.updateSelectedColor(selRed, selGreen, blue); return true; } public void paint(Graphics canvasG) // paints all of the scrollable area - one pixel at a time // this is very, very inefficient and much better algorithms // are available. { for (int redPixel = 0; redPixel < 128; ++redPixel) for (int greenPixel = 0; greenPixel < 128; ++greenPixel) { canvasG.setColor(new Color(redPixel + startRed, greenPixel+startGreen, blue)); canvasG.drawRect(greenPixel, redPixel, 1, 1); } } }