RGB Scrollbar version 4 (with all slider) source code
Return to RGB Page
//John T. Mayer
//rgbScrollbars4.java
/*
This version puts in an "all" scrollbar to move the colors up and down
for BG & FG.
*/
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class rgbScrollbars4 extends Applet implements AdjustmentListener
{
private Label lblHeaderBG, lblRedBG, lblGrnBG, lblBluBG, lblDecBG, lblHexBG, lbl3HexBG;
private Scrollbar sbRedBG, sbGrnBG, sbBluBG;
private TextField txtRedBGDec, txtGrnBGDec, txtBluBGDec;
private TextField txtRedBGHex, txtGrnBGHex, txtBluBGHex, txt3HexBG;
private int RedBG=192, GreenBG=192, BlueBG=192;
private Label lblHeaderFG, lblRedFG, lblGrnFG, lblBluFG, lblDecFG, lblHexFG, lbl3HexFG;
private Scrollbar sbRedFG, sbGrnFG, sbBluFG;
private TextField txtRedFGDec, txtGrnFGDec, txtBluFGDec;
private TextField txtRedFGHex, txtGrnFGHex, txtBluFGHex, txt3HexFG;
private int RedFG=0, GreenFG=0, BlueFG=0;
private Label lblAllBG, lblAllFG;
private Scrollbar sbAllBG, sbAllFG, sTemp;
private int allBGmin, allBGmax, allFGmin, allFGmax;
private int diffBG, diffFG, oldBGValue=192, oldFGValue=0;
private String hex="0123456789ABCDEF";
public void init()
{
int x=10, y=10;
setLayout(null);
//instantiate and add labels
lblHeaderBG = new Label( " Scroll to change the background color" );
add(lblHeaderBG);
lblRedBG = new Label( " Red" );
add(lblRedBG);
lblGrnBG = new Label( " Green" );
add(lblGrnBG);
lblBluBG = new Label( " Blue" );
add(lblBluBG);
lblDecBG = new Label( " Dec" );
add(lblDecBG);
lblHexBG = new Label( " Hex" );
add(lblHexBG);
lbl3HexBG = new Label( " Standard 3-byte hex representation: " );
add(lbl3HexBG);
txtRedBGDec = new TextField( "0", 10 );
add(txtRedBGDec);
txtRedBGHex = new TextField( "00", 10 );
add(txtRedBGHex);
txtGrnBGDec = new TextField( "0", 10 );
add(txtGrnBGDec);
txtGrnBGHex = new TextField( "00", 10 );
add(txtGrnBGHex);
txtBluBGDec = new TextField( "0", 10 );
add(txtBluBGDec);
txtBluBGHex = new TextField( "00", 10 );
add(txtBluBGHex);
lblAllBG = new Label( " Move all" );
add(lblAllBG);
txt3HexBG = new TextField( " 000000", 20 );
add(txt3HexBG);
lblHeaderFG = new Label( " Scroll to change the foreground color" );
add(lblHeaderFG);
lblRedFG = new Label( " Red" );
add(lblRedFG);
lblGrnFG = new Label( " Green" );
add(lblGrnFG);
lblBluFG = new Label( " Blue" );
add(lblBluFG);
lblDecFG = new Label( " Dec" );
add(lblDecFG);
lblHexFG = new Label( " Hex" );
add(lblHexFG);
lbl3HexFG = new Label( " Standard 3-byte hex representation: " );
add(lbl3HexFG);
txtRedFGDec = new TextField( "0", 10 );
add(txtRedFGDec);
txtRedFGHex = new TextField( "00", 10 );
add(txtRedFGHex);
txtGrnFGDec = new TextField( "0", 10 );
add(txtGrnFGDec);
txtGrnFGHex = new TextField( "00", 10 );
add(txtGrnFGHex);
txtBluFGDec = new TextField( "0", 10 );
add(txtBluFGDec);
txtBluFGHex = new TextField( "00", 10 );
add(txtBluFGHex);
lblAllFG = new Label( " Move all" );
add(lblAllFG);
txt3HexFG = new TextField( " 000000", 20 );
add(txt3HexFG);
//instantiate Scrollbars
sbRedBG = new Scrollbar(Scrollbar.HORIZONTAL, RedBG, 1, 0, 256);
sbGrnBG = new Scrollbar(Scrollbar.HORIZONTAL, GreenBG, 1, 0, 256);
sbBluBG = new Scrollbar(Scrollbar.HORIZONTAL, BlueBG, 1, 0, 256);
sbRedBG.setBlockIncrement( 16 );
sbGrnBG.setBlockIncrement( 16 );
sbBluBG.setBlockIncrement( 16 );
sbAllBG = new Scrollbar(Scrollbar.HORIZONTAL, RedBG, 1, 0, 256);
sbAllBG.setBlockIncrement( 16 );
sbRedFG = new Scrollbar(Scrollbar.HORIZONTAL, RedFG, 1, 0, 256);
sbGrnFG = new Scrollbar(Scrollbar.HORIZONTAL, GreenFG, 1, 0, 256);
sbBluFG = new Scrollbar(Scrollbar.HORIZONTAL, BlueFG, 1, 0, 256);
sbRedFG.setBlockIncrement( 16 );
sbGrnFG.setBlockIncrement( 16 );
sbBluFG.setBlockIncrement( 16 );
sbAllFG = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 256);
sbAllFG.setBlockIncrement( 16 );
//add scrollbars, position and size scrollbars and labels and textboxes
lblHeaderBG.setBounds( x, y, 260, 20 );
lblDecBG.setBounds( x+270, y, 35, 20 );
lblHexBG.setBounds( x+320, y, 35, 20 );
add(sbRedBG);
lblRedBG.setBounds( x, y+=25, 50, 20 );
sbRedBG.setBounds( x+60, y, 200, 20 );
txtRedBGDec.setBounds( x+270, y, 35, 20 );
txtRedBGHex.setBounds( x+320, y, 35, 20 );
add(sbGrnBG);
lblGrnBG.setBounds( x, y+=25, 50, 20 );
sbGrnBG.setBounds( x+60, y, 200, 20 );
txtGrnBGDec.setBounds( x+270, y, 35, 20 );
txtGrnBGHex.setBounds( x+320, y, 35, 20 );
add(sbBluBG);
lblBluBG.setBounds( x, y+=25, 50, 20 );
sbBluBG.setBounds( x+60, y, 200, 20 );
txtBluBGDec.setBounds( x+270, y, 35, 20 );
txtBluBGHex.setBounds( x+320, y, 35, 20 );
add(sbAllBG);
sbAllBG.setBounds( x+60, y+=25, 200, 20 );
lblAllBG.setBounds( x, y, 50, 20 );
lbl3HexBG.setBounds( x+60, y+=30, 200, 20 );
txt3HexBG.setBounds( x+270, y, 85, 20 );
//add scrollbars, position and size scrollbars and labels and textboxes
lblHeaderFG.setBounds( x, y+=30, 260, 20 );
lblDecFG.setBounds( x+270, y, 35, 20 );
lblHexFG.setBounds( x+320, y, 35, 20 );
add(sbRedFG);
lblRedFG.setBounds( x, y+=25, 50, 20 );
sbRedFG.setBounds( x+60, y, 200, 20 );
txtRedFGDec.setBounds( x+270, y, 35, 20 );
txtRedFGHex.setBounds( x+320, y, 35, 20 );
add(sbGrnFG);
lblGrnFG.setBounds( x, y+=25, 50, 20 );
sbGrnFG.setBounds( x+60, y, 200, 20 );
txtGrnFGDec.setBounds( x+270, y, 35, 20 );
txtGrnFGHex.setBounds( x+320, y, 35, 20 );
add(sbBluFG);
lblBluFG.setBounds( x, y+=25, 50, 20 );
sbBluFG.setBounds( x+60, y, 200, 20 );
txtBluFGDec.setBounds( x+270, y, 35, 20 );
txtBluFGHex.setBounds( x+320, y, 35, 20 );
add(sbAllFG);
sbAllFG.setBounds( x+60, y+=25, 200, 20 );
lblAllFG.setBounds( x, y, 50, 20 );
lbl3HexFG.setBounds( x+60, y+=30, 200, 20 );
txt3HexFG.setBounds( x+270, y, 85, 20 );
//create scrollbarHandlers
sbRedBG.addAdjustmentListener(this);
sbGrnBG.addAdjustmentListener(this);
sbBluBG.addAdjustmentListener(this);
sbAllBG.addAdjustmentListener(this);
sbRedFG.addAdjustmentListener(this);
sbGrnFG.addAdjustmentListener(this);
sbBluFG.addAdjustmentListener(this);
sbAllFG.addAdjustmentListener(this);
// sbAllBG.setVisibleAmount(1);
}
private String intToHex( int i )
{
int ihi, ilo;
ihi = i / 16;
ilo = i % 16;
return hex.substring( ihi, ihi+1 ) + hex.substring( ilo, ilo+1 );
}
public void paint( Graphics g)
{
g.drawString( "(mine says:) java.awt.Font[family=Dialog,name=Dialog,style=plain,size=12]", 10, 390 );
g.drawString( "(your's below says:) something else, the specifics tell me your setup ([family/name/style/size])", 10, 410 );
g.drawString( g.getFont().toString(), 50, 430 );
setBackground(new Color(RedBG, GreenBG, BlueBG));
g.setColor(new Color(RedFG, GreenFG, BlueFG));
txtRedBGDec.setText( "" + RedBG );
txtRedBGHex.setText( intToHex( RedBG ) );
txtGrnBGDec.setText( "" + GreenBG );
txtGrnBGHex.setText( intToHex( GreenBG ) );
txtBluBGDec.setText( "" + BlueBG );
txtBluBGHex.setText( intToHex( BlueBG ) );
txt3HexBG.setText( " "+intToHex(RedBG)+intToHex(GreenBG)+intToHex(BlueBG) );
txtRedFGDec.setText( "" + RedFG );
txtRedFGHex.setText( intToHex( RedFG ) );
txtGrnFGDec.setText( "" + GreenFG );
txtGrnFGHex.setText( intToHex( GreenFG ) );
txtBluFGDec.setText( "" + BlueFG );
txtBluFGHex.setText( intToHex( BlueFG ) );
txt3HexFG.setText( " "+intToHex(RedFG)+intToHex(GreenFG)+intToHex(BlueFG) );
g.setFont( new Font( "Serif", Font.BOLD, 35 ) );
g.drawString( "Sample Big Text", 20, 370 );
g.setFont( new Font( "Serif", Font.BOLD, 12 ) );
g.drawString( "Sample little Text ", 280, 370 );
}
public void adjustmentValueChanged(AdjustmentEvent e)
{
int rBG, gBG, bBG, rFG, gFG, bFG;
allBGmin=255;
allBGmax=0;
allFGmin=255;
allFGmax=0;
// Scrollbar sTemp =(Scrollbar) e.getAdjustable();
sTemp =(Scrollbar) e.getAdjustable();
if(sTemp==sbRedBG)
{
RedBG = e.getValue();
bgSetMinMax();
}
else if(sTemp==sbGrnBG)
{
GreenBG = e.getValue();
bgSetMinMax();
}
else if(sTemp==sbBluBG)
{
BlueBG = e.getValue();
bgSetMinMax();
}
else if(sTemp==sbRedFG)
{
RedFG = e.getValue();
fgSetMinMax();
}
else if(sTemp==sbGrnFG)
{
GreenFG = e.getValue();
fgSetMinMax();
}
else if(sTemp==sbBluFG)
{
BlueFG = e.getValue();
fgSetMinMax();
}
else if(sTemp==sbAllBG)
{
diffBG = sbAllBG.getValue() - oldBGValue;
RedBG+=diffBG;
RedBG = RedBG > 255 ? 255 : RedBG;
RedBG = RedBG < 0 ? 0 : RedBG;
sbRedBG.setValue(RedBG);
GreenBG+=diffBG;
GreenBG = GreenBG > 255 ? 255 : GreenBG;
GreenBG = GreenBG < 0 ? 0 : GreenBG;
sbGrnBG.setValue(GreenBG);
BlueBG+=diffBG;
BlueBG = BlueBG > 255 ? 255 : BlueBG;
BlueBG = BlueBG < 0 ? 0 : BlueBG;
sbBluBG.setValue(BlueBG);
bgSetMinMax();
oldBGValue = sbAllBG.getValue();
}
if(sTemp==sbAllFG)
{
diffFG = sbAllFG.getValue() - oldFGValue;
RedFG+=diffFG;
RedFG = RedFG > 255 ? 255 : RedFG;
RedFG = RedFG < 0 ? 0 : RedFG;
sbRedFG.setValue(RedFG);
GreenFG+=diffFG;
GreenFG = GreenFG > 255 ? 255 : GreenFG;
GreenFG = GreenFG < 0 ? 0 : GreenFG;
sbGrnFG.setValue(GreenFG);
BlueFG+=diffFG;
BlueFG = BlueFG > 255 ? 255 : BlueFG;
BlueFG = BlueFG < 0 ? 0 : BlueFG;
sbBluFG.setValue(BlueFG);
fgSetMinMax();
oldFGValue = sbAllFG.getValue();
}
repaint();
}
public void bgSetMinMax()
{
allBGmin = RedBG < allBGmin ? RedBG : allBGmin;
allBGmin = GreenBG < allBGmin ? GreenBG : allBGmin;
allBGmin = BlueBG < allBGmin ? BlueBG : allBGmin;
allBGmax = RedBG > allBGmax ? RedBG : allBGmax;
allBGmax = GreenBG > allBGmax ? GreenBG : allBGmax;
allBGmax = BlueBG > allBGmax ? BlueBG : allBGmax;
sbAllBG.setVisibleAmount( allBGmax - allBGmin );
sbAllBG.setValue( allBGmin );
oldBGValue = allBGmin;
}
public void fgSetMinMax()
{
allFGmin = RedFG < allFGmin ? RedFG : allFGmin;
allFGmin = GreenFG < allFGmin ? GreenFG : allFGmin;
allFGmin = BlueFG < allFGmin ? BlueFG : allFGmin;
allFGmax = RedFG > allFGmax ? RedFG : allFGmax;
allFGmax = GreenFG > allFGmax ? GreenFG : allFGmax;
allFGmax = BlueFG > allFGmax ? BlueFG : allFGmax;
sbAllFG.setVisibleAmount( allFGmax - allFGmin );
sbAllFG.setValue( allFGmin );
oldFGValue = allFGmin;
}
}