Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.IOException;
- import java.text.DecimalFormat;
- import java.util.Scanner;
- public class ExampleGUI extends JPanel {
- // ***Variables are created ***
- //*** GUIs are made up of JPanels. Panels are created
- //*** here and named appropriately to describe what will
- //*** be placed in each of them.
- JPanel titlePanel = new JPanel();
- JPanel questionPanel = new JPanel();
- JPanel inputNumberPanel = new JPanel();
- JPanel NextCoefficent = new JPanel();
- JPanel answerPanel = new JPanel();
- JPanel nextNumberPanel = new JPanel();
- //*** a JLabel is a text string that is given a String value
- //*** and is placed in its corresponding JPanel or JButton
- JLabel titleLabel = new JLabel();
- JLabel questionLabel = new JLabel();
- JLabel inputNumberLabel = new JLabel();
- JLabel NextCoefficentLabel = new JLabel();
- JLabel answerLabel = new JLabel();
- JLabel nextNumberLabel = new JLabel();
- //*** three JButtons are created. When pushed, each button calls
- //*** its corresponding actionPerformed() method from the class created
- //*** for each button. This method executes the method code, performing
- //*** what the button is to do.
- JButton NextCoefficentButton = new JButton();
- JButton nextNumberButton = new JButton();
- //*** a JTextField creates a location where the client can place
- //*** text
- JTextField inputTextField = new JTextField(15);
- //*** constructor
- //*** Variables are given initial values
- public ExampleGUI() {
- //*** set panel layouts
- //*** panels could be LEFT, or RIGHT justified.
- titlePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
- questionPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
- inputNumberPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
- NextCoefficent.setLayout(new FlowLayout(FlowLayout.CENTER));
- answerPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
- nextNumberPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
- //*** set Label fonts. You can use other coeffiecents besides 30,20
- //*** or 15 for the font size. There are other fonts.
- Font quizBigFont = new Font("Helvetica Bold", Font.BOLD, 30);
- Font quizMidFont = new Font("Helvetica Bold", Font.BOLD, 20);
- Font quizSmallFont = new Font("Helvetica Bold", Font.BOLD, 15);
- titleLabel.setFont(quizBigFont);
- questionLabel.setFont(quizMidFont);
- inputNumberLabel.setFont(quizMidFont);
- NextCoefficentLabel.setFont(quizSmallFont);
- answerLabel.setFont(quizBigFont);
- nextNumberLabel.setFont(quizSmallFont);
- inputTextField.setFont(quizMidFont);
- //*** labels are given string values
- titleLabel.setText("Add or Subtract Five");
- questionLabel.setText("Please enter an integer coeffiecent.");
- inputNumberLabel.setText("Number:");
- NextCoefficentButton.setText(" Next Coefficient ");
- answerLabel.setText("");
- nextNumberButton.setText(" New Number ");
- //*** the 3 buttons are connected to their classes
- NextCoefficentButton.addActionListener(new NextCoefficentButton());
- nextNumberButton.addActionListener(new nextNumberButton());
- //*** Labels, buttons and textFields are added to their
- //*** panels
- titlePanel.add(titleLabel);
- questionPanel.add(questionLabel);
- //*** inputNumberPanel has 2 items added
- inputNumberPanel.add(inputNumberLabel);
- inputNumberPanel.add(inputTextField);
- //*** submitPanel has two items added
- NextCoefficent.add(NextCoefficentButton);
- answerPanel.add(answerLabel);
- nextNumberPanel.add(nextNumberButton);
- //*** The panels are added in the order that they should appear.
- //*** Throughout the declarations and initializations variables were
- //*** kept in this order to aid in keeping them straight
- setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
- add(titlePanel);
- add(questionPanel);
- add(inputNumberPanel);
- add(NextCoefficent);
- add(answerPanel);
- add(nextNumberPanel);
- //*** The method writeToFile() is called from the constructor.
- //*** One could call a read method from the constructor.
- // writeToFile();
- }// constructor
- //*** This method writes 4 lines to a file. Eclipse puts the file in
- //*** the folder of the project but not in the src folder. Put any
- //*** file that you want read in the same place so that Eclipse can
- //*** find it.
- /*
- private void writeToFile()
- {
- String fileName = "textFile.txt";
- String line = null;
- int count;
- Scanner scan = new Scanner(System.in);
- PrintWriter textStream = TextFileIO.createTextWrite(fileName);
- System.out.println("Enter 4 lines of text:");
- for (count = 1; count <= 4; count++)
- {
- line = scan.nextLine();
- textStream.println(count + " " + line);
- }
- textStream.close( ); //*** did not require error handling
- System.out.println("Four lines were written to " + fileName);
- }
- */
- //*** display() is called from main to get things going
- public void display() { //*** A JFrame is where the components of the screen
- //*** will be put.
- JFrame theFrame = new JFrame("GUI Example");
- //*** When the frame is closed it will exit to the
- //*** previous window that called it.
- theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //*** puts the panels in the JFrame
- theFrame.setContentPane(this);
- //*** sets the dimensions in pixels
- theFrame.setPreferredSize(new Dimension(600, 380));
- theFrame.pack();
- //*** make the window visible
- theFrame.setVisible(true);
- }
- //*** method doSomething is called from an actionPerformend method
- //*** demonstrating calling methods that can do work for you.
- /*private void doSomething()
- {
- for(int x = 1; x <= 10; x++)
- System.out.println(" in doSomething method x is " + x);
- }*/
- //*** This class has one method that is called when the NextCoefficentButton
- //*** is pushed. It retrieves the string from the JTextField
- //*** inputTextField, converts it to an integer and manipulates the
- //*** coeffiecent.
- //*** NOTE: a string of integers can be formed by creating a string
- //*** with one of the coeffiecents and then concatenating the other integers
- //*** to form the string.
- class NextCoefficentButton extends Bisection implements ActionListener {
- private double coefficients[];
- // checking the root accuracy
- private static final double ACCURACY = 0.0000001;
- public void actionPerformed(ActionEvent e) {
- System.out.println(" in add5Button class");
- //*** other methods can be called
- double[] coefficient = new Double[].parseDouble(inputTextField.getText());
- DecimalFormat df = new DecimalFormat("#.######");
- Scanner input = new Scanner(System.in);
- coefficient = new double[6];
- System.out.println("Enter the coefficients of the polynomial (one per line):");
- // reading coefficients for the polynomial into the array
- for (int i = 0; i < 6; i++) {
- coefficients[i] = input.nextDouble();
- input.nextLine();
- }
- System.out.println("\n\nRoots are: ");
- double x;
- int k = 0;
- // calculating and displaying roots using bisection method
- for (double upper = -9; upper < 9; upper = upper + 0.1) {
- double a = upper;
- double b = a + 0.09;
- while ((a < b) && ((f(a) * f(b)) <= 0)) {
- if (f(a) == 0)
- x = a;
- else if (f(b) == 0)
- x = b;
- else
- x = (a + b) / 2;
- if (f(x) == 0 || (Math.abs(f(x)) < ACCURACY)) {
- // displaying obtained root
- System.out.println("\n" + df.format(x) + " ");
- System.out.println("Number of Iterations: " + k);
- System.out.println("Root obtained: " + df.format(x));
- System.out.println("Estimated error: "
- + df.format(ACCURACY - f(x)));
- break;
- } else if ((f(x) * f(b)) < 0) {
- a = x;
- } else if ((f(a) * f(x)) < 0) {
- b = x;
- }
- k++;
- }
- }
- String stringNumber = "" + coefficient;
- answerLabel.setText(stringNumber);//*** answerLabel gets a new value
- inputTextField.setText(stringNumber);
- }
- }
- //*** this method resets the values of inputTextField and answerLable
- class nextNumberButton implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- inputTextField.setText("");//*** erases the values of this JTextField
- answerLabel.setText("");//*** erases the value of this JLabel
- }
- }
- public static void main(String[] args) throws IOException
- {
- ExampleGUI gameGUI = new ExampleGUI();
- System.out.println("we can print to the console");
- gameGUI.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement