samkopite

Untitled

Apr 1st, 2019
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.99 KB | None | 0 0
  1. 1. Text fx program
  2. import javafx.application.Application;
  3. import javafx.scene.Group;
  4. import javafx.scene.Scene;
  5. import javafx.stage.Stage;
  6. import javafx.scene.text.Text;
  7. import javafx.scene.text.Font;
  8. import javafx.scene.text.FontPosture;
  9. import javafx.scene.text.FontWeight;
  10. import javafx.scene.paint.Color;
  11.  
  12. public class textfx extends Application
  13. {
  14.  
  15. public void start(Stage stage)
  16. {
  17. //Creating a Text object
  18. Text text = new Text();
  19. text.setFont(Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 20)); // font of the text
  20.  
  21. //Setting the text to be added.
  22. text.setText("Hi!! welcome to JAVA FX programming");
  23.  
  24. //setting the position of the text
  25. text.setX(50);
  26. text.setY(50);
  27. text.setFill(Color.BROWN);
  28.  
  29. //Setting the Stroke
  30. text.setStrokeWidth(2);
  31. text.setUnderline(true);
  32.  
  33. // Setting the stroke color
  34. text.setStroke(Color.BLUE);
  35.  
  36. //Creating a Group object
  37. Group root = new Group(text);
  38.  
  39. //Creating a scene object
  40. Scene scene = new Scene(root, 600, 300);
  41.  
  42. //Setting title to the Stage
  43. stage.setTitle("TEXT Application");
  44.  
  45. //Adding scene to the stage
  46. stage.setScene(scene);
  47.  
  48. //Displaying the contents of the stage
  49. stage.show();
  50. }
  51. public static void main(String args[]){
  52. launch(args);
  53. }
  54. }
  55.  
  56. 2.Line program using javafx
  57. import javafx.application.Application;
  58. import javafx.scene.Group;
  59. import javafx.scene.Scene;
  60. import javafx.scene.shape.Line;
  61. import javafx.stage.Stage;
  62.  
  63. public class line extends Application{
  64. @Override
  65. public void start(Stage stage) {
  66. //Creating a line object
  67. Line line = new Line();
  68.  
  69. //Setting the properties to a line
  70. line.setStartX(100.0);
  71. line.setStartY(150.0);
  72. line.setEndX(500.0);
  73. line.setEndY(150.0);
  74.  
  75. //Creating a Group
  76. Group root = new Group(line);
  77.  
  78. //Creating a Scene
  79. Scene scene = new Scene(root, 600, 300);
  80.  
  81. //Setting title to the scene
  82. stage.setTitle("Sample application");
  83.  
  84. //Adding the scene to the stage
  85. stage.setScene(scene);
  86.  
  87. //Displaying the contents of a scene
  88. stage.show();
  89. }
  90. public static void main(String args[]){
  91. launch(args);
  92. }
  93. }
  94.  
  95.  
  96. 3.Rectangle using javaFx
  97. import javafx.application.Application;
  98. import javafx.scene.Group;
  99. import javafx.scene.Scene;
  100. import javafx.stage.Stage;
  101. import javafx.scene.shape.Rectangle;
  102.  
  103. public class rectangle1 extends Application {
  104. @Override
  105. public void start(Stage stage) {
  106. //Drawing a Rectangle
  107. Rectangle rectangle = new Rectangle();
  108.  
  109. //Setting the properties of the rectangle
  110. rectangle.setX(150.0f);
  111. rectangle.setY(75.0f);
  112. rectangle.setWidth(300.0f);
  113. rectangle.setHeight(150.0f);
  114.  
  115. //Creating a Group object
  116. Group root = new Group(rectangle);
  117.  
  118. //Creating a scene object
  119. Scene scene = new Scene(root, 600, 300);
  120.  
  121. //Setting title to the Stage
  122. stage.setTitle("Drawing a Rectangle");
  123.  
  124. //Adding scene to the stage
  125. stage.setScene(scene);
  126.  
  127. //Displaying the contents of the stage
  128. stage.show();
  129. }
  130. public static void main(String args[]){
  131. launch(args);
  132. }
  133. }
  134.  
  135. 4. Rotate program using javaFx
  136. import javafx.animation.RotateTransition;
  137. import javafx.application.Application;
  138. import static javafx.application.Application.launch;
  139. import javafx.scene.Group;
  140. import javafx.scene.Scene;
  141. import javafx.scene.paint.Color;
  142. import javafx.scene.shape.Polygon;
  143. import javafx.stage.Stage;
  144. import javafx.util.Duration;
  145.  
  146. public class rotate extends Application
  147. {
  148.  
  149. public void start(Stage stage) {
  150. //Creating a hexagon
  151. Polygon hexagon = new Polygon();
  152.  
  153. //Adding coordinates to the hexagon
  154. hexagon.getPoints().addAll(new Double[]{
  155. 200.0, 50.0,
  156. 400.0, 50.0,
  157. 450.0, 150.0,
  158. 400.0, 250.0,
  159. 200.0, 250.0,
  160. 150.0, 150.0,
  161. });
  162. //Setting the fill color for the hexagon
  163. hexagon.setFill(Color.BLUE);
  164.  
  165. //Creating a rotate transition
  166. RotateTransition rotateTransition = new RotateTransition();
  167.  
  168. //Setting the duration for the transition
  169. rotateTransition.setDuration(Duration.millis(1000));
  170.  
  171. //Setting the node for the transition
  172. rotateTransition.setNode(hexagon);
  173.  
  174. //Setting the angle of the rotation
  175. rotateTransition.setByAngle(360);
  176.  
  177. //Setting the cycle count for the transition
  178. rotateTransition.setCycleCount(50);
  179.  
  180. //Setting auto reverse value to false
  181. rotateTransition.setAutoReverse(false);
  182.  
  183. //Playing the animation
  184. rotateTransition.play();
  185.  
  186. //Creating a Group object
  187. Group root = new Group(hexagon);
  188.  
  189. //Creating a scene object
  190. Scene scene = new Scene(root, 600, 300);
  191.  
  192. //Setting title to the Stage
  193. stage.setTitle("Rotate transition example ");
  194.  
  195. //Adding scene to the stage
  196. stage.setScene(scene);
  197.  
  198. //Displaying the contents of the stage
  199. stage.show();
  200. }
  201. public static void main(String args[]){
  202. launch(args);
  203. }
  204. }
  205.  
  206. 5.button program using javaFx
  207.  
  208. import javafx.application.Application;
  209. import javafx.scene.Scene;
  210. import javafx.scene.control.*;
  211. import javafx.scene.layout.*;
  212. import javafx.stage.Stage;
  213. import javafx.event.ActionEvent;
  214. import javafx.event.EventHandler;
  215.  
  216. public class buttonfx extends Application
  217. {
  218.  
  219. public void start(Stage primaryStage) throws Exception
  220. {
  221.  
  222. //StackPane root = new StackPane();
  223. Button btn=new Button("This is a button");
  224. Button b2 = new Button("Exit");
  225. TextField tf1=new TextField();
  226. btn.setOnAction(new EventHandler<ActionEvent>() {
  227.  
  228. @Override
  229. public void handle(ActionEvent e)
  230. {
  231.  
  232. tf1.setText("Button clicked");
  233.  
  234. }
  235. } );
  236.  
  237. b2.setOnAction(new EventHandler<ActionEvent>() {
  238.  
  239. @Override
  240. public void handle(ActionEvent e)
  241. {
  242.  
  243. System.exit(0);
  244.  
  245. }
  246. } );
  247.  
  248.  
  249. GridPane root = new GridPane();
  250. root.addRow(0, tf1,btn);
  251. root.addRow(1,b2);
  252. Scene scene=new Scene(root,300,300);
  253. // root.getChildren().add(btn);
  254. // root.getChildren().add(tf1);
  255. primaryStage.setScene(scene);
  256. primaryStage.setTitle("Button Class Example");
  257. primaryStage.show();
  258.  
  259. }
  260. public static void main(String[] args) {
  261. launch(args);
  262. }
  263. }
  264.  
  265. 6. Menu program using javaFx
  266. import javafx.application.Application;
  267. import javafx.scene.Scene;
  268. import javafx.scene.control.*;
  269. import javafx.scene.layout.BorderPane;
  270. import javafx.stage.Stage;
  271. public class javafxmenu extends Application {
  272. public static void main(String[] args) {
  273. launch(args);
  274. }
  275. public void start(Stage primaryStage) throws Exception
  276. {
  277. BorderPane root = new BorderPane();
  278. Scene scene = new Scene(root,200,300);
  279. MenuBar menubar = new MenuBar();
  280. Menu FileMenu = new Menu("Colors");
  281. MenuItem filemenu1=new MenuItem("RED");
  282. MenuItem filemenu2=new MenuItem("GREEN");
  283. MenuItem filemenu3=new MenuItem("BLUE");
  284. Menu EditMenu=new Menu("Flowers");
  285. MenuItem EditMenu1=new MenuItem("ROSE");
  286. MenuItem EditMenu2=new MenuItem("JASMINE");
  287. MenuItem EditMenu3=new MenuItem("LILLY");
  288. EditMenu.getItems().addAll(EditMenu1,EditMenu2,EditMenu3);
  289. root.setTop(menubar);
  290. FileMenu.getItems().addAll(filemenu1,filemenu2,filemenu3);
  291. menubar.getMenus().addAll(FileMenu,EditMenu);
  292. primaryStage.setScene(scene);
  293. primaryStage.show();
  294.  
  295. }
  296. }
  297.  
  298. 7.Event filter program
  299.  
  300. import javafx.application.Application;
  301. import static javafx.application.Application.launch;
  302. import javafx.event.EventHandler;// event handler
  303.  
  304. import javafx.scene.Group;
  305. import javafx.scene.Scene;
  306. import javafx.scene.input.MouseEvent; //event
  307. import javafx.scene.paint.Color;
  308. import javafx.scene.shape.Circle;
  309.  
  310. import javafx.scene.text.Font;
  311. import javafx.scene.text.FontWeight;
  312. import javafx.scene.text.Text;
  313. import javafx.stage.Stage;
  314.  
  315. public class eventfiltercircle extends Application
  316. {
  317. public void start(Stage stage)
  318. {
  319. Circle circle = new Circle();
  320. circle.setCenterX(300.0f);
  321. circle.setCenterY(135.0f);
  322. circle.setRadius(25.0f);
  323. circle.setFill(Color.BROWN);
  324. circle.setStrokeWidth(20);
  325. Text text = new Text("Click on the circle to change its color");
  326. // text.setFont(Font.font(null, FontWeight.BOLD, 15));
  327. text.setFill(Color.RED);
  328. text.setX(150);
  329. text.setY(50);
  330. EventHandler<MouseEvent> eventHandler = new EventHandler<MouseEvent>()
  331. {
  332. public void handle(MouseEvent e)
  333. {
  334. System.out.println("Sucess");
  335. circle.setFill(Color.BLUE);
  336. // System.exit(0);
  337. }
  338. };
  339. //Registering the event filter
  340. circle.addEventFilter(MouseEvent.MOUSE_CLICKED, eventHandler);
  341.  
  342. Group root = new Group(circle, text);
  343. Scene scene = new Scene(root, 600, 300);
  344. scene.setFill(Color.LAVENDER);
  345. stage.setTitle("Event Filters Example");
  346. stage.setScene(scene);
  347. stage.show();
  348. }
  349. public static void main(String args[]){
  350. launch(args);
  351. }
  352. }
  353.  
  354. 8.Event handler program using javaFx
  355. import javafx.animation.RotateTransition;
  356. import javafx.application.Application;
  357. import javafx.event.EventHandler;
  358.  
  359. import javafx.scene.Group;
  360. import javafx.scene.PerspectiveCamera;
  361. import javafx.scene.Scene;
  362. import javafx.scene.control.TextField;
  363. import javafx.scene.input.KeyEvent;
  364. import javafx.scene.paint.Color;
  365.  
  366. import javafx.scene.shape.Box;
  367. import javafx.scene.text.Font;
  368. import javafx.scene.text.FontWeight;
  369. import javafx.scene.text.Text;
  370. import javafx.scene.transform.Rotate;
  371. import javafx.stage.Stage;
  372. import javafx.util.Duration;
  373.  
  374. public class eventhandler extends Application
  375. {
  376.  
  377. public void start(Stage stage) {
  378. Box box = new Box();
  379. box.setWidth(150.0);
  380. box.setHeight(150.0);
  381. box.setDepth(100.0);
  382. box.setTranslateX(350);
  383. box.setTranslateY(150);
  384. box.setTranslateZ(50);
  385. Text text = new Text("Write something on the text box and click box to stop");
  386. text.setFont(Font.font(null, FontWeight.BOLD, 15));
  387. text.setFill(Color.RED);
  388. text.setX(20);
  389. text.setY(50);
  390. RotateTransition rotateTransition = new RotateTransition();
  391. rotateTransition.setDuration(Duration.millis(1000));
  392. rotateTransition.setNode(box);
  393. rotateTransition.setAxis(Rotate.Y_AXIS);
  394. rotateTransition.setByAngle(360);
  395. rotateTransition.setCycleCount(50);
  396. rotateTransition.setAutoReverse(false);
  397. TextField textField = new TextField();
  398. textField.setLayoutX(50);
  399. textField.setLayoutY(100);
  400.  
  401. //Handling the key typed event
  402. EventHandler<KeyEvent> etext = new EventHandler<KeyEvent>()
  403. {
  404. public void handle(KeyEvent event) {
  405. rotateTransition.play();
  406. }
  407. };
  408. //Adding an event handler to the text feld
  409. textField.addEventHandler(KeyEvent.KEY_TYPED, etext);
  410.  
  411. //Handling the mouse clicked event(on box)
  412. EventHandler<javafx.scene.input.MouseEvent> ebox =
  413. new EventHandler<javafx.scene.input.MouseEvent>()
  414. {
  415. public void handle(javafx.scene.input.MouseEvent e)
  416. {
  417. rotateTransition.stop();
  418. }
  419. };
  420. //Adding the event handler to the box
  421. box.addEventHandler(javafx.scene.input.MouseEvent.MOUSE_CLICKED, ebox);
  422.  
  423. Group root = new Group(box, textField, text);
  424. Scene scene = new Scene(root, 600, 300);
  425. stage.setTitle("Event Handlers Example");
  426. stage.setScene(scene);
  427. stage.show();
  428. }
  429. public static void main(String args[]){
  430. launch(args);
  431. }
  432. }
Add Comment
Please, Sign In to add comment