Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java code level: beginner write a method In Streamline Java - import javafx.scene.; import javafx.scene. shape.*; import javafx.stage.Stage; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import

java code

level: beginner

write a method

image text in transcribedimage text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

In Streamline Java -

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

import javafx.scene.; import javafx.scene. shape.*; import javafx.stage.Stage; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javafx.animation.*; import javafx.animation.PathTransition.*; import javafx.application.Application; import javafx.event.EventHandler; import javafx.event.ActionEvent; import javafx.scene.Group; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.paint.*; import javafx.util.Duration; public class Guistreamline extends Application static final double SCENE WIDTH 500; static final double SCENE-HEIGHT = 600; static final String TITLE "CSE 8b Streamline GUI"; static final String USAGE = "Usage: In" + "> java Guistreamline - to start a game with default" + - to start a game by reading g" + "> java Guistreamline to start a game by reading a"+ the specified directory and "+ " size 6 5 and random obstacles + " java GuiStreamline "ame state from the specified fileln"+ "1l game states from files inln" + "playing them in order n" static final Color TRAIL COLOR Color.PALEVIOLETRED static final Color GOAL_COLOR Color.MEDIUMAQUAMARINE; static final Color OBSTACLE-COLOR = Color. DIMGRAY; Trail radius will be set to this fraction of the size of a board square static final double TRAIL RADIUS FRACTION = 0.1 // Squares wilL be resized to this fraction of the size of a board square static final double SQUAREFRACTION # 0.8; - Scene mainScene; Group leve1Group; Group rootGroup; Player playerRect Roundedsquare goalRect; // For obstacles and trails / Parent group for everything else 7 GUI representation of the player 77 GUI representation of the goal Shape []I] grid; 7 Same dimensions as the game board Streamline game; /The current Level // Future Levels ArrayListstreamline> nextGames; MyKeyHandler mykeyHandler; / for keyboard input /returns the width of the board for the current Level public int getBoardwidth) return game.currentState.board[0].length; // the height of the board for the current Level public int getBoardHeight() return game.currentState.board.length; Find a size for a single square of the board that will fit nicely //in the current scene size. public double getsquaresize)f For example, given a scene size of 1000 by 600 and a board size of 5 by 6, we have room for each square to be 200x100. Since we want squares not rectangles, return the minimum which is 100 in this example. return 1ee; // Destroy and recreate grid and all trail and obstacle shapes. /Assumes the dimensions of the board may have changed public void resetGrid) f TODO/ / Hints: Empty out grid, trailsGroup, and obstaclesGroup. ALso makes sure grid is the right size, in case the size of the // board changed. // Sets the fill color of all trail Circles making them visible or not / depending on if that board position equals TRAIL CHAR public void updateTrailcolors) t TODO/ rooe Coverts the given board column and row into scene coordinates Gives the center of the corresponding tile. @param boardCol a board column to be converted to a scene x @param boardRow a board row to be converted to a sceney , @return scene coordinates as Length 2 array where index S X static final double MIDDLE OFFSET-0.5; public double[] boardIdxToScenePos (int boardCol, int boardRow) f double sceneX ((boardco1 + MIDDLE-OFFSET) * double sceneV = ((boardRow + MIDDLE OFFSET) * return new double[](scenex, sceneYj; (mainScene.getwidth) 1)) / getBoardwidth(): (mainScene.getHeight() 1)) / getBoardHeight); / Makes trail markers visible and changes player position. / To be called when the user moved the player and the GUI needs to be /7 updated to show the new position / Parameters are the old position, new position, and whether it was an // undo movement. public void onPlayerMoved (int fromCol, int fromRow, int toCol, int toRow, boolean isUndo) // If the position is the same, just return if (fromCol - tocol && fromRow toRow) t PT000 / To be called when a key is pressed st.setInterpolator(Interpolator.EASE_IN); // Scale enough to eventually cover the entire scene st.setByX(DOUBLE_MULTIPLIER * mainscene.getMidth() / animatedGoal.geti dth()); st.setByY (DOUBLE_MULTIPLIER* mainScene.getHeight)/ animatedGoal.getHeight()); /2 This will be called after the scale animation finishes. If there is no next Level, quit Otherwise switch to it and fade out the animated cloned goal to reveal the new Level st.setOnFinished (e1 - /T TO00 : check if there is no next game and if so, quit TODO: update the instances variables game and nextGames to switch to the next Level / Update UI to the next Level, but it won't be visible yet // because it's covered by the animated cloned goal onLevelLoaded () TODO: use a FadeTransition on animatedGoal, with FADE TIME as the duration. Use setonFinished() to schedule code to run after this animation is finished. When the animation finishes, remove animatedGoal from rootGroup. / // Start the scale animation st.play(); public class Streamline final static int DEFAULT HEIGHT6 final static int DEFAULT-WIDTH- 5; final static int DEFAULT PLAYERROW 5; final static int DEFAULT PLAYERCOL ; final static int DEFAULT-GOALROW- 0; final static int DEFAULT-GOALCOL 4; final static int DEFAULT-OBSTACLES 3; final static char SPACECHAR final static String SPACE STRING "" - final static String OUTFILE NAME saved streamline game GameState currentstate; List previousstates; public Streamline()f this.currentState new Gamestate(DEFAULT-HEGHT, DEFAULT-WIDTH, - DEFAULT PLAYERROW, DEFAULT PLAYERCOL DEFAULT GOALROW, DEFAULT GOALCOL); currentstate.addRandomObstacles (DEFAULT_OBSTACLES); this.previousStates new ArrayList); public Streamline (String filename) ( try loadFromFile(filename); ) catch (IOException e) f e.printstackTrace() protected void loadFromFile(String filename) throws IOException t Scanner scannernew Scanner(new File(filename)); int height scanner.nextInt); int width scanner.nextInt); int playerRold scanner.nextInt(); int playerco1 scanner.nextInt(); int goalRow Scanner.nextInt(): int goalcol # scanner.nextInt(); Gamestate gameLoad new GameState (height, width, playerRow, playerCol, goalRow, goalCol) string emptyString = scanner.nextLine(); while (row e) t for (int colone = 0; colone O; //Check if gameover if (goalRow player Row && goalcol -playerCol)f currentState.levelPassed - true; return; void recordAndMove (Direction direction) [ if (direction null)f return int current_size - previousStates.size); //checks is the list previousStates is empty and updates it if (previousStates.isEmpty)) GameState temp new GameState(currentState); previousStates.add(temp); currentState.move (direction); //checks if the currentstate is different from previousStates Last state else if (! (currentstate.equals(previousstates.get(current_size 1)))) GameState tempnew GameState (currentState); previousStates.add (temp) currentState.move (direction); //If the next move does not change the state, remove the Last state if (currentstate.equals(temp))f previousStates.remove (previousStates.size()-1); //if currentState similar to the Last state, then moves else currentState.move (direction); This mehtod undos the previous movement, and goes back to Last game state then updates the field to that previous movemnet. void undo) if (previousStates.isEmpty ()) ( return this.previousStates previousStates; this.currentState currentState; void play() while(currentstate.levelPassed !-true) System.out.println(currentstate.toString ()); System.out.print(">) Scanner theUser new Scanner (System.in); string theinput theUser.nextLine(); if ("w".equals (theInput)) t recordAndMove (Direction.UP); else if("a".equals (theInput)) f recordAndMove (Direction. LEFT); else if ("s".equals (theInput)) t recordAndMove (Direction. DOWN); else if ("d".equals (theInput)) t recordAndMove (Direction. RIGHT) else if ("u".equals (theInput)) undo (); else if ("o".equals (theInput)) t this.saveToFile() else if ("q".equals (theInput)) [ return; else System.out.println("Wrong Input, Try Again."); System.out.println(currentstate.tostring)); if (currentstate.levelPassed true) System.out.println("Level Passed!"); return void saveToFile) f try t PrintWriter saveFile new PrintWriter(OUTFILE NAME); String stringone currentstate.board.length SPACE_STRING + currentstate.board[e].length; saveFile.println(stringone); String stringTwo currentState.playerRow SPACE _STRING + currentstate.playerCol; saveFile.println(stringTwo); String stringThree currentState.goalRow SPACE_STRING currentstate.goalcol; saveFile.println (stringThree); for Loop the currentState's board and then print for (int i-e; icurrentState.board.length; H) { for (int j ; jcurrentState.board[e].1ength; j++) { if (currentState.board[i][j]SPACE CHAR){ saveFile.print (SPACE STRING); else ( saveFile.print (currentState.board[i][j]); saveFile.println); saveFile.close(): System.out.println("Saved current state to: saved streamline_game"); throw new IOException ): catch (IOException e) f e.printstackTrace (); import javafx.scene.; import javafx.scene. shape.*; import javafx.stage.Stage; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javafx.animation.*; import javafx.animation.PathTransition.*; import javafx.application.Application; import javafx.event.EventHandler; import javafx.event.ActionEvent; import javafx.scene.Group; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.paint.*; import javafx.util.Duration; public class Guistreamline extends Application static final double SCENE WIDTH 500; static final double SCENE-HEIGHT = 600; static final String TITLE "CSE 8b Streamline GUI"; static final String USAGE = "Usage: In" + "> java Guistreamline - to start a game with default" + - to start a game by reading g" + "> java Guistreamline to start a game by reading a"+ the specified directory and "+ " size 6 5 and random obstacles + " java GuiStreamline "ame state from the specified fileln"+ "1l game states from files inln" + "playing them in order n" static final Color TRAIL COLOR Color.PALEVIOLETRED static final Color GOAL_COLOR Color.MEDIUMAQUAMARINE; static final Color OBSTACLE-COLOR = Color. DIMGRAY; Trail radius will be set to this fraction of the size of a board square static final double TRAIL RADIUS FRACTION = 0.1 // Squares wilL be resized to this fraction of the size of a board square static final double SQUAREFRACTION # 0.8; - Scene mainScene; Group leve1Group; Group rootGroup; Player playerRect Roundedsquare goalRect; // For obstacles and trails / Parent group for everything else 7 GUI representation of the player 77 GUI representation of the goal Shape []I] grid; 7 Same dimensions as the game board Streamline game; /The current Level // Future Levels ArrayListstreamline> nextGames; MyKeyHandler mykeyHandler; / for keyboard input /returns the width of the board for the current Level public int getBoardwidth) return game.currentState.board[0].length; // the height of the board for the current Level public int getBoardHeight() return game.currentState.board.length; Find a size for a single square of the board that will fit nicely //in the current scene size. public double getsquaresize)f For example, given a scene size of 1000 by 600 and a board size of 5 by 6, we have room for each square to be 200x100. Since we want squares not rectangles, return the minimum which is 100 in this example. return 1ee; // Destroy and recreate grid and all trail and obstacle shapes. /Assumes the dimensions of the board may have changed public void resetGrid) f TODO/ / Hints: Empty out grid, trailsGroup, and obstaclesGroup. ALso makes sure grid is the right size, in case the size of the // board changed. // Sets the fill color of all trail Circles making them visible or not / depending on if that board position equals TRAIL CHAR public void updateTrailcolors) t TODO/ rooe Coverts the given board column and row into scene coordinates Gives the center of the corresponding tile. @param boardCol a board column to be converted to a scene x @param boardRow a board row to be converted to a sceney , @return scene coordinates as Length 2 array where index S X static final double MIDDLE OFFSET-0.5; public double[] boardIdxToScenePos (int boardCol, int boardRow) f double sceneX ((boardco1 + MIDDLE-OFFSET) * double sceneV = ((boardRow + MIDDLE OFFSET) * return new double[](scenex, sceneYj; (mainScene.getwidth) 1)) / getBoardwidth(): (mainScene.getHeight() 1)) / getBoardHeight); / Makes trail markers visible and changes player position. / To be called when the user moved the player and the GUI needs to be /7 updated to show the new position / Parameters are the old position, new position, and whether it was an // undo movement. public void onPlayerMoved (int fromCol, int fromRow, int toCol, int toRow, boolean isUndo) // If the position is the same, just return if (fromCol - tocol && fromRow toRow) t PT000 / To be called when a key is pressed st.setInterpolator(Interpolator.EASE_IN); // Scale enough to eventually cover the entire scene st.setByX(DOUBLE_MULTIPLIER * mainscene.getMidth() / animatedGoal.geti dth()); st.setByY (DOUBLE_MULTIPLIER* mainScene.getHeight)/ animatedGoal.getHeight()); /2 This will be called after the scale animation finishes. If there is no next Level, quit Otherwise switch to it and fade out the animated cloned goal to reveal the new Level st.setOnFinished (e1 - /T TO00 : check if there is no next game and if so, quit TODO: update the instances variables game and nextGames to switch to the next Level / Update UI to the next Level, but it won't be visible yet // because it's covered by the animated cloned goal onLevelLoaded () TODO: use a FadeTransition on animatedGoal, with FADE TIME as the duration. Use setonFinished() to schedule code to run after this animation is finished. When the animation finishes, remove animatedGoal from rootGroup. / // Start the scale animation st.play(); public class Streamline final static int DEFAULT HEIGHT6 final static int DEFAULT-WIDTH- 5; final static int DEFAULT PLAYERROW 5; final static int DEFAULT PLAYERCOL ; final static int DEFAULT-GOALROW- 0; final static int DEFAULT-GOALCOL 4; final static int DEFAULT-OBSTACLES 3; final static char SPACECHAR final static String SPACE STRING "" - final static String OUTFILE NAME saved streamline game GameState currentstate; List previousstates; public Streamline()f this.currentState new Gamestate(DEFAULT-HEGHT, DEFAULT-WIDTH, - DEFAULT PLAYERROW, DEFAULT PLAYERCOL DEFAULT GOALROW, DEFAULT GOALCOL); currentstate.addRandomObstacles (DEFAULT_OBSTACLES); this.previousStates new ArrayList); public Streamline (String filename) ( try loadFromFile(filename); ) catch (IOException e) f e.printstackTrace() protected void loadFromFile(String filename) throws IOException t Scanner scannernew Scanner(new File(filename)); int height scanner.nextInt); int width scanner.nextInt); int playerRold scanner.nextInt(); int playerco1 scanner.nextInt(); int goalRow Scanner.nextInt(): int goalcol # scanner.nextInt(); Gamestate gameLoad new GameState (height, width, playerRow, playerCol, goalRow, goalCol) string emptyString = scanner.nextLine(); while (row e) t for (int colone = 0; colone O; //Check if gameover if (goalRow player Row && goalcol -playerCol)f currentState.levelPassed - true; return; void recordAndMove (Direction direction) [ if (direction null)f return int current_size - previousStates.size); //checks is the list previousStates is empty and updates it if (previousStates.isEmpty)) GameState temp new GameState(currentState); previousStates.add(temp); currentState.move (direction); //checks if the currentstate is different from previousStates Last state else if (! (currentstate.equals(previousstates.get(current_size 1)))) GameState tempnew GameState (currentState); previousStates.add (temp) currentState.move (direction); //If the next move does not change the state, remove the Last state if (currentstate.equals(temp))f previousStates.remove (previousStates.size()-1); //if currentState similar to the Last state, then moves else currentState.move (direction); This mehtod undos the previous movement, and goes back to Last game state then updates the field to that previous movemnet. void undo) if (previousStates.isEmpty ()) ( return this.previousStates previousStates; this.currentState currentState; void play() while(currentstate.levelPassed !-true) System.out.println(currentstate.toString ()); System.out.print(">) Scanner theUser new Scanner (System.in); string theinput theUser.nextLine(); if ("w".equals (theInput)) t recordAndMove (Direction.UP); else if("a".equals (theInput)) f recordAndMove (Direction. LEFT); else if ("s".equals (theInput)) t recordAndMove (Direction. DOWN); else if ("d".equals (theInput)) t recordAndMove (Direction. RIGHT) else if ("u".equals (theInput)) undo (); else if ("o".equals (theInput)) t this.saveToFile() else if ("q".equals (theInput)) [ return; else System.out.println("Wrong Input, Try Again."); System.out.println(currentstate.tostring)); if (currentstate.levelPassed true) System.out.println("Level Passed!"); return void saveToFile) f try t PrintWriter saveFile new PrintWriter(OUTFILE NAME); String stringone currentstate.board.length SPACE_STRING + currentstate.board[e].length; saveFile.println(stringone); String stringTwo currentState.playerRow SPACE _STRING + currentstate.playerCol; saveFile.println(stringTwo); String stringThree currentState.goalRow SPACE_STRING currentstate.goalcol; saveFile.println (stringThree); for Loop the currentState's board and then print for (int i-e; icurrentState.board.length; H) { for (int j ; jcurrentState.board[e].1ength; j++) { if (currentState.board[i][j]SPACE CHAR){ saveFile.print (SPACE STRING); else ( saveFile.print (currentState.board[i][j]); saveFile.println); saveFile.close(): System.out.println("Saved current state to: saved streamline_game"); throw new IOException ): catch (IOException e) f e.printstackTrace ()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Next Generation Databases NoSQLand Big Data

Authors: Guy Harrison

1st Edition

1484213300, 978-1484213308

More Books

Students also viewed these Databases questions

Question

Do all companies need a cash budget? Why or why not? Provide short

Answered: 1 week ago

Question

=+10. What is the brand's character or personality?

Answered: 1 week ago