Currently I try to integrate a JavaFX BarChart into my existing Java application. Therefore I need a BufferedImage of my BarChart, so that I can render that snapshot on a Graphics instance.
The TickLabels are visible If I use JDK 1.7u51 but if I change to JDK 1.8u05.... than the stlye of my BarChart will be changed and my tick labels are not visible.
package barchartsample;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.image.WritableImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BarChartSample
{
/**
* Initialize JavaFX
*/
private static final JFXPanel initializeJFX = new JFXPanel();
final static String austria = "Austria";
final static String brazil = "Brazil";
final static String france = "France";
final static String italy = "Italy";
final static String usa = "USA";
public static void main(String[] args) throws InterruptedException, ExecutionException
{
JFrame frame = new JFrame();
frame.setSize(815, 640);
frame.add(new JPanel()
{
@Override
public void paint(Graphics g)
{
try
{
g.drawImage(getChartImage(), 0, 0, null);
}
catch (Throwable t)
{
t.printStackTrace();
}
}
});
frame.setVisible(true);
}
static BufferedImage getChartImage() throws InterruptedException, ExecutionException
{
FutureTask<BufferedImage> future = new FutureTask<BufferedImage>(new Callable<BufferedImage>()
{
@Override
public BufferedImage call() throws Exception
{
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
final BarChart<String, Number> bc =
new BarChart<>(xAxis, yAxis);
bc.setTitle("Country Summary");
xAxis.setLabel("Country");
yAxis.setLabel("Value");
XYChart.Series series1 = new XYChart.Series();
series1.setName("2003");
series1.getData().add(new XYChart.Data(austria, 25601.34));
series1.getData().add(new XYChart.Data(brazil, 20148.82));
series1.getData().add(new XYChart.Data(france, 10000));
series1.getData().add(new XYChart.Data(italy, 35407.15));
series1.getData().add(new XYChart.Data(usa, 12000));
XYChart.Series series2 = new XYChart.Series();
series2.setName("2004");
series2.getData().add(new XYChart.Data(austria, 57401.85));
series2.getData().add(new XYChart.Data(brazil, 41941.19));
series2.getData().add(new XYChart.Data(france, 45263.37));
series2.getData().add(new XYChart.Data(italy, 117320.16));
series2.getData().add(new XYChart.Data(usa, 14845.27));
XYChart.Series series3 = new XYChart.Series();
series3.setName("2005");
series3.getData().add(new XYChart.Data(austria, 45000.65));
series3.getData().add(new XYChart.Data(brazil, 44835.76));
series3.getData().add(new XYChart.Data(france, 18722.18));
series3.getData().add(new XYChart.Data(italy, 17557.31));
series3.getData().add(new XYChart.Data(usa, 92633.68));
bc.getData().addAll(series1, series2, series3);
Scene scene = new Scene(bc, 800, 600);
WritableImage fximage = scene.snapshot(null);
return SwingFXUtils.fromFXImage(fximage, new BufferedImage((int) Math.ceil(fximage.getWidth()), (int) Math.ceil(fximage.getHeight()), BufferedImage.TYPE_INT_ARGB_PRE));
}
});
Platform.runLater(future);
return future.get();
}
}