Okay so I'm trying to highlight all the nodes in a vertical column in a gridpane when I mouse over any node in the column. So right now I'm getting the columnIndex of the node my mouse is over and then creating an array of all nodes that share that column index. Return that array to the main method and then change the background color of the nodes in the array to a color.
This is the mouse over function:
for (Node node : officeHoursGridPane.getChildren()) {
node.setOnMouseEntered((MouseEvent t) -> {
node.setStyle("-fx-background-color:#f9f3c5;");
Node source = (Node)t.getSource();
Integer colIndex = GridPane.getColumnIndex(source);
Integer rowIndex = GridPane.getRowIndex(source);
//ystem.out.println("Column #: " + colIndex + "\nRow #: " + rowIndex);
for(int c = 0; c <= colIndex; c++){
Node[] colNode = getNodeByColumnIndex(colIndex, officeHoursGridPane);
int colCount=0;
for(int v = 0; v <= colNode.length; v++){
Node vertNode = colNode[v];
vertNode.setStyle("-fx-background-color:#f9f3c5;");
}
}
});
node.setOnMouseExited((MouseEvent t) -> {
node.setStyle("-fx-background-color:#ffffff;");
});
}
This is my Node[] builder:
public Node[] getNodeByColumnIndex (final int column, GridPane gridPane) {
Node[] result = null;
ObservableList<Node> childrens = gridPane.getChildren();
int count = 0;
for (Node node : childrens) {
if(GridPane.getColumnIndex(node) == column) {
result[count] = node;
count++;
if(count > column){
break;
}
}
}
return result;
}
You should find all the nodes with the same column index in the gridpane's children:
for (Node node : officeHoursGridPane.getChildren()) {
node.setOnMouseEntered(e -> officeHoursGridPane.getChildren().forEach(c -> {
Integer targetIndex = GridPane.getColumnIndex(node);
if (GridPane.getColumnIndex(c) == targetIndex) {
c.setStyle("-fx-background-color:#f9f3c5;");
}
}));
node.setOnMouseExited(e -> officeHoursGridPane.getChildren().forEach(c -> {
Integer targetIndex = GridPane.getColumnIndex(node);
if (GridPane.getColumnIndex(c) == targetIndex) {
c.setStyle("-fx-background-color:#ffffff;");
}
}));
}
Note, that in order to not highlight extra nodes you probably should also check the row index as well.
Related
Im trying to read data from an global hashtable into a linked list. I cant seem to see where I went wrong. Each time I run the program, I get a runtime error.
node *locallinkedlist = NULL;
//Reading Data From the global hashtable into a local linked list to find data using binary search
for(int p = 0; p < 25; p++)
{
for(node *c = hashtable[p]; c != NULL; c = c->next)
{
if(locallinkedlist == NULL)
{
locallinkedlist = c;
}
else
{
c = locallinkedlist;
locallinkedlist = c;
}
}
}
for(node *h = locallinkedlist; h != NULL; h=h->next)
{
printf("%s",h->employeefirstname);
}
I figured it out!
I need to create a node pointer using malloc inside the inner for loop, for each employee and store each of those employee name inside the node pointer and then put it into the list. Heres my revides code!!:
node *head = NULL;
for(int i = 0; i < 25; i++)
{
for(node *j = hashtable[i]; j != NULL; j = j->next)
{
node *m = malloc(sizeof(node));
if(m == NULL)//Checking for a valid memory address
{
return 1;
}
strcpy(m->employeefirstname,j->employeefirstname);
m->next = NULL;
if(head == NULL)
{
head = m;
}
else
{
m->next = head;
head = m;
}
}
}
for(node *c = head; c != NULL; c = c->next)
{
printf("%s\n",c->employeefirstname);
}
I use nearly the same method as in the discussion. But mine reaches the time limitation but his passes all cases. I want to know how to improve my code and why there is difference?
Here is my entire code:
boolean[] visited;
public int dfs(int step, int[] edges, int node, Map<Integer, Integer> path) {
path.put(node, step);
visited[node] = true;
if (edges[node] == -1) {
return -1;
}
if (path.containsKey(edges[node])) {
return step - path.get(edges[node]) + 1;
}
return dfs(step + 1, edges, edges[node], path);
}
public int longestCycle(int[] edges) {
int res = -1;
visited = new boolean[edges.length];
for (int i = 0; i < edges.length; i++) {
if (visited[i]) {
continue;
}
int maxCircleLength = dfs(0, edges, i, new HashMap<Integer, Integer>());
res = Math.max(maxCircleLength, res);
}
return res;
}
This is his solution:
public int longestCycle(int[] edges) {
int longest = -1;
boolean visited[] = new boolean[edges.length]; // global visisted
HashMap<Integer, Integer> map;
for(int i=0; i<edges.length; i++){
if(visited[i]) continue;
int distance = 0, curr_node = i;
map = new HashMap<>(); // local visited
while(curr_node != -1){
if(map.containsKey(curr_node)){
longest = Math.max(longest, distance - map.get(curr_node));
break;
}
if(visited[curr_node]) break;
visited[curr_node] = true;
map.put(curr_node, distance);
curr_node = edges[curr_node];
distance++;
}
}
return longest;
}
I try to create a gridPane in JavaFx with a circle in it.I want the gridPane cells to use all the available space in the gridPane.(The GridPane is in the Center of a BorderPane) but the cells keep resizing to the dimensions of the inner objects.How do I get the cells to use all space available? (and how do I set the radius of the circle to a fraction of the space available in the Center of the BorderPane.
I am quite new to JavaFx but I tried to use Columnconstraints and RowConstraints to match my need. It didn't work.I tried also to bind the size of my objects in the GridPane to use a fraction of the stage size but it does not work properly as it does not correspond to the plane in the BorderPane.
public void start(Stage primaryStage) throws Exception{
BorderPane applicationLayout = new BorderPane();
primaryStage.setTitle("Multi-level feedback simulator");
Scene scene = new Scene(applicationLayout, 600, 600);
primaryStage.setScene(scene);
//Add the menu Bar
//MainMenuBar menuBar = new MainMenuBar(primaryStage);
//applicationLayout.setTop(menuBar);
//Add the main zone of drawing
TreeDrawingZone treeDrawingZone = new TreeDrawingZone(primaryStage,applicationLayout,3,3);
applicationLayout.setCenter(treeDrawingZone);
primaryStage.show();
primaryStage.setMaximized(true);
}
The GridPane code with the constraints.
The biggest part of the constructor creates lines dans circles to display a tree.
The drawings functions are createLine() and createCircle()
public class TreeDrawingZone extends Parent {
private GridPane drawingZoneLayout;
private Stage stage;
private int columnNumber;
private int rowNumber;
private Pane rootPane;
private List<Pair<Integer,Integer>> circlePositions;
public TreeDrawingZone(Stage stage,Pane rootPane, int treeHeight, int childrenPerNode){
this.stage = stage;
drawingZoneLayout = new GridPane();
columnNumber = 2*(int)Math.pow(childrenPerNode,treeHeight-1)-1;
rowNumber = 2*treeHeight-1;
circlePositions = new ArrayList<>();
this.rootPane = rootPane;
//TODO Use the correct height of the borderLayout (maybe with a upper level layout)
System.out.println(columnNumber);
System.out.println(rowNumber);
//column Constraints
for(int i = 1 ; i <= columnNumber ; i++){
ColumnConstraints columnConstraints = new ColumnConstraints();
columnConstraints.setPercentWidth((double) 100/columnNumber);
columnConstraints.setFillWidth(true);
drawingZoneLayout.getColumnConstraints().add(columnConstraints);
}
//row Constraints
for(int i = 1 ; i <= rowNumber ; i++){
RowConstraints rowConstraints = new RowConstraints();
rowConstraints.setPercentHeight((double) 100/rowNumber);
rowConstraints.setFillHeight(true);
drawingZoneLayout.getRowConstraints().add(rowConstraints);
}
//Tree Representation
//Base Line
List<Integer> circleLineRepartition = new ArrayList<>();
for(int i = 0 ; i < columnNumber; i ++){
if(i % 2 == 0){
circleLineRepartition.add(i);
}
}
System.out.println(circleLineRepartition);
//Creation of the grid line per line
for(int i = rowNumber-1 ; i >=0 ; i-=2){
if(i % 2 == 0) {
//Case of the line with circles
for (Integer circlePosition : circleLineRepartition) {
Pane circlePane;
if (i == 0) {
circlePane = createCircle(true, false);
} else if (i == rowNumber - 1) {
circlePane = createCircle(false, true);
} else {
circlePane = createCircle();
}
drawingZoneLayout.add(circlePane, circlePosition, i);
circlePositions.add(new Pair<>(circlePosition, i));
}
List<Integer> upperCircleLineRepartition;
//Create the lines
//The following block enumerates the different cases to create the lines between the dotes
try {
upperCircleLineRepartition = getoddlyRepartedCenters(childrenPerNode, circleLineRepartition);
if (i > 0) {
int minPosition = circleLineRepartition.get(0);
int maxPosition = circleLineRepartition.get(circleLineRepartition.size() - 1);
int position = 0;
boolean drawHorizontal = true;
int linkedNodeCount = 0;
for (int j = minPosition; j <= maxPosition; j++) {
Pane linesPane;
if (j == circleLineRepartition.get(position) && minPosition != maxPosition) {
//Update the number of linked Nodes
if(drawHorizontal) {
linkedNodeCount += 1;
if(linkedNodeCount == childrenPerNode)
drawHorizontal = false;
}else{
linkedNodeCount = 1;
drawHorizontal = true;
}
//First element
if (linkedNodeCount == 1) {
if(upperCircleLineRepartition.contains(j)){
linesPane = createLines(LineDirection.NORTH,LineDirection.SOUTH,LineDirection.EAST);
}else {
linesPane = createLines(LineDirection.SOUTH, LineDirection.EAST);
}
}
//Last element
else if (linkedNodeCount == childrenPerNode) {
if(upperCircleLineRepartition.contains(j)){
linesPane = createLines(LineDirection.NORTH,LineDirection.SOUTH,LineDirection.WEST);
}else {
linesPane = createLines(LineDirection.WEST, LineDirection.SOUTH);
}
}//bridge with under and upper level
else if(upperCircleLineRepartition.contains(j)) {
linesPane = createLines(LineDirection.SOUTH, LineDirection.NORTH, LineDirection.EAST, LineDirection.WEST);
}
//other children
else{
linesPane = createLines(LineDirection.SOUTH, LineDirection.EAST, LineDirection.WEST);
}
position++;
}
//Only one child
else if (minPosition == maxPosition) {
linesPane = createLines(LineDirection.SOUTH, LineDirection.NORTH);
}
//Bridge between children
else {
if(drawHorizontal) {
if (upperCircleLineRepartition.contains(j)) {
linesPane = createLines(LineDirection.NORTH, LineDirection.EAST, LineDirection.WEST);
} else {
linesPane = createLines(LineDirection.WEST, LineDirection.EAST);
}
}else{
linesPane = createLines();
}
}
drawingZoneLayout.add(linesPane, j, i - 1);
}
}
circleLineRepartition = new ArrayList<>(upperCircleLineRepartition);
} catch (Exception e) {
System.out.println("Invalid line given");
}
}
}
drawingZoneLayout.setMaxSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
//TODO remove GridLines after debug
drawingZoneLayout.setGridLinesVisible(true);
this.getChildren().add(drawingZoneLayout);
}
private Pane createCircle(){
return createCircle(false,false);
}
private Pane createCircle(boolean isRoot, boolean isLeaf){
Pane circlePane = new Pane();
Circle circle = new Circle();
circle.centerXProperty().bind(stage.widthProperty().divide(columnNumber).divide(2));
circle.centerYProperty().bind(stage.heightProperty().divide(rowNumber).divide(2));
circle.radiusProperty().bind(Bindings.min(stage.widthProperty().divide(columnNumber).divide(2),stage.heightProperty().divide(rowNumber).divide(2)));
circlePane.getChildren().add(circle);
if(!isLeaf) {
circlePane.getChildren().add(createLines(LineDirection.SOUTH));
}
if(!isRoot){
circlePane.getChildren().add(createLines(LineDirection.NORTH));
}
return circlePane;
}
private Pane createLines(LineDirection ... directions){
Pane linesGroup = new Pane();
for(LineDirection direction : directions){
linesGroup.getChildren().add(createLine(direction));
}
return linesGroup;
}
private Line createLine(LineDirection direction){
Line line = new Line();
if(direction == LineDirection.EAST || direction == LineDirection.WEST){
line.startYProperty().bind(stage.heightProperty().divide(rowNumber).divide(2));
line.endYProperty().bind(stage.heightProperty().divide(rowNumber).divide(2));
line.startXProperty().bind(stage.widthProperty().divide(columnNumber).divide(2));
if(direction == LineDirection.EAST){
line.endXProperty().bind(stage.widthProperty().divide(columnNumber));
}
else{
line.setEndX(0);
}
}
else{
line.startXProperty().bind(stage.widthProperty().divide(columnNumber).divide(2));
line.endXProperty().bind(stage.widthProperty().divide(columnNumber).divide(2));
line.startYProperty().bind(stage.heightProperty().divide(rowNumber).divide(2));
if(direction == LineDirection.NORTH){
line.setEndY(0);
}else{
line.endYProperty().bind(stage.heightProperty().divide(rowNumber));
}
}
line.setStrokeWidth(1);
line.setFill(null);
line.setStroke(Color.BLACK);
return line;
}
private int getCenter(List<Integer> childrenNodesPosition) throws Exception {
if (childrenNodesPosition.size() == 0){
throw new Exception("Tried to get the center of an empty list");
}else{
int sum = 0;
for(int childNodePosition : childrenNodesPosition){
sum += childNodePosition;
}
return sum/childrenNodesPosition.size();
}
}
private List<Integer> getoddlyRepartedCenters(int nodeNumberPerParent, List<Integer> childrenNodesPosition) throws Exception {
int parentNumber = childrenNodesPosition.size()/nodeNumberPerParent;
int nextPosition = 0;
List<Integer> regularParentCenters = new ArrayList<>(parentNumber);
for(int i = 0 ; i < parentNumber ; i++){
regularParentCenters.add(getCenter(childrenNodesPosition.subList(nextPosition,nextPosition + nodeNumberPerParent)));
nextPosition = nextPosition + nodeNumberPerParent;
}
return regularParentCenters;
}
}
The result that I want to correct
I'm currently making a game as a school project and I'm trying to figure out the pathfinding of enemies. I've made a basic BFS which works pretty well but does not take in account obstacles, so enemies are stuck by an obstacle is there's one when trying to reach the player. I've tried different things but all I got was null pointer (which I kind of understand, but I don't know how to make this works).
public class BFS {
private Player player;
private Field field;
private Queue<Tile> queue;
private HashMap<Tile, Tile> parents;
private ArrayList<Tile> adjTiles;
public BFS(Player player, Field field) {
this.player = player;
this.field = field;
this.queue = new LinkedList<>();
this.parents = new HashMap<Tile, Tile>();
this.adjTiles = new ArrayList<>();
}
public void lancerBFS() {
int x = player.getIndiceX();
int y = player.getIndiceY();
Tile player = field.getNextTile(y, x);
this.parents.clear();
this.queue.clear();
this.adjTiles.clear();
this.queue.add(field.getNextTile(y, x));
this.parents.put(field.getNextTile(y, x), field.getNextTile(y, x));
while (!queue.isEmpty()) {
Tile temp = queue.remove();
y = temp.getI();
x = temp.getJ();
if (x > 0) {
this.adjTiles.add(field.getNextTile(y, x-1));
}
if (y > 0) {
this.adjTiles.add(field.getNextTile(y-1, x));
}
if (x < 24) {
this.adjTiles.add(field.getNextTile(y, x+1));
}
if (y < 24) {
this.adjTiles.add(field.getNextTile(y+1, x));
}
for (int i = 0 ; i < adjTiles.size() ; i++) {
if (!this.parents.containsKey(adjTiles.get(i))) {
this.parents.put(this.adjTiles.get(i), temp);
this.queue.add(this.adjTiles.get(i));
}
}
this.adjTiles.clear();
}
}
public Tile searchWay(AnimatedEntity entity) {
int x = entity.getIndiceX();
int y = entity.getIndiceY();
Tile t = this.field.getNextTile(y, x);
return this.parents.get(t);
}
public HashMap<Tile, Tile> getParents() {
return parents;
}
}
How I use it (my tiles are 32x32 on a 25x25 map, and enemies move 4 pixels by 4 pixels)
public void moveEnemy(AnimatedEntity e) {
Tile nextTile = this.bfs.searchWay(e);
Tile enemyAt = this.map.getNextTile(e.getIndiceY(), e.getIndiceX());
if (nextTile.getI() == enemyAt.getI() && nextTile.getJ() < enemyAt.getJ()) {
e.moveLeft(entities, inanimatedEntities);
}
if (nextTile.getI() < enemyAt.getI() && nextTile.getJ() == enemyAt.getJ()) {
e.moveUp(entities, inanimatedEntities);
}
if (nextTile.getI() == enemyAt.getI() && nextTile.getJ() > enemyAt.getJ()) {
e.moveRight(entities, inanimatedEntities);
}
if (nextTile.getI() > enemyAt.getI() && nextTile.getJ() == enemyAt.getJ()) {
e.moveDown(entities, inanimatedEntities);
}
}
How enemies get stuck in game:
How enemies get stuck after trying to include isObstacle notion
I build a gridpane in scenebuilder.I have a image view in every cell.I want to build a dynamic gallery pictures.I want to remove the last image in every row and add it to the first column in next row?How can I do it?I am beginer in javafx, please help me :(
Thank you
Not tested, but this should work:
// if you know how many columns you have (and their indexes) this step is unnecessary:
int minColIndex = Integer.MAX_VALUE ;
int maxColIndex = Integer.MIN_VALUE ;
for (Node node : gridPane.getChildren()) {
int colIndex = GridPane.getColumnIndex(node);
if (colIndex < minColIndex) minColIndex = colIndex ;
if (colIndex > maxColIndex) maxColIndex = colIndex ;
}
// Update row and column indexes:
for (Node node : gridPane.getChildren()) {
int colIndex = GridPane.getColumnIndex(node);
if (colIndex == maxColIndex) {
int rowIndex = GridPane.getRowIndex(node);
GridPane.setRowIndex(node, rowIndex+1);
GridPane.setColIndex(node, minColIndex);
} else {
GridPane.setColIndex(node, colIndex + 1) ;
}
}
Are you sure a TilePane wouldn't suit your needs better, though?