Posted By

veelci on 11/21/12


Tagged


Versions (?)

Jelly Bean Applet


 / Published in: Java
 

Jelly bean randomizer.

  1. import java.awt.BorderLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.List;
  7.  
  8. import javax.swing.JApplet;
  9. import javax.swing.JButton;
  10. import javax.swing.JLabel;
  11. import javax.swing.JPanel;
  12. import javax.swing.JScrollPane;
  13. import javax.swing.JSpinner;
  14. import javax.swing.JTextArea;
  15. import javax.swing.SpinnerNumberModel;
  16.  
  17. public class JellyBeanApplet extends JApplet implements ActionListener
  18. {
  19. private static List<String> beans = new ArrayList<String>();
  20.  
  21. private static final int DEFAULT_FLAVORS = 0;
  22. private static final int MIN_FLAVORS = 0;
  23. private static final int MAX_FLAVORS = 10;
  24.  
  25. private JSpinner flavorSpinner;
  26. private JTextArea flavorResults;
  27.  
  28. static {
  29. beans.add("A&W Cream Soda");
  30. beans.add("A&W Root Beer");
  31. beans.add("Berry blue");
  32. beans.add("Blueberry");
  33. beans.add("Bubble Gum");
  34. beans.add("Buttered Popcorn");
  35. beans.add("Cantalope");
  36. beans.add("Cappuccino");
  37. beans.add("Caramel Corn");
  38. beans.add("Chili Mango");
  39. beans.add("Chocolate Pudding");
  40. beans.add("Cinnamon");
  41. beans.add("Coconut");
  42. beans.add("Cotton Candy");
  43. beans.add("Crushed Pineapple");
  44. beans.add("Dr. Pepper");
  45. beans.add("French Vanilla");
  46. beans.add("Green Apple");
  47. beans.add("Island Punch");
  48. beans.add("Juicy Pear");
  49. beans.add("Kiwi");
  50. beans.add("Lemon Drop");
  51. beans.add("Lemon Lime");
  52. beans.add("Licorice");
  53. beans.add("Mango");
  54. beans.add("Margarita");
  55. beans.add("Mixed Berry Smoothie");
  56. beans.add("Orange Sherbet");
  57. beans.add("Peach");
  58. beans.add("Pi���±a Colada");
  59. beans.add("Plum");
  60. beans.add("Pomegranate");
  61. beans.add("Raspberry");
  62. beans.add("Red Apple");
  63. beans.add("Sizzling Cinnamon");
  64. beans.add("Strawberry Cheesecake");
  65. beans.add("Strawberry Daiquiri");
  66. beans.add("Strawberry Jam");
  67. beans.add("Sour Cherry");
  68. beans.add("Sunkist Lemon");
  69. beans.add("Sunkist Lime");
  70. beans.add("Sunkist Orange");
  71. beans.add("Sunkist Pink Grapefruit");
  72. beans.add("Sunkist Tangerine");
  73. beans.add("Toasted Marshmallow");
  74. beans.add("Top Banana");
  75. beans.add("Tutti-Fruitti");
  76. beans.add("Very Cherry");
  77. beans.add("Watermelon");
  78. beans.add("Wild Blackberry");
  79. beans.add("Guava");
  80. beans.add("Passion fruit");
  81. beans.add("Green tea");
  82. }
  83.  
  84. private static final long serialVersionUID = -3786589842545919211L;
  85.  
  86. public void init() {
  87.  
  88. JLabel flavorLabel = new JLabel("Choose # flavors");
  89. flavorSpinner = new JSpinner(new SpinnerNumberModel(DEFAULT_FLAVORS,MIN_FLAVORS,MAX_FLAVORS,1));
  90. JPanel flavorPanel = new JPanel();
  91. flavorPanel.add(flavorLabel);
  92. flavorPanel.add(flavorSpinner);
  93. getContentPane().add(flavorPanel, BorderLayout.NORTH);
  94.  
  95. flavorResults = new JTextArea();
  96. JScrollPane scrollPane = new JScrollPane(flavorResults);
  97. getContentPane().add(scrollPane, BorderLayout.CENTER);
  98.  
  99. JButton flavorButton = new JButton("Click");
  100. flavorButton.addActionListener(this);
  101. getContentPane().add(flavorButton, BorderLayout.SOUTH);
  102. }
  103.  
  104. @Override
  105. public void actionPerformed(ActionEvent e)
  106. {
  107. int flavors = (Integer) flavorSpinner.getValue();
  108.  
  109. List<String> randomBeans = new ArrayList<String>(beans);
  110. Collections.shuffle(randomBeans);
  111.  
  112. flavorResults.append("Flavors chosen: " + flavors + ".\n");
  113. flavorResults.append("Generating flavor combo: \n");
  114.  
  115. for (int i = 0; i < flavors; i++)
  116. {
  117. flavorResults.append(" " + randomBeans.get(i) + "\n");
  118. }
  119. }
  120. }

Report this snippet  

You need to login to post a comment.