Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Applet Program with Background and Foreground Checkbox and set background images in Java

  1. import java.awt.*;
  2.  
  3. import java.awt.event.*;
  4.  
  5. import java.applet.*;
  6.  
  7. /*<applet code="exp" width=400 height=400>
  8.  
  9. </applet>*/
  10.  
  11. public class exp extends Applet implements ItemListener
  12.  
  13. {
  14.  
  15. int currcolor=5;
  16.  
  17. int flag=1;
  18.  
  19. String text="Click any of the buttons";
  20.  
  21. Button buttons[]=new Button[5];
  22.  
  23. String colours[]={"Red","Blue","Green","Yellow","Magenta"};
  24.  
  25. Image img;
  26.  
  27. CheckboxGroup cbg=new CheckboxGroup();
  28.  
  29. Checkbox box1=new Checkbox("Background Color",cbg,true);
  30.  
  31. Checkbox box2=new Checkbox("Text Color",cbg,false);
  32.  
  33. Checkbox box3=new Checkbox("Loading Image",cbg,false);
  34.  
  35. public void init()
  36.  
  37. {
  38.  
  39. for(int i=0;i<5;i++)
  40.  
  41. {
  42.  
  43. buttons[i]=new Button(" ");
  44.  
  45. add(buttons[i]);
  46.  
  47. }
  48.  
  49. buttons[0].setBackground(Color.red);
  50.  
  51. buttons[1].setBackground(Color.blue);
  52.  
  53. buttons[2].setBackground(Color.green);
  54.  
  55. buttons[3].setBackground(Color.yellow);
  56.  
  57. buttons[4].setBackground(Color.magenta);
  58.  
  59. add(box1);
  60.  
  61. add(box2);
  62.  
  63. add(box3);
  64.  
  65. box1.addItemListener(this);
  66.  
  67. box2.addItemListener(this);
  68.  
  69. box3.addItemListener(this);
  70.  
  71. }
  72.  
  73. public void itemStateChanged(ItemEvent ev)
  74.  
  75. {
  76.  
  77. if(box1.getState()==true)
  78.  
  79. flag=1;
  80.  
  81. else if(box2.getState()==true)
  82.  
  83. {
  84.  
  85. text="Default color is black";
  86.  
  87. flag=2;
  88.  
  89. }
  90.  
  91. else if(box3.getState()==true)
  92.  
  93. {
  94.  
  95. img=getImage(getDocumentBase(),"Water lilies.jpg");
  96.  
  97. flag=3;
  98.  
  99. }
  100.  
  101. repaint();
  102.  
  103. }
  104.  
  105. public void paint(Graphics g)
  106.  
  107. {
  108.  
  109. if(flag==2)
  110.  
  111. {
  112.  
  113. g.drawString(text,30,100);
  114.  
  115. switch(currcolor)
  116.  
  117. {
  118.  
  119. case 0:
  120.  
  121. g.setColor(Color.red);
  122.  
  123. break;
  124.  
  125. case 1:
  126.  
  127. g.setColor(Color.blue);
  128.  
  129. break;
  130.  
  131. case 2:
  132.  
  133. g.setColor(Color.green);
  134.  
  135. break;
  136.  
  137. case 3:
  138.  
  139. g.setColor(Color.yellow);
  140.  
  141. break;
  142.  
  143. case 4:
  144.  
  145. g.setColor(Color.magenta);
  146.  
  147. break;
  148.  
  149. case 5:
  150.  
  151. g.setColor(Color.black);
  152.  
  153. break;
  154.  
  155. }
  156.  
  157. g.drawString(text,30,100);
  158.  
  159. }
  160.  
  161. else if(flag==1)
  162.  
  163. {
  164.  
  165. g.drawString(text,30,100);
  166.  
  167. switch(currcolor)
  168.  
  169. {
  170.  
  171. case 0:
  172.  
  173. setBackground(Color.red);
  174.  
  175. break;
  176.  
  177. case 1:
  178.  
  179. setBackground(Color.blue);
  180.  
  181. break;
  182.  
  183. case 2:
  184.  
  185. setBackground(Color.green);
  186.  
  187. break;
  188.  
  189. case 3:
  190.  
  191. setBackground(Color.yellow);
  192.  
  193. break;
  194.  
  195. case 4:
  196.  
  197. setBackground(Color.magenta);
  198.  
  199. break;
  200.  
  201. case 5:
  202.  
  203. setBackground(Color.white);
  204.  
  205. break;
  206.  
  207. }
  208.  
  209. }
  210.  
  211. else if(flag==3)
  212.  
  213. {
  214.  
  215. g.drawImage(img,20,90,this);
  216.  
  217. }
  218.  
  219. }
  220.  
  221. public boolean action(Event e,Object o)
  222.  
  223. {
  224.  
  225. for(int i=0;i<5;i++)
  226.  
  227. {
  228.  
  229. if(e.target==buttons[i])
  230.  
  231. {
  232.  
  233. currcolor=i;
  234.  
  235. text="You have chosen "+colours[i];
  236.  
  237. repaint();
  238.  
  239. return true;
  240.  
  241. }
  242.  
  243. }
  244.  
  245. return false;
  246.  
  247. }
  248.  
  249. }
  250.