package fjr.test.javafxpanel;
import java.awt.Dimension;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.util.Duration;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class TestJavaFXPanel {
private XYChart.Series hourDataSeries;
private XYChart.Series minuteDataSeries;
private NumberAxis xAxis;
private Timeline animation;
private double hours = 0;
private double minutes = 0;
private double timeInHours = 0;
private double prevY = 10;
private double y = 10;
public TestJavaFXPanel() {
initAndShowGUI();
}
private void initAndShowGUI() {
JFrame frame = new JFrame("Test Swing");
frame.setPreferredSize(new Dimension(600, 400));
frame.pack();
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel);
frame.setVisible(true);
Platform.runLater(new Runnable() {
@Override
public void run() {
initFX(fxPanel);
}
});
}
private void initFX(JFXPanel fxPanel) {
Scene scene = createScene();
fxPanel.setScene(scene);
animation = new Timeline();
animation.getKeyFrames().add(new KeyFrame(Duration.millis(1000/60), new EventHandler() {
@Override public void handle(ActionEvent actionEvent) {
for(int count=0; count < 6; count++) {
nextTime();
plotTime();
}
}
}));
animation.setCycleCount(Animation.INDEFINITE);
}
private Scene createScene() {
StackPane root = new StackPane(){{
setTranslateX(0);
setTranslateY(0);
}};
Button button = new Button("PLAY"){{
setMinSize(100, 30);
setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent arg0) {
play();
}
});
}};
Button button2 = new Button("STOP"){{
setMinSize(100, 30);
setOnAction(new EventHandler() {
@Override
public void handle(ActionEvent arg0) {
// TODO Auto-generated method stub
stop();
}
} );
}};
HBox box = new HBox(){{
setSpacing(10);
setTranslateX(10);
setTranslateY(10);
}};
box.getChildren().addAll(button, button2);
root.getChildren().addAll( new BorderPane(){{
setLeft(createChart());
}} , box);
Scene sc = new Scene(root);
return sc;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestJavaFXPanel();
}
});
}
protected LineChart createChart() {
xAxis = new NumberAxis(0,24,3);
final NumberAxis yAxis = new NumberAxis(0,100,10);
final LineChart lc = new LineChart(xAxis,yAxis);
lc.setCreateSymbols(false);
lc.setAnimated(false);
lc.setLegendVisible(false);
lc.setTitle("PLOT GRAFIK");
xAxis.setForceZeroInRange(false);
yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis,"$",null));
hourDataSeries = new XYChart.Series();
minuteDataSeries = new XYChart.Series();
hourDataSeries.getData().add(new XYChart.Data(timeInHours,prevY));
minuteDataSeries.getData().add(new XYChart.Data(timeInHours,prevY));
for (double m=0; m<(60); m++) {
nextTime();
plotTime();
}
lc.getData().add(minuteDataSeries);
lc.getData().add(hourDataSeries);
lc.setTranslateX(10);
lc.setTranslateY(40);
lc.setMaxWidth(400);
lc.setMaxHeight(330);
return lc;
}
private void nextTime() {
if (minutes == 59) {
hours ++;
minutes = 0;
} else {
minutes ++;
}
timeInHours = hours + ((1d/60d)*minutes);
}
private void plotTime() {
if ((timeInHours % 1) == 0) {
double oldY = y;
y = prevY - 10 + (Math.random()*20);
prevY = oldY;
while (y < 10 || y > 90) y = y - 10 + (Math.random()*20);
hourDataSeries.getData().add(new XYChart.Data(timeInHours, prevY));
if (timeInHours > 25) hourDataSeries.getData().remove(0);
if (timeInHours > 24) {
xAxis.setLowerBound(xAxis.getLowerBound()+1);
xAxis.setUpperBound(xAxis.getUpperBound()+1);
}
}
double min = (timeInHours % 1);
double randomPickVariance = Math.random();
if (randomPickVariance < 0.3) {
double minY = prevY + ((y-prevY) * min) - 4 + (Math.random()*8);
minuteDataSeries.getData().add(new XYChart.Data(timeInHours,minY));
} else if (randomPickVariance < 0.7) {
double minY = prevY + ((y-prevY) * min) - 6 + (Math.random()*12);
minuteDataSeries.getData().add(new XYChart.Data(timeInHours,minY));
} else if (randomPickVariance < 0.95) {
double minY = prevY + ((y-prevY) * min) - 10 + (Math.random()*20);
minuteDataSeries.getData().add(new XYChart.Data(timeInHours,minY));
} else {
double minY = prevY + ((y-prevY) * min) - 15 + (Math.random()*30);
minuteDataSeries.getData().add(new XYChart.Data(timeInHours,minY));
}
if (timeInHours > 25) minuteDataSeries.getData().remove(0);
}
public void play() {
animation.play();
}
public void stop() {
animation.pause();
}
}
Potongan hasil eksekusinya adalah sebagai berikut Sesungguhnya shalat itu mencegah dari (perbuatan-perbuatan) keji dan mungkar
Q.S. Al-'Ankabut Ayat 45
Tuesday, January 7, 2014
Cara menggabungkan javaFX dengan java Swing
Tanpa perlu berbasa-basi berikut saya berikan contoh menggabungkan teknologi javaFX dengan java Swing. Berdasarkan informasi yang saya peroleh javaFX mungkin kedepannya dipake untuk menggantikan Swing.
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment