Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.68 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.io.IOException;
  6. import java.text.DecimalFormat;
  7. import java.util.Scanner;
  8.  
  9.  
  10.  
  11. public class ExampleGUI extends JPanel {
  12.     // ***Variables are created ***
  13.     //*** GUIs are made up of JPanels.  Panels are created
  14.     //*** here and named appropriately to describe what will
  15.     //*** be placed in each of them.
  16.     JPanel titlePanel = new JPanel();
  17.     JPanel questionPanel = new JPanel();
  18.     JPanel inputNumberPanel = new JPanel();
  19.     JPanel NextCoefficent = new JPanel();
  20.     JPanel answerPanel = new JPanel();
  21.     JPanel nextNumberPanel = new JPanel();
  22.     //*** a JLabel is a text string that is given a String value
  23.     //*** and is placed in its corresponding JPanel or JButton
  24.     JLabel titleLabel = new JLabel();
  25.     JLabel questionLabel = new JLabel();
  26.     JLabel inputNumberLabel = new JLabel();
  27.     JLabel NextCoefficentLabel = new JLabel();
  28.  
  29.     JLabel answerLabel = new JLabel();
  30.     JLabel nextNumberLabel = new JLabel();
  31.     //*** three JButtons are created.  When pushed, each button calls
  32.     //*** its corresponding actionPerformed() method from the class created
  33.     //*** for each button. This method executes the method code, performing
  34.     //*** what the button is to do.
  35.     JButton NextCoefficentButton = new JButton();
  36.  
  37.     JButton nextNumberButton = new JButton();
  38.     //*** a JTextField creates a location where the client can place
  39.     //*** text
  40.     JTextField inputTextField = new JTextField(15);
  41.  
  42.  
  43.     //*** constructor
  44.     //*** Variables are given initial values
  45.  
  46.     public ExampleGUI() {
  47.         //*** set panel layouts
  48.         //*** panels could be LEFT, or RIGHT justified.
  49.         titlePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
  50.         questionPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
  51.         inputNumberPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
  52.         NextCoefficent.setLayout(new FlowLayout(FlowLayout.CENTER));
  53.         answerPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
  54.         nextNumberPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
  55.  
  56.         //*** set Label fonts.  You can use other coeffiecents besides 30,20
  57.         //*** or 15 for the font size.  There are other fonts.
  58.         Font quizBigFont = new Font("Helvetica Bold", Font.BOLD, 30);
  59.         Font quizMidFont = new Font("Helvetica Bold", Font.BOLD, 20);
  60.         Font quizSmallFont = new Font("Helvetica Bold", Font.BOLD, 15);
  61.         titleLabel.setFont(quizBigFont);
  62.         questionLabel.setFont(quizMidFont);
  63.         inputNumberLabel.setFont(quizMidFont);
  64.         NextCoefficentLabel.setFont(quizSmallFont);
  65.  
  66.         answerLabel.setFont(quizBigFont);
  67.         nextNumberLabel.setFont(quizSmallFont);
  68.         inputTextField.setFont(quizMidFont);
  69.         //*** labels are given string values
  70.         titleLabel.setText("Add or Subtract Five");
  71.         questionLabel.setText("Please enter an integer coeffiecent.");
  72.         inputNumberLabel.setText("Number:");
  73.         NextCoefficentButton.setText("   Next Coefficient   ");
  74.  
  75.         answerLabel.setText("");
  76.         nextNumberButton.setText("   New Number   ");
  77.         //*** the 3 buttons are connected to their classes
  78.         NextCoefficentButton.addActionListener(new NextCoefficentButton());
  79.         nextNumberButton.addActionListener(new nextNumberButton());
  80.         //*** Labels, buttons and textFields are added to their
  81.         //*** panels
  82.         titlePanel.add(titleLabel);
  83.         questionPanel.add(questionLabel);
  84.         //*** inputNumberPanel has 2 items added
  85.         inputNumberPanel.add(inputNumberLabel);
  86.         inputNumberPanel.add(inputTextField);
  87.         //*** submitPanel has two items added
  88.         NextCoefficent.add(NextCoefficentButton);
  89.  
  90.         answerPanel.add(answerLabel);
  91.         nextNumberPanel.add(nextNumberButton);
  92.         //*** The panels are added in the order that they should appear.
  93.         //*** Throughout the declarations and initializations variables were
  94.         //*** kept in this order to aid in keeping them straight
  95.         setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
  96.         add(titlePanel);
  97.         add(questionPanel);
  98.         add(inputNumberPanel);
  99.         add(NextCoefficent);
  100.         add(answerPanel);
  101.         add(nextNumberPanel);
  102.  
  103.         //*** The method writeToFile() is called from the constructor.
  104.         //*** One could call a read method from the constructor.
  105.  
  106.         //  writeToFile();
  107.     }// constructor
  108.  
  109.     //*** This method writes 4 lines to a file.  Eclipse puts the file in
  110.     //*** the folder of the project but not in the src folder.  Put any
  111.     //*** file that you want read in the same place so that Eclipse can
  112.     //*** find it.
  113.     /*
  114.     private void writeToFile()
  115.     {
  116.         String fileName = "textFile.txt";
  117.         String line = null;
  118.         int count;
  119.         Scanner scan = new  Scanner(System.in);
  120.         PrintWriter textStream = TextFileIO.createTextWrite(fileName);
  121.         System.out.println("Enter 4 lines of text:");
  122.         for (count = 1; count <= 4; count++)
  123.         {
  124.             line = scan.nextLine();
  125.             textStream.println(count + " " + line);
  126.         }
  127.         textStream.close( ); //*** did not require error handling
  128.         System.out.println("Four lines were written to " + fileName);
  129.     }
  130.     */
  131.     //*** display() is called from main to get things going
  132.  
  133.     public void display() {    //*** A JFrame is where the components of the screen
  134.         //*** will be put.
  135.         JFrame theFrame = new JFrame("GUI Example");
  136.         //*** When the frame is closed it will exit to the
  137.         //*** previous window that called it.
  138.         theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  139.         //*** puts the panels in the JFrame
  140.         theFrame.setContentPane(this);
  141.         //*** sets the dimensions in pixels
  142.         theFrame.setPreferredSize(new Dimension(600, 380));
  143.         theFrame.pack();
  144.         //*** make the window visible
  145.         theFrame.setVisible(true);
  146.     }
  147.  
  148.     //*** method doSomething is called from an actionPerformend method
  149.     //*** demonstrating calling methods that can do work for you.
  150.  
  151.     /*private void doSomething()
  152.     {
  153.         for(int x = 1; x <= 10; x++)
  154.             System.out.println(" in doSomething method x is " + x);
  155.     }*/
  156.  
  157.  
  158.     //*** This class has one method that is called when the NextCoefficentButton
  159.     //*** is pushed.  It retrieves the string from the JTextField
  160.     //*** inputTextField, converts it to an integer and manipulates the
  161.     //*** coeffiecent.
  162.     //*** NOTE: a string of integers can be formed by creating a string
  163.     //*** with one of the coeffiecents and then concatenating the other integers
  164.     //*** to form the string.
  165.  
  166.     class NextCoefficentButton extends Bisection implements ActionListener {
  167.         private double coefficients[];
  168.         // checking the root accuracy
  169.         private static final double ACCURACY = 0.0000001;
  170.  
  171.         public void actionPerformed(ActionEvent e) {
  172.             System.out.println(" in add5Button class");
  173.             //*** other methods can be called
  174.             double[] coefficient = new Double[].parseDouble(inputTextField.getText());
  175.             DecimalFormat df = new DecimalFormat("#.######");
  176.             Scanner input = new Scanner(System.in);
  177.             coefficient = new double[6];
  178.             System.out.println("Enter the coefficients of the polynomial (one per line):");
  179.             // reading coefficients for the polynomial into the array
  180.  
  181.             for (int i = 0; i < 6; i++) {
  182.                 coefficients[i] = input.nextDouble();
  183.                 input.nextLine();
  184.             }
  185.  
  186.             System.out.println("\n\nRoots are: ");
  187.             double x;
  188.             int k = 0;
  189.             // calculating and displaying roots using bisection method
  190.             for (double upper = -9; upper < 9; upper = upper + 0.1) {
  191.  
  192.                 double a = upper;
  193.                 double b = a + 0.09;
  194.  
  195.                 while ((a < b) && ((f(a) * f(b)) <= 0)) {
  196.  
  197.                     if (f(a) == 0)
  198.                         x = a;
  199.                     else if (f(b) == 0)
  200.                         x = b;
  201.                     else
  202.                         x = (a + b) / 2;
  203.  
  204.                     if (f(x) == 0 || (Math.abs(f(x)) < ACCURACY)) {
  205.  
  206.                         // displaying obtained root
  207.                         System.out.println("\n" + df.format(x) + " ");
  208.                         System.out.println("Number of Iterations: " + k);
  209.                         System.out.println("Root obtained: " + df.format(x));
  210.                         System.out.println("Estimated error: "
  211.                                 + df.format(ACCURACY - f(x)));
  212.                         break;
  213.                     } else if ((f(x) * f(b)) < 0) {
  214.                         a = x;
  215.                     } else if ((f(a) * f(x)) < 0) {
  216.                         b = x;
  217.                     }
  218.                     k++;
  219.                 }
  220.             }
  221.  
  222.             String stringNumber = "" + coefficient;
  223.             answerLabel.setText(stringNumber);//*** answerLabel gets a new value
  224.             inputTextField.setText(stringNumber);
  225.  
  226.  
  227.         }
  228.     }
  229.  
  230.  
  231.     //*** this method resets the values of inputTextField and answerLable
  232.  
  233.     class nextNumberButton implements ActionListener {
  234.         public void actionPerformed(ActionEvent e) {
  235.             inputTextField.setText("");//*** erases the values of this JTextField
  236.             answerLabel.setText("");//*** erases the value of this JLabel
  237.         }
  238.     }
  239.  
  240.     public static void main(String[] args) throws IOException
  241.     {
  242.         ExampleGUI gameGUI = new ExampleGUI();
  243.         System.out.println("we can print to the console");
  244.         gameGUI.display();
  245.  
  246.     }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement