Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package application;
- import javafx.application.Application;
- import javafx.application.Platform;
- import javafx.beans.value.ChangeListener;
- import javafx.beans.value.ObservableValue;
- import javafx.stage.Stage;
- import javafx.scene.Scene;
- import javafx.scene.layout.VBox;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.Slider;
- import javafx.scene.paint.*;
- import javafx.scene.text.Font;
- import javafx.scene.canvas.*;
- import javafx.scene.input.MouseEvent;
- import javafx.event.EventHandler;
- public class Main extends Application {
- //PART 1
- Slider slider = new Slider();
- Label label = new Label(Double.toString(slider.getValue()));
- //Part 2 initial values **********************************************
- double Ymouse = 0;
- double Ytop = 4;
- double Ybottom = 232;
- BarControl barctrl1 = new BarControl(200, 0, 100);
- BarControl barctrl2 = new BarControl(600, 200, 260);
- BarControl barctrl3 = new BarControl(100, 25, 60);
- //PART 1
- @Override
- public void start(Stage primaryStage) {
- try {
- VBox root = new VBox();
- Scene scene = new Scene(root,400,400);
- scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
- primaryStage.setScene(scene);
- //PART 2 **********************************************
- //Labels
- Label label1 = new Label(Double.toString(barctrl1.getSetVal()));
- Label label2 = new Label(Double.toString(barctrl2.getSetVal()));
- Label label3 = new Label(Double.toString(barctrl3.getSetVal()));
- label1.setTranslateX(60);
- label1.setTranslateY(-400);
- label1.setFont(new Font("Verdana", 24));
- //label1.textProperty().setValue(String.format("%.1f"));
- label2.setTranslateX(190);
- label2.setTranslateY(-430);
- label2.setFont(new Font("Verdana", 24));
- //label2.textProperty().setValue(String.format("%.1f"));
- label3.setTranslateX(330);
- label3.setTranslateY(-460);
- label3.setFont(new Font("Verdana", 24));
- //label3.textProperty().setValue(String.format("%.1f"));
- //Creating canvas
- Canvas canvas = new Canvas(400,400);
- GraphicsContext gc = canvas.getGraphicsContext2D();
- changeShape(gc, barctrl1, barctrl1.getHeight(), 4);
- root.getChildren().add(canvas);
- root.getChildren().add(label1);
- root.getChildren().add(label2);
- root.getChildren().add(label3);
- initialShape(gc, barctrl1, barctrl2, barctrl3);
- //Mouse event
- root.setOnMouseClicked(new EventHandler<MouseEvent>() {
- public void handle(MouseEvent event) {
- Ymouse = event.getY();
- barctrl1.changeYellow(Ymouse, Ytop, Ybottom);
- }
- });
- canvas.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>(){
- @Override
- public void handle (MouseEvent event){
- double x = event.getX();
- double y = event.getY();
- if((x >= 2.6666 && x <= 54.6666) && (y >= 1.99999999999999 && y <= 230)) {
- //Bar Control 1
- barctrl1.changeYellow(y, Ytop, Ybottom);
- changeShape(gc, barctrl1, y, 1);
- label1.setText(barctrl1.display);
- }
- else if((x >= 134 && x <= 184) && (y >= 1.99999999999999 && y <= 230)) {
- //Bar Control 2
- barctrl2.changeYellow(y, Ytop, Ybottom);
- changeShape(gc, barctrl2, y, 2);
- label2.setText(barctrl2.display);
- }
- else if((x >= 272.6666 && x <= 328) && (y >= 1.99999999999999 && y <= 230)) {
- //Bar Control 3
- barctrl3.changeYellow(y, Ytop, Ybottom);
- changeShape(gc, barctrl3, y, 3);
- label3.setText(barctrl3.display);
- }
- else{
- }
- }
- });
- canvas.addEventHandler(MouseEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>(){
- @Override
- public void handle (MouseEvent event){
- double x = event.getX();
- double y = event.getY();
- if((x >= 2.6666 && x <= 54.6666) && (y >= 1.99999999999999 && y <= 230)) {
- //Bar Control 1
- barctrl1.changeYellow(y, Ytop, Ybottom);
- changeShape(gc, barctrl1, y, 1);
- label1.setText(barctrl1.display);
- }
- else if((x >= 134 && x <= 184) && (y >= 1.99999999999999 && y <= 230)) {
- //Bar Control 2
- barctrl2.changeYellow(y, Ytop, Ybottom);
- changeShape(gc, barctrl2, y, 2);
- label2.setText(barctrl2.display);
- }
- else if((x >= 272.6666 && x <= 328) && (y >= 1.99999999999999 && y <= 230)) {
- //Bar Control 3
- barctrl3.changeYellow(y, Ytop, Ybottom);
- changeShape(gc, barctrl3, y, 3);
- label3.setText(barctrl3.display);
- }
- else{
- }
- }
- });
- //PART 1
- root.getChildren().add(slider);
- slider.setTranslateY(-170);
- label.setTranslateY(-170);
- label.setFont(new Font("Verdana", 24));
- root.getChildren().add(label);
- slider.valueProperty().addListener(new ChangeListener<Object>(){
- public void changed(ObservableValue<? extends Object> arg0, Object arg1, Object arg2) {
- label.textProperty().setValue(String.format(("%.1f"), slider.getValue()));
- }
- });
- slider.setMin(150.0);
- slider.setMax(1000.0);
- slider.setValue(275.0);
- Button quit = new Button("Quit");
- root.getChildren().add(quit);
- quit.setTranslateY(-170);
- primaryStage.show();
- quit.setOnAction(click -> {
- Platform.exit();
- System.out.print("Goodbye!");
- });
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- //PART 2 ***********************************************************************************
- //Method to form the rectangles
- private void initialShape(GraphicsContext gc1, BarControl bc1, BarControl bc2, BarControl bc3){
- gc1.setStroke(Color.BLACK);
- gc1.setFill(Color.YELLOW);
- gc1.setLineWidth(60);
- gc1.strokeRect(0, 10, 30, 200);
- gc1.fillRect(5, 5, 50, 230);
- gc1.strokeRect(160, 10, 0, 200);
- gc1.fillRect(135, 5, 50, 230);
- gc1.strokeRect(300, 10, 0, 200);
- gc1.fillRect(275, 5, 50, 230);
- gc1.setFill(Color.BLACK);
- gc1.fillRect(5, 5, 50, barctrl1.getSetValHeight());
- gc1.fillRect(135, 5, 50, barctrl2.getSetValHeight());
- gc1.fillRect(275, 5, 50, barctrl3.getSetValHeight());
- }
- private void changeShape(GraphicsContext gc1, BarControl barctrl, double height, int barNum){
- if(barNum ==1){
- gc1.setStroke(Color.BLACK);
- gc1.setFill(Color.YELLOW);
- gc1.setLineWidth(60);
- gc1.strokeRect(0, 10, 30, 200);
- gc1.fillRect(5, 5, 50, 230);
- gc1.setFill(Color.BLACK);
- gc1.fillRect(5, 5, 50, barctrl.height);
- }
- else if (barNum==2){
- gc1.setStroke(Color.BLACK);
- gc1.setFill(Color.YELLOW);
- gc1.setLineWidth(60);
- gc1.strokeRect(160, 10, 0, 200);
- gc1.fillRect(135, 5, 50, 230);
- gc1.setFill(Color.BLACK);
- gc1.fillRect(135, 5, 50, barctrl.height);
- }
- else if(barNum ==3){
- gc1.setStroke(Color.BLACK);
- gc1.setFill(Color.YELLOW);
- gc1.setLineWidth(60);
- gc1.strokeRect(300, 10, 0, 200);
- gc1.fillRect(275, 5, 50, 230);
- gc1.setFill(Color.BLACK);
- gc1.fillRect(275, 5, 50, barctrl.height);
- }
- else{
- }
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
- package application;
- import java.text.DecimalFormat;
- public class BarControl {
- private double maxVal;
- private double minVal;
- private double setVal;
- public BarControl(double maxVal1, double minVal1, double setVal1){
- maxVal = maxVal1;
- minVal = minVal1;
- setVal = setVal1;
- }
- public double getSetVal(){
- return setVal;
- }
- public double getMinVal(){
- return minVal;
- }
- public double getMaxVal(){
- return maxVal;
- }
- double percent;
- double height = setVal;
- String display;
- //String numDisplay;
- DecimalFormat df = new DecimalFormat("#.0");
- //double setValHeight;
- public double getSetValHeight(){
- return (setVal/maxVal)*230;
- }
- public void changeYellow (double Ymouse, double Ytop, double Ybottom){
- percent = ((Ymouse)/(Ybottom-Ytop));
- height = (230*percent)-5; //230 is the height of the yellow box
- //height = df.format(height);
- //gc1.fillRect(5, 5, 50, height); //Change the height of yellow
- display = df.format((1-percent)*maxVal); //Having issues
- }
- public double getHeight(){
- return height;
- }
- public void setHeight(double newheight){
- height = newheight;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement