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

2. import javax.swing.* ; 

3. import javax.swing.event.* ; 

4. import java.awt.*; 

5. import java.awt.event.*; 

6. import java.util.*; 

7.  

8. public class OwnComponent2Sw 

9.     extends JPanel 

10.     implements MouseListener 

11.

12.   private EventListenerList ll ; 

13.   private int shiftcoords = 0

14.  

15.   public void 

16.   mouseClicked ( MouseEvent ev ) 

17.   { 

18.     fireActionEvent(); 

19.     setBackground ( Color.pink ) ; 

20.   } 

21.  

22.   protected void 

23.   fireActionEvent( ) 

24.   { 

25.     Object[] listeners = ll.getListenerList(); 

26.     ActionEvent aev = new ActionEvent 

27.      ( this, ActionEvent.ACTION_PERFORMED, "doit" ); 

28.  

29.     for (int i = listeners.length-2; i>=0; i-=2

30.     { 

31.        if ( listeners[i] == ActionListener.class

32.        { 

33.         ActionListener al = 

34.            (ActionListener)listeners[i+1]; 

35.         al.actionPerformed ( aev ); 

36.        } 

37.     } 

38.   } 

39.   public  void 

40.   addActionListener ( ActionListener lis ) 

41.   { 

42.     ll.add ( ActionListener.class, lis ) ; 

43.   } 

44.   public  void 

45.   removeActionListener ( ActionListener lis ) 

46.   { 

47.     ll.remove ( ActionListener.class, lis ) ; 

48.   } 

49.   public void 

50.   paintComponent ( Graphics gr ) 

51.   { 

52.     super.paintComponent( gr ); 

53.     gr.drawString ( "I can take care of myself"

54.           50 + shiftcoords, 50 + shiftcoords ) ; 

55.   } 

56.   public 

57.   OwnComponent2Sw(  ) 

58.   { 

59.     ll = new EventListenerList(); 

60.     setBackground ( Color.green ) ; 

61.     addMouseListener ( this ) ; 

62.     setPreferredSize( new Dimension(300,100) ); 

63.   } 

64.   public void mousePressed (MouseEvent e) 

65.   { 

66.     shiftcoords = 2

67.     repaint (); 

68.   } 

69.   public void mouseReleased (MouseEvent e) 

70.   { 

71.     shiftcoords = 0

72.     repaint (); 

73.   } 

74.   public void mouseExited (MouseEvent e) { } 

75.   public void mouseEntered (MouseEvent e) { } 

76.

77. ====  Customcomp2Sw.java  ============ 

78. import javax.swing.* ; 

79. import java.awt.* ; 

80. import java.applet.* ; 

81. import java.awt.event.*; 

82.  

83. public class Customcomp2Sw 

84.     extends JApplet 

85.     implements ActionListener 

86.

87.   OwnComponent2Sw oc ; 

88.   JTextArea ta ; 

89.   JScrollPane sp ; 

90.   int cnt = 1

91.   Container cp ; 

92.  

93.   public 

94.   Customcomp2Sw( ) 

95.   { 

96.     oc = new OwnComponent2Sw( ) ; 

97.     ta = new JTextArea ( 10, 40 ) ; 

98.     sp = new JScrollPane ( ta ) ; 

99.   } 

100.   public void 

101.   init() 

102.   { 

103.     cp =getContentPane( ); 

104.     cp.setLayout( new FlowLayout() ); 

105.     cp.add( oc ) ; 

106.     oc.addActionListener ( this ) ; 

107.  

108.     cp.add( sp ) ; 

109.  

110.     cp.setBackground( Color.orange ) ; 

111.   } 

112.   public void actionPerformed ( ActionEvent ev ) 

113.   { 

114.     ta.append( (cnt++) + " " + ev + "\n" ) ; 

115.   } 

116.