Take a look and run the code in the lab attendance box for today. 

Also from the lecture box:

http://silo.cs.indiana.edu:8346/c212/sum2019/ch10/

6. Exceptions:

import java.util.Scanner; 

public class Six {
  public static void main(String[] args) {
    Scanner a = new Scanner(System.in);  
    while (true) { 
      System.out.print("Enter number: "); 
      String input = a.nextLine(); 
      try {
        double n = Double.parseDouble(input);  
        System.out.println("The square root of " + n + " is " + Math.sqrt(n)); 
        break;
      } catch (Exception e) {
        System.out.println( e );  
      }
    }
  }
}

3. Timer events:

https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html

https://docs.oracle.com/javase/8/docs/api/javax/swing/Timer.html

https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

https://docs.oracle.com/javase/8/docs/api/java/awt/event/ActionListener.html

import javax.swing.Timer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Three implements ActionListener {
  int count = -10; 
  public void actionPerformed(ActionEvent e) {
    System.out.println("Tick... " + ++this.count); 
  }
  public static void main(String[] args) {
    Timer t = new Timer(1000, new Three());      
    t.start(); 
  }
}

1. MouseMotionListener 

2. MouseListener 

3. Timer

4. Comparable

5. Comparator 

6. Exceptions

8. Graphics

--

7. Key Events

9. GUIs

Ben Nariman  Chun Murun Aarani Daohui Mary 
 Dustin Yuying  Sai  Qi  Zack
   Sunghyun Grant Chase Jiongran Michael JeVante 
    Stephen K Emma Peter W ... Young Hwan 

--

Next week: Generics, Collections and Algorithms Analysis (Sorting/Searching). 

Run this:


import java.awt.Color; 
import java.awt.Graphics; 

public class Circle {
  int x, y; 
  int radius;
  Color c;
  public String toString() {
    return this.c + " circle (" + this.x + ", " + this.y + ") with radius + " + this.radius;  
  } // I needed this for debugging 
  public Circle(int x, int y, int r, Color c) {
    this.x = x; 
    this.y = y; 
    this.radius = r;
    this.c = c; 
  }
  public void draw(Graphics g) {
    g.setColor(this.c); 
    g.fillOval(this.x - this.radius, 
               this.y - this.radius, 
               this.radius * 2, 
               this.radius * 2);  
    g.setColor(Color.BLACK);     
    g.drawOval(this.x - this.radius, 
               this.y - this.radius, 
               this.radius * 2, 
               this.radius * 2);  
    
  }
}

// this looks much like the first stage of your project

import javax.swing.JFrame; 
import javax.swing.JComponent; 
import java.awt.Graphics;
import java.util.ArrayList; 
import java.awt.Color;

public class Eight extends JComponent {
  ArrayList<Circle> circles = new ArrayList<Circle>(); 
  public Eight() {
    for (int i = 0; i < 30; i++) {
      this.circles.add(new Circle((int)(Math.random()*400), 
                                  (int)(Math.random()*400), 
                                  (int)(Math.random() * 10 + 30),
                                  new Color((float)Math.random(), 
                                            (float)Math.random(), 
                                            (float)Math.random())));  
    }
  }
  public void paintComponent(Graphics g) {
    g.drawOval(40, 40, 100, 100); // likewise, not needed, just a sanity check
    for (Circle c : this.circles)
      c.draw(g); 
    System.out.println("I am being called." + this.circles); // for illustration only 
  }
  public static void main(String[] args) {
    JFrame a = new JFrame(); 
    a.add( new Eight() ); 
    a.setSize(400, 400); 
    a.setVisible(true); 
  }
}

We then developed/ran the following in Eclipse:

package tuesday;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.control.TextField;

public class SalaryGuiFx extends Application {
   @Override
   public void start(Stage applicationStage) {
      int hourlyWage;
      int yearlySalary;
      Scene scene = null;                  // Scene contains all content
      Pane pane = null;                    // Positions components within scene
      TextField outputField = null;        // Displays output salary
      
      pane = new Pane();                   // Create an empty pane     
      scene = new Scene(pane);             // Create a scene containing the pane
            
      // Calculate yearly salary
      hourlyWage = 20;
      yearlySalary = hourlyWage * 40 * 50;
              
      // Create text field and display program output using the text field
      outputField = new TextField();
      outputField.setText("An hourly wage of $" + hourlyWage + "/hr " +
                          "yields $" + yearlySalary + "/yr.");
      outputField.setEditable(false);      // Prevent user from editing text
      outputField.setPrefColumnCount(22);  
      
      pane.getChildren().add(outputField); // Add text field to pane
      
      applicationStage.setScene(scene);    // Set window's scene  
      applicationStage.setTitle("Salary"); // Set window's title
      applicationStage.show();             // Display window
   }
   
   public static void main(String [] args) {
      launch(args); // Launch application
   }
}

--

package tuesday;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;

public class Something extends Application {
    @Override
    public void start(Stage applicationStage) {
        int hourlyWage;
        int yearlySalary;
        Scene scene = null;         // Scene contains all content
        GridPane gridPane = null;   // Positions components within scene
        Label wageLabel = null;     // Label for hourly salary
        Label salaryLabel = null;   // Label for yearly salary
        TextField salField = null;  // Displays yearly salary
        TextField wageField = null; // Displays hourly wage
        Insets gridPadding = null;

        gridPane = new GridPane();   // Create an empty pane
        scene = new Scene(gridPane); // Create scene containing the grid pane

        // Calculate yearly salary
        hourlyWage = 20;
        yearlySalary = hourlyWage * 40 * 50;

        // Set hourly and yearly salary
        wageLabel = new Label("Hourly wage:");
        salaryLabel = new Label("Yearly salary:");

        // Create wage and salary text fields
        wageField = new TextField();
        wageField.setPrefColumnCount(15);
        wageField.setEditable(false);
        wageField.setText(Integer.toString(hourlyWage));

        salField = new TextField();
        salField.setPrefColumnCount(15);
        salField.setEditable(false);
        salField.setText(Integer.toString(yearlySalary));

        gridPane.add(wageLabel, 0, 0);   // Add wage label to location (0, 0)
        gridPane.add(wageField, 1, 0);   // Add wage text field to location (1, 0)
        gridPane.add(salaryLabel, 0, 1); // Add salary label to location (0, 1)
        gridPane.add(salField, 1, 1);    // Add salary text field to location (1, 1)

        gridPadding = new Insets(10, 10, 10, 10); // Padding values for top, right, bottom, and left
        gridPane.setPadding(gridPadding);         // Set padding around  grid
        gridPane.setHgap(10);                     // Spacing between columns
        gridPane.setVgap(10);                     // Spacing between rows

        applicationStage.setScene(scene);    // Set window's scene  
        applicationStage.setTitle("Salary"); // Set window's title
        applicationStage.show();             // Display window
    }

    public static void main(String [] args) {
        launch(args); // Launch application
    }
}

--

package tuesday;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.geometry.Insets;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;

public class Whatever extends Application {
    private Label wageLabel;     // Label for hourly salary
    private Label salLabel;      // Label for yearly salary
    private TextField salField;  // Displays hourly salary 
    private TextField wageField; // Displays yearly salary
    private Button calcButton;   // Triggers salary calculation

    @Override
    public void start(Stage applicationStage) {
        Scene scene = null;         // Scene contains all content
        GridPane gridPane = null;   // Positions components within scene

        gridPane = new GridPane();   // Create an empty pane
        scene = new Scene(gridPane); // Create scene containing the grid pane

        // Set hourly and yearly salary
        wageLabel = new Label("Hourly wage:");
        salLabel = new Label("Yearly salary:");

        wageField = new TextField(); 
        wageField.setPrefColumnCount(15);
        wageField.setEditable(true);
        wageField.setText("0");

        salField = new TextField(); 
        salField.setPrefColumnCount(15);
        salField.setEditable(false);

        // Create a "Calculate" button
        calcButton = new Button("Calculate");

        gridPane.setPadding(new Insets(10, 10, 10, 10)); // Padding around  grid
        gridPane.setHgap(10);                            // Spacing between columns
        gridPane.setVgap(10);                            // Spacing between rows

        gridPane.add(wageLabel, 0, 0);  // Add wage label to location (0, 0)
        gridPane.add(wageField, 1, 0);  // Add wage text field to location (1, 0)
        gridPane.add(salLabel, 0, 1);   // Add salary label to location (0, 1)
        gridPane.add(salField, 1, 1);   // Add salary text field to location (1, 1)
        gridPane.add(calcButton, 0, 2); // Add calculate button to location (0, 2)

        // Set an event handler to handle button presses
        calcButton.setOnAction(new EventHandler<ActionEvent>() {
            /* Method is automatically called when an event 
                    occurs (e.g, button is pressed) */
            @Override
            public void handle(ActionEvent event) {
                String userInput; 
                int hourlyWage;    
                int yearlySalary;    

                // Get user's wage input and calculate yearly salary
                userInput = wageField.getText();            
                hourlyWage = Integer.parseInt(userInput);
                yearlySalary = hourlyWage * 40 * 50;

                // Display calculated salary
                salField.setText(Integer.toString(yearlySalary));
            } 
        });

        applicationStage.setScene(scene);    // Set window's scene  
        applicationStage.setTitle("Salary"); // Set window's title
        applicationStage.show();             // Display window
    }

    public static void main(String [] args) {
        launch(args); // Launch application
    }
}

--