Date post: | 06-Apr-2018 |
Category: |
Documents |
Upload: | meljun-cortes-mbampa |
View: | 214 times |
Download: | 0 times |
8/3/2019 MELJUN_CORTES_JAVA FX Sample Program Graphics MELJUN
http://slidepdf.com/reader/full/meljuncortesjava-fx-sample-program-graphics-meljun 1/13
Transitionrotate.java----------sample source of JAVA FX
/**
*
meljun cortes*
*/ package transitionrotate;
import javafx.animation.RotateTransition;import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;import javafx.stage.Stage;import javafx.util.Duration;
/*** A sample in which a node rotates around its center over a given time.
*
* @related animation/transitions/ScaleTransition
* @related animation/transitions/TranslateTransition* @related animation/transitions/FadeTransition
* @see javafx.animation.TransitionRotate
* @see javafx.animation.Transition*/
public class TransitionRotate extends Application {
private RotateTransition rotateTransition;
private void init(Stage primaryStage) {Group root = new Group();
primaryStage.setResizable(false);
primaryStage.setScene(new Scene(root, 140,140));
Rectangle rect = new Rectangle(20, 20, 100, 100);rect.setArcHeight(20);
rect.setArcWidth(20);rect.setFill(Color.ORANGE);
8/3/2019 MELJUN_CORTES_JAVA FX Sample Program Graphics MELJUN
http://slidepdf.com/reader/full/meljuncortesjava-fx-sample-program-graphics-meljun 2/13
root.getChildren().add(rect);
rotateTransition = new RotateTransition(Duration.seconds(4), rect);
rotateTransition.setFromAngle(0);
rotateTransition.setToAngle(720);
rotateTransition.setCycleCount(Timeline.INDEFINITE);
rotateTransition.setAutoReverse(true);
}
public void play() {
rotateTransition.play();}
@Override public void stop() {rotateTransition.stop();
}
@Override public void start(Stage primaryStage) throws Exception {
init(primaryStage);primaryStage.show();
play();}
public static void main(String[] args) { launch(args); }
}
Transitionpath.java----------sample source of JAVA FX
/**
*meljun cortes*
*/
package transitionpath;import javafx.animation.PathTransition;
import javafx.animation.PathTransition.OrientationType;
import javafx.animation.Timeline;
import javafx.application.Application;import javafx.scene.Group;
import javafx.scene.Scene;import javafx.scene.paint.Color;
8/3/2019 MELJUN_CORTES_JAVA FX Sample Program Graphics MELJUN
http://slidepdf.com/reader/full/meljuncortesjava-fx-sample-program-graphics-meljun 3/13
import javafx.scene.shape.CubicCurveTo;
import javafx.scene.shape.MoveTo;import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;import javafx.util.Duration;
/**
* A sample in which a node moves along a path from end to end over a given time.*
* @related animation/transitions/TranslateTransition
* @related animation/transitions/RotateTransition* @related animation/transitions/ScaleTransition
* @see javafx.animation.PathTransition
* @see javafx.animation.Transition*/
public class TransitionPath extends Application {
private PathTransition pathTransition;
private void init(Stage primaryStage) {
Group root = new Group();primaryStage.setResizable(false);
primaryStage.setScene(new Scene(root, 400,260));
Rectangle rect = new Rectangle (0, 0, 40, 40);rect.setArcHeight(10);
rect.setArcWidth(10);
rect.setFill(Color.ORANGE);root.getChildren().add(rect);
Path path = new Path();
path.getElements().add(new MoveTo(20,20));path.getElements().add(new CubicCurveTo(380, 0, 380, 120, 200, 120));path.getElements().add(new CubicCurveTo(0, 120, 0, 240, 380, 240));
path.setStroke(Color.DODGERBLUE);
path.getStrokeDashArray().setAll(5d,5d);root.getChildren().add(path);
pathTransition = new PathTransition();
pathTransition.setDuration(Duration.seconds(4));pathTransition.setPath(path);
pathTransition.setNode(rect);
pathTransition.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT);
pathTransition.setCycleCount(Timeline.INDEFINITE);pathTransition.setAutoReverse(true);
}
public void play() {
pathTransition.play();
}
@Override public void stop() {
pathTransition.stop();}
@Override public void start(Stage primaryStage) throws Exception {
init(primaryStage);primaryStage.show();
play();
}public static void main(String[] args) { launch(args); }
}
8/3/2019 MELJUN_CORTES_JAVA FX Sample Program Graphics MELJUN
http://slidepdf.com/reader/full/meljuncortesjava-fx-sample-program-graphics-meljun 4/13
TimelineInterpolator.java----------sample source of JAVA FX
/**
*meljun cortes*
*/
package timelineinterpolator;
import javafx.application.Application;import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.animation.Interpolator;import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.effect.Lighting;import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;import javafx.util.Duration;
/**
* A sample that shows various types of interpolation between key frames in a* timeline. There are five circles, each animated with a different
* interpolation method. The Linear interpolator is the default. Use the
* controls to reduce opacity to zero for some circles to compare with others,* or change circle color to distinguish between individual interpolators.
*
* @see javafx.animation.Interpolator * @see javafx.animation.KeyFrame
* @see javafx.animation.KeyValue
* @see javafx.animation.Timeline
* @see javafx.util.Duration*/
public class TimelineInterpolator extends Application {
private final Timeline timeline = new Timeline();
8/3/2019 MELJUN_CORTES_JAVA FX Sample Program Graphics MELJUN
http://slidepdf.com/reader/full/meljuncortesjava-fx-sample-program-graphics-meljun 5/13
private void init(Stage primaryStage) {
Group root = new Group();primaryStage.setResizable(false);
primaryStage.setScene(new Scene(root, 250, 90));
//create circles by method createMovingCircle listed below
Circle circle1 = createMovingCircle(Interpolator.LINEAR); //default interpolator
circle1.setOpacity(0.7);
Circle circle2 = createMovingCircle(Interpolator.EASE_BOTH); //circle slows down when reached bothends of trajectory
circle2.setOpacity(0.45);
Circle circle3 = createMovingCircle(Interpolator.EASE_IN);Circle circle4 = createMovingCircle(Interpolator.EASE_OUT);
Circle circle5 = createMovingCircle(Interpolator.SPLINE(0.5, 0.1, 0.1, 0.5)); //one can define own
behaviour of interpolator by spline method
root.getChildren().addAll(
circle1,circle2,
circle3,circle4,
circle5);
}
private Circle createMovingCircle(Interpolator interpolator) {
//create a transparent circle
Circle circle = new Circle(45,45, 40, Color.web("1c89f4"));circle.setOpacity(0);
//add effect
circle.setEffect(new Lighting());
//create a timeline for moving the circle
timeline.setCycleCount(Timeline.INDEFINITE);timeline.setAutoReverse(true);
//create a keyValue for horizontal translation of circle to the position 155px with given interpolator KeyValue keyValue = new KeyValue(circle.translateXProperty(), 155, interpolator);
//create a keyFrame with duration 4s
KeyFrame keyFrame = new KeyFrame(Duration.seconds(4), keyValue);
//add the keyframe to the timeline
timeline.getKeyFrames().add(keyFrame);
return circle;
}
public void play() {
timeline.play();}
@Override public void stop() {
timeline.stop();}
@Override public void start(Stage primaryStage) throws Exception {init(primaryStage);
primaryStage.show();
play();}
public static void main(String[] args) { launch(args); }
}
8/3/2019 MELJUN_CORTES_JAVA FX Sample Program Graphics MELJUN
http://slidepdf.com/reader/full/meljuncortesjava-fx-sample-program-graphics-meljun 6/13
TimelineEvents.java----------sample source of JAVA FX
/**
*meljun cortes*
*/
package timelineevents;
import javafx.animation.AnimationTimer;import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;import javafx.scene.Scene;
import javafx.scene.effect.Lighting;
import javafx.scene.layout.StackPane;import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;import javafx.stage.Stage;
import javafx.util.Duration;
/*** A sample that demonstrates events triggered during timeline play. The circle
* changes its radius in a linear fashion during each key frame and randomly
* jumps to a new location along the x coordinate at the end of the key frame.*
* @see javafx.animation.KeyFrame
* @see javafx.animation.KeyValue* @see javafx.animation.Timeline
* @see javafx.util.Duration
* @see javafx.event.ActionEvent
* @see javafx.event.EventHandler */
public class TimelineEvents extends Application {
//main timeline
8/3/2019 MELJUN_CORTES_JAVA FX Sample Program Graphics MELJUN
http://slidepdf.com/reader/full/meljuncortesjava-fx-sample-program-graphics-meljun 7/13
private Timeline timeline;
private AnimationTimer timer;
//variable for storing actual frame
private Integer i=0;
private void init(Stage primaryStage) {
Group root = new Group();
primaryStage.setResizable(false);primaryStage.setScene(new Scene(root, 260,100));
//create a circle with effect
final Circle circle = new Circle(20, Color.rgb(156,216,255));circle.setEffect(new Lighting());
//create a text inside a circle
final Text text = new Text (i.toString());text.setStroke(Color.BLACK);
//create a layout for circle with text inside
final StackPane stack = new StackPane();stack.getChildren().addAll(circle, text);
stack.setLayoutX(30);stack.setLayoutY(30);
//create a timeline for moving the circle
timeline = new Timeline();timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(true);
//one can add a specific action when each frame is started. There are one or more frames during
// executing one KeyFrame depending on set Interpolator.
timer = new AnimationTimer() {@Overridepublic void handle(long l) {
text.setText(i.toString());
i++;}
};
//create a keyValue with factory: scaling the circle 2times
KeyValue keyValueX = new KeyValue(stack.scaleXProperty(), 2);
KeyValue keyValueY = new KeyValue(stack.scaleYProperty(), 2);
//create a keyFrame, the keyValue is reached at time 2s
Duration duration = Duration.seconds(2);//one can add a specific action when the keyframe is reached
EventHandler<ActionEvent> onFinished = new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {stack.setTranslateX(java.lang.Math.random()*200);
//reset counter
i = 0;}
};
KeyFrame keyFrame = new KeyFrame(duration, onFinished , keyValueX, keyValueY);
//add the keyframe to the timeline
timeline.getKeyFrames().add(keyFrame);
root.getChildren().add(stack);
}
public void play() {
timeline.play();timer.start();
}
8/3/2019 MELJUN_CORTES_JAVA FX Sample Program Graphics MELJUN
http://slidepdf.com/reader/full/meljuncortesjava-fx-sample-program-graphics-meljun 8/13
@Override public void stop() {
timeline.stop();timer.stop();
}
@Override public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
play();}
public static void main(String[] args) { launch(args); }
}
SwingInterop.java
SampleTableModel.java----------sample source of JAVA FX/**
*meljun cortes*
*/
package swinginterop;
import java.awt.BorderLayout;import java.awt.Color;import java.awt.Component;
import java.awt.Dimension;
import java.text.DecimalFormat;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javafx.embed.swing.JFXPanel;
import javafx.application.Platform;import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.value.ChangeListener;
8/3/2019 MELJUN_CORTES_JAVA FX Sample Program Graphics MELJUN
http://slidepdf.com/reader/full/meljuncortesjava-fx-sample-program-graphics-meljun 9/13
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.Chart;
import javafx.scene.chart.NumberAxis;import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.Label;import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;import javafx.scene.layout.Region;
import javafx.scene.web.WebEngine;import javafx.scene.web.WebView;
import javax.swing.*;
import javax.swing.event.TableModelEvent;import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableCellRenderer;
/**
* SwingInterop
** Note using the browser might require setting the properties* - http.proxyHost
* - http.proxyPort
** e.g. -Dhttp.proxyHost=webcache.mydomain.com -Dhttp.proxyPort=8080
*
*/ public class SwingInterop extends JApplet {
private static final int PANEL_WIDTH_INT = 675;
private static final int PANEL_HEIGHT_INT = 400;private static final int TABLE_PANEL_HEIGHT_INT = 100;
private static JFXPanel chartFxPanel;
private static JFXPanel browserFxPanel;private static SampleTableModel tableModel;
private Chart chart;
private Pane browser;
public void init() {
tableModel = new SampleTableModel();// create javafx panel for charts
chartFxPanel = new JFXPanel();chartFxPanel.setPreferredSize(new Dimension(PANEL_WIDTH_INT, PANEL_HEIGHT_INT));
// create javafx panel for browser
browserFxPanel = new JFXPanel();
//create tabbed pane
JTabbedPane tabbedPane = new JTabbedPane();
//JTable
JTable table = new JTable(tableModel);
table.setAutoCreateRowSorter(true);table.setGridColor(Color.DARK_GRAY);
DecimalFormatRenderer renderer = new DecimalFormatRenderer();
renderer.setHorizontalAlignment(JLabel.RIGHT);
8/3/2019 MELJUN_CORTES_JAVA FX Sample Program Graphics MELJUN
http://slidepdf.com/reader/full/meljuncortesjava-fx-sample-program-graphics-meljun 10/13
for (int i = 0; i < table.getColumnCount(); i++) {
table.getColumnModel().getColumn(i).setCellRenderer(renderer);}
JScrollPane tablePanel = new JScrollPane(table);
tablePanel.setPreferredSize(new Dimension(PANEL_WIDTH_INT, TABLE_PANEL_HEIGHT_INT));
JPanel chartTablePanel = new JPanel();
chartTablePanel.setLayout(new BorderLayout());
//Split pane that holds both chart and table
JSplitPane jsplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
jsplitPane.setTopComponent(chartTablePanel);jsplitPane.setBottomComponent(tablePanel);
jsplitPane.setDividerLocation(410);
chartTablePanel.add(chartFxPanel, BorderLayout.CENTER);
tabbedPane.addTab("JavaFX Chart and Swing JTable", jsplitPane);
tabbedPane.addTab("Web Browser", browserFxPanel);
add(tabbedPane, BorderLayout.CENTER);
// create JavaFX scenePlatform.runLater(new Runnable() {
public void run() {
createScene();}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");} catch (Exception e) {}
JFrame frame = new JFrame("JavaFX 2.0 in Swing");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new SwingInterop();
applet.init();
frame.setContentPane(applet.getContentPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
applet.start();
}});
}
private void createScene() {chart = createBarChart();
chartFxPanel.setScene(new Scene(chart));
browser = createBrowser();browserFxPanel.setScene(new Scene(browser));
}
private BarChart createBarChart() {
CategoryAxis xAxis = new CategoryAxis();
xAxis.setCategories(FXCollections.<String>observableArrayList(tableModel.getColumnNames()));xAxis.setLabel("Year");
double tickUnit = tableModel.getTickUnit();
8/3/2019 MELJUN_CORTES_JAVA FX Sample Program Graphics MELJUN
http://slidepdf.com/reader/full/meljuncortesjava-fx-sample-program-graphics-meljun 11/13
NumberAxis yAxis = new NumberAxis();yAxis.setTickUnit(tickUnit);
yAxis.setLabel("Units Sold");
final BarChart chart = new BarChart(xAxis, yAxis, tableModel.getBarChartData());
tableModel.addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {if (e.getType() == TableModelEvent.UPDATE) {
final int row = e.getFirstRow();
final int column = e.getColumn();final Object value = ((SampleTableModel) e.getSource()).getValueAt(row, column);
Platform.runLater(new Runnable() {public void run() {
XYChart.Series<String, Number> s = (XYChart.Series<String, Number>)
chart.getData().get(row);BarChart.Data data = s.getData().get(column);
data.setYValue(value);}
});}
}
});return chart;
}
private Pane createBrowser() {
Double widthDouble = new Integer(PANEL_WIDTH_INT).doubleValue();
Double heightDouble = new Integer(PANEL_HEIGHT_INT).doubleValue();WebView view = new WebView();view.setMinSize(widthDouble, heightDouble);
view.setPrefSize(widthDouble, heightDouble);
final WebEngine eng = view.getEngine();final Label warningLabel = new Label("Do you need to specify web proxy information?");
eng.load("http://www.oracle.com/us/index.html");
ChangeListener handler = new ChangeListener<Number>() {
@Override public void changed(ObservableValue<? extends Number> observable, Number oldValue,
Number newValue) {
if (warningLabel.isVisible()) {warningLabel.setVisible(false);
}
}};
eng.getLoadWorker().progressProperty().addListener(handler);
final TextField locationField = new TextField("http://www.oracle.com/us/index.html");
locationField.setMaxHeight(Double.MAX_VALUE);
Button goButton = new Button("Go");goButton.setDefaultButton(true);
EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {@Override public void handle(ActionEvent event) {
eng.load(locationField.getText().startsWith("http://") ? locationField.getText(): "http://" + locationField.getText());
}
};goButton.setOnAction(goAction);
locationField.setOnAction(goAction);
eng.locationProperty().addListener(new ChangeListener<String>() {@Override public void changed(ObservableValue<? extends String> observable, String oldValue, String
newValue) {
locationField.setText(newValue);}
});
GridPane grid = new GridPane();
8/3/2019 MELJUN_CORTES_JAVA FX Sample Program Graphics MELJUN
http://slidepdf.com/reader/full/meljuncortesjava-fx-sample-program-graphics-meljun 12/13
grid.setPadding(new Insets(5));
grid.setVgap(5);grid.setHgap(5);
GridPane.setConstraints(locationField, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS,
Priority.SOMETIMES);GridPane.setConstraints(goButton, 1, 0);
GridPane.setConstraints(view, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS,
Priority.ALWAYS);
GridPane.setConstraints(warningLabel, 0, 2, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS,Priority.SOMETIMES);
grid.getColumnConstraints().addAll(
new ColumnConstraints(widthDouble - 200, widthDouble - 200, Double.MAX_VALUE,Priority.ALWAYS, HPos.CENTER, true),
new ColumnConstraints(40, 40, 40, Priority.NEVER, HPos.CENTER, true));
grid.getChildren().addAll(locationField, goButton, warningLabel, view);return grid;
}
private static class DecimalFormatRenderer extends DefaultTableCellRenderer {
private static final DecimalFormat formatter = new DecimalFormat("#.0");
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, booleanhasFocus, int row, int column) {
value = formatter.format((Number) value);
return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);}
}
}******************************************************************************************
/**
*SampleTableModel*/
package swinginterop;
import java.util.Arrays;import java.util.List;
import javax.swing.table.AbstractTableModel;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.chart.BarChart;
/**
* SampleTableModel*/
public class SampleTableModel extends AbstractTableModel {
private static ObservableList<BarChart.Series> bcData;
private final String[] names = {"2007", "2008", "2009"};
private Object[][] data = {{new Double(567), new Double(956), new Double(1154)},
{new Double(1292), new Double(1665), new Double(1927)},
{new Double(1292), new Double(2559), new Double(2774)}};
public double getTickUnit() {
return 1000;
}
public List<String> getColumnNames() {return Arrays.asList(names);
8/3/2019 MELJUN_CORTES_JAVA FX Sample Program Graphics MELJUN
http://slidepdf.com/reader/full/meljuncortesjava-fx-sample-program-graphics-meljun 13/13
}
@Override
public int getRowCount() {
return data.length;}
@Override
public int getColumnCount() {return names.length;
}
@Override
public Object getValueAt(int row, int column) {
return data[row][column];}
@Overridepublic String getColumnName(int column) {
return names[column];}
@Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();}
@Overridepublic boolean isCellEditable(int row, int column) {
return true;
}
@Override
public void setValueAt(Object value, int row, int column) {
if (value instanceof Double) {data[row][column] = (Double) value;
}
fireTableCellUpdated(row, column);
}
public ObservableList<BarChart.Series> getBarChartData() {if (bcData == null) {
bcData = FXCollections.<BarChart.Series>observableArrayList();
for (int row = 0; row < getRowCount(); row++) {ObservableList<BarChart.Data> series = FXCollections.<BarChart.Data>observableArrayList();
for (int column = 0; column < getColumnCount(); column++) {
series.add(new BarChart.Data(getColumnName(column), getValueAt(row, column)));}
bcData.add(new BarChart.Series(series));
}}
return bcData;}
}