/* 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 java.awt.* ; 

3. import java.awt.event.MouseEvent

4. import java.awt.event.MouseListener

5.  

6. public class PaintSwing 

7.     extends JApplet 

8.

9.   public void 

10.   init () 

11.   { 

12.     JPanel pl = new OurContent() ; 

13.     pl.setOpaque( true ); 

14.     pl.setBackground ( Color.pink ) ; 

15.     pl.setLayout( new FlowLayout() ); 

16.     pl.add ( new Label ( "Swing painting" ) ) ; 

17.     setContentPane( pl ); 

18.   } 

19.

20. class OurContent 

21.   extends JPanel 

22.   implements MouseListener 

23.

24.   int rnd = 255

25.   public void 

26.   paintComponent ( Graphics gr ) 

27.   { 

28.     super.paintComponent ( gr ) ; 

29.  

30.     for ( int i = 0 ; i < 256; i++ ) 

31.     { 

32.       gr.setColor ( new Color ( rnd, i, 255-i ) ); 

33.       gr.drawLine ( 128, 128, i, 255 ) ; 

34.       gr.drawLine ( 128, 128, 255-i, 0 ) ; 

35.       gr.drawLine ( 128, 128, 0, i ) ; 

36.       gr.drawLine ( 128, 128, 255, 255-i ) ; 

37.     } 

38.   } 

39.   OurContent() 

40.   { 

41.       addMouseListener ( this ) ; 

42.   } 

43.   public void mouseClicked ( MouseEvent ev ) 

44.   { 

45.     rnd = (int)(Math.random() * 255) ; 

46.     repaint () ; 

47.     Toolkit.getDefaultToolkit().beep(); 

48.   } 

49.   public void mousePressed(MouseEvent ev) {} 

50.   public void mouseReleased(MouseEvent ev) {} 

51.   public void mouseEntered(MouseEvent ev) {} 

52.   public void mouseExited(MouseEvent ev) {} 

53.

54.  

55.