Posted By

manchestertaylor on 08/16/14


Tagged

math java gui buttons arithmetic actionListener


Versions (?)

Ratio Game


 / Published in: Java
 

In a GUI you are given two ratios and you must decide if one is greater than, less than, or equal to the other by clicking one of the buttons.

  1. import java.awt.*;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. import javax.swing.*;
  6. import javax.swing.border.Border;
  7.  
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.ActionEvent;
  10.  
  11. /*
  12.  * Taymaster's ratio game
  13.  */
  14.  
  15. public class RatioGameGUI implements ActionListener{
  16.  
  17. JPanel ratioPanel, buttonPanel;
  18. JLabel num1Label, denom1Label, num2Label, denom2Label, divisor1, divisor2, directions;
  19. JButton lessThan, greaterThan, equalTo;
  20. JTextArea middle, feedback;
  21.  
  22. public static double[] ratios = new double[4];
  23. public static String[] stringRatios = new String[4];
  24.  
  25. private static void createAndShowGUI()
  26. {
  27. JFrame.setDefaultLookAndFeelDecorated(true);
  28. JFrame frame = new JFrame("Ratio Game");
  29.  
  30. //Create and set up the content pane.
  31. RatioGameGUI demo = new RatioGameGUI();
  32. frame.setContentPane(demo.createContentPane());
  33.  
  34. // The other bits and pieces that make our program a bit more stable.
  35. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  36. frame.setSize(700, 550);
  37. frame.setVisible(true);
  38. }
  39.  
  40. public JPanel createContentPane()
  41. {
  42. //one JPanel to rule them all
  43. JPanel totalGUI = new JPanel();
  44. totalGUI.setLayout(null);
  45.  
  46. Font font = new Font("Tempus Sans ITC", Font.BOLD, 32);
  47. Font font2 = new Font("Tempus Sans ITC", Font.BOLD, 48);
  48. Font font3 = new Font("Tempus Sans ITC", Font.BOLD, 14);
  49. Border blackline = BorderFactory.createLineBorder(Color.black);
  50.  
  51. generateRatios();
  52.  
  53.  
  54. // //a panel for the user to choose the difficulty
  55. // difficultyPanel = new JPanel();
  56. // feedbackPanel.setLayout(null);
  57. // feedbackPanel.setLocation(100, 100);
  58. // feedbackPanel.setSize(500, 180);
  59. // feedbackPanel.setBorder(blackline);
  60. // totalGUI.add(feedbackPanel);
  61.  
  62.  
  63. //a panel to hold the ratios, blank box, directions, and feedback
  64. ratioPanel = new JPanel();
  65. ratioPanel.setLayout(null);
  66. ratioPanel.setLocation(50, 50);
  67. ratioPanel.setSize(645, 230);
  68. //ratioPanel.setBorder(blackline);
  69. totalGUI.add(ratioPanel);
  70.  
  71. feedback = new JTextArea("Correct?\n" + "your answer\n");
  72. feedback.setFont(font3);
  73. feedback.setEditable(false);
  74. feedback.setLocation(460,60);
  75. feedback.setSize(175,160);
  76. feedback.setBorder(blackline);
  77. ratioPanel.add(feedback);
  78.  
  79. directions = new JLabel("Fill in the blank"); //add ratio text here <----------
  80. directions.setFont(font3);
  81. directions.setLocation(150, 10);
  82. directions.setSize(200, 50);
  83. directions.setHorizontalAlignment(0);
  84. ratioPanel.add(directions);
  85.  
  86. num1Label = new JLabel(stringRatios[0]); //add ratio text here <----------
  87. num1Label.setFont(font);
  88. num1Label.setLocation(0, 60);
  89. num1Label.setSize(200, 50);
  90. // num1Label.setBorder(blackline);
  91. num1Label.setHorizontalAlignment(0);
  92. ratioPanel.add(num1Label);
  93.  
  94. divisor1 = new JLabel("-"); //add ratio text here <----------
  95. divisor1.setFont(font);
  96. divisor1.setLocation(0, 115);
  97. divisor1.setSize(200, 50);
  98. // divisor1.setBorder(blackline);
  99. divisor1.setHorizontalAlignment(0);
  100. ratioPanel.add(divisor1);
  101.  
  102. denom1Label = new JLabel(stringRatios[1]); //add ratio text here <----------
  103. denom1Label.setFont(font);
  104. denom1Label.setLocation(0, 170);
  105. denom1Label.setSize(200, 50);
  106. // denom1Label.setBorder(blackline);
  107. denom1Label.setHorizontalAlignment(0);
  108. ratioPanel.add(denom1Label);
  109.  
  110. middle = new JTextArea("");
  111. middle.setFont(font2);
  112. middle.setLocation(210, 95);
  113. middle.setSize(80, 90);
  114. middle.setBorder(blackline);
  115. middle.setEditable(false);
  116. ratioPanel.add(middle);
  117.  
  118. num2Label = new JLabel(stringRatios[2]); //add ratio text here <----------
  119. num2Label.setFont(font);
  120. num2Label.setLocation(300, 60);
  121. num2Label.setSize(200, 50);
  122. // num2Label.setBorder(blackline);
  123. num2Label.setHorizontalAlignment(0);
  124. ratioPanel.add(num2Label);
  125.  
  126. divisor2 = new JLabel("-"); //add ratio text here <----------
  127. divisor2.setFont(font);
  128. divisor2.setLocation(300, 115);
  129. divisor2.setSize(200, 50);
  130. // divisor2.setBorder(blackline);
  131. divisor2.setHorizontalAlignment(0);
  132. ratioPanel.add(divisor2);
  133.  
  134. denom2Label = new JLabel(stringRatios[3]); //add ratio text here <----------
  135. denom2Label.setFont(font);
  136. denom2Label.setLocation(300, 170);
  137. denom2Label.setSize(200, 50);
  138. // denom2Label.setBorder(blackline);
  139. denom2Label.setHorizontalAlignment(0);
  140. ratioPanel.add(denom2Label);
  141.  
  142. // A panel to hold the buttons
  143. buttonPanel = new JPanel();
  144. buttonPanel.setLayout(null);
  145. buttonPanel.setLocation(50, 300);
  146. buttonPanel.setSize(500, 45);
  147. //buttonPanel.setBorder(blackline);
  148. totalGUI.add(buttonPanel);
  149.  
  150. lessThan = new JButton("<");
  151. lessThan.setLocation(108,5);
  152. lessThan.setSize(90,35);
  153. lessThan.setBorder(blackline);
  154. lessThan.addActionListener(this);
  155. buttonPanel.add(lessThan);
  156.  
  157. greaterThan = new JButton(">");
  158. greaterThan.setLocation(205,5);
  159. greaterThan.setSize(90, 35);
  160. greaterThan.setBorder(blackline);
  161. greaterThan.addActionListener(this);
  162. buttonPanel.add(greaterThan);
  163.  
  164. equalTo = new JButton("=");
  165. equalTo.setLocation(300,5);
  166. equalTo.setSize(90,35);
  167. equalTo.setBorder(blackline);
  168. equalTo.addActionListener(this);
  169. buttonPanel.add(equalTo);
  170.  
  171. totalGUI.setOpaque(true);
  172. return totalGUI;
  173. }
  174.  
  175. public void actionPerformed(ActionEvent e) {
  176. if(e.getSource() == equalTo)
  177. {
  178. if (isCorrect("="))
  179. {
  180. feedback.setText("Correct\n"+stringRatios[0]+" / "+stringRatios[1]+" = "+stringRatios[2]+" / "+stringRatios[3]);
  181. generateRatios();
  182. num1Label.setText(stringRatios[0]);
  183. denom1Label.setText(stringRatios[1]);
  184. num2Label.setText(stringRatios[2]);
  185. denom2Label.setText(stringRatios[3]);
  186. }
  187. else {
  188. feedback.setText("incorrect\n"+stringRatios[0]+" / "+stringRatios[1]+" = "+stringRatios[2]+" / "+stringRatios[3]);
  189. generateRatios();
  190. num1Label.setText(stringRatios[0]);
  191. denom1Label.setText(stringRatios[1]);
  192. num2Label.setText(stringRatios[2]);
  193. denom2Label.setText(stringRatios[3]);
  194. }
  195. }
  196. else if(e.getSource() == lessThan)
  197. {
  198. if (isCorrect("<")){
  199. feedback.setText("Correct\n"+stringRatios[0]+" / "+stringRatios[1]+" < "+stringRatios[2]+" / "+stringRatios[3]);
  200. generateRatios();
  201. num1Label.setText(stringRatios[0]);
  202. denom1Label.setText(stringRatios[1]);
  203. num2Label.setText(stringRatios[2]);
  204. denom2Label.setText(stringRatios[3]);
  205. }
  206. else {
  207. feedback.setText("incorrect\n"+stringRatios[0]+" / "+stringRatios[1]+" < "+stringRatios[2]+" / "+stringRatios[3]);
  208. generateRatios();
  209. num1Label.setText(stringRatios[0]);
  210. denom1Label.setText(stringRatios[1]);
  211. num2Label.setText(stringRatios[2]);
  212. denom2Label.setText(stringRatios[3]);
  213. }
  214. }
  215. else if(e.getSource() == greaterThan)
  216. {
  217. if (isCorrect(">")) {
  218. feedback.setText("Correct\n"+stringRatios[0]+" / "+stringRatios[1]+" > "+stringRatios[2]+" / "+stringRatios[3]);
  219. generateRatios();
  220. num1Label.setText(stringRatios[0]);
  221. denom1Label.setText(stringRatios[1]);
  222. num2Label.setText(stringRatios[2]);
  223. denom2Label.setText(stringRatios[3]);
  224. }
  225. else {
  226. feedback.setText("incorrect\n"+stringRatios[0]+" / "+stringRatios[1]+" < "+stringRatios[2]+" / "+stringRatios[3]);
  227. generateRatios();
  228. num1Label.setText(stringRatios[0]);
  229. denom1Label.setText(stringRatios[1]);
  230. num2Label.setText(stringRatios[2]);
  231. denom2Label.setText(stringRatios[3]);
  232. }
  233. }
  234. }
  235.  
  236. public static boolean isCorrect(String answer)
  237. {
  238. double ratio1=ratios[0]/ratios[1];
  239. double ratio2=ratios[2]/ratios[3];
  240. boolean truth=false;
  241. if (answer.equals("=") && ratio1==ratio2)
  242. truth=true;
  243. else if (answer.equals("<") && ratio1<ratio2)
  244. truth=true;
  245. else if (answer.equals(">") && ratio1>ratio2)
  246. truth=true;
  247. return truth;
  248. }
  249.  
  250. public static void generateRatios()
  251. {
  252. Random random = new Random();
  253. double epsilon=0; double ratio1, ratio2;
  254.  
  255. ratios[0]=1+random.nextInt(20);
  256. ratios[1]=5+random.nextInt(20);
  257. while(ratios[0]>=ratios[1])
  258. {//make sure numerator>denominator
  259. ratios[0]=1+random.nextInt(20);
  260. }
  261. ratios[2]=1+random.nextInt(20);
  262. ratios[3]=5+random.nextInt(20);
  263. while(ratios[2]>=ratios[3])
  264. {//make sure numerator>denominator
  265. ratios[2]=1+random.nextInt(20);
  266. }
  267. ratio1=ratios[0]/ratios[1];
  268. ratio2=ratios[2]/ratios[3];
  269. epsilon=Math.abs(ratio1-ratio2);
  270.  
  271. for (int j = 0; j<60; j++)
  272. {
  273. if (epsilon > .1) {
  274. ratios[2]=1+random.nextInt(20);
  275. while (ratios[2]>ratios[3]) {
  276. ratios[2]=1+random.nextInt(20);
  277. }
  278. ratio2=ratios[2]/ratios[3];
  279. epsilon=Math.abs(ratio1-ratio2);
  280. //System.out.println(i+" "+ratios[0]+" "+ratios[1]+" "+ratios[2]+" "+ratios[3]);
  281. }
  282. else
  283. break;
  284. }
  285.  
  286. if (epsilon > .1){
  287. ratios[3]=ratios[3]=5+random.nextInt(20);
  288. for (int j = 0; j<60; j++)
  289. {
  290. if (epsilon > .1) {
  291. ratios[2]=1+random.nextInt(20);
  292. while (ratios[2]>ratios[3]) {
  293. ratios[2]=1+random.nextInt(20);
  294. }
  295. ratio2=ratios[2]/ratios[3];
  296. epsilon=Math.abs(ratio1-ratio2);
  297. //System.out.println(i+" "+ratios[0]+" "+ratios[1]+" "+ratios[2]+" "+ratios[3]);
  298. }
  299. else
  300. break;
  301. }
  302. }
  303.  
  304. if (epsilon > .1){
  305. ratios[2]=ratios[0]; ratios[3]=ratios[1];
  306. }
  307. //System.out.println(i+" "+ratios[0]+" "+ratios[1]+" "+ratios[2]+" "+ratios[3]);
  308.  
  309. stringRatios = convertToString(ratios);
  310. }
  311.  
  312. public static String[] convertToString(double[] doubles) {
  313. String[] newArray = new String[4];
  314. newArray[0] = Double.toString(doubles[0]);
  315. newArray[0]=newArray[0].substring(0, newArray[0].indexOf("."));
  316. newArray[1] = Double.toString(doubles[1]);
  317. newArray[1]=newArray[1].substring(0, newArray[1].indexOf("."));
  318. newArray[2] = Double.toString(doubles[2]);
  319. newArray[2]=newArray[2].substring(0, newArray[2].indexOf("."));
  320. newArray[3] = Double.toString(doubles[3]);
  321. newArray[3]=newArray[3].substring(0, newArray[3].indexOf("."));
  322. return newArray;
  323. }
  324.  
  325. public static void main(String[] args) {
  326. SwingUtilities.invokeLater(new Runnable() {
  327. public void run() {
  328. createAndShowGUI();
  329. }
  330. });
  331.  
  332. }
  333.  
  334. }

Report this snippet  

You need to login to post a comment.