Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.46 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.File;
  4. import java.io.IOException;
  5.  
  6. import javax.imageio.ImageIO;
  7. import javax.swing.*;
  8.  
  9.  
  10. public class CKDJavaGame extends JFrame{
  11.  
  12. public CKDJavaGame()
  13. {
  14. setSize(1350, 800);
  15. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16. setLocation(0,0);
  17.  
  18. MainPan pan = new MainPan();
  19. add(pan);
  20. setResizable(true);
  21. setVisible(true);
  22. }
  23. public static void main (String []args)
  24. {
  25. CKDJavaGame ckd = new CKDJavaGame();
  26. }
  27.  
  28. }
  29.  
  30. class MainPan extends JPanel
  31. {
  32. private int xcor, ycor;
  33. private CardLayout layout; // cardlayout instance
  34. private MainPan mainp;
  35. private boolean topics, topicsB;
  36.  
  37. public MainPan()
  38. {
  39. layout = new CardLayout();
  40.  
  41. this.setLayout(layout); // setting this class to card layout
  42.  
  43. topics=false;
  44. topicsB=false;
  45.  
  46. ycor = 800;
  47. xcor = 1350;
  48.  
  49. GamePagePanel page = new GamePagePanel(this);
  50. add(page, "CKD Detector");
  51.  
  52. /*
  53. InstructionPanel instruction = new InstructionPanel(this); // for instructions, has its own button
  54. add(instruction, "Instructions");
  55. */
  56.  
  57. layout.show(this, "CKD Detector");
  58.  
  59.  
  60. PlayPanel play = new PlayPanel(this); // learn about the parts and functions of the respiratory system
  61. add(play, "Test to see if you have CKD!");
  62.  
  63. /*
  64. BasicFunctionPanel bfunc = new BasicFunctionPanel(this); // game mode basic
  65. add(bfunc, "The basic level of the functions");
  66.  
  67. AdvancedFunctionPanel afunc = new AdvancedFunctionPanel(this); // game mode advanced
  68. add(afunc, "The advanced level of the functions");
  69.  
  70. LearnBreathePanel breathe = new LearnBreathePanel(this); // learning about a new topic, breathing and diseases
  71. add(breathe, "Learning about the process of breathing");
  72.  
  73. BasicBreathePanel bb = new BasicBreathePanel(this); // game mode for breathing and diseases
  74. add(bb, "The basic level of breathing processes");
  75.  
  76. AdvancedBreathePanel ab = new AdvancedBreathePanel(this); // advanced game mode for breathing and diseases
  77. add(ab, "The advanced level of breathing processes");
  78. */
  79.  
  80.  
  81. }
  82.  
  83. public class GamePagePanel extends JPanel implements ActionListener
  84. {
  85.  
  86. ButtonGroup group;
  87. Font font;
  88. private Image background;
  89.  
  90. public GamePagePanel(MainPan mpI) // initializes radio buttons and font, calls other methods
  91. {
  92.  
  93. mainp = mpI; // main panels instance being initialized
  94.  
  95. setBackground(Color.RED);
  96. setLayout(null);
  97.  
  98. font = new Font("SansSerif", Font.BOLD, 20);
  99. setFont(font);
  100.  
  101. buttonMethod();
  102.  
  103. getMyImages();
  104.  
  105. }
  106.  
  107. public void buttonMethod() // intializes all the buttons, sets the bounds, adds ActionListener
  108. {
  109.  
  110.  
  111. JButton btn1 = new JButton("Instructions");
  112. JButton btn2 = new JButton("Take the Test");
  113. //JButton btn3 = new JButton("<html><p>Respiratory</p><p>Diseases</p>");
  114.  
  115. btn1.setFont(font);
  116. btn2.setFont(font);
  117. //btn3.setFont(font);
  118.  
  119.  
  120. add(btn1);
  121. add(btn2);
  122. //add(btn3);
  123.  
  124. btn1.addActionListener(this);
  125. btn2.addActionListener(this);
  126. //btn3.addActionListener(this);
  127.  
  128. btn1.setBounds(160, 390, 150, 50); // left, top, width, height
  129. btn2.setBounds(50, 600, 250, 80); // left, top, width, height
  130. //btn3.setBounds(350, 600, 200, 80); // left, top, width, height
  131.  
  132. btn1.setForeground(Color.WHITE);
  133. btn1.setBackground(Color.BLUE);
  134. btn2.setForeground(Color.WHITE);
  135. btn2.setBackground(Color.BLUE);
  136. //btn3.setForeground(Color.WHITE);
  137. //btn3.setBackground(Color.BLUE);
  138.  
  139. }
  140.  
  141. public void getMyImages() // gets images like the respiratory image that I have on my front page
  142. {
  143. try
  144. {
  145. background = ImageIO.read(new File("src/kidney.png"));
  146. }
  147. catch(IOException e)
  148. {
  149. System.err.println(" can't be found");
  150. e.printStackTrace();
  151. }
  152.  
  153. }
  154. public void paintComponent(Graphics g) // paints String to match JButtons and JRadioButtons, also makes the title
  155. {
  156. super.paintComponent(g);
  157.  
  158. Font font2 = new Font("Courier", Font.BOLD, 60);
  159. g.setFont(font2);
  160.  
  161. g.setColor(Color.WHITE);
  162. g.drawImage(background, 0, 0, 1350, 800, this);
  163.  
  164. g.setColor(Color.BLUE);
  165. g.drawString("CKD Detector ", 60, 80);
  166.  
  167. g.setFont(font);
  168.  
  169. g.setColor(Color.BLACK);
  170.  
  171. }
  172.  
  173. public void actionPerformed(ActionEvent e) // checks if button pressed is instructions, if so displays the instructions
  174. {
  175.  
  176. // Concept List: .equals() is a String method
  177. if(e.getActionCommand().equals("Instructions"))
  178. {
  179. System.out.println("hello");
  180. layout.show(mainp, "Instructions");
  181. }
  182.  
  183. // checks to see if other buttons are pressed and if the radio button chosen are learn or play mode, this leads to different panels
  184. // with checks using CardLayout
  185. if(e.getActionCommand().equals("Take the Test"))
  186. {
  187. layout.show(mainp, "Test to see if you have CKD!");
  188.  
  189. }
  190.  
  191. } // end of method
  192.  
  193. } // end of class
  194.  
  195. class PlayPanel extends JPanel implements ActionListener
  196. {
  197. JButton checkResult;
  198. private int i1, counter;
  199. public int numberArray[];
  200.  
  201. private boolean rep;
  202.  
  203. private int threatScore, symptomScore;
  204. JRadioButton radioNeverButtons[] = new JRadioButton[15];
  205. JRadioButton radioRareButtons[] = new JRadioButton[15];
  206. JRadioButton radioOftenButtons[] = new JRadioButton[15];
  207. JRadioButton radioAlwaysButtons[] = new JRadioButton[15];
  208.  
  209. private String questionArray[];
  210.  
  211. JButton return1, start;
  212.  
  213. Timer move;
  214.  
  215. public PlayPanel(MainPan mpI) // intializes JLabel, return button to go back to home page
  216. // adds Mouse and KeyListeners
  217. {
  218. mainp = mpI;
  219. setLayout(null);
  220. setBackground(Color.GREEN);
  221.  
  222. numberArray = new int [15];
  223. rep = false;
  224. threatScore=0;
  225. symptomScore=0;
  226.  
  227. questionArray = new String []{"Do you feel fatigue?","Do you feel cold when the ambient temperature is hot?","Are you short of breath after very little effort?",
  228. "Do you feel faint, dizzy, or weak?", "Do you have trouble thinking clearly?", "Do you feel very itchy?", "Are you experiencing swelling in your hands and feet?" ,
  229. "Do you have a swollen or puffy face?", "Does food taste like metal?", "Do you have ammonia breath?", "Do you experience nausea and vomiting?",
  230. "Do you have to urinate frequently at night?", "Do you have foamy or bubbly urine", " When you urinate, do you see an abnormal urine color?",
  231. "Do you experience pressure when you urinate?"};
  232.  
  233. i1 = 0;
  234. counter=0;
  235.  
  236. radioButtonMethod();
  237.  
  238. return1 = new JButton("Return to main page");
  239. add(return1);
  240. return1.addActionListener(new ActionListener() {
  241. public void actionPerformed(ActionEvent e)
  242. {
  243. return1.setVisible(false);
  244. layout.show(mainp, "CKD Detector");
  245. }
  246. });
  247. return1.setBounds(1050, 20, 200, 50);
  248.  
  249. start = new JButton("Start");
  250. add(start);
  251. start.setBounds(1000, 600, 150, 60);
  252. start.setBackground(Color.RED);
  253. start.setForeground(Color.YELLOW);
  254.  
  255. start.addActionListener(new ActionListener(){
  256. public void actionPerformed(ActionEvent e)
  257. {
  258. start.setVisible(false);
  259. move.start();
  260. }
  261. });
  262.  
  263. //getTheImages();
  264.  
  265. //timer = new Timer(2000, this);
  266.  
  267. move= new Timer(10000, this);
  268.  
  269. // make an array of moveyButton and movexButton for each button and replace the values when you
  270. // are doing the coordinating
  271. }
  272.  
  273. public void radioButtonMethod()
  274. {
  275. /*
  276. JRadioButton radioNeverButtons[] = new JRadioButton[15];
  277. JRadioButton radioRareButtons[] = new JRadioButton[15];
  278. JRadioButton radioOftenButtons[] = new JRadioButton[15];
  279. JRadioButton radioAlwaysButtons[] = new JRadioButton[15];
  280. */
  281. for(int i=0; i<15; i++)
  282. {
  283. radioNeverButtons[i] = new JRadioButton("Never");
  284. radioRareButtons[i] = new JRadioButton("Rarely");
  285. radioOftenButtons[i] = new JRadioButton("Often");
  286. radioAlwaysButtons[i] = new JRadioButton("Always");
  287.  
  288. //radioNeverButtons[i].setSelected(true);
  289. if (i==0)
  290. {
  291. ButtonGroup group1 = new ButtonGroup();
  292. group1.add(radioNeverButtons[i]); group1.add(radioRareButtons[i]); group1.add(radioOftenButtons[i]); group1.add(radioAlwaysButtons[i]);
  293. }
  294. if (i==1)
  295. {
  296. ButtonGroup group2 = new ButtonGroup();
  297. group2.add(radioNeverButtons[i]); group2.add(radioRareButtons[i]); group2.add(radioOftenButtons[i]); group2.add(radioAlwaysButtons[i]);
  298. }
  299. if (i==2)
  300. {
  301. ButtonGroup group3 = new ButtonGroup();
  302. group3.add(radioNeverButtons[i]); group3.add(radioRareButtons[i]); group3.add(radioOftenButtons[i]); group3.add(radioAlwaysButtons[i]);
  303. }
  304.  
  305. if (i==3)
  306. {
  307. ButtonGroup group4 = new ButtonGroup();
  308. group4.add(radioNeverButtons[i]); group4.add(radioRareButtons[i]); group4.add(radioOftenButtons[i]); group4.add(radioAlwaysButtons[i]);
  309. }
  310. if (i==4)
  311. {
  312. ButtonGroup group5 = new ButtonGroup();
  313. group5.add(radioNeverButtons[i]); group5.add(radioRareButtons[i]); group5.add(radioOftenButtons[i]); group5.add(radioAlwaysButtons[i]);
  314. }
  315. if (i==5)
  316. {
  317. ButtonGroup group6 = new ButtonGroup();
  318. group6.add(radioNeverButtons[i]); group6.add(radioRareButtons[i]); group6.add(radioOftenButtons[i]); group6.add(radioAlwaysButtons[i]);
  319. }
  320. if (i==6)
  321. {
  322. ButtonGroup group7 = new ButtonGroup();
  323. group7.add(radioNeverButtons[i]); group7.add(radioRareButtons[i]); group7.add(radioOftenButtons[i]); group7.add(radioAlwaysButtons[i]);
  324. }
  325. if (i==7)
  326. {
  327. ButtonGroup group8 = new ButtonGroup();
  328. group8.add(radioNeverButtons[i]); group8.add(radioRareButtons[i]); group8.add(radioOftenButtons[i]); group8.add(radioAlwaysButtons[i]);
  329. }
  330. if (i==8)
  331. {
  332. ButtonGroup group9 = new ButtonGroup();
  333. group9.add(radioNeverButtons[i]); group9.add(radioRareButtons[i]); group9.add(radioOftenButtons[i]); group9.add(radioAlwaysButtons[i]);
  334. }
  335. if (i==9)
  336. {
  337. ButtonGroup group10 = new ButtonGroup();
  338. group10.add(radioNeverButtons[i]); group10.add(radioRareButtons[i]); group10.add(radioOftenButtons[i]); group10.add(radioAlwaysButtons[i]);
  339. }
  340. if (i==10)
  341. {
  342. ButtonGroup group11 = new ButtonGroup();
  343. group11.add(radioNeverButtons[i]); group11.add(radioRareButtons[i]); group11.add(radioOftenButtons[i]); group11.add(radioAlwaysButtons[i]);
  344. }
  345. if (i==11)
  346. {
  347. ButtonGroup group12 = new ButtonGroup();
  348. group12.add(radioNeverButtons[i]); group12.add(radioRareButtons[i]); group12.add(radioOftenButtons[i]); group12.add(radioAlwaysButtons[i]);
  349. }
  350. if (i==12)
  351. {
  352. ButtonGroup group13 = new ButtonGroup();
  353. group13.add(radioNeverButtons[i]); group13.add(radioRareButtons[i]); group13.add(radioOftenButtons[i]); group13.add(radioAlwaysButtons[i]);
  354. }
  355. if (i==13)
  356. {
  357. ButtonGroup group14 = new ButtonGroup();
  358. group14.add(radioNeverButtons[i]); group14.add(radioRareButtons[i]); group14.add(radioOftenButtons[i]); group14.add(radioAlwaysButtons[i]);
  359. }
  360. if (i==14)
  361. {
  362. ButtonGroup group15 = new ButtonGroup();
  363. group15.add(radioNeverButtons[i]); group15.add(radioRareButtons[i]); group15.add(radioOftenButtons[i]); group15.add(radioAlwaysButtons[i]);
  364. }
  365.  
  366. radioNeverButtons[i].addActionListener(new ActionHandler());
  367. radioRareButtons[i].addActionListener(new ActionHandler());
  368. radioOftenButtons[i].addActionListener(new ActionHandler());
  369. radioAlwaysButtons[i].addActionListener(new ActionHandler());
  370.  
  371. radioNeverButtons[i].setBackground(Color.CYAN); radioRareButtons[i].setBackground(Color.CYAN); radioOftenButtons[i].setBackground(Color.CYAN); radioAlwaysButtons[i].setBackground(Color.CYAN);
  372. }
  373.  
  374. }
  375.  
  376. public void paintComponent(Graphics g) // draws String for the Instructions, in proper format
  377. {
  378.  
  379. super.paintComponent(g);
  380. Font f4 = new Font("Serif", Font.BOLD + Font.ITALIC, 35);
  381. g.setFont(f4);
  382. g.setColor(Color.RED);
  383. g.fillRect(400, 20, 500, 50);
  384. g.setColor(Color.BLACK);
  385. g.drawString("Chronic Kidney Disease Detector ", 400, 50);
  386.  
  387. f4 = new Font("Serif", Font.BOLD, 25);
  388. g.setFont(f4);
  389. int x=100;
  390. g.setColor(Color.WHITE);
  391.  
  392. if(i1<15)
  393. {
  394. g.drawString( " "+ questionArray[i1], 50, 150);
  395. add(radioNeverButtons[i1]); add(radioRareButtons[i1]); add(radioOftenButtons[i1]); add(radioAlwaysButtons[i1]);
  396. radioNeverButtons[i1].setBounds(160, 200, 100, 30); radioRareButtons[i1].setBounds(360, 200, 100, 30);
  397. radioOftenButtons[i1].setBounds(560, 200, 100, 30); radioAlwaysButtons[i1].setBounds(760, 200, 100, 30);
  398.  
  399. g.drawString( " "+ questionArray[i1+1], 50, 320);
  400. add(radioNeverButtons[i1+1]); add(radioRareButtons[i1+1]); add(radioOftenButtons[i1+1]); add(radioAlwaysButtons[i1+1]);
  401. radioNeverButtons[i1+1].setBounds(160, 400, 100, 30); radioRareButtons[i1+1].setBounds(360, 400, 100, 30);
  402. radioOftenButtons[i1+1].setBounds(560, 400, 100, 30); radioAlwaysButtons[i1+1].setBounds(760, 400, 100, 30);
  403.  
  404. g.drawString( " "+ questionArray[i1+2], 50, 520);
  405. add(radioNeverButtons[i1+2]); add(radioRareButtons[i1+2]); add(radioOftenButtons[i1+2]); add(radioAlwaysButtons[i1+2]);
  406. radioNeverButtons[i1+2].setBounds(160, 600, 100, 30); radioRareButtons[i1+2].setBounds(360, 600, 100, 30);
  407. radioOftenButtons[i1+2].setBounds(560, 600, 100, 30); radioAlwaysButtons[i1+2].setBounds(760, 600, 100, 30);
  408. }
  409.  
  410. if(i1==15 && rep)
  411. {
  412. for(int i = 0; i<15; i++)
  413. {
  414. remove(radioNeverButtons[i]); remove(radioRareButtons[i]); remove(radioOftenButtons[i]); remove(radioAlwaysButtons[i]);
  415. }
  416. setBackground(Color.WHITE);
  417. symScore();
  418.  
  419. g.setColor(Color.BLUE);
  420. g.drawString("Your symptom score is " + symptomScore + " and the threat score is " + threatScore, 200, 100);
  421.  
  422. if(symptomScore-threatScore>=15)
  423. g.drawString(" You are at a high risk of developing this disease or already most likely have chronic kidney disease. Please visit a doctor in the near future. ", 200, 300);
  424. else if(symptomScore-threatScore <15 && symptomScore-threatScore >= 5 )
  425. g.drawString(" Your symptoms put you at a risk for developing this disease and you may already have it. Please consult a pediatrician. ", 200, 300);
  426. else if(symptomScore-threatScore <5 && symptomScore-threatScore >= -5 )
  427. g.drawString("It is inconclusive to if you certainly have the disease but you are at a risk to developing it. Pleaes rethink your health habits and see a professional.", 200, 300 );
  428.  
  429. else if (symptomScore-threatScore <-5 && symptomScore-threatScore >= -15 )
  430. g.drawString(" You are probably not at risk for developing the disease, but watch your health habits. ", 200, 300);
  431.  
  432. else
  433. g.drawString("You are not at risk for developing this disease ", 200, 300);
  434. }
  435. /*
  436. g.drawString("you are granted with two buttons: One to go back to the learn mode, and another to go to the advanced mode.",50, x+600);
  437. g.drawString(" If you go back to the learn mode, you have to play the game again, from the basic level.",50, x+630);
  438. */
  439.  
  440. } // end of paint component
  441.  
  442. public void symScore()
  443. {
  444. threatScore = 20;
  445. for(int i=0; i<15; i++)
  446. {
  447. symptomScore = numberArray[i] + symptomScore;
  448. System.out.println(numberArray[i]+ " ");
  449. }
  450. }
  451.  
  452. public void actionPerformed(ActionEvent e)
  453. {
  454. i1 = i1+ 3;
  455. if(i1==15)
  456. {
  457. move.stop();
  458. checkResult = new JButton("Check to see your result");
  459. add(checkResult);
  460. checkResult.setBounds(1050, 600, 200, 50);
  461. checkResult.addActionListener(new ActionListener() {
  462. public void actionPerformed(ActionEvent e)
  463. {
  464. checkResult.setVisible(false);
  465. return1.setVisible(false);
  466. rep = true;
  467. repaint();
  468. }
  469. });
  470. }
  471. repaint();
  472.  
  473. }
  474.  
  475. class ActionHandler extends JPanel implements ActionListener
  476. {
  477.  
  478. public ActionHandler() {}
  479.  
  480. public void actionPerformed(ActionEvent e)
  481. {
  482. //System.out.println(i1);
  483.  
  484. if(radioNeverButtons[i1].isSelected()==true)
  485. {
  486. numberArray[i1] = 0;
  487. }
  488. else if(radioRareButtons[i1].isSelected()==true)
  489. {
  490. numberArray[i1] = 1;
  491. //System.out.println("rare selected");
  492. }
  493.  
  494. else if(radioOftenButtons[i1].isSelected()==true)
  495. {
  496. numberArray[i1] = 2;
  497. //System.out.println("often selected");
  498. }
  499.  
  500. else if(radioAlwaysButtons[i1].isSelected()==true)
  501. {
  502. numberArray[i1] = 3;
  503. //System.out.println("always selected");
  504. }
  505.  
  506. if(radioNeverButtons[i1+1].isSelected()==true)
  507. {
  508. numberArray[i1+1] = 0;
  509. //System.out.println("never selected");
  510. }
  511.  
  512. else if(radioRareButtons[i1+1].isSelected()==true)
  513. {
  514. numberArray[i1+1] = 1;
  515. //System.out.println("rare selected");
  516.  
  517. }
  518.  
  519. else if(radioOftenButtons[i1+1].isSelected()==true)
  520. {
  521. numberArray[i1+1] = 2;
  522. //System.out.println("often selected");
  523. }
  524.  
  525. else if(radioAlwaysButtons[i1+1].isSelected()==true)
  526. {
  527. numberArray[i1+1] = 3;
  528. //System.out.println("always selected");
  529. }
  530.  
  531. if(radioNeverButtons[i1+2].isSelected()==true)
  532. {
  533. numberArray[i1+2] = 0;
  534. //System.out.println("never selected");
  535. }
  536.  
  537. else if(radioRareButtons[i1+2].isSelected()==true)
  538. {
  539. numberArray[i1+2] = 1;
  540. //System.out.println("rare selected");
  541. }
  542.  
  543. else if(radioOftenButtons[i1+2].isSelected()==true)
  544. {
  545. numberArray[i1+2] = 2;
  546. //System.out.println("often selected");
  547. }
  548.  
  549. else if(radioAlwaysButtons[i1+2].isSelected()==true)
  550. {
  551. numberArray[i1+2] = 3;
  552. //System.out.println("always selected");
  553. }
  554.  
  555. }
  556.  
  557. }
  558.  
  559. }
  560.  
  561. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement