Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JRadioButton;
- import javax.swing.JTextField;
- import javax.swing.JButton;
- import java.awt.event.InputMethodListener;
- import java.awt.event.InputMethodEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.beans.PropertyChangeListener;
- import java.beans.PropertyChangeEvent;
- import java.awt.Font;
- import javax.swing.ButtonGroup;
- public class stage2 {
- private JFrame frame;
- private JTextField txtStartingpop;
- private JTextField txtSpecies;
- private JTextField txtGenerations;
- private JTextField txtFixedrate;
- private JTextField txtVgr1;
- private JTextField txtVgr2;
- private JTextField txtVgr3;
- private JTextField txtVgr4;
- private JTextField txtVgr5;
- private JTextField txtVgr6;
- private JTextField txtVgr7;
- private JTextField txtVgr8;
- private JTextField txtVgr9;
- private JTextField txtVgr10;
- private JTextField txtHabitatname;
- private final ButtonGroup buttonGroup = new ButtonGroup();
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- stage2 window = new stage2();
- window.frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the application.
- */
- public stage2() {
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- */
- private void initialize() {
- frame = new JFrame();
- frame.setBounds(100, 100, 500, 300);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().setLayout(null);
- JLabel lblSpecies = new JLabel("Species:");
- lblSpecies.setBounds(35, 30, 80, 14);
- frame.getContentPane().add(lblSpecies);
- JLabel lblStartingPopulation = new JLabel("Starting Population:");
- lblStartingPopulation.setBounds(35, 55, 125, 14);
- frame.getContentPane().add(lblStartingPopulation);
- JLabel lblGenerations = new JLabel("Generations:");
- lblGenerations.setBounds(35, 80, 109, 14);
- frame.getContentPane().add(lblGenerations);
- JLabel lblGrowthRate = new JLabel("Growth Rate(%):");
- lblGrowthRate.setBounds(35, 105, 109, 14);
- frame.getContentPane().add(lblGrowthRate);
- JRadioButton rdbtnFixed = new JRadioButton("Fixed");
- buttonGroup.add(rdbtnFixed);
- rdbtnFixed.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- }
- });
- rdbtnFixed.setBounds(35, 126, 56, 23);
- frame.getContentPane().add(rdbtnFixed);
- JRadioButton rdbtnVariable = new JRadioButton("Variable");
- buttonGroup.add(rdbtnVariable);
- rdbtnVariable.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- }
- });
- rdbtnVariable.setBounds(35, 152, 109, 23);
- frame.getContentPane().add(rdbtnVariable);
- JLabel lblHabitat = new JLabel("Habitat:");
- lblHabitat.setBounds(279, 30, 46, 14);
- frame.getContentPane().add(lblHabitat);
- JLabel lblFinalPopulation = new JLabel("Final Population:");
- lblFinalPopulation.setBounds(279, 55, 109, 14);
- frame.getContentPane().add(lblFinalPopulation);
- JLabel lblFinalpop = new JLabel("");
- lblFinalpop.addPropertyChangeListener(new PropertyChangeListener() {
- public void propertyChange(PropertyChangeEvent arg0) {
- }
- });
- lblFinalpop.setBounds(388, 55, 46, 14);
- frame.getContentPane().add(lblFinalpop);
- JLabel lblDed = new JLabel("");
- lblDed.setFont(new Font("Tahoma", Font.PLAIN, 9));
- lblDed.addPropertyChangeListener(new PropertyChangeListener() {
- public void propertyChange(PropertyChangeEvent arg0) {
- }
- });
- lblDed.setBounds(314, 80, 160, 14);
- frame.getContentPane().add(lblDed);
- txtStartingpop = new JTextField();
- txtStartingpop.addInputMethodListener(new InputMethodListener() {
- public void caretPositionChanged(InputMethodEvent arg0) {
- }
- public void inputMethodTextChanged(InputMethodEvent arg0) {
- }
- });
- txtStartingpop.setBounds(164, 52, 86, 20);
- frame.getContentPane().add(txtStartingpop);
- txtStartingpop.setColumns(10);
- txtSpecies = new JTextField();
- txtSpecies.setBounds(164, 27, 86, 20);
- frame.getContentPane().add(txtSpecies);
- txtSpecies.setColumns(10);
- txtGenerations = new JTextField();
- txtGenerations.addInputMethodListener(new InputMethodListener() {
- public void caretPositionChanged(InputMethodEvent event) {
- }
- public void inputMethodTextChanged(InputMethodEvent event) {
- }
- });
- txtGenerations.setBounds(164, 77, 86, 20);
- frame.getContentPane().add(txtGenerations);
- txtGenerations.setColumns(10);
- txtFixedrate = new JTextField();
- txtFixedrate.addInputMethodListener(new InputMethodListener() {
- public void caretPositionChanged(InputMethodEvent event) {
- }
- public void inputMethodTextChanged(InputMethodEvent event) {
- }
- });
- txtFixedrate.setBounds(164, 127, 86, 20);
- frame.getContentPane().add(txtFixedrate);
- txtFixedrate.setColumns(10);
- txtVgr1 = new JTextField();
- txtVgr1.setBounds(35, 182, 35, 20);
- frame.getContentPane().add(txtVgr1);
- txtVgr1.setColumns(10);
- txtVgr2 = new JTextField();
- txtVgr2.setColumns(10);
- txtVgr2.setBounds(80, 182, 35, 20);
- frame.getContentPane().add(txtVgr2);
- txtVgr3 = new JTextField();
- txtVgr3.setColumns(10);
- txtVgr3.setBounds(125, 182, 35, 20);
- frame.getContentPane().add(txtVgr3);
- txtVgr4 = new JTextField();
- txtVgr4.setColumns(10);
- txtVgr4.setBounds(170, 182, 35, 20);
- frame.getContentPane().add(txtVgr4);
- txtVgr5 = new JTextField();
- txtVgr5.setColumns(10);
- txtVgr5.setBounds(215, 182, 35, 20);
- frame.getContentPane().add(txtVgr5);
- txtVgr6 = new JTextField();
- txtVgr6.setColumns(10);
- txtVgr6.setBounds(35, 213, 35, 20);
- frame.getContentPane().add(txtVgr6);
- txtVgr7 = new JTextField();
- txtVgr7.setColumns(10);
- txtVgr7.setBounds(80, 213, 35, 20);
- frame.getContentPane().add(txtVgr7);
- txtVgr8 = new JTextField();
- txtVgr8.setColumns(10);
- txtVgr8.setBounds(125, 213, 35, 20);
- frame.getContentPane().add(txtVgr8);
- txtVgr9 = new JTextField();
- txtVgr9.setColumns(10);
- txtVgr9.setBounds(170, 213, 35, 20);
- frame.getContentPane().add(txtVgr9);
- txtVgr10 = new JTextField();
- txtVgr10.setColumns(10);
- txtVgr10.setBounds(215, 213, 35, 20);
- frame.getContentPane().add(txtVgr10);
- JButton btnCalculate = new JButton("Calculate");
- btnCalculate.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- //starting with all the variables
- int iStart = Integer.parseInt(txtStartingpop.getText());
- int iGenerations = Integer.parseInt(txtGenerations.getText());
- //int iFixedRate = Integer.parseInt(txtFixedrate.getText());
- //int iVariableRate[];
- //iVariableRate = new int[10];
- int iFinal = 0;
- int iMaxPopulation = 5000;
- //if the fixed button is selected
- if(rdbtnFixed.isSelected()){
- int iFixedRate = Integer.parseInt(txtFixedrate.getText());
- for(int i = 0; i < iGenerations; i++);
- {
- iFinal = (int) (iStart + ((iStart * iFixedRate)/100.00));
- iStart = iFinal;
- }
- }
- else if (rdbtnVariable.isSelected())
- {
- int iVariableRate[];
- iVariableRate = new int[10];
- for(int i = 0; i < 10; i++)
- {
- iVariableRate[0] = Integer.parseInt(txtVgr1.getText());
- iVariableRate[1] = Integer.parseInt(txtVgr2.getText());
- iVariableRate[2] = Integer.parseInt(txtVgr3.getText());
- iVariableRate[3] = Integer.parseInt(txtVgr4.getText());
- iVariableRate[4] = Integer.parseInt(txtVgr5.getText());
- iVariableRate[5] = Integer.parseInt(txtVgr6.getText());
- iVariableRate[6] = Integer.parseInt(txtVgr7.getText());
- iVariableRate[7] = Integer.parseInt(txtVgr8.getText());
- iVariableRate[8] = Integer.parseInt(txtVgr9.getText());
- iVariableRate[9] = Integer.parseInt(txtVgr10.getText());
- }
- for(int i = 0; i < 10; i++)
- {
- iFinal = (int) (iStart + ((iStart * iVariableRate[i])/100.00));
- iStart = iFinal;
- }
- }
- //end else if statement
- String iFinalString = new String("");
- iFinalString = Integer.toString(iFinal);
- lblFinalpop.setText(iFinalString);
- if (iFinal > iMaxPopulation) {
- lblDed.setText("Species Died to Overcrowding");
- }
- }
- });
- btnCalculate.setBounds(321, 181, 89, 23);
- frame.getContentPane().add(btnCalculate);
- JButton btnExit = new JButton("Exit");
- btnExit.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- //exits the program
- System.exit(0);
- };
- });
- btnExit.setBounds(321, 212, 89, 23);
- frame.getContentPane().add(btnExit);
- JLabel lblPopulationGrowthCalculator = new JLabel("Population Growth Calculator");
- lblPopulationGrowthCalculator.setFont(new Font("Tahoma", Font.BOLD, 11));
- lblPopulationGrowthCalculator.setBounds(140, 2, 174, 17);
- frame.getContentPane().add(lblPopulationGrowthCalculator);
- txtHabitatname = new JTextField();
- txtHabitatname.setBounds(388, 27, 86, 20);
- frame.getContentPane().add(txtHabitatname);
- txtHabitatname.setColumns(10);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement