/* Name: Chris Johnson johnson@dcs.gla.ac.uk Last modified: 27/10/98 Description: 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. 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). ColorCanvas implements a small area of the screen that is updated with the colour that a user selects by clicking their mouse on a pixel within the ScrollCanvas. Labels are used to provide a textual message of the exact RGB value for that pixel. */ import java.applet.Applet; import java.awt.*; public class ColorChooser extends Applet { private ScrollCanvas redGreenCanvas = new ScrollCanvas(this); private ColorCanvas chosenColorCanvas = new ColorCanvas(); private Scrollbar redScroll, greenScroll, blueScroll; private Label redLabel, greenLabel, blueLabel; private NewGridBagLayout layout; static boolean DEBUG = true; public void init() { layout = new NewGridBagLayout(); setLayout(layout); layout.setRareConstraints(GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0), 0,0,1,1); redScroll = new Scrollbar(Scrollbar.VERTICAL, 0, 128, 0, 128); greenScroll = new Scrollbar(Scrollbar.HORIZONTAL, 0, 128, 0, 128); blueScroll = new Scrollbar(Scrollbar.HORIZONTAL, 0, 255, 0, 255); redGreenCanvas.resize(128, 128); Panel redGreenPanel = new Panel(); redGreenPanel.setLayout(new BorderLayout()); redGreenPanel.add("West", redScroll); redGreenPanel.add("South", greenScroll); redGreenPanel.add("Center", redGreenCanvas); Panel redGreenFrame = new Panel(); NewGridBagLayout redGreenFrameLayout = new NewGridBagLayout(); redGreenFrame.setLayout(redGreenFrameLayout); redGreenFrameLayout.setRareConstraints( GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,10,0,0), 0,0,1,1); Label redScrollLabel = new Label("Red"); Label greenScrollLabel = new Label("Green"); redGreenFrameLayout.setPositionSizeAdd(redScrollLabel, redGreenFrame, 0,0,1,1); redGreenFrameLayout.setPositionSizeAdd(redGreenPanel, redGreenFrame, 1,0,1,1); redGreenFrameLayout.setPositionSizeAdd(greenScrollLabel, redGreenFrame, 1,1,1,1); layout.setPositionSizeAdd(redGreenFrame, this, 0,0,1,1); Panel bluePanel = new Panel(); NewGridBagLayout bluePanelLayout = new NewGridBagLayout(); bluePanel.setLayout(bluePanelLayout); bluePanelLayout.setRareConstraints(GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0,0,0,0), 0,0,1,1); Label blueScrollLabel = new Label("Use this scrollbar to adjust blue component"); bluePanelLayout.setPositionSizeAdd(blueScrollLabel, bluePanel, 0, 0, 1, 1); bluePanelLayout.setPositionSizeAdd(blueScroll, bluePanel, 0, 1, 1, 1); layout.setPositionSizeAdd(bluePanel, this, 0, 1, 1, 1); Panel selectedColorPanel = new Panel(); NewGridBagLayout selectedColorPanelLayout = new NewGridBagLayout(); selectedColorPanel.setLayout(selectedColorPanelLayout); selectedColorPanelLayout.setRareConstraints( GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,10,0,0), 0,0,1,1); Label selectedColorLabel = new Label("Selected color:"); selectedColorPanelLayout.setPositionSizeAdd(selectedColorLabel, selectedColorPanel, 0,0,1,1); chosenColorCanvas.resize(32, 32); selectedColorPanelLayout.setPositionSizeAdd(chosenColorCanvas, selectedColorPanel, 1,0,1,1); redLabel = new Label("Red Component " + redGreenCanvas.getRed() + " "); greenLabel = new Label("Green Component " + redGreenCanvas.getGreen() + " "); blueLabel = new Label("Blue Component " + redGreenCanvas.getBlue() + " "); selectedColorPanelLayout.setPositionSizeAdd(redLabel, selectedColorPanel, 0,1,1,1); selectedColorPanelLayout.setPositionSizeAdd(greenLabel, selectedColorPanel, 0,2,1,1); selectedColorPanelLayout.setPositionSizeAdd(blueLabel, selectedColorPanel, 0,3,1,1); layout.setPositionSizeAdd(selectedColorPanel, this, 1, 0, 1, 1); } public void updateSelectedColor(int selRed, int selGreen, int selBlue) { chosenColorCanvas.setBackground(new Color(selRed, selGreen, selBlue)); chosenColorCanvas.repaint(); redLabel.setText("Red component: " + selRed); greenLabel.setText("Green component: " + selGreen); blueLabel.setText("Blue component: " + selBlue); } public boolean handleEvent(Event event) { switch (event.id) { case Event.SCROLL_LINE_UP: case Event.SCROLL_LINE_DOWN: case Event.SCROLL_PAGE_UP: case Event.SCROLL_PAGE_DOWN: case Event.SCROLL_ABSOLUTE: if (event.target == redScroll || event.target == greenScroll) { redGreenCanvas.setWindow(redScroll.getValue(), greenScroll.getValue()); return true; } else if (event.target == blueScroll) { redGreenCanvas.setBlue(blueScroll.getValue()); return true; } } return super.handleEvent(event); } } /* Name: Chris Johnson johnson@dcs.gla.ac.uk Last modified: 27/10/98 Description: 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. */ 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; selRed = selGreen = 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) { 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); } } } /* Name: Chris Johnson johnson@dcs.gla.ac.uk Last modified: 27/10/98 Description: 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. */ class ColorCanvas extends Canvas { ColorCanvas() { super(); setBackground(Color.black); } }