Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.35 KB | None | 0 0
  1. package application;
  2.  
  3. import javafx.application.Application;
  4. import javafx.application.Platform;
  5. import javafx.beans.value.ChangeListener;
  6. import javafx.beans.value.ObservableValue;
  7. import javafx.stage.Stage;
  8. import javafx.scene.Scene;
  9. import javafx.scene.layout.VBox;
  10. import javafx.scene.control.Button;
  11. import javafx.scene.control.Label;
  12. import javafx.scene.control.Slider;
  13. import javafx.scene.paint.*;
  14. import javafx.scene.text.Font;
  15. import javafx.scene.canvas.*;
  16. import javafx.scene.input.MouseEvent;
  17. import javafx.event.EventHandler;
  18.  
  19. public class Main extends Application {
  20.  
  21. //PART 1
  22. Slider slider = new Slider();
  23. Label label = new Label(Double.toString(slider.getValue()));
  24.  
  25. //Part 2 initial values **********************************************
  26. double Ymouse = 0;
  27. double Ytop = 4;
  28. double Ybottom = 232;
  29. BarControl barctrl1 = new BarControl(200, 0, 100);
  30. BarControl barctrl2 = new BarControl(600, 200, 260);
  31. BarControl barctrl3 = new BarControl(100, 25, 60);
  32.  
  33.  
  34. //PART 1
  35. @Override
  36. public void start(Stage primaryStage) {
  37.  
  38. try {
  39. VBox root = new VBox();
  40. Scene scene = new Scene(root,400,400);
  41. scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  42. primaryStage.setScene(scene);
  43.  
  44. //PART 2 **********************************************
  45. //Labels
  46. Label label1 = new Label(Double.toString(barctrl1.getSetVal()));
  47. Label label2 = new Label(Double.toString(barctrl2.getSetVal()));
  48. Label label3 = new Label(Double.toString(barctrl3.getSetVal()));
  49. label1.setTranslateX(60);
  50. label1.setTranslateY(-400);
  51. label1.setFont(new Font("Verdana", 24));
  52. //label1.textProperty().setValue(String.format("%.1f"));
  53. label2.setTranslateX(190);
  54. label2.setTranslateY(-430);
  55. label2.setFont(new Font("Verdana", 24));
  56. //label2.textProperty().setValue(String.format("%.1f"));
  57. label3.setTranslateX(330);
  58. label3.setTranslateY(-460);
  59. label3.setFont(new Font("Verdana", 24));
  60. //label3.textProperty().setValue(String.format("%.1f"));
  61.  
  62. //Creating canvas
  63. Canvas canvas = new Canvas(400,400);
  64. GraphicsContext gc = canvas.getGraphicsContext2D();
  65. changeShape(gc, barctrl1, barctrl1.getHeight(), 4);
  66. root.getChildren().add(canvas);
  67. root.getChildren().add(label1);
  68. root.getChildren().add(label2);
  69. root.getChildren().add(label3);
  70. initialShape(gc, barctrl1, barctrl2, barctrl3);
  71.  
  72. //Mouse event
  73. root.setOnMouseClicked(new EventHandler<MouseEvent>() {
  74. public void handle(MouseEvent event) {
  75. Ymouse = event.getY();
  76. barctrl1.changeYellow(Ymouse, Ytop, Ybottom);
  77. }
  78. });
  79.  
  80.  
  81. canvas.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>(){
  82. @Override
  83. public void handle (MouseEvent event){
  84. double x = event.getX();
  85. double y = event.getY();
  86. if((x >= 2.6666 && x <= 54.6666) && (y >= 1.99999999999999 && y <= 230)) {
  87. //Bar Control 1
  88. barctrl1.changeYellow(y, Ytop, Ybottom);
  89. changeShape(gc, barctrl1, y, 1);
  90. label1.setText(barctrl1.display);
  91. }
  92. else if((x >= 134 && x <= 184) && (y >= 1.99999999999999 && y <= 230)) {
  93. //Bar Control 2
  94. barctrl2.changeYellow(y, Ytop, Ybottom);
  95. changeShape(gc, barctrl2, y, 2);
  96. label2.setText(barctrl2.display);
  97. }
  98. else if((x >= 272.6666 && x <= 328) && (y >= 1.99999999999999 && y <= 230)) {
  99. //Bar Control 3
  100. barctrl3.changeYellow(y, Ytop, Ybottom);
  101. changeShape(gc, barctrl3, y, 3);
  102. label3.setText(barctrl3.display);
  103. }
  104. else{
  105.  
  106. }
  107.  
  108.  
  109.  
  110.  
  111. }
  112. });
  113.  
  114. canvas.addEventHandler(MouseEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>(){
  115. @Override
  116. public void handle (MouseEvent event){
  117. double x = event.getX();
  118. double y = event.getY();
  119. if((x >= 2.6666 && x <= 54.6666) && (y >= 1.99999999999999 && y <= 230)) {
  120. //Bar Control 1
  121. barctrl1.changeYellow(y, Ytop, Ybottom);
  122. changeShape(gc, barctrl1, y, 1);
  123. label1.setText(barctrl1.display);
  124. }
  125. else if((x >= 134 && x <= 184) && (y >= 1.99999999999999 && y <= 230)) {
  126. //Bar Control 2
  127. barctrl2.changeYellow(y, Ytop, Ybottom);
  128. changeShape(gc, barctrl2, y, 2);
  129. label2.setText(barctrl2.display);
  130. }
  131. else if((x >= 272.6666 && x <= 328) && (y >= 1.99999999999999 && y <= 230)) {
  132. //Bar Control 3
  133. barctrl3.changeYellow(y, Ytop, Ybottom);
  134. changeShape(gc, barctrl3, y, 3);
  135. label3.setText(barctrl3.display);
  136. }
  137. else{
  138.  
  139. }
  140.  
  141.  
  142.  
  143.  
  144. }
  145. });
  146.  
  147.  
  148.  
  149. //PART 1
  150. root.getChildren().add(slider);
  151. slider.setTranslateY(-170);
  152. label.setTranslateY(-170);
  153. label.setFont(new Font("Verdana", 24));
  154. root.getChildren().add(label);
  155. slider.valueProperty().addListener(new ChangeListener<Object>(){
  156. public void changed(ObservableValue<? extends Object> arg0, Object arg1, Object arg2) {
  157. label.textProperty().setValue(String.format(("%.1f"), slider.getValue()));
  158. }
  159. });
  160. slider.setMin(150.0);
  161. slider.setMax(1000.0);
  162. slider.setValue(275.0);
  163. Button quit = new Button("Quit");
  164. root.getChildren().add(quit);
  165. quit.setTranslateY(-170);
  166. primaryStage.show();
  167. quit.setOnAction(click -> {
  168. Platform.exit();
  169. System.out.print("Goodbye!");
  170. });
  171. } catch(Exception e) {
  172. e.printStackTrace();
  173. }
  174. }
  175.  
  176.  
  177. //PART 2 ***********************************************************************************
  178. //Method to form the rectangles
  179. private void initialShape(GraphicsContext gc1, BarControl bc1, BarControl bc2, BarControl bc3){
  180. gc1.setStroke(Color.BLACK);
  181. gc1.setFill(Color.YELLOW);
  182. gc1.setLineWidth(60);
  183. gc1.strokeRect(0, 10, 30, 200);
  184. gc1.fillRect(5, 5, 50, 230);
  185. gc1.strokeRect(160, 10, 0, 200);
  186. gc1.fillRect(135, 5, 50, 230);
  187. gc1.strokeRect(300, 10, 0, 200);
  188. gc1.fillRect(275, 5, 50, 230);
  189. gc1.setFill(Color.BLACK);
  190. gc1.fillRect(5, 5, 50, barctrl1.getSetValHeight());
  191. gc1.fillRect(135, 5, 50, barctrl2.getSetValHeight());
  192. gc1.fillRect(275, 5, 50, barctrl3.getSetValHeight());
  193. }
  194.  
  195. private void changeShape(GraphicsContext gc1, BarControl barctrl, double height, int barNum){
  196. if(barNum ==1){
  197. gc1.setStroke(Color.BLACK);
  198. gc1.setFill(Color.YELLOW);
  199. gc1.setLineWidth(60);
  200. gc1.strokeRect(0, 10, 30, 200);
  201. gc1.fillRect(5, 5, 50, 230);
  202. gc1.setFill(Color.BLACK);
  203. gc1.fillRect(5, 5, 50, barctrl.height);
  204. }
  205. else if (barNum==2){
  206. gc1.setStroke(Color.BLACK);
  207. gc1.setFill(Color.YELLOW);
  208. gc1.setLineWidth(60);
  209. gc1.strokeRect(160, 10, 0, 200);
  210. gc1.fillRect(135, 5, 50, 230);
  211. gc1.setFill(Color.BLACK);
  212. gc1.fillRect(135, 5, 50, barctrl.height);
  213. }
  214. else if(barNum ==3){
  215. gc1.setStroke(Color.BLACK);
  216. gc1.setFill(Color.YELLOW);
  217. gc1.setLineWidth(60);
  218. gc1.strokeRect(300, 10, 0, 200);
  219. gc1.fillRect(275, 5, 50, 230);
  220. gc1.setFill(Color.BLACK);
  221. gc1.fillRect(275, 5, 50, barctrl.height);
  222. }
  223. else{
  224.  
  225. }
  226.  
  227. }
  228.  
  229. public static void main(String[] args) {
  230. launch(args);
  231. }
  232. }
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266. package application;
  267.  
  268. import java.text.DecimalFormat;
  269.  
  270. public class BarControl {
  271. private double maxVal;
  272. private double minVal;
  273. private double setVal;
  274.  
  275. public BarControl(double maxVal1, double minVal1, double setVal1){
  276. maxVal = maxVal1;
  277. minVal = minVal1;
  278. setVal = setVal1;
  279. }
  280.  
  281. public double getSetVal(){
  282. return setVal;
  283. }
  284.  
  285. public double getMinVal(){
  286. return minVal;
  287. }
  288.  
  289. public double getMaxVal(){
  290. return maxVal;
  291. }
  292.  
  293. double percent;
  294. double height = setVal;
  295. String display;
  296. //String numDisplay;
  297. DecimalFormat df = new DecimalFormat("#.0");
  298. //double setValHeight;
  299.  
  300. public double getSetValHeight(){
  301. return (setVal/maxVal)*230;
  302. }
  303.  
  304.  
  305.  
  306. public void changeYellow (double Ymouse, double Ytop, double Ybottom){
  307. percent = ((Ymouse)/(Ybottom-Ytop));
  308. height = (230*percent)-5; //230 is the height of the yellow box
  309. //height = df.format(height);
  310. //gc1.fillRect(5, 5, 50, height); //Change the height of yellow
  311. display = df.format((1-percent)*maxVal); //Having issues
  312.  
  313. }
  314. public double getHeight(){
  315. return height;
  316. }
  317.  
  318. public void setHeight(double newheight){
  319. height = newheight;
  320. }
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement