Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. package game1;
  2.  
  3. import static com.sun.java.accessibility.util.AWTEventMonitor.addKeyListener;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.event.KeyListener;
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.FileNotFoundException;
  10. import java.io.InputStream;
  11. import javafx.animation.PathTransition;
  12. import javafx.animation.PathTransition.OrientationType;
  13. import javafx.application.Application;
  14. import javafx.event.ActionEvent;
  15. import javafx.event.EventHandler;
  16. import javafx.geometry.Pos;
  17. import javafx.scene.Group;
  18. import javafx.scene.Node;
  19. import javafx.scene.Scene;
  20. import javafx.scene.control.Button;
  21. import javafx.scene.control.TextField;
  22. import javafx.scene.image.Image;
  23. import javafx.scene.image.ImageView;
  24. import javafx.scene.input.KeyCode;
  25. import static javafx.scene.input.KeyCode.ENTER;
  26. import javafx.scene.layout.Pane;
  27. import javafx.scene.layout.StackPane;
  28. import javafx.scene.layout.VBox;
  29. import javafx.scene.paint.Color;
  30. import javafx.scene.paint.ImagePattern;
  31. import javafx.scene.shape.LineTo;
  32. import javafx.scene.shape.MoveTo;
  33. import javafx.scene.shape.Path;
  34. import javafx.scene.shape.Rectangle;
  35. import javafx.stage.Stage;
  36. import javafx.util.Duration;
  37. import javax.swing.JOptionPane;
  38. import javax.swing.Timer;
  39. import sun.audio.AudioPlayer;
  40. import sun.audio.AudioStream;
  41.  
  42. /**
  43. *
  44. * @author USER
  45. */
  46. public class GAME1 extends Application{
  47. Stage window;//window is a stage type variable
  48. Scene scn,scn1;//scn and scn1 are two scene type variables
  49. private int x=30;
  50. private int y=500;
  51. private int speedX=1;
  52. private int speedY=1;
  53.  
  54.  
  55.  
  56. @Override
  57. public void start(Stage primaryStage)throws FileNotFoundException {
  58. window=primaryStage;
  59.  
  60. //image1 specification starts:
  61. Image image = new Image(new FileInputStream("C:\\Users\\USER\\Documents\\NetBeansProjects\\GAME1\\src\\interface.jpg")); //path of the image is given here for first scene;
  62.  
  63. ImageView imageView = new ImageView(image);
  64.  
  65. imageView.setFitHeight(600);
  66. imageView.setFitWidth(600); //these values are set so that the image can fit the stage/scene;
  67.  
  68. imageView.setPreserveRatio(true);
  69. //image2 specification ends..
  70. //->button1 specification starts:
  71. Button btn1= new Button("CLICK TO PLAY");//clicking on this button takes you to the next screen;
  72. btn1.setLayoutX(250);//position in the x axis.
  73. btn1.setLayoutY(350);//position in the Y axix;
  74. btn1.setOnAction(e->window.setScene(scn));//here i used lambda expression to switch between the scenes;
  75.  
  76. //->here i am joining the button with enter key below.
  77. btn1.setOnKeyPressed(
  78. event->{
  79. switch(event.getCode()){
  80. case ENTER:
  81. window.setScene(scn) ;
  82. }
  83. }
  84. );
  85. //<<joining the button1 with keyboard's enter key is complete..
  86.  
  87. //here we are adding a text field to take name as input from the user
  88. TextField jt= new TextField("Enter your nickname:");
  89. jt.setAlignment(Pos.CENTER);
  90. jt.setPrefWidth(300);
  91. jt.setLayoutX(150);
  92. jt.setLayoutY(300);
  93. //adding completed..
  94.  
  95. //<<button1 specification ends..
  96. Group layout1=new Group();
  97. layout1.getChildren().add(imageView);//here the image1 is being added to the layout.
  98. layout1.getChildren().add(btn1);//here button1 is being added to the layout.
  99. layout1.getChildren().add(jt);//here we are adding the textfield to the layout.
  100.  
  101. scn1=new Scene(layout1,600,600);//here all the components of scene1 is being integrated.
  102.  
  103.  
  104. //image2
  105. Image image1= new Image(new FileInputStream("C:\\Users\\USER\\Documents\\NetBeansProjects\\GAME1\\src\\Mymazesiva.png"));//image2 file path is given here.
  106. ImageView img = new ImageView(image1);
  107.  
  108. //adding pictures to rectangle below:
  109. FileInputStream input = new FileInputStream("C:\\Users\\USER\\Documents\\NetBeansProjects\\GAME1\\src\\labour.jpg");
  110. Image img3=new Image(input);
  111. ImagePattern image_pattern = new ImagePattern(img3, x, y,40,40, false);
  112. Rectangle rect = new Rectangle(x, y, 40, 40);
  113. rect.setFill(image_pattern);
  114.  
  115.  
  116.  
  117. //image2 specification starts:
  118. img.setFitHeight(600);
  119. img.setFitWidth(600);
  120. img.setPreserveRatio(true);
  121. //image2 specification ends.
  122.  
  123. Button btn2=new Button("switch");
  124. btn2.setOnAction(e->window.setScene(scn1));
  125. Group layout2=new Group();
  126. layout2.getChildren().add(img);
  127. layout2.getChildren().add(rect);
  128. scn=new Scene(layout2,600,600);
  129. window.setScene(scn1);
  130. window.show();
  131. //keyboard controls are added below for the main character to move left,right,up and down:
  132. scn.setOnKeyPressed(new EventHandler<javafx.scene.input.KeyEvent>(){
  133. @Override
  134. public void handle(javafx.scene.input.KeyEvent event){
  135.  
  136. if (event.getCode() == KeyCode.RIGHT) {
  137. rect.setLayoutX(rect.getLayoutX() + 10);
  138. } else if (event.getCode() == KeyCode.LEFT) {
  139. rect.setLayoutX(rect.getLayoutX() - 10);
  140. }else if(event.getCode() == KeyCode.UP){
  141. rect.setLayoutY(rect.getLayoutY() - 10);
  142. }else if(event.getCode()==KeyCode.DOWN){
  143. rect.setLayoutY(rect.getLayoutY() + 10);
  144. }
  145. }
  146. });
  147. //keyboard control is completed..
  148.  
  149. //enemy creating:
  150. Rectangle rect2 = new Rectangle();
  151. rect2.setX(30);
  152. rect2.setY(0);
  153. rect2.setWidth(50);
  154. rect2.setHeight(50);
  155. rect2.setFill(Color.BLUE);
  156. layout2.getChildren().add(rect2);
  157. Path path = new Path();
  158. path.getElements().add (new MoveTo (50,0));
  159. path.getElements().add (new LineTo(50,600));
  160.  
  161. PathTransition pathTransition = new PathTransition();
  162. pathTransition.setDuration(Duration.millis(20000));
  163. Node cbTypeCrc;
  164. pathTransition.setNode(rect2);
  165. pathTransition.setPath(path);
  166. pathTransition.setOrientation(OrientationType.NONE);
  167. pathTransition.setAutoReverse(true);
  168. pathTransition.play();
  169. }
  170.  
  171.  
  172. public static void main(String[] args) {
  173. GAME1("C:\\Users\\USER\\Documents\\NetBeansProjects\\GAME1\\src\\short_message_tone.wav");
  174. launch(args);
  175. }
  176. //adding audio to the file:
  177. public static void GAME1(String Filepath){
  178. InputStream music;
  179. try{
  180. music=new FileInputStream(new File("C:\\Users\\USER\\Documents\\NetBeansProjects\\GAME1\\src\\short_message_tone.wav"));
  181. AudioStream audios=new AudioStream(music);
  182. AudioPlayer.player.start(audios);
  183. }catch(Exception e){
  184. JOptionPane.showMessageDialog(null,"error");
  185. }
  186.  
  187. }
  188.  
  189.  
  190.  
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement