20-CS-694 Advanced Programming Techniques Spring 2012
Sliders

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

     Previous      Next      Applet Source      All lectures

JScrollBar Test

Slide a notch in a scroll bar left and right to adjust the intensity of one of three colors: red, green, blue. To slide, press and continue pressing the left mouse button over a notch, then drag the notch to the right or left.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

// Separate canvas for posting color values.  Uses its own image to prevent
// flicker and to enable automatics redraws in case of window events.      
class ValueCanvas extends Canvas {
   Image oi;
   Graphics og;
   Color background = new Color(192,192,192);
   int red, grn, blu;

   public void setImage () {
      oi = createImage(this.getSize().width, this.getSize().height);
      og = oi.getGraphics();
      setBackground(background);
   }

   // Put all the values of Red, Green, Blue intensity here
   public void putValues (int r, int g, int b, double h, double s, double br) {
      og.setColor(background);
      og.fillRect(0,0,this.getSize().width,this.getSize().height);
      og.setColor(Color.black);
         
      og.drawString("Red",10,10);
      og.drawString(String.valueOf(r),60,10);
      og.drawString(String.valueOf(hex(r)), 100, 10);

      og.drawString("Green", 10,33);
      og.drawString(String.valueOf(g),60,33);
      og.drawString(String.valueOf(hex(g)), 100, 33);
         
      og.drawString("Blue", 10,56);
      og.drawString(String.valueOf(b),60,56);
      og.drawString(String.valueOf(hex(b)), 100, 56);
      
      og.drawString("Hue",10,100);
      og.drawString(String.valueOf((int)(h*100)/100.0), 100, 100);

      og.drawString("Saturation", 10,123);
      og.drawString(String.valueOf((int)(s*100)/100.0), 100, 123);
         
      og.drawString("Brightness", 10,146);
      og.drawString(String.valueOf((int)(br*100)/100.0), 100, 146);
         
      getGraphics().drawImage(oi, 0, 0, this);
   }
   
   public void paint (Graphics g) {
      g.drawImage(oi, 0, 0, this);
   }
   
   // This only returns a text string representing integer n in Hex
   public String hex(int n) {
      char b[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                   'A', 'B', 'C', 'D', 'E', 'F' };
      int t[] = new int[100];
      String a = "";
      int i=0;
      if (n == 0) return "0";
      while (n > 0) {
          t[i++] += n % 16;
          n /= 16;
      }
      for (int j = i-1 ; j >= 0 ; j--) a += b[t[j]];
      return a;
   }
}

public class ColorApplet extends Applet implements AdjustmentListener {
   JScrollBar red, green, blue, hue, sat, brg;
   Canvas easel = new Canvas();
   ValueCanvas values = new ValueCanvas();
   int redv = 34, bluv = 96, grnv = 134;
   double huev = 0.43, satv = 0.74, brgv = 0.52;
   
   public void init() {
      setLayout(new BorderLayout());
      Panel q = new Panel();
      q.setLayout(new GridLayout(1,2));
      q.add(values);
      
      Panel p = new Panel();
      p.setLayout(new GridLayout(7, 1));
      
      p.add(red = new JScrollBar(JScrollBar.HORIZONTAL, redv, 30, 0, 285));
      red.setBlockIncrement(16);
      red.addAdjustmentListener(this);
      
      p.add(green = new JScrollBar(JScrollBar.HORIZONTAL, grnv, 30, 0, 285));
      green.setBlockIncrement(16);
      green.addAdjustmentListener(this);
      
      p.add(blue = new JScrollBar(JScrollBar.HORIZONTAL, bluv, 30, 0, 285));
      blue.setBlockIncrement(16);
      blue.addAdjustmentListener(this);
      
      p.add(new Label());

      p.add(hue = new JScrollBar(JScrollBar.HORIZONTAL, (int)(huev*100), 10, 0, 110));
      hue.setBlockIncrement(10);
      hue.addAdjustmentListener(this);
      
      p.add(sat = new JScrollBar(JScrollBar.HORIZONTAL, (int)(satv*100), 10, 0, 110));
      sat.setBlockIncrement(10);
      sat.addAdjustmentListener(this);
      
      p.add(brg = new JScrollBar(JScrollBar.HORIZONTAL, (int)(brgv*100), 10, 0, 110));
      brg.setBlockIncrement(10);
      brg.addAdjustmentListener(this);

      q.add(p);
      add("South", q);
      
      add("Center", easel);
   }
   
   public void start () {
      values.setImage();
      refresh();
   }

   void refresh () {
      values.putValues(redv, grnv, bluv, huev, satv, brgv);
      values.paint(values.getGraphics());
      easel.setBackground(new Color(redv, grnv, bluv));
      easel.repaint();
   }

   public void adjustmentValueChanged (AdjustmentEvent evt) {
      red.removeAdjustmentListener(this);
      green.removeAdjustmentListener(this);
      blue.removeAdjustmentListener(this);
      hue.removeAdjustmentListener(this);
      sat.removeAdjustmentListener(this);
      brg.removeAdjustmentListener(this);

      if (evt.getSource() == red || 
	  evt.getSource() == green ||
	  evt.getSource() == blue) {
	 redv = red.getValue();
	 grnv = green.getValue();
	 bluv = blue.getValue();
	 float[] HSB = Color.RGBtoHSB(redv, grnv, bluv, new float[3]);
	 huev = HSB[0];
	 satv = HSB[1];
	 brgv = HSB[2];
	 hue.setValue((int)(huev*100));
	 sat.setValue((int)(satv*100));
	 brg.setValue((int)(brgv*100));
      } else if (evt.getSource() == hue || 
		 evt.getSource() == sat ||
		 evt.getSource() == brg) {
	 huev = (double)hue.getValue()/100.0;
	 satv = (double)sat.getValue()/100.0;
	 brgv = (double)brg.getValue()/100.0;
	 Color color = Color.getHSBColor((float)huev,(float)satv,(float)brgv);
	 redv = color.getRed();
	 grnv = color.getGreen();
	 bluv = color.getBlue();
	 red.setValue(redv);
	 green.setValue(grnv);
	 blue.setValue(bluv);
      }
      refresh();
      red.addAdjustmentListener(this);
      green.addAdjustmentListener(this);
      blue.addAdjustmentListener(this);
      hue.addAdjustmentListener(this);
      sat.addAdjustmentListener(this);
      brg.addAdjustmentListener(this);
   }
}