printchart.java

来自「一个完整的」· Java 代码 · 共 49 行

JAVA
49
字号
package cn.com.fcsoft.chart.examples;
import cn.com.fcsoft.chart.*;

import java.awt.*;


/**
 * This example prints a bar chart.
 * @author meiqi.
 */
public class PrintChart {

	public static void main(String[] argv) {
		// create the chart
		double[] values = new double[20];
		for (int i = 0; i < values.length; i++) {
			values[i] = Math.round(Math.random()*100);
		}
		BarChart chart = new BarChart();
		chart.setSampleCount(values.length);
		chart.setSampleValues(0, values);
		chart.setValueLinesOn(true);
		chart.setTitle("this chart will be printed");
		chart.setTitleOn(true);
		
		// display the chart
		Frame f = new Frame();
		f.add("Center", chart);
		f.setSize(400,300);
		f.show();
		
		// try to print the chart
		Toolkit kit = Toolkit.getDefaultToolkit();
		PrintJob job = kit.getPrintJob(f, "barchart", null);
		if (job != null) {
			Graphics g = job.getGraphics();
			// normally you should call the chart.print(Graphics) method
			chart.print(g);
			// but some java VMs and printers have problems printing applets
			// and you can call the paint(Graphics) method to print the 
			// applet as a bitmap and work around this problem. However,
			// the resolution of the printed bitmap chart will be poor
			// chart.paint(g);
			job.end();
		} else {
			System.out.println("Could not get a print job");
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?