In JavaFx I have a LineChart lc for which I create and add a series,
lc.getData().addAll(series1);
It displays just fine, but seems to sample all data points to fit the whole domain into the chart.
Is there a way to force the LineChart to just display the last 10 data points, say, of the series?
Ultimately I would like to create a way to pan the chart contents so that a window of the data is displayed, as a function of pan position.
I found this SO post but it removes data, which means I would then need to add it back. I would rather keep all data in the series if possible, and set the usable range of that series if possible.
I worked out the solution myself using the standard fx libraries.
So long as the horizontal axis is a NumberAxis, you can use the setLowerBound() and setUpperBound() functions.
Combined with a scrollbar this works perfectly.
For example,
final static int MAX_DISPLAY = 100
LineChart<Number, Number> lineChart;
ScrollBar scroller;
scroller.setMin(0);
scroller.setMax(1000 - MAX_DISPLAY);
scroller.valueProperty().addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> observableValue, Number number, Number t1) {
double pos = scroller.getValue();
NumberAxis na = (NumberAxis)lineChart.getXAxis();
na.setLowerBound(pos);
na.setUpperBound(pos + MAX_DISPLAY);
}
});
Note - if using Scene Builder, your LineChart x-axis will default to CatergoryAxis, so make sure to edit the .fxml file and change it to NumberAxis manually.
Additionally, for my use case since the x-axis represents Unix epoch times, you can convert the Number values into something else using the following code (in my case a date formatted string),
NumberAxis xAxis = (NumberAxis)lineChart.getXAxis();
xAxis.setTickLabelFormatter(new StringConverter<Number>() {
#Override
public String toString(Number object) {
return convertToDate(object.longValue());
}
#Override
public Number fromString(String string) {
return 0;
}
});
Related
I have two problems one this that, if i want to show score with the circle object:
layoutV.getChildren().addAll(virus, score);
I get the following error:
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: duplicate children added: parent = Pane#6661fc86[styleClass=root].
As far as I understand it is because the Task wants to show multiple scores. So should I use another scene or layout to show score?
My other problem is the hitbox of the object, right know everytime i click the score goes up. I looked up the mouse event getTarget but it does not seem like I can make it so that my object is the only target to use the mouse event on.
public class Main extends Application {
private Stage window;
private Pane layoutV;
private Scene scene;
private Circle virus;
private int score;
private Label scores;
#Override
public void start(Stage primaryStage) {
window = primaryStage;
window.setTitle("Enemy TEST");
this.score = 0;
scores = new Label("Score "+ score);
layoutV = new Pane();
scene = new Scene(layoutV, 600, 600);
window.setScene(scene);
window.show();
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
}
Task task = new Task<Void>() {
#Override
protected Void call() throws Exception {
while (true) {
Platform.runLater(new Runnable() {
#Override
public void run() {
drawCircles();
}
});
Thread.sleep(1000);
}
}
};
public void drawCircles() {
double x = (double)(Math.random() * ((550 - 50) + 1)) + 50;
double y = (double)(Math.random() * ((550 - 50) + 1)) + 50;
double r = (double)(Math.random() * ((30 - 10) + 1)) + 10;
virus = new Circle(x, y, r, Color.VIOLET);
layoutV.setOnMouseClicked(e -> {
if (e.getButton() == MouseButton.PRIMARY) {
layoutV.getChildren().remove(e.getTarget());
this.score++;
System.out.println("score: "+ this.score);
}
});
layoutV.getChildren().addAll(virus);
scene.setRoot(layoutV);
window.setScene(scene);
}
public static void main(String[] args) {
launch(args);
}
}
You have lots of issues, not just the ones from your question:
Although it will work as you coded
it, I don't advise spawning a thread to draw your circles, instead
see:
JavaFX periodic background task
You don't need to set the root in the scene and the scene in the
window every time you draw a new circle.
Nor do you need to set the
mouse handler on the layout every time you draw a circle.
Rather than setting a mouse handler on the layout, you are better off setting a mouse handler on the circles themselves (which you can do before you add them to the scene).
score is an int, not a node you can only add nodes to the scene
graph.
See the documentation for the scene package:
A node may occur at most once anywhere in the scene graph. Specifically, a node must appear no more than once in the children list of a Parent or as the clip of a Node. See the Node class for more details on these restrictions.
How you are adding the node more than once is not clear to me, because you are probably doing it in code different than the Main class you provided.
To add a circle with a score on top, use a StackPane with the score in a label, but make the label mouse transparent, so that it does not register any clicks:
Label scoreLabel = new Label(score + "");
scoreLabel.setMouseTransparent(true);
StackPane balloon = new StackPane(circle, scoreLabel);
layoutV.getChildren.add(balloon);
Add the click handler on the balloon.
And additional issues I don't detail here but are solved in the demo code provided.
To fix all your errors, I would write some code like below. Perhaps you can review it and compare it with your code to help understand one way to create this game.
The example code might not be exactly the functionality you are looking for (that is not really its purpose), but it should be enough to keep you on the right track for implementing your application.
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.*;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.concurrent.ThreadLocalRandom;
public class Inoculation extends Application {
public static final int W = 600;
public static final int H = 600;
private final IntegerProperty score = new SimpleIntegerProperty(0);
private final Pane playingField = new Pane();
#Override
public void start(Stage stage) {
StackPane overlay = createOverlay();
Pane layout = new StackPane(playingField, overlay);
stage.setScene(new Scene(layout, W, H));
stage.show();
Infection infection = new Infection(playingField, score);
infection.begin();
}
private StackPane createOverlay() {
Label totalScoreLabel = new Label();
totalScoreLabel.textProperty().bind(
Bindings.concat(
"Score ", score.asString()
)
);
StackPane overlay = new StackPane(totalScoreLabel);
StackPane.setAlignment(totalScoreLabel, Pos.TOP_LEFT);
overlay.setMouseTransparent(true);
return overlay;
}
public static void main(String[] args) {
launch(args);
}
}
class Infection {
private static final Duration SPAWN_PERIOD = Duration.seconds(1);
private static final int NUM_SPAWNS = 10;
private final Timeline virusGenerator;
public Infection(Pane playingField, IntegerProperty score) {
virusGenerator = new Timeline(
new KeyFrame(
SPAWN_PERIOD,
event -> spawnVirus(
playingField,
score
)
)
);
virusGenerator.setCycleCount(NUM_SPAWNS);
}
public void begin() {
virusGenerator.play();
}
private void spawnVirus(Pane playingField, IntegerProperty score) {
Virus virus = new Virus();
virus.setOnMouseClicked(
event -> {
score.set(score.get() + virus.getVirusScore());
playingField.getChildren().remove(virus);
}
);
playingField.getChildren().add(virus);
}
}
class Virus extends StackPane {
private static final int MAX_SCORE = 3;
private static final int RADIUS_INCREMENT = 10;
private final int virusScore = nextRandInt(MAX_SCORE) + 1;
public Virus() {
double r = (MAX_SCORE + 1 - virusScore) * RADIUS_INCREMENT;
Circle circle = new Circle(
r,
Color.VIOLET
);
Text virusScoreText = new Text("" + virusScore);
virusScoreText.setBoundsType(TextBoundsType.VISUAL);
virusScoreText.setMouseTransparent(true);
getChildren().setAll(
circle,
virusScoreText
);
setLayoutX(nextRandInt((int) (Inoculation.W - circle.getRadius() * 2)));
setLayoutY(nextRandInt((int) (Inoculation.H - circle.getRadius() * 2)));
setPickOnBounds(false);
}
public int getVirusScore() {
return virusScore;
}
// next random int between 0 (inclusive) and bound (exclusive)
private int nextRandInt(int bound) {
return ThreadLocalRandom.current().nextInt(bound);
}
}
Some additional notes on this implementation that might be useful to know:
The total score is placed in an overlayPane so that it is not obscured by elements added to the playingField (which contains the virus spawns).
The overlayPane is made mouseTransparent, so that it won't intercept any mouse events, and the clicks will fall through to the items in the playing field.
The app currently generates viruses within a fixed field size, regardless of whether you resize the window. That is just the way it is designed and coded, you could code it otherwise if wished. It would be more work to do so.
The Bindings class is used to create a string expression binding which concatenates the static string "Score " with an integer property representing the score. This allows the string representing the score to be bound to the score label text in the overlay so that it automatically updates whenever the score is changed.
The virus generation uses a timeline and is based on the concepts from:
JavaFX periodic background task
The application class is kept deliberately simple to handle mostly just the core application lifecycle, and the actual functionality of the application is abstracted to an Infection class which handles the spawning of the virus and a Virus class that generates a new virus.
This technique is used to center a score for each individual virus on the virus:
how to put a text into a circle object to display it from circle's center?
The virus itself is laid out in a StackPane. The pane has pick on bounds set to false. To remove the virus infection, you must click on the circle which represents the virus and not just anywhere in the square for the stack pane.
Because circle coordinates are in local co-ordinates and the circle is in a parent stack pane representing the virus, the circle itself does not need x and y values set, instead layout x and y values are set on the enclosing pane to allow positioning of the pane representing the entire virus.
The following technique is used to generate random integers in acceptable ranges using ThreadLocalRandom:
How do I generate random integers within a specific range in Java?
I am working on a paint program in which a button is clicked that toggles the button that draws lines. My code doesn't do what I want it to and instead draws a strange fan shape whenever I try to draw multiple lines. I want it to be able to draw multiple straight lines with the mouse.
canvas.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
initialTouch = new Pair<>(event.getX(), event.getY());
}
});
canvas.addEventHandler(MouseEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
gc.strokeLine(initialTouch.getKey(), initialTouch.getValue(), event.getX(), event.getY());
}
});
canvas.addEventHandler(MouseEvent.MOUSE_RELEASED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
}
});
}```
That method is called when a button is pushed to draw the line. I am expecting multiple lines, but I stead get a fan shape.
What you do, is:
You create start Point.
And then for each DRAG movement you create a OWN line from the Start location to the new Location. Hence you create a lot of lines that resemble a shape.
What you want is:
Create a start point and than draw to the end Point.
So you should ONLY draw a line to the Graphical Context when the Mouse released.
just Move what you do in "dragged" to the "released" part and it sould work.
When you want "Preview" you should Use a Line Object and add it to the View, and then when release the mouse you should delete the Object it.
I want to put a WorldWindowGLJPanel into a Pane, and I want to make it resizable, but I can't, even when I call resize or setSize method.
Here's what I'm doing :
wwd = new WorldWindowGLJPanel();
wwd.setPreferredSize(new java.awt.Dimension(300, 300));
wwd.setModel(new BasicModel());
swingNode = new SwingNode();
swingNode.setContent(wwd);
wwdPane = new Pane();
wwdPane.getChildren().add(swingNode);
Then I use this wwdPane to display World Wind.
I want my world wind panel to have the size of the pane which contains it, and I want to make this world wind panel resizable.
I thought about give the size to my world wind panel of my pane with a setSize(PaneDimenson) and then bind the size of my worldwindpanel with my pane , but the setSize function doesn't work.
EDIT : I found an alternative solution by not using a pane, but directly the swingNode, the resize is now automatic. But if you want to use a pane there's still a problem, and you're force to use a group.
The setSize is working, try this code:
scene.widthProperty().addListener(new ChangeListener<Number>() {
#Override public void changed( ObservableValue<? extends Number> o, Number b, Number a ) {
Platform.runLater(new Runnable() {
public void run() {
wwd.setSize((int)(a.intValue()*0.5), wwd.getHeight());
}
});
or with Java8
scene.widthProperty().addListener((o,b,a)->Platform.runLater(()->
wwd.setSize((int)(a.intValue()*0.5), wwd.getHeight())));
But I couldn't make the resize work in my sample code, because the SwingNode somehow mess up the resizing, I think you should try the solution advised here.
I have a variable ObservableIntegerValue called score .
I wanted to create a listener for it to listen for the changes of its value and according to that change the javafx label text displayed on my pane.
But in the method initialize() i need to instantiate and give it the initial value of 500 let's say.
How could it be done?
Beside an observable value, to make the score variable also bindable, you can use IntegerProperty instead of ObservableIntegerValue. IntegerProperty is an IntegerExpression, so it also implements ObservableIntegerValue interface, where IntegerExpression is,
A IntegerExpression is a ObservableIntegerValue plus additional
convenience methods to generate bindings in a fluent style.
IntegerProperty score = new SimpleIntegerProperty(500);
Text text = new Text("-");
// Bind score to text, to show on scene.
text.textProperty().bind(score.asString());
score.set(700); // new value
and the listener
score.addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
// value changed
}
});
This is a follow up to my last question:
How can I draw a circle to the screen with PlayN?
For my simple case, I want to programmatically create a single colored circle and move it across a 2-D plain (doesn't need to use box2d lib).
A real-world example would likely involve animating several circles. Two real-world examples for this case (sorry, I had to remove the links -- not enough karma!):
Browsmos for Chrome
Ants AI Challenge
It was suggested in response to my last question that I would want to use the ImmediateLayer class, so I am looking to understand how to properly incorporate this into my game loop.
Here's is my code sample:
public class SimpleCircleAnimation implements Game {
// Surface
private GroupLayer rootLayer;
private ImmediateLayer surface;
private Canvas canvas;
private Circle circle;
private CanvasImage circleImage;
#Override
public void init() {
// create root layer
rootLayer = graphics().rootLayer();
// a simple circle object
int circleX = 0; int circleY = 0;
int circleRadius = 20;
circle = new Circle(circleX, circleY, circleRadius);
// create an immediate layer and add to root layer
ImmediateLayer circleLayer = graphics().createImmediateLayer(new ImmediateLayer.Renderer() {
public void render (Surface surf) {
circleImage = graphics().createImage(circle.radius*2, circle.radius*2);
canvas = circleImage.canvas();
canvas.setFillColor(0xff0000eb);
canvas.fillCircle(circle.radius, circle.radius, circle.radius);
surf.drawImage(circleImage, circle.x, circle.y);
}
});
rootLayer.add(circleLayer);
}
#Override
public void paint(float alpha) {
}
#Override
public void update(float delta) {
// move circle
int newX = circle.x + 4; int newY = circle.y + 4;
circle.setPoint(newX, newY);
}
#Override
public int updateRate() {
return 25;
}
}
This successfully moves the circle diagonally down the screen from left to right. A couple questions:
Is this implemented properly?
In the case of multiple animated circles, is the idea with ImmediateLayer that you would create a circle image for each circle within the Renderer callback? Or would you perhaps create an Immediate Layer for each circle and add those to the root layer?
I would not use ImmediateLayer wiht render (Surface surf) adapter. Here u have, inside the render method creation of an image
circleImage = graphics().createImage(circle.radius*2, circle.radius*2);
just put this in the paint method
surf.drawImage(circleImage, circle.x, circle.y);
using the normal layer and u should be fine
Painting is done in paint method, and do not put calculations there
Update is for calculations, and physics oriented stuff
I discovered a detailed practical example of ImmediateLayer usage in the Cute Game source within the PlayN Samples:
CuteGame.java (code.google.com)