/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. import javax.swing.* ; 

2. import javax.swing.event.* ; 

3. import java.awt.* ; 

4. import java.awt.event.*; 

5.  

6. public class ScrollSw 

7.       extends JApplet 

8.       implements AdjustmentListener

9.           ChangeListener 

10.

11.   JScrollBar sb ; 

12.   JSlider sld ; 

13.   JLabel lbl ; 

14.   Container cp ; 

15.   int val = 255

16.  

17.   public ScrollSw ( ) 

18.   { 

19.     sb = new JScrollBar

20.         JScrollBar.HORIZONTAL

21.         0, 10

22.         0, 265 ); 

23.     sld = new JSlider

24.         JSlider.HORIZONTAL

25.         0, 255

26.         10 ); 

27.     sld.setMajorTickSpacing( 50 ); 

28.     sld.setPaintTicks(true); 

29.     sld.setPaintLabels(true); 

30.     lbl = new JLabel ( val + "", JLabel.CENTER ) ; 

31.   } 

32.   public void init() 

33.   { 

34.     cp = this.getContentPane(); 

35.     cp.setLayout ( new GridLayout ( 5,0 )  ) ; 

36.     sb.setValue( val ); 

37.     cp.setBackground( new Color( val, 0, 0 )  ); 

38.     cp.add ( sb ) ; 

39.     sb.addAdjustmentListener ( this ) ; 

40.     sld.addChangeListener( this ); 

41.  

42.     lbl.setBackground ( Color.pink ) ; 

43.     lbl.setOpaque(true); 

44.     cp.add ( lbl ) ; 

45.     cp.add( sld ) ; 

46.   } 

47.  

48.   public void adjustmentValueChanged( AdjustmentEvent ev ) 

49.   { 

50.     int val = sb.getValue() ; 

51.     Color cl = new Color ( val, 0, 0 ) ; 

52.     cp.setBackground ( cl ) ; 

53.     lbl.setText ( val + "" ) ; 

54.   } 

55.   public void stateChanged( ChangeEvent ev ) 

56.   { 

57.     int val = sld.getValue() ; 

58.     Color cl = new Color ( val, 0, 0 ) ; 

59.     cp.setBackground ( cl ) ; 

60.     lbl.setText ( val + "" ) ; 

61.   } 

62.