在try {}中发生错误我只想让它工作。配有用于jfree和itext的jar。请帮忙:(
package com.pieChart.sample;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.orsonpdf.DefaultFontMapper;
public class PieChart {
public static void main(String[] args) {
writeChartToPDF(generateBarChart(), 500, 400, "C://barchart.pdf");
writeChartToPDF(generatePieChart(), 500, 400, "C://piechart.pdf");
}
public static JFreeChart generatePieChart() {
DefaultPieDataset dataSet = new DefaultPieDataset();
dataSet.setValue("China", 19.64);
dataSet.setValue("India", 17.3);
dataSet.setValue("United States", 4.54);
dataSet.setValue("Indonesia", 3.4);
dataSet.setValue("Brazil", 2.83);
dataSet.setValue("Pakistan", 2.48);
dataSet.setValue("Bangladesh", 2.38);
JFreeChart chart = ChartFactory.createPieChart(
"World Population by countries", dataSet, true, true, false);
return chart;
}
public static JFreeChart generateBarChart() {
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
dataSet.setValue(791, "Population", "1750 AD");
dataSet.setValue(978, "Population", "1800 AD");
dataSet.setValue(1262, "Population", "1850 AD");
dataSet.setValue(1650, "Population", "1900 AD");
dataSet.setValue(2519, "Population", "1950 AD");
dataSet.setValue(6070, "Population", "2000 AD");
JFreeChart chart = ChartFactory.createBarChart(
"World Population growth", "Year", "Population in millions",
dataSet, PlotOrientation.VERTICAL, false, true, false);
return chart;
}
public static void writeChartToPDF(JFreeChart chart, int width, int height, String fileName) {
PdfWriter writer = null;
Document document = new Document(null);
try {
writer = PdfWriter.getInstance(document, new FileOutputStream(
fileName));
document.open();
PdfContentByte contentByte = writer.getDirectContent();
PdfTemplate template = contentByte.createTemplate(width, height);
Graphics2D graphics2d = template.createGraphics(width, height,
new DefaultFontMapper());
Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,
height);
chart.draw(graphics2d, rectangle2d);
graphics2d.dispose();
contentByte.addTemplate(template, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
document.close();
}
}
Try {}的错误 1.PdfWriter 2.PdfContentByte 3.PdfTemplate 4.Graphics2D 5.Default字体映射器 6.Rectangle2D 所有这些错误都表示"无法解决"或"无法解析为#34;
类型线程中的异常" main" java.lang.Error:未解决的编译问题: PdfWriter无法解析为某种类型 文档无法解析为某种类型 文档无法解析为某种类型 PdfWriter无法解析 FileOutputStream无法解析为某个类型 PdfContentByte无法解析为某种类型 PdfTemplate无法解析为某种类型 Graphics2D无法解析为某种类型 DefaultFontMapper无法解析为类型 Rectangle2D无法解析为某种类型 Rectangle2D无法解析为类型