Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.*;
- public class CKDJavaGame extends JFrame{
- public CKDJavaGame()
- {
- setSize(1350, 800);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLocation(0,0);
- MainPan pan = new MainPan();
- add(pan);
- setResizable(true);
- setVisible(true);
- }
- public static void main (String []args)
- {
- CKDJavaGame ckd = new CKDJavaGame();
- }
- }
- class MainPan extends JPanel
- {
- private int xcor, ycor;
- private CardLayout layout; // cardlayout instance
- private MainPan mainp;
- private boolean topics, topicsB;
- public MainPan()
- {
- layout = new CardLayout();
- this.setLayout(layout); // setting this class to card layout
- topics=false;
- topicsB=false;
- ycor = 800;
- xcor = 1350;
- GamePagePanel page = new GamePagePanel(this);
- add(page, "CKD Detector");
- /*
- InstructionPanel instruction = new InstructionPanel(this); // for instructions, has its own button
- add(instruction, "Instructions");
- */
- layout.show(this, "CKD Detector");
- PlayPanel play = new PlayPanel(this); // learn about the parts and functions of the respiratory system
- add(play, "Test to see if you have CKD!");
- /*
- BasicFunctionPanel bfunc = new BasicFunctionPanel(this); // game mode basic
- add(bfunc, "The basic level of the functions");
- AdvancedFunctionPanel afunc = new AdvancedFunctionPanel(this); // game mode advanced
- add(afunc, "The advanced level of the functions");
- LearnBreathePanel breathe = new LearnBreathePanel(this); // learning about a new topic, breathing and diseases
- add(breathe, "Learning about the process of breathing");
- BasicBreathePanel bb = new BasicBreathePanel(this); // game mode for breathing and diseases
- add(bb, "The basic level of breathing processes");
- AdvancedBreathePanel ab = new AdvancedBreathePanel(this); // advanced game mode for breathing and diseases
- add(ab, "The advanced level of breathing processes");
- */
- }
- public class GamePagePanel extends JPanel implements ActionListener
- {
- ButtonGroup group;
- Font font;
- private Image background;
- public GamePagePanel(MainPan mpI) // initializes radio buttons and font, calls other methods
- {
- mainp = mpI; // main panels instance being initialized
- setBackground(Color.RED);
- setLayout(null);
- font = new Font("SansSerif", Font.BOLD, 20);
- setFont(font);
- buttonMethod();
- getMyImages();
- }
- public void buttonMethod() // intializes all the buttons, sets the bounds, adds ActionListener
- {
- JButton btn1 = new JButton("Instructions");
- JButton btn2 = new JButton("Take the Test");
- //JButton btn3 = new JButton("<html><p>Respiratory</p><p>Diseases</p>");
- btn1.setFont(font);
- btn2.setFont(font);
- //btn3.setFont(font);
- add(btn1);
- add(btn2);
- //add(btn3);
- btn1.addActionListener(this);
- btn2.addActionListener(this);
- //btn3.addActionListener(this);
- btn1.setBounds(160, 390, 150, 50); // left, top, width, height
- btn2.setBounds(50, 600, 250, 80); // left, top, width, height
- //btn3.setBounds(350, 600, 200, 80); // left, top, width, height
- btn1.setForeground(Color.WHITE);
- btn1.setBackground(Color.BLUE);
- btn2.setForeground(Color.WHITE);
- btn2.setBackground(Color.BLUE);
- //btn3.setForeground(Color.WHITE);
- //btn3.setBackground(Color.BLUE);
- }
- public void getMyImages() // gets images like the respiratory image that I have on my front page
- {
- try
- {
- background = ImageIO.read(new File("src/kidney.png"));
- }
- catch(IOException e)
- {
- System.err.println(" can't be found");
- e.printStackTrace();
- }
- }
- public void paintComponent(Graphics g) // paints String to match JButtons and JRadioButtons, also makes the title
- {
- super.paintComponent(g);
- Font font2 = new Font("Courier", Font.BOLD, 60);
- g.setFont(font2);
- g.setColor(Color.WHITE);
- g.drawImage(background, 0, 0, 1350, 800, this);
- g.setColor(Color.BLUE);
- g.drawString("CKD Detector ", 60, 80);
- g.setFont(font);
- g.setColor(Color.BLACK);
- }
- public void actionPerformed(ActionEvent e) // checks if button pressed is instructions, if so displays the instructions
- {
- // Concept List: .equals() is a String method
- if(e.getActionCommand().equals("Instructions"))
- {
- System.out.println("hello");
- layout.show(mainp, "Instructions");
- }
- // checks to see if other buttons are pressed and if the radio button chosen are learn or play mode, this leads to different panels
- // with checks using CardLayout
- if(e.getActionCommand().equals("Take the Test"))
- {
- layout.show(mainp, "Test to see if you have CKD!");
- }
- } // end of method
- } // end of class
- class PlayPanel extends JPanel implements ActionListener
- {
- JButton checkResult;
- private int i1, counter;
- public int numberArray[];
- private boolean rep;
- private int threatScore, symptomScore;
- JRadioButton radioNeverButtons[] = new JRadioButton[15];
- JRadioButton radioRareButtons[] = new JRadioButton[15];
- JRadioButton radioOftenButtons[] = new JRadioButton[15];
- JRadioButton radioAlwaysButtons[] = new JRadioButton[15];
- private String questionArray[];
- JButton return1, start;
- Timer move;
- public PlayPanel(MainPan mpI) // intializes JLabel, return button to go back to home page
- // adds Mouse and KeyListeners
- {
- mainp = mpI;
- setLayout(null);
- setBackground(Color.GREEN);
- numberArray = new int [15];
- rep = false;
- threatScore=0;
- symptomScore=0;
- 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?",
- "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?" ,
- "Do you have a swollen or puffy face?", "Does food taste like metal?", "Do you have ammonia breath?", "Do you experience nausea and vomiting?",
- "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?",
- "Do you experience pressure when you urinate?"};
- i1 = 0;
- counter=0;
- radioButtonMethod();
- return1 = new JButton("Return to main page");
- add(return1);
- return1.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- return1.setVisible(false);
- layout.show(mainp, "CKD Detector");
- }
- });
- return1.setBounds(1050, 20, 200, 50);
- start = new JButton("Start");
- add(start);
- start.setBounds(1000, 600, 150, 60);
- start.setBackground(Color.RED);
- start.setForeground(Color.YELLOW);
- start.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e)
- {
- start.setVisible(false);
- move.start();
- }
- });
- //getTheImages();
- //timer = new Timer(2000, this);
- move= new Timer(10000, this);
- // make an array of moveyButton and movexButton for each button and replace the values when you
- // are doing the coordinating
- }
- public void radioButtonMethod()
- {
- /*
- JRadioButton radioNeverButtons[] = new JRadioButton[15];
- JRadioButton radioRareButtons[] = new JRadioButton[15];
- JRadioButton radioOftenButtons[] = new JRadioButton[15];
- JRadioButton radioAlwaysButtons[] = new JRadioButton[15];
- */
- for(int i=0; i<15; i++)
- {
- radioNeverButtons[i] = new JRadioButton("Never");
- radioRareButtons[i] = new JRadioButton("Rarely");
- radioOftenButtons[i] = new JRadioButton("Often");
- radioAlwaysButtons[i] = new JRadioButton("Always");
- //radioNeverButtons[i].setSelected(true);
- if (i==0)
- {
- ButtonGroup group1 = new ButtonGroup();
- group1.add(radioNeverButtons[i]); group1.add(radioRareButtons[i]); group1.add(radioOftenButtons[i]); group1.add(radioAlwaysButtons[i]);
- }
- if (i==1)
- {
- ButtonGroup group2 = new ButtonGroup();
- group2.add(radioNeverButtons[i]); group2.add(radioRareButtons[i]); group2.add(radioOftenButtons[i]); group2.add(radioAlwaysButtons[i]);
- }
- if (i==2)
- {
- ButtonGroup group3 = new ButtonGroup();
- group3.add(radioNeverButtons[i]); group3.add(radioRareButtons[i]); group3.add(radioOftenButtons[i]); group3.add(radioAlwaysButtons[i]);
- }
- if (i==3)
- {
- ButtonGroup group4 = new ButtonGroup();
- group4.add(radioNeverButtons[i]); group4.add(radioRareButtons[i]); group4.add(radioOftenButtons[i]); group4.add(radioAlwaysButtons[i]);
- }
- if (i==4)
- {
- ButtonGroup group5 = new ButtonGroup();
- group5.add(radioNeverButtons[i]); group5.add(radioRareButtons[i]); group5.add(radioOftenButtons[i]); group5.add(radioAlwaysButtons[i]);
- }
- if (i==5)
- {
- ButtonGroup group6 = new ButtonGroup();
- group6.add(radioNeverButtons[i]); group6.add(radioRareButtons[i]); group6.add(radioOftenButtons[i]); group6.add(radioAlwaysButtons[i]);
- }
- if (i==6)
- {
- ButtonGroup group7 = new ButtonGroup();
- group7.add(radioNeverButtons[i]); group7.add(radioRareButtons[i]); group7.add(radioOftenButtons[i]); group7.add(radioAlwaysButtons[i]);
- }
- if (i==7)
- {
- ButtonGroup group8 = new ButtonGroup();
- group8.add(radioNeverButtons[i]); group8.add(radioRareButtons[i]); group8.add(radioOftenButtons[i]); group8.add(radioAlwaysButtons[i]);
- }
- if (i==8)
- {
- ButtonGroup group9 = new ButtonGroup();
- group9.add(radioNeverButtons[i]); group9.add(radioRareButtons[i]); group9.add(radioOftenButtons[i]); group9.add(radioAlwaysButtons[i]);
- }
- if (i==9)
- {
- ButtonGroup group10 = new ButtonGroup();
- group10.add(radioNeverButtons[i]); group10.add(radioRareButtons[i]); group10.add(radioOftenButtons[i]); group10.add(radioAlwaysButtons[i]);
- }
- if (i==10)
- {
- ButtonGroup group11 = new ButtonGroup();
- group11.add(radioNeverButtons[i]); group11.add(radioRareButtons[i]); group11.add(radioOftenButtons[i]); group11.add(radioAlwaysButtons[i]);
- }
- if (i==11)
- {
- ButtonGroup group12 = new ButtonGroup();
- group12.add(radioNeverButtons[i]); group12.add(radioRareButtons[i]); group12.add(radioOftenButtons[i]); group12.add(radioAlwaysButtons[i]);
- }
- if (i==12)
- {
- ButtonGroup group13 = new ButtonGroup();
- group13.add(radioNeverButtons[i]); group13.add(radioRareButtons[i]); group13.add(radioOftenButtons[i]); group13.add(radioAlwaysButtons[i]);
- }
- if (i==13)
- {
- ButtonGroup group14 = new ButtonGroup();
- group14.add(radioNeverButtons[i]); group14.add(radioRareButtons[i]); group14.add(radioOftenButtons[i]); group14.add(radioAlwaysButtons[i]);
- }
- if (i==14)
- {
- ButtonGroup group15 = new ButtonGroup();
- group15.add(radioNeverButtons[i]); group15.add(radioRareButtons[i]); group15.add(radioOftenButtons[i]); group15.add(radioAlwaysButtons[i]);
- }
- radioNeverButtons[i].addActionListener(new ActionHandler());
- radioRareButtons[i].addActionListener(new ActionHandler());
- radioOftenButtons[i].addActionListener(new ActionHandler());
- radioAlwaysButtons[i].addActionListener(new ActionHandler());
- radioNeverButtons[i].setBackground(Color.CYAN); radioRareButtons[i].setBackground(Color.CYAN); radioOftenButtons[i].setBackground(Color.CYAN); radioAlwaysButtons[i].setBackground(Color.CYAN);
- }
- }
- public void paintComponent(Graphics g) // draws String for the Instructions, in proper format
- {
- super.paintComponent(g);
- Font f4 = new Font("Serif", Font.BOLD + Font.ITALIC, 35);
- g.setFont(f4);
- g.setColor(Color.RED);
- g.fillRect(400, 20, 500, 50);
- g.setColor(Color.BLACK);
- g.drawString("Chronic Kidney Disease Detector ", 400, 50);
- f4 = new Font("Serif", Font.BOLD, 25);
- g.setFont(f4);
- int x=100;
- g.setColor(Color.WHITE);
- if(i1<15)
- {
- g.drawString( " "+ questionArray[i1], 50, 150);
- add(radioNeverButtons[i1]); add(radioRareButtons[i1]); add(radioOftenButtons[i1]); add(radioAlwaysButtons[i1]);
- radioNeverButtons[i1].setBounds(160, 200, 100, 30); radioRareButtons[i1].setBounds(360, 200, 100, 30);
- radioOftenButtons[i1].setBounds(560, 200, 100, 30); radioAlwaysButtons[i1].setBounds(760, 200, 100, 30);
- g.drawString( " "+ questionArray[i1+1], 50, 320);
- add(radioNeverButtons[i1+1]); add(radioRareButtons[i1+1]); add(radioOftenButtons[i1+1]); add(radioAlwaysButtons[i1+1]);
- radioNeverButtons[i1+1].setBounds(160, 400, 100, 30); radioRareButtons[i1+1].setBounds(360, 400, 100, 30);
- radioOftenButtons[i1+1].setBounds(560, 400, 100, 30); radioAlwaysButtons[i1+1].setBounds(760, 400, 100, 30);
- g.drawString( " "+ questionArray[i1+2], 50, 520);
- add(radioNeverButtons[i1+2]); add(radioRareButtons[i1+2]); add(radioOftenButtons[i1+2]); add(radioAlwaysButtons[i1+2]);
- radioNeverButtons[i1+2].setBounds(160, 600, 100, 30); radioRareButtons[i1+2].setBounds(360, 600, 100, 30);
- radioOftenButtons[i1+2].setBounds(560, 600, 100, 30); radioAlwaysButtons[i1+2].setBounds(760, 600, 100, 30);
- }
- if(i1==15 && rep)
- {
- for(int i = 0; i<15; i++)
- {
- remove(radioNeverButtons[i]); remove(radioRareButtons[i]); remove(radioOftenButtons[i]); remove(radioAlwaysButtons[i]);
- }
- setBackground(Color.WHITE);
- symScore();
- g.setColor(Color.BLUE);
- g.drawString("Your symptom score is " + symptomScore + " and the threat score is " + threatScore, 200, 100);
- if(symptomScore-threatScore>=15)
- 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);
- else if(symptomScore-threatScore <15 && symptomScore-threatScore >= 5 )
- 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);
- else if(symptomScore-threatScore <5 && symptomScore-threatScore >= -5 )
- 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 );
- else if (symptomScore-threatScore <-5 && symptomScore-threatScore >= -15 )
- g.drawString(" You are probably not at risk for developing the disease, but watch your health habits. ", 200, 300);
- else
- g.drawString("You are not at risk for developing this disease ", 200, 300);
- }
- /*
- 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);
- g.drawString(" If you go back to the learn mode, you have to play the game again, from the basic level.",50, x+630);
- */
- } // end of paint component
- public void symScore()
- {
- threatScore = 20;
- for(int i=0; i<15; i++)
- {
- symptomScore = numberArray[i] + symptomScore;
- System.out.println(numberArray[i]+ " ");
- }
- }
- public void actionPerformed(ActionEvent e)
- {
- i1 = i1+ 3;
- if(i1==15)
- {
- move.stop();
- checkResult = new JButton("Check to see your result");
- add(checkResult);
- checkResult.setBounds(1050, 600, 200, 50);
- checkResult.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- checkResult.setVisible(false);
- return1.setVisible(false);
- rep = true;
- repaint();
- }
- });
- }
- repaint();
- }
- class ActionHandler extends JPanel implements ActionListener
- {
- public ActionHandler() {}
- public void actionPerformed(ActionEvent e)
- {
- //System.out.println(i1);
- if(radioNeverButtons[i1].isSelected()==true)
- {
- numberArray[i1] = 0;
- }
- else if(radioRareButtons[i1].isSelected()==true)
- {
- numberArray[i1] = 1;
- //System.out.println("rare selected");
- }
- else if(radioOftenButtons[i1].isSelected()==true)
- {
- numberArray[i1] = 2;
- //System.out.println("often selected");
- }
- else if(radioAlwaysButtons[i1].isSelected()==true)
- {
- numberArray[i1] = 3;
- //System.out.println("always selected");
- }
- if(radioNeverButtons[i1+1].isSelected()==true)
- {
- numberArray[i1+1] = 0;
- //System.out.println("never selected");
- }
- else if(radioRareButtons[i1+1].isSelected()==true)
- {
- numberArray[i1+1] = 1;
- //System.out.println("rare selected");
- }
- else if(radioOftenButtons[i1+1].isSelected()==true)
- {
- numberArray[i1+1] = 2;
- //System.out.println("often selected");
- }
- else if(radioAlwaysButtons[i1+1].isSelected()==true)
- {
- numberArray[i1+1] = 3;
- //System.out.println("always selected");
- }
- if(radioNeverButtons[i1+2].isSelected()==true)
- {
- numberArray[i1+2] = 0;
- //System.out.println("never selected");
- }
- else if(radioRareButtons[i1+2].isSelected()==true)
- {
- numberArray[i1+2] = 1;
- //System.out.println("rare selected");
- }
- else if(radioOftenButtons[i1+2].isSelected()==true)
- {
- numberArray[i1+2] = 2;
- //System.out.println("often selected");
- }
- else if(radioAlwaysButtons[i1+2].isSelected()==true)
- {
- numberArray[i1+2] = 3;
- //System.out.println("always selected");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement