you will simulate the motion of a fly inside a bounded domain (circle or rectang
ID: 3761304 • Letter: Y
Question
you will simulate the motion of a fly inside a bounded domain (circle or rectangle). We will assume that the fly is moving in a grid (See figure below) of nodes. The nodes are potential locations of the fly. The x and y location of the nodes are integers. The idea is to move the fly from one node to another node by obeying a set of rules. Quick Summary of Main Classes: (you can create additional java classes if you need to) 1) Generate a network of nodes of a given size determined either by user input (Rows, Columns for Rectangle or radius for a circle arena). a. The network is a grid of nodes with each neighbor node connected to four neighbors (undirected network). There are no links diagonally. You only need the nodes which are inside the boundary. For example, you only need the nodes which are <= radius away from the center of the circle. b. For each node, you need to track Node Visitation Increments each time it is visited by the fly. c. It will be helpful if you have another class called “Node” and then Network contains objects of the class “Node”. 2) Generate a fly with characteristics determined by the following parameters. a. Visual field determines the total angular field of nodes that can be observed by the fly. Half of the field is to the left of the fly and half to the right. For example, a 90 degree field consists of 45 degrees to the right and left. CS 3300 Fall 2015 b. The Sight Distance defines the radius of a circle of nodes that the fly can see. This circle is subsequently modified if there is a constrained visual field. In essence, this is how far the fly can see. c. The Random Turn Angle defines the first fly behavior. When no target nodes are available (because the fly is at a wall or stuck in a corner) then the fly executes a random turn (randomly chosen as a right or left turn) of random degrees (0,RandomTurnAngle). The fly then executes this turn (only changing the facing direction, not the current node position) and then searches for candidate target nodes again. d. The Facing Direction is the starting angle of the fly from (-180, 180] where 0 degrees is due east. 3) The Experiment (make this a graphical interface) which allows the user to choose what kind of bounded domain to use (circle or rectangle), parameters associated with the bounded domain (such as radius for the circle), number of iterations, name of the simulation, and fly’s parameters. Once the grid is created and the fly placed on that grid in its starting grid position and facing direction, the following occurs for each iteration: 1) A set of candidate target points is determined based on the fly’s sight distance and its Visual Field. This is done by creating a circle of nodes with radius equal to the fly’s sight distance that is centered on the fly and then trimming and transforming (rotating and translating to take into account the fly’s facing distance and current position) these nodes to include only those in the visual field. 2) These candidate target nodes are then examined in the context of the network (Environment) to exclude those that are not appropriate, such as if the target node is a boundary, is unreachable (e.g., behind a wall), or is outside the network. 3) If there are no candidate target nodes remaining then the fly executes a random turn and we go to step #1 again. 4) Once the target set of possible destination nodes has been determined then a series of steps are taken: a. The fly then chooses its target point from the set of candidate destination nodes at random. b. The fly then moves to the new target node, and its current position is updated. c. The new node’s value is incremented according to the appropriate parameter. d. The fly’s new facing direction is calculated using the angle generated between the origin and target nodes. 5) The iteration counter is incremented CS 3300 Fall 2015 6) The sequence starts again at Step #1. When the simulation is done, the trajectory history (x, y coordinates at each iteration) of the fly should be saved as a txt file, the number of visitations at each node in the environment should also be saved in a text file. The interface displays “simulation done”. A General Graphical interface is given BELOW How to Submit the Project: In a text file, write down the names of the members of the team. Compress the the .java files and the text file in one single zipped file and upload it to blackboard.
Explanation / Answer
package flysimulate1;
import java.util.Scanner; // to cater for user input
import java.awt.Graphics.*; // to cater for drawline etc
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javafx.application.Application;
import javafx.animation.Animation;
import javafx.animation.*;
import javafx.geometry.BoundingBox;
import javafx.geometry.Point2D;
import javafx.application.*;
import javafx.scene.paint.Color;
class FlySimu1 extends Application {
@Override
public void start(Stage FlyStage1) throws Exception {
FlyStage1.setTitle(" Fly Simulation using java Swing of Java FX") ;
Group root=new Group();
Scene FlySimuScene1=new Scene(root,120,170, Color.BLUE);
// statrt a scene of fly simulation in a blue color
root.getChildren().add(new Text(FlySimuScene1.getWidth()/2, FlySimuScene.getHeight()/2, "Simulating a Fly's Flying"));
root.getChildren().add(new Line(1,4, FlySimuScene.getWidth(), 4));
root.getChildren().add(new Line(4, 1,4, FlySimuScene.getWidth() ));
FlyStage1.setScene(FlySimuScene1);
FlyStage1.show();
} // end start method / function
} // end class FlySimu1
class Fly{
String VisualField; // total angular field of nodes observable by the fly
String leftHalf, rightHalf;
double startDistance; // how many nodes that the fly can see
double randomTurnAngle; // define the fly behaviour
double facingDirection;
void simulateFlyMotion() {
// fly moves in grid
int xLocationOfNode=1, yLocationOfNode=1;
int flyRule;
// generate the moving rules in a function
} // end class simulate Fly motion
void trackNodeVisitation(int length, int width) { // rectangular Grid
int alreadyVisitedNodes;
int nodesYetToVisit;
int i, j;
int [][] allNodes = new int[length][width]; // declare the rectangle grid
for ( i = 1; i <= length; i++)
for (j = 1; j <= width; j++)
allNodes[i][j] = i+j;
int flagVisted; // = 1 if visited , = 0 if not visited
for ( i = 1; i <= length; i++)
for (j = 1; j <= width; j++) {
if ( flagVisited == 1)
alreadyVisitedNodes = allNodes[i][j];
else
nodesYetToVisit = allNodes[i][j];
} // end for inner loop
} // end function track Nodes visited
} // end class Fly
class Node{
} // end class Node
class generateNodesNetwork{
int size;
String nodeShape; // in our case it is Rectangle
int nodeLength, nodeWidth;
int gridHorizontalLink;
int gridVerticalLink;
void network() {
Node objNode1;
Node objNode2;
} // end function network
} // end class gen nw nodes
public class FlySimulate1 extends JApplet {
public void
private final int JFXPANEL_WIDTH_INT = 300;
private final int JFXPANEL_HEIGHT_INT = 250;
private JFXPanel fxContainer;
public void main(String[] args) { // simulate the fly java fx Swing program with graphics
// we will use a rectangular grid of dimension length by width - the grid parameters are receivable from the user
// methods to use for graphics control
// setFont: Gothic, Times New Roman, Calibri, etc
// setColor : to choose whether blue, yellow or pink, rose, grey, red etc
// graphicsClassOrObject.drawString("Fly Simulation in a rectangle", 20, 15);
// will draw the text string as a graphics entity
// graphicsClassOrObject.drawImage takes the following parameters
// param1: Image
// param: start location for X, start Y, width, height,
// param: start X, start Y, imageWidth, imageHeight,
System.out.println(" Testing Fly Simulation 1");
// get rectangular grid dimensions from the user
Scanner scan = new Scanner(System.in);
int length = scan.nextInt();
int width = scan.nextInt();
System.out.println(" Creating a grid of length: " + length + " by width: " + width);
// display the graphical grid of the dimensions length by width
//Graphics.drawLine(10,75, length, width);
Graphics.class.getMethods();
//Graphics.drawLine(11,76, length, width);
//.drawLine(11,76, length, width);
// before this graphics control takes precedence due to override, we can execute commands statements listed above
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) { // end try
} // end catch
//Stage flyStage1;
JFrame frame = new JFrame("JavaFX 2 in Swing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new FlySimulate1();
applet.init();
frame.setContentPane(applet.getContentPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
applet.start();
}
});
}
@Override
public void init() {
fxContainer = new JFXPanel();
fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT));
add(fxContainer, BorderLayout.CENTER);
// create JavaFX scene
Platform.runLater(new Runnable() {
@Override
public void run() {
createScene();
}
});
}
private void createScene() {
Button btn = new Button();
btn.setText("Say ' Welcome to the World of Fly Simulation '");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Welcome to your one stop solution for all Fly Simulation needs !");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
fxContainer.setScene(new Scene(root));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.