honeycomb layout in javafx (with flowpane?) - java

I'm trying to make a honeycomb flow with buttons in JavaFX with so far a FlowPane
So far I got it half working by using a negative VGap on my FlowPane, but as soon as I resize it and make the 3-4 row go to 3-3-1 it obviously goes wrong
I'm trying to find a solution that will keep the honeycomb layout with variable amounts of buttons, but so far I havent had much success. So I was wondering if anyone knows a solution for this.
edit: here is the basic code I have at the moment
FlowPane root = new FlowPane();
root.setHgap(3.0); root.setVgap(-23.0);
root.setAlignment(Pos.CENTER);
Button[] array = new Button[7];
for(int i = 0; i <= 6; i++){
Button button = new Button();
button.setShape(polygon); (made a polygon in the same of a hexagon)
array[i] = button;
}
root.getChildren().addAll(array);

Predetermined columns/ rows
This is a bit more convenient than using a FlowPane to place the fields.
You can observe that using column spans you can place the Buttons in a GridPane: Every field fills 2 columns of sqrt(3/4) times the field height; the odd/even rows start at column 0/1 respectively. Every field fills 3 rows and the size of the column constraints alternate between one quarter and one half of the field height.
Example
public static GridPane createHoneyComb(int rows, int columns, double size) {
double[] points = new double[12];
for (int i = 0; i < 12; i += 2) {
double angle = Math.PI * (0.5 + i / 6d);
points[i] = Math.cos(angle);
points[i + 1] = Math.sin(angle);
}
Polygon polygon = new Polygon(points);
GridPane result = new GridPane();
RowConstraints rc1 = new RowConstraints(size / 4);
rc1.setFillHeight(true);
RowConstraints rc2 = new RowConstraints(size / 2);
rc2.setFillHeight(true);
double width = Math.sqrt(0.75) * size;
ColumnConstraints cc = new ColumnConstraints(width/2);
cc.setFillWidth(true);
for (int i = 0; i < columns; i++) {
result.getColumnConstraints().addAll(cc, cc);
}
for (int r = 0; r < rows; r++) {
result.getRowConstraints().addAll(rc1, rc2);
int offset = r % 2;
int count = columns - offset;
for (int c = 0; c < count; c++) {
Button b = new Button();
b.setPrefSize(width, size);
b.setShape(polygon);
result.add(b, 2 * c + offset, 2 * r, 2, 3);
}
}
result.getRowConstraints().add(rc1);
return result;
}
FlowPane-like behavior
Making the x position depend on the row the child is added is not a good idea in a FlowPane. Instead I recommend extending Pane and overriding layoutChildren method place the children at custom positions.
In your case the following class could be used:
public class OffsetPane extends Pane {
public interface PositionFunction {
public Point2D getNextPosition(int index, double x, double y, double width, double height);
}
private static final PositionFunction DEFAULT_FUNCTION = new PositionFunction() {
#Override
public Point2D getNextPosition(int index, double x, double y, double width, double height) {
return new Point2D(x, y);
}
};
private final ObjectProperty<PositionFunction> hPositionFunction;
private final ObjectProperty<PositionFunction> vPositionFunction;
private ObjectProperty<PositionFunction> createPosProperty(String name) {
return new SimpleObjectProperty<PositionFunction>(this, name, DEFAULT_FUNCTION) {
#Override
public void set(PositionFunction newValue) {
if (newValue == null) {
throw new IllegalArgumentException();
} else if (get() != newValue) {
super.set(newValue);
requestLayout();
}
}
};
}
public OffsetPane() {
this.hPositionFunction = createPosProperty("hPositionFunction");
this.vPositionFunction = createPosProperty("vPositionFunction");
}
#Override
protected void layoutChildren() {
super.layoutChildren();
double width = getWidth();
List<Node> children = getManagedChildren();
final int childSize = children.size();
if (childSize > 0) {
int row = 0;
Node lastRowStart = children.get(0);
Node lastNode = lastRowStart;
lastRowStart.relocate(0, 0);
PositionFunction hFunc = getHPositionFunction();
PositionFunction vFunc = getVPositionFunction();
int index = 1;
int columnIndex = 0;
while (index < childSize) {
Node child = children.get(index);
Bounds lastBounds = lastNode.getLayoutBounds();
Bounds bounds = child.getLayoutBounds();
Point2D pt = hFunc.getNextPosition(columnIndex, lastNode.getLayoutX(), lastNode.getLayoutY(), lastBounds.getWidth(), lastBounds.getHeight());
if (pt.getX() + bounds.getWidth() > width) {
// break row
lastBounds = lastRowStart.getLayoutBounds();
pt = vFunc.getNextPosition(row, lastRowStart.getLayoutX(), lastRowStart.getLayoutY(), lastBounds.getWidth(), lastBounds.getHeight());
child.relocate(pt.getX(), pt.getY());
lastRowStart = child;
row++;
columnIndex = 0;
} else {
child.relocate(pt.getX(), pt.getY());
columnIndex++;
}
lastNode = child;
index++;
}
}
}
public final PositionFunction getHPositionFunction() {
return this.hPositionFunction.get();
}
public final void setHPositionFunction(PositionFunction value) {
this.hPositionFunction.set(value);
}
public final ObjectProperty<PositionFunction> hPositionFunctionProperty() {
return this.hPositionFunction;
}
public final PositionFunction getVPositionFunction() {
return this.vPositionFunction.get();
}
public final void setVPositionFunction(PositionFunction value) {
this.vPositionFunction.set(value);
}
public final ObjectProperty<PositionFunction> vPositionFunctionProperty() {
return this.vPositionFunction;
}
}
double[] points = new double[12];
for (int i = 0; i < 12; i += 2) {
double angle = Math.PI * (0.5 + i / 6d);
points[i] = Math.cos(angle);
points[i + 1] = Math.sin(angle);
}
Polygon polygon = new Polygon(points);
OffsetPane op = new OffsetPane();
double fieldHeight = 100;
double fieldWidth = Math.sqrt(0.75) * fieldHeight;
for (int i = 0; i < 23; i++) {
Button button = new Button();
button.setShape(polygon);
button.setPrefSize(fieldWidth, fieldHeight);
op.getChildren().add(button);
}
// horizontal placement just right of the last element
op.setHPositionFunction((int index, double x, double y, double width, double height) -> new Point2D(x + width, y));
// vertical position half the size left/right depending on index and
// 1/4 the node height above the bottom of the last node
op.setVPositionFunction((int index, double x, double y, double width, double height) -> new Point2D(x + (index % 2 == 0 ? width : -width) / 2, y + height * 0.75));

