/* 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. ========= ShapeShifter.java ============== 

2. public interface ShapeShifter 

3.

4.   public void change () ; 

5.

6. ===========  Interfacing2.java  =============== 

7. import java.awt.*; 

8. import java.awt.event.*; 

9. import java.applet.*; 

10.  

11. public class Interfacing2 

12.     extends Applet 

13.     implements ActionListener 

14.

15.   ShapeShifter[] shs ; 

16.  

17.   OurTxF tun, tdos ; 

18.   OurBt obt ; 

19.   OurLb olb ; 

20.  

21.   Button btn ; 

22.  

23.   public Interfacing2 (  ) 

24.   { 

25.     shs = new ShapeShifter [ 4 ] ; 

26.     shs [ 0 ] = tun = new OurTxF () ; 

27.     shs [ 1 ] = obt = new OurBt () ; 

28.     shs [ 2 ] = olb = new OurLb () ; 

29.     shs [ 3 ] = tdos = new OurTxF () ; 

30.  

31.     btn = new Button ( "All change" ) ; 

32.   } 

33.   public void 

34.     actionPerformed ( ActionEvent ev ) 

35.   { 

36.     setLayout (null); 

37.  

38.     for ( int i = 0 ; i < shs.length ; i++ ) 

39.     { 

40.       shs[ i ].change() ; 

41.     } 

42.     validate(); 

43.   } 

44.   public void init() 

45.   { 

46.     setLayout( 

47.      new FlowLayout( FlowLayout.CENTER, 50, 70 ) ); 

48.  

49.     btn.addActionListener ( this ); 

50.     btn.addActionListener ( olb ); 

51.  

52.     add ( btn ) ; 

53.  

54.     tun.setText("OurTxF tun "); 

55.     add ( tun ) ; 

56.     olb.setText("OurLb olb "); 

57.     add ( olb ) ; 

58.     tdos.setText("OurTxF tdos "); 

59.     add ( tdos ) ; 

60.     obt.setLabel("OurBt obt "); 

61.     add ( obt ) ; 

62.   } 

63.

64. //================================= 

65. class OurTxF 

66.   extends TextField 

67.   implements ShapeShifter 

68.

69.   public void change () 

70.   { 

71.     int w ; 

72.     w = getSize().width ; 

73.     w = w + 5

74.     setSize ( w, 30 ); 

75.     setText( "w=" + w ); 

76.   } 

77.

78. //================================= 

79. class OurBt 

80.   extends Button 

81.   implements ShapeShifter 

82.

83.   public   void change () 

84.   { 

85.     int w, h ; 

86.     w = getSize().width ; 

87.     h = getSize().height ; 

88.     w = w + w/6

89.     h = h + h/5

90.     setSize( w, h ); 

91.   } 

92.

93. //================================= 

94. class OurLb 

95.   extends Label 

96.   implements ShapeShifter 

97.           , ActionListener 

98.

99.   public   void change () 

100.   { 

101.     setSize ( 

102.     (int)( Math.random() * 150 + 30 )  , 

103.       (int)( Math.random() * 100 + 20 )  ); 

104.   } 

105.   public   void actionPerformed ( ActionEvent ev ) 

106.   { 

107.     setBackground( 

108.       new Color

109.       (int) ( Math.random() * Integer.MAX_VALUE

110.       ) 

111.     ); 

112.   } 

113.

114.