0% found this document useful (0 votes)
37 views6 pages

Patel Arth Lab6

This document contains code for two Java programs: 1) A calculator application created with JavaFX that allows users to input numbers and perform addition and subtraction calculations. 2) An application that displays a graphical representation of BB-8 and allows the user to move it left and right using arrow keys.

Uploaded by

Arth Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views6 pages

Patel Arth Lab6

This document contains code for two Java programs: 1) A calculator application created with JavaFX that allows users to input numbers and perform addition and subtraction calculations. 2) An application that displays a graphical representation of BB-8 and allows the user to move it left and right using arrow keys.

Uploaded by

Arth Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab 6

Course: COSC 1047 Introduction to Computer Science II


Name: Arth Patel
Section: COSC 1047 J
Student number: 239548040

1. Write a program that creates a calculator as follows:

Methodology:
This program provides Java FX application is a calculator with GUI.
First I created a text field where user input the numeric value and add buttons for mathematic
operations. At last Create a GridPane for managing the layout.

##Code
package application;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class CalculatorJava extends Application


{
private TextField number1;
private TextField number2;
private TextField result;

@Override
public void start(Stage primaryStage)
{

number1 = new TextField();


number2 = new TextField();
result = new TextField();
result.setEditable(false);

Button addButton = new Button("Add");


addButton.setOnAction(e -> addNumbers());
Button subtractButton = new Button("Subtract");
subtractButton.setOnAction(e -> subtractNumbers());
Button clearButton = new Button("Clear");
clearButton.setOnAction(e -> clearFields());

GridPane pane = new GridPane();


pane.setPadding(new Insets(18));
pane.setHgap(18);
pane.setVgap(18);

number1.setPrefWidth(80);
HBox number1Box = new HBox(new Label("FIRST OPERAND"), number1);
number1Box.setAlignment(Pos.CENTER_LEFT);
number1Box.setSpacing(37);
pane.add(number1Box, 0, 0);

number2.setPrefWidth(80);
HBox number2Box = new HBox(new Label("SECOND OPERAND"), number2);
number2.setAlignment(Pos.CENTER_LEFT);
number2Box.setSpacing(18);
pane.add(number2Box, 0, 1);

result.setPrefWidth(80);
HBox resultBox = new HBox(new Label("RESULT"), result);
result.setAlignment(Pos.CENTER_LEFT);
resultBox.setSpacing(77);
pane.add(resultBox, 0, 2);

HBox buttonsBox = new HBox(10, addButton, subtractButton,


clearButton);
buttonsBox.setAlignment(Pos.CENTER);
buttonsBox.setPadding(new Insets(10, 0, 0, 0));
pane.add(buttonsBox, 0, 3);

Scene scene = new Scene(pane);


primaryStage.setTitle("CALCULATOR");
primaryStage.setScene(scene);
primaryStage.show();
}

private void addNumbers()


{
double int1 = Double.parseDouble(number1.getText());
double int2 = Double.parseDouble(number2.getText());
double add = int1 + int2;
result.setText(String.valueOf(add));
}

private void subtractNumbers()


{
double int1 = Double.parseDouble(number1.getText());
double int2 = Double.parseDouble(number2.getText());
double sub = int1 - int2;
result.setText(String.valueOf(sub));
}

private void clearFields()


{
number1.clear();
number2.clear();
result.clear();
}

public static void main(String[] args)


{
launch(args);
}
}
2) Move BB-8 using Arrow key (Move left or Right)

package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class MovingBB8 extends Application


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

@Override
public void start(Stage primaryStage)
{

BB8Pane pane = new BB8Pane();


pane.setOnKeyPressed(e ->
{
if (e.getCode() == KeyCode.LEFT)
{
pane.moveLeft();
} else if (e.getCode() == KeyCode.RIGHT)
{
pane.moveRight();
}
});

Scene scene = new Scene(pane, 400, 120);


primaryStage.setTitle("MOVINGBB8");
primaryStage.setScene(scene);
primaryStage.show();

pane.requestFocus();
}
}

class BB8Pane extends Pane


{
private Circle circle1 = new Circle(200, 50, 15);
private Circle circle2 = new Circle(200, 70, 20);
private Line line = new Line(200, 25, 200, 35);

public BB8Pane()
{
line.setStroke(Color.BLACK);
circle1.setFill(Color.CORAL);
circle2.setFill(Color.CORAL);

getChildren().addAll(circle1, circle2, line);


}

public void moveLeft()


{
circle1.setCenterX(circle1.getCenterX() - 10);
circle2.setCenterX(circle2.getCenterX() - 10);
line.setStartX(line.getStartX() - 10);
line.setEndX(line.getEndX() - 10);
}

public void moveRight()


{
circle1.setCenterX(circle1.getCenterX() + 10);
circle2.setCenterX(circle2.getCenterX() + 10);
line.setStartX(line.getStartX() + 10);
line.setEndX(line.getEndX() + 10);
}
}

You might also like