Related

Displaying text in an object seems to break new text being generated

I am trying to create a program that renders blocks of text as moving object to be used in Box2D. You start by typing some words, the words themselves scale with the amount of characters. Then, when pressing enter, the program separates the words and put each of them inside an Array List. It's all good after the first enter, however when typing text after that, the text doesn't function as intended. It's all wonky. But when I remove the part that displays the text inside the TEMPTEXT class, and only displays the rectangle, it all works perfectly.
Here is my code:
PFont xanhMono;
String quote = "";
String tempQuote;
ArrayList<TEMPTEXT> box;
float tw;
float ts=200;
float qtX, qtY, qtW, qtH;
int qtStart, qtEnd, qtNum;
void setup() {
size(800, 800);
pixelDensity(displayDensity());
xanhMono = createFont("XanhMono-Regular.ttf", 1000);
box = new ArrayList<TEMPTEXT>();
}
void draw() {
background(240);
textFont(xanhMono);
textSize(int(ts));
textLeading(int(ts));
textAlign(LEFT, TOP);
fill(0);
text(quote, 0, 0, width+50, height);
if (frameCount%60<30) {
float twq = textWidth(quote);
line(twq, 0, twq, ts);
}
for (TEMPTEXT txt : box) {
txt.display();
}
}
class TEMPTEXT{
float x;
float y;
float speed;
float gravity;
float h;
float w;
String q;
TEMPTEXT(float tempX, float tempY, float tempW, float tempH, String tempQ){
x = tempX;
y = tempY;
w = tempW;
h = tempH;
q = tempQ;
speed = 0;
gravity = 0.1;
}
void display(){
noFill();
stroke(2);
rect(x, y, w, h);
//if i delete the next 3 lines, everything is all good
textSize(h);
textAlign(LEFT,TOP);
text(q, x, y);
}
}
void keyTyped() {
if (key == '\n' ) {
quote = trim(quote);
qtStart = 0;
qtEnd = 0;
qtNum = 0;
qtX = 0;
qtY = 0;
qtW = 0;
tempQuote = "";
for (int i = 0; i < quote.length(); i++) {
if (quote.charAt(i)==' ') {
qtEnd = i;
tempQuote = quote.substring(qtStart, qtEnd);
qtW = textWidth(tempQuote);
qtH = ts;
qtX = ts/2*qtStart;
TEMPTEXT p = new TEMPTEXT(qtX, qtY, qtW, qtH, tempQuote);
box.add(p);
qtNum++;
qtStart = i+1;
} else if (i==quote.length()-1) {
tempQuote = quote.substring(qtStart, quote.length());
qtW = textWidth(tempQuote);
qtH = ts;
qtX=ts/2*qtStart;
TEMPTEXT p = new TEMPTEXT(qtX, qtY, qtW, qtH, tempQuote);
box.add(p);
}
}
quote = "";
} else if (key == BACKSPACE) {
if (quote.length() > 0) {
quote = quote.substring(0, quote.length()-1);
}
} else {
quote=quote+key;
}
tw=textWidth(quote);
if (tw!=0) {
ts*=width/tw;
if (ts>=height) {
ts=height/1.5;
}
}
}

How to output text with a justify alignment

I am trying to create a component for libgdx which can output text in a manner equal to what html would do with a <p align=”justify”> ... </p>.
My idea was to add some custom component which can achieve this by adjusting the relative x and y coordinations of the components.
import java.util.ArrayList;
import java.util.List;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup;
import com.badlogic.gdx.utils.Align;
public class Paragraph extends WidgetGroup {
private Label space;
public Paragraph(String text, float width, LabelStyle style) {
super();
setWidth(width);
this.space = new Label(" ", style);
this.space.pack();
String[] words = text.split(" ");
for (String word : words) {
Label label = new Label(word, style);
label.pack();
addActor(label);
}
}
public void layout () {
float size = 0;
List<Actor> elements = new ArrayList<Actor>();
float x = getX(Align.topLeft);
float y = getY(Align.topLeft);
for (Actor actor : this.getChildren().items) {
if (actor != null) {
if (elements.isEmpty()) {
elements.add(actor);
size = actor.getWidth();
} else {
if (size + space.getWidth() + actor.getWidth() <= this.getWidth()) {
elements.add(actor);
size += (space.getWidth() + actor.getWidth());
} else {
float spacing = space.getWidth() + ((getWidth() - size) / elements.size());
Actor element = elements.get(0);
element.setPosition(x, y, Align.topLeft);
x += element.getWidth();
for (int i = 1; i < elements.size(); i++) {
element = elements.get(i);
element.setPosition(x + spacing, y, Align.topLeft);
x += (spacing + element.getWidth());
}
// new line
elements.clear();
x = getX(Align.topLeft);
y += (this.space.getHeight() * 1.5);
elements.add(actor);
size = actor.getWidth();
}
}
}
}
if (elements.isEmpty() == false) {
float spacing = space.getWidth();
Actor element = elements.get(0);
element.setPosition(x, y, Align.topLeft);
x += element.getWidth();
for (int i = 1; i < elements.size(); i++) {
element = elements.get(i);
element.setPosition(x + spacing, y, Align.topLeft);
x += (spacing + element.getWidth());
}
}
}
}
My problem is that the x and y coordinate I retrieve with getX(Align.topLeft) & getY(Align.topLeft) always returns (0,0)
instead of the real coordinates on the stage.
Component structure looks like this:
Stage
+ Container
+ Table
+ ScrollPane
+ Table
+ Table
+ Paragraph
+ Paragraph
+ Paragraph
+ Paragraph
+ Image
So the end result is that all the text contained in the different paragraphs is drawn on top of each other. Not on position (0,0), but on the same position inside of the surrounding table.
I figured it out myself.
Seems it was a bad idea to override the layout method for that.
I now call a different method from the constructor.
Also I messed up with the x and y coordinates, so I had to rewrite that method.
Ps.:
I adjusted the question as well, as I figured out the solution, it was
not really about the x and y coodinates of the widget group.
import java.util.ArrayList;
import java.util.List;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup;
import com.badlogic.gdx.utils.Align;
public class Paragraph extends WidgetGroup {
private static final class RowData {
final List<Actor> elements;
final float size;
final boolean lastRow;
RowData(List<Actor> elements, float size, boolean lastRow) {
this.elements = elements;
this.size = size;
this.lastRow = lastRow;
}
static RowData createNewRow(List<Actor> elements, float size) {
return new RowData(elements, size, false);
}
static RowData createLastRow(List<Actor> elements, float size) {
return new RowData(elements, size, true);
}
}
private Label space;
public Paragraph(String text, float width, LabelStyle style) {
super();
setWidth(width);
this.space = new Label(" ", style);
this.space.pack();
String[] words = text.split(" ");
for (String word : words) {
Label label = new Label(word, style);
label.pack();
addActor(label);
}
arrangeActors();
}
private void arrangeActors() {
float size = 0;
float height = space.getHeight();
List<RowData> rows = new ArrayList<RowData>();
List<Actor> elements = new ArrayList<Actor>();
Actor[] actors = this.getChildren().begin();
for (Actor actor : actors) {
if (actor != null) {
if (elements.isEmpty()) {
elements.add(actor);
size = actor.getWidth();
} else if (size + space.getWidth() + actor.getWidth() <= this.getWidth()) {
elements.add(actor);
size += (space.getWidth() + actor.getWidth());
} else {
rows.add(RowData.createNewRow(elements, size));
elements = new ArrayList<Actor>();
height += space.getHeight();
elements.add(actor);
size = actor.getWidth();
}
}
}
this.getChildren().end();
rows.add(RowData.createLastRow(elements, size));
height += space.getHeight();
setHeight(height);
float y = height;
for (RowData data : rows) {
float spacing = space.getWidth();
if (data.lastRow == false) {
spacing += ((getWidth() - data.size) / data.elements.size());
}
Actor element = data.elements.get(0);
element.setPosition(0, y, Align.topLeft);
float x = element.getWidth();
for (int i = 1; i < data.elements.size(); i++) {
element = data.elements.get(i);
element.setPosition(x + spacing, y, Align.topLeft);
x += (spacing + element.getWidth());
}
y -= this.space.getHeight();
}
}
#Override
public float getPrefWidth () {
return getWidth();
}
#Override
public float getPrefHeight () {
return getHeight();
}
}

How to more realistically simulate light on a sphere?

I am attempting to simulate a sphere, and shade it realistically given an origin vector for the light, and the sphere being centered around the origin. Moreover, the light's vector is the normal vector on a larger invisible sphere at a chosen point. The sphere looks off.
https://imgur.com/a/IDIwQQF
The problem, is that it is very difficult to bug fix this kind of program. Especially considering that I know how I want it to look in my head, but when looking at the numbers in my program there is very little meaning attached to them.
Since I don't know where the issue is, I'm forced to paste all of it here.
public class SphereDrawing extends JPanel {
private static final long serialVersionUID = 1L;
private static final int ADJ = 320;
private static final double LIGHT_SPHERE_RADIUS = 5;
private static final double LIGHT_X = 3;
private static final double LIGHT_Y = 4;
private static final double LIGHT_Z = 0;
private static final double DRAWN_SPHERE_RADIUS = 1;
private static final int POINT_COUNT = 1000000;
private static Coord[] points;
private static final double SCALE = 200;
public SphereDrawing() {
setPreferredSize(new Dimension(640, 640));
setBackground(Color.white);
points = new Coord[POINT_COUNT];
initializePoints();
for (int i = 0; i < points.length; i++) {
points[i].scale();
}
new Timer(17, (ActionEvent e) -> {
repaint();
}).start();
}
public void initializePoints() { //finding the points on the surface of the sphere (hopefully somewhat equidistant)
double random = Math.random() * (double)POINT_COUNT;
double offset = 2/(double)POINT_COUNT;
double increment = Math.PI * (3 - Math.sqrt(5));
for (int i = 0; i < POINT_COUNT; i++) {
double y = ((i * offset) - 1) + (offset / 2);
double r = Math.sqrt(1 - Math.pow(y, 2));
double phi = ((i + random) % (double)POINT_COUNT) * increment;
double x = Math.cos(phi) * r;
double z = Math.sin(phi) * r;
points[i] = new Coord(x, y, z);
}
}
public void drawSphere(Graphics2D g) {
g.translate(ADJ, ADJ); //shifting from origin for drawing purposes
Arrays.sort(points); //sorting points by their z coordinates
double iHat = -2 * LIGHT_X;
double jHat = -2 * LIGHT_Y; //Light vector
double kHat = -2 * LIGHT_Z;
double angL1 = 0;
if (Math.abs(iHat) != 0.0)
angL1 = Math.atan(jHat / iHat); //converting light vector to spherical coordinates
else
angL1 = Math.PI/2;
double angL2 = Math.atan(Math.sqrt(Math.pow(iHat, 2) + Math.pow(jHat, 2))/ kHat);
double maxArcLength = LIGHT_SPHERE_RADIUS * Math.PI; // maximum arc length
for (int i = 0; i < points.length; i++) {
if(points[i].checkValid()) {
double siHat = -2 * points[i].x;
double sjHat = -2 * points[i].y; //finding normal vector for the given point on the sphere
double skHat = -2 * points[i].z;
double angSF1 = -1 * Math.abs(Math.atan(sjHat / siHat)); // converting vector to spherical coordinates
double angSF2 = Math.atan(Math.sqrt(Math.pow(siHat, 2) + Math.pow(sjHat, 2))/ skHat);
double actArcLength = LIGHT_SPHERE_RADIUS * Math.acos(Math.cos(angL1) * Math.cos(angSF1) + Math.sin(angL1) * Math.sin(angSF1) * Math.cos(angL2 - angSF2)); //calculating arc length at this point
double comp = actArcLength / maxArcLength; // comparing the maximum arc length to the calculated arc length for this vector
int col = (int)(comp * 255);
col = Math.abs(col);
g.setColor(new Color(col, col, col));
double ovalDim = (4 * Math.PI * Math.pow(DRAWN_SPHERE_RADIUS, 2))/POINT_COUNT; //using surface area to determine how large size of each point should be drawn
if (ovalDim < 1) // if it too small, make less small
ovalDim = 2;
g.fillOval((int)points[i].x, (int)points[i].y, (int)ovalDim, (int)ovalDim); //draw this oval
}
}
}
#Override
public void paintComponent(Graphics gg) {
super.paintComponent(gg);
Graphics2D g = (Graphics2D) gg;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
drawSphere(g);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Sphere");
f.setResizable(false);
f.add(new SphereDrawing(), BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
#SuppressWarnings("rawtypes")
private class Coord implements Comparable {
public double x;
public double y;
public double z;
public Coord(double x2, double y2, double z2) {
x = x2;
y = y2;
z = z2;
}
public void scale() {
x *= SCALE;
y *= SCALE; //drawing purposes
z *= SCALE;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(Object c) {
double diff = this.z - ((Coord)c).z;
if (diff < 0)
return -1;
else if (diff > 0) //for sorting the array of points
return 1;
else
return 0;
}
public boolean checkValid() {
return (z > 0); //checks if need to draw this point
}
}
}
I was hoping to at least draw a realistic looking sphere, even if not completely accurate, and I couldn't tell you what exactly is off with mine

A* algorithm current node spreading outwards instead of one direction

The yellow circle indicates the starting node and the red circle is indicating the goal node. I can't understand why my current node is spreading outwards instead of the image below where the node is just going straight into the goal.
I'm currently following this guide Link
I just can't get this part into my head on how should I choose a better path using G cost. It says that a lower G cost means a better path. but what node should I compare to that lower G cost?
If it is on the open list already, check to see if this path to that square is better, using G cost as the measure. A lower G cost means that this is a better path. If so, change the parent of the square to the current square, and recalculate the G and F scores of the square.
My desired output should be like this.
public class AStar {
private List<Node> open = new ArrayList<Node>();
private List<Node> close = new ArrayList<Node>();
private Node[][] nodes;
private GIS gis;
private MapListener mapListener;
private Arrow arrow;
public AStar(GIS gis) {
this.gis = gis;
mapListener = new MapListener(this);
createMapNodes();
}
private void createMapNodes() {
nodes = new Node[gis.getUslMap().getTileWidth()][gis.getUslMap().getTileHeight()];
for (int x = 0; x < gis.getUslMap().getTileWidth(); x++) {
for (int y = 0; y < gis.getUslMap().getTileHeight(); y++) {
TiledMapTileLayer.Cell cell = gis.getUslMap().getPathLayer().getCell(x, y);
arrow = new Arrow();
nodes[x][y] = new Node(cell, arrow, x, y);
if (cell != null) {
nodes[x][y].setBounds(x * Map.TILE.getSize(), y * Map.TILE.getSize(), Map.TILE.getSize(), Map.TILE.getSize());
nodes[x][y].getLabel().setBounds(x * Map.TILE.getSize(), y * Map.TILE.getSize(), Map.TILE.getSize(), Map.TILE.getSize());
nodes[x][y].getArrow().getImage().setBounds(x * Map.TILE.getSize(), y * Map.TILE.getSize(), Map.TILE.getSize(), Map.TILE.getSize());
mapListener = new MapListener(this);
nodes[x][y].addListener(mapListener);
gis.getUslMap().getStage().getActors().add(nodes[x][y].getLabel());
gis.getUslMap().getStage().getActors().add(nodes[x][y].getArrow().getImage());
gis.getUslMap().getStage().addActor(nodes[x][y]);
nodes[x][y].debug();
}
}
}
}
private void clearNodes() {
for (int x = 0; x < gis.getUslMap().getTileWidth(); x++) {
for (int y = 0; y < gis.getUslMap().getTileHeight(); y++) {
nodes[x][y].gCost = 0;
nodes[x][y].hCost = 0;
nodes[x][y].fCost = 0;
nodes[x][y].getLabel().setText("");
nodes[x][y].getArrow().setDrawable("blank");
}
}
close.clear();
open.clear();
}
public void search(Vector2 start, Node goal) {
clearNodes();
Node current = nodes[(int) start.x][(int) start.y];
open.add(current);
while (!open.isEmpty()) {
current = getLowestFCost(open);
if (current == goal)
return;
open.remove(current);
close.add(current);
// Prints the Fcost.
current.getLabel().setText(current.fCost + "");
// Detect the adjacent tiles or nodes of the current node
// and calculate the G, H and F cost
for (int x = -1; x < 2; x++) {
for (int y = -1; y < 2; y++) {
int dx = current.x + x;
int dy = current.y + y;
if (isValidLocation(dx, dy)) {
if (isWalkable(x, y, nodes[dx][dy]))
continue;
if (!open.contains(nodes[dx][dy])) {
open.add(nodes[dx][dy]);
nodes[dx][dy].parent = current;
if (isDiagonal(x, y))
nodes[dx][dy].gCost = current.gCost + 14;
else
nodes[dx][dy].gCost = current.gCost + 10;
nodes[dx][dy].fCost = nodes[dx][dy].gCost + heuristic(nodes[dx][dy], goal);
} else if (open.contains(nodes[dx][dy])&&) {
}
}
}
}
}
}
private boolean isWalkable(int x, int y, Node node) {
return x == 0 && y == 0 || node.getCell() == null || close.contains(node);
}
private boolean isValidLocation(int dx, int dy) {
return dx > 0 && dx < gis.getUslMap().getTileWidth() &&
dy > 0 && dy < gis.getUslMap().getTileHeight();
}
private boolean isDiagonal(int x, int y) {
return x == -1 && y == 1 || x == 1 && y == 1 ||
x == -1 && y == -1 || y == -1 && x == 1;
}
private Node getLowestFCost(List<Node> open) {
int lowestCost = 0;
int index = 0;
for (int i = 0; i < open.size(); i++) {
if (open.get(i).fCost <= lowestCost) {
lowestCost = open.get(i).fCost;
index = i;
}
}
return open.get(index);
}
private int heuristic(Node start, Node goal) {
int dx = Math.abs(start.x - goal.x);
int dy = Math.abs(start.y - goal.y);
start.hCost = 10 * (dx + dy);
return start.hCost;
}
}
My node.class
private TiledMapTileLayer.Cell cell;
private Label label;
private Arrow arrow;
boolean diagonal;
Node parent;
int x;
int y;
int hCost;
int gCost;
int fCost;
public Node(TiledMapTileLayer.Cell cell, Arrow arrow, int x, int y) {
this.cell = cell;
this.x = x;
this.y = y;
this.arrow = arrow;
label = new Label("", Assets.getInstance().getMapAsset().getAssetSkin(), "default");
label.setPosition(this.getX(), this.getY());
}
TiledMapTileLayer.Cell getCell() {
return cell;
}
Label getLabel() {
return label;
}
public Arrow getArrow() {
return arrow;
}
I think you have a problem to get the lowestcost:
private Node getLowestFCost(List<Node> open) {
int lowestCost = 0;
int index = 0;
for (int i = 0; i < open.size(); i++) {
if (open.get(i).fCost <= lowestCost) {
lowestCost = open.get(i).fCost;
index = i;
}
}
return open.get(index);
}
you initiate lowestCost = 0 but fCost always more than 0, so this function is not really work. It makes the lowest cost obtained from initial lowestCost, not from fCost open list. Try to initiate lowestCost with big number or first value in open list.

javafx Glow shatter the animation

Im just starting to code and learn javafx and doing an easy project and my problem is after i add a Glow effect on my graphic context the animation slows down dramaticly.
/**
* JavaFX rocks ;)
*/
public class CDLMatrix extends Application {
Stage matrixStage;
private final String characters = "Effect glow = new Glow(1.0); gc.setEffect(glow); WHY SHATTERS ?!";
private final Random random = new Random();
protected final Font font = Font.font("MS PGothic", FontWeight.BOLD, 15);
char[] data = new char[2000 * 2000];
int[] path = new int[2000 * 2000];
private long lastTime = 0;
int getNumberOfCharsPerRow() {
return (int) matrixStage.getWidth() / 12;
}
int getNumberOfCharsPerColumn() {
return (int) matrixStage.getHeight() / 12;
}
// takes random for now
private char getChar() {
return characters.charAt(Math.abs(random.nextInt()
% characters.length()));
}
void update(long now) {
if (lastTime == 0) {
lastTime = now;
}
// fadeTime = how fast trail will fade out
// flipRate = how fast trail chars will change
final int fadeTime = 3;
final float flipRate = 0.01f;
final int fillStart = 100;
final float fillRate = 0.01f;
int numberOfCharsPerRow = getNumberOfCharsPerRow();
int numberOfCharsPerColumn = getNumberOfCharsPerColumn();
int numberOfChars = numberOfCharsPerRow * numberOfCharsPerColumn;
for (int i = numberOfChars - 1; i >= 0; --i) {
if (i + numberOfCharsPerRow < numberOfChars) {
if (path[i] == 255) {
// This means char was just set
// Initialize the next row at this X
// position
path[i + numberOfCharsPerRow] = 255;
data[i + numberOfCharsPerRow] = getChar();
}
}
// path[i] > 64 means if this char Green component > 25%
if (path[i] > 64 && random.nextFloat() < flipRate) {
data[i] = getChar();
}
// Decrement the char Green component
if (path[i] > fadeTime) {
path[i] -= fadeTime;
} else {
path[i] = 0;
}
// First row
// Start doing stuff only if the Green component > 40%
if (i < numberOfCharsPerRow && path[i] <= fillStart) {
if (random.nextFloat() < fillRate) {
path[i] = 255;
data[i] = getChar();
}
}
}
lastTime = now;
}
#Override
public void start(Stage stage) throws Exception {
this.matrixStage = stage;
matrixStage.setTitle("CDL Matrix");
Group root = new Group();
Scene scene = new Scene(root, 1024, 768);
scene.addEventHandler(KeyEvent.KEY_PRESSED,
new EventHandler<KeyEvent>() {
#Override
// F key for full screen ;)
public void handle(KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.F) {
matrixStage.setFullScreen(!matrixStage
.isFullScreen());
}
// ctrl + Q = exit
if (keyEvent.isControlDown()
&& keyEvent.getCode() == KeyCode.Q) {
matrixStage.close();
}
}
});
Canvas canvas = new Canvas();
canvas.widthProperty().bind(matrixStage.widthProperty());
canvas.heightProperty().bind(matrixStage.heightProperty());
final GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFont(font);
// Effect glow = new Glow(1.0); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// gc.setEffect(glow); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
new AnimationTimer() {
#Override
public void handle(long now) {
update(now);
gc.clearRect(0, 0, matrixStage.getWidth(),
matrixStage.getHeight());
gc.setFill(Color.rgb(0, 1, 0));
gc.fillRect(0, 0, matrixStage.getWidth(),
matrixStage.getHeight());
int y = 0;
int numberOfCharsPerRow = getNumberOfCharsPerRow();
int numberOfCharsPerColumn = getNumberOfCharsPerColumn();
// Colors
for (int i = 0; i < numberOfCharsPerRow
* numberOfCharsPerColumn; ++i) {
gc.setFill(Color.rgb(0, path[i], 0));
String text = String.valueOf(data[i]);
gc.fillText(text, (i % numberOfCharsPerRow) * 12 + 1,
y + 13);
if (i % numberOfCharsPerRow == numberOfCharsPerRow - 1) {
y += 12;
}
}
}
}.start();
root.getChildren().add(canvas);
matrixStage.setScene(scene);
matrixStage.show();
}
}
Apply the glow to the Canvas node rather than to the GraphicsContext.
Animation will then occur at normal speed rather than slideshow speed.
Change:
gc.setEffect(glow);
To:
canvas.setEffect(glow);
There must be something in the internal implementation of JavaFX which causes applying effects such as Glow to a GraphicsContext rather than a Node to be orders of magnitude less efficient. You might want to file an issue in the JavaFX issue tracker to have a developer look into it.
Test environment: Java 8u40, OS X 10.9

Categories