Advertisement
Guest User

mycode

a guest
Jan 13th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.66 KB | None | 0 0
  1. import java.applet.Applet;
  2. import java.awt.Dimension;
  3. import java.awt.GridLayout;
  4.  
  5. import javax.swing.*;
  6. import java.io.FileNotFoundException;
  7. import java.util.Formatter;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.io.*;
  11. import java.util.Scanner;
  12. import javax.swing.ImageIcon;
  13. import javax.swing.JPanel;
  14. import javax.swing.Timer;
  15.  
  16.  
  17. public class applet extends JApplet {
  18.  
  19. @Override
  20. public void init() {
  21. start();
  22. }
  23.  
  24.  
  25. public class UserInterface
  26. {
  27. JLabel Bmi= new JLabel("Your Bmi is:");
  28. JLabel BMIOutPut= new JLabel("BMIOUTPUT");
  29. JLabel image1= new JLabel();
  30. JLabel image2= new JLabel("IMAGE");
  31. JTextArea Goal= new JTextArea("Please Enter Your Goal Weight:");
  32. JTextArea GoalInput = new JTextArea("Delete this text");
  33. JTextArea Week= new JTextArea("Please Enter How Many Weeks \n untill you plan to reach your goal");
  34. JTextArea WeekInput = new JTextArea("Delete this text");
  35. JButton Write = new JButton("Calculate");
  36. JTextArea Safe= new JTextArea("Default");
  37. JTextArea colarieOutPut= new JTextArea("Default");
  38. public void Interface()
  39. {
  40.  
  41. JPanel Panel = new JPanel();
  42. Panel.setMaximumSize(new Dimension(400, 400));
  43. Panel.setSize(800,500);
  44. Panel.setLocation(300,150);
  45.  
  46. Panel.setVisible(false);
  47. GridLayout Layout = new GridLayout(0,4);
  48. Panel.setLayout(Layout);
  49.  
  50.  
  51.  
  52. Panel.add(Bmi);
  53.  
  54.  
  55.  
  56. Panel.add(BMIOutPut);
  57.  
  58.  
  59.  
  60. Panel.add(image1);
  61. ImageIcon Icon = new ImageIcon("Default.jpg");
  62. image1.setIcon(Icon);
  63.  
  64.  
  65. Panel.add(image2);
  66.  
  67.  
  68.  
  69. Goal.setEditable(false);
  70. Goal.setCursor(null);
  71. Goal.setOpaque(false);
  72. Goal.setFont(UIManager.getFont("Label.font"));
  73. Panel.add(Goal);
  74.  
  75.  
  76.  
  77.  
  78. GoalInput.setOpaque(false);
  79. GoalInput.setFont(UIManager.getFont("Label.font"));
  80. Panel.add(GoalInput);
  81.  
  82.  
  83. Week.setEditable(false);
  84. Week.setCursor(null);
  85. Week.setOpaque(false);
  86. Week.setFont(UIManager.getFont("Label.font"));
  87. Panel.add(Week);
  88.  
  89.  
  90.  
  91.  
  92.  
  93. WeekInput.setOpaque(false);
  94. WeekInput.setFont(UIManager.getFont("Label.font"));
  95. Panel.add(WeekInput);
  96.  
  97.  
  98.  
  99. Panel.add(Write);
  100. Write.setSize(10,10);
  101. Write.setOpaque(false);
  102. Write.setFont(UIManager.getFont("Label.font"));
  103.  
  104.  
  105. Safe.setEditable(false);
  106. Safe.setCursor(null);
  107. Safe.setOpaque(false);
  108. Safe.setSize(1, 1);
  109. Safe.setFont(UIManager.getFont("Label.font"));
  110. Panel.add(Safe);
  111.  
  112.  
  113. JTextArea colarie= new JTextArea("To Obtain your target \n weight you need to consume:");
  114. colarie.setEditable(false);
  115. colarie.setCursor(null);
  116. colarie.setOpaque(false);
  117. colarie.setSize(1, 1);
  118. Panel.add(colarie);
  119. colarie.setFont(UIManager.getFont("Label.font"));
  120.  
  121.  
  122.  
  123. colarieOutPut.setEditable(false);
  124. colarieOutPut.setCursor(null);
  125. colarieOutPut.setOpaque(false);
  126. colarieOutPut.setSize(1, 1);
  127. Panel.add(colarieOutPut);
  128. colarieOutPut.setFont(UIManager.getFont("Label.font"));
  129.  
  130. Panel.setVisible(true);
  131. Panel.repaint();
  132. }
  133. }
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147. public class MainCode extends UserInterface
  148. {
  149. ImageIcon Icon_T = new ImageIcon("Thin.jpg");
  150. ImageIcon Icon_n = new ImageIcon("normal.jpg");
  151. ImageIcon Icon_F = new ImageIcon("Fatty.jpg");
  152. int delay1 = 250;
  153. int delay2 = 500;
  154. private double Bmi_Output = 0;
  155. private int height = 0;
  156. private int weight = 0;
  157. private int calorie_input = 0;
  158. private int calorie = 0;
  159. private int calorie_temp = 0;
  160. private int CALORIE = 3500;
  161. private int BMI = 703;
  162. private int weight_goal= 0;
  163. private int weeks = 0;
  164. public int gain_lose_or_maintain = 0;
  165. private boolean saftey = false;
  166.  
  167. private Scanner input;;
  168.  
  169.  
  170.  
  171. public void openFile()
  172. {
  173.  
  174. try
  175. {
  176. input = new Scanner(new File("User"));
  177. }//try
  178.  
  179.  
  180. catch( FileNotFoundException fileNotFoundException )
  181. {
  182. System.err.println("Error opening file.");
  183. System.exit(1);
  184. }//catch
  185.  
  186.  
  187. }//openFile
  188.  
  189. public double calculate_BMI()
  190. {
  191. height = input.nextInt();
  192. weight = input.nextInt();
  193.  
  194. height = height * height;
  195. Bmi_Output = weight / height;
  196. Bmi_Output = Bmi_Output * BMI;
  197. return Bmi_Output ;
  198. }//calc BMI
  199.  
  200. public int calorie_calc()
  201. {
  202. UserInterface You_Told_me_not_to_use_trololol_so_i_didnt = new UserInterface();
  203. calorie_input= input.nextInt();
  204. String temp_input= You_Told_me_not_to_use_trololol_so_i_didnt.GoalInput.getText();
  205. weight_goal = Integer.parseInt(temp_input);
  206. weight = input.nextInt();
  207. ;
  208. if (weight > weight_goal)
  209. {
  210. calorie_temp = (weight-weight_goal) * CALORIE;
  211. gain_lose_or_maintain = 1;
  212. }
  213. else if (weight < weight_goal)
  214. {
  215. calorie_temp = (weight_goal-weight) * CALORIE;
  216. gain_lose_or_maintain = 2;
  217. }
  218. else
  219. {
  220. calorie_temp = 0;
  221. gain_lose_or_maintain = 3;
  222. }
  223. String Temp_input = You_Told_me_not_to_use_trololol_so_i_didnt.WeekInput.getText();
  224. weeks = Integer.parseInt(Temp_input);
  225. calorie = calorie_input - (calorie_temp / weeks);
  226. return calorie;
  227.  
  228. }//calorie calc
  229.  
  230. public boolean determine_if_safe()
  231. {
  232. double temp = weight_goal / height;
  233.  
  234. if (temp > 18.5 && temp < 24.9)
  235. {
  236.  
  237. if (calorie < 1200)
  238. {
  239. saftey = false;
  240.  
  241. }
  242. else if (calorie >= 1200 && calorie_temp > (calorie_input*.2))
  243. {
  244. saftey = true;
  245.  
  246. }
  247. else
  248. {
  249. saftey = false;
  250. }
  251. }
  252. else
  253. {
  254. saftey = false;
  255. }
  256. return saftey;
  257. }//safe
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267. public void Change_Bmi()
  268. {
  269. BMIOutPut.setText("" + Bmi_Output);
  270.  
  271.  
  272. if(Bmi_Output < 18.5)
  273. {
  274.  
  275. ActionListener Thin_Pic_label1 = new ActionListener() {
  276. public void actionPerformed(ActionEvent evt) {
  277.  
  278.  
  279. image1.setIcon(Icon_T);
  280. image2.setText(null);
  281.  
  282.  
  283. }
  284. };
  285.  
  286. ActionListener Thin_Pic_label2 = new ActionListener() {
  287. public void actionPerformed(ActionEvent evt) {
  288.  
  289.  
  290. image2.setIcon(Icon_T);
  291. image1.setText(null);
  292.  
  293.  
  294. }
  295. };
  296. new Timer (delay1,Thin_Pic_label1).start();
  297. new Timer (delay2,Thin_Pic_label2).start();
  298.  
  299.  
  300. }
  301. else if(Bmi_Output > 18.5 && Bmi_Output < 24.9)
  302. {
  303. ActionListener Normal_Pic_label1 = new ActionListener() {
  304. public void actionPerformed(ActionEvent evt) {
  305.  
  306.  
  307. image1.setIcon(Icon_n);
  308. image2.setText(null);
  309.  
  310.  
  311. }
  312. };
  313.  
  314. ActionListener Normal_Pic_label2 = new ActionListener() {
  315. public void actionPerformed(ActionEvent evt) {
  316.  
  317.  
  318. image2.setIcon(Icon_n);
  319. image1.setText(null);
  320.  
  321.  
  322. }
  323. };
  324. new Timer (delay1,Normal_Pic_label1).start();
  325. new Timer (delay2,Normal_Pic_label2).start();
  326. }
  327. else
  328. {
  329. ActionListener Fatty_Pic_label1 = new ActionListener() {
  330. public void actionPerformed(ActionEvent evt) {
  331.  
  332.  
  333. image1.setIcon(Icon_F);
  334. image2.setText(null);
  335.  
  336.  
  337. }
  338. };
  339.  
  340. ActionListener Fatty_Pic_label2 = new ActionListener() {
  341. public void actionPerformed(ActionEvent evt) {
  342.  
  343.  
  344. image2.setIcon(Icon_F);
  345. image1.setText(null);
  346.  
  347.  
  348. }
  349. };
  350. new Timer (delay1,Fatty_Pic_label1).start();
  351. new Timer (delay2,Fatty_Pic_label2).start();
  352. }
  353. }
  354.  
  355.  
  356. public void Change_safe()
  357. {
  358. if (saftey = true)
  359. Safe.setText("It Is safe to reach that goal!");
  360. else
  361. Safe.setText("It Is NOT safe to reach that goal");
  362. }
  363.  
  364.  
  365. public void Change_Calorie()
  366. {
  367. colarieOutPut.setText("" + calorie);
  368. }
  369.  
  370.  
  371. public void Change_InterFace()
  372. {
  373. Write.addActionListener(new ActionListener(){
  374. public void actionPerformed( ActionEvent event)
  375. {
  376.  
  377. Change_Bmi();
  378. Change_safe();
  379. Change_Calorie();
  380. WhatToOpen runs =new WhatToOpen();
  381. try {
  382. runs.write_What_To_Open_Runner();
  383. } catch (FileNotFoundException e) {
  384. // TODO Auto-generated catch block
  385. e.printStackTrace();
  386. }
  387. }
  388. });
  389. }
  390.  
  391. }//end
  392.  
  393.  
  394.  
  395.  
  396. public class WhatToOpen
  397. {
  398.  
  399. private Formatter output;
  400. public void openFile() throws FileNotFoundException
  401. {
  402. output = new Formatter( "LoseMaintainOrGain" );
  403. }//open
  404.  
  405. public void write_What_To_Open()
  406. {
  407. MainCode main = new MainCode();
  408.  
  409. output.format("%f",main.gain_lose_or_maintain);
  410.  
  411. }//whattowrite
  412.  
  413. public void closeFile()
  414. {
  415. if ( output != null )
  416. output.close();
  417. } // end method closeFile
  418.  
  419. public void write_What_To_Open_Runner() throws FileNotFoundException
  420. {
  421. openFile();
  422. write_What_To_Open();
  423. closeFile();
  424. }
  425. }//end
  426. public class Client
  427. {
  428. public void main(String[] args)
  429. {
  430. UserInterface runner = new UserInterface();
  431. runner.Interface();
  432.  
  433. MainCode run = new MainCode();
  434. run.Change_InterFace();
  435.  
  436.  
  437.  
  438.  
  439. }
  440.  
  441.  
  442. }
  443. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement