AWT之—画图

package edu.ch4;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.util.Calendar;
import java.util.Date;

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;
import org.jfree.data.general.DefaultPieDataset;

public class Test extends ApplicationWindow {

    /** *//**
     * Create the application window
     */
    public Test() {
        super(null);
        addToolBar(SWT.FLAT | SWT.WRAP);
        addMenuBar();
        addStatusLine();
    }

    /** *//**
     * Create contents of the application window
     *
     * @param parent
     */
    @Override
    protected Control createContents(Composite parent) {
        TabFolder tf = new TabFolder(parent, SWT.TOP);
        TabItem ti = new TabItem(tf, SWT.NULL);
        ti.setText("分类");
        Composite composite = new Composite(tf, SWT.NO_BACKGROUND
                | SWT.EMBEDDED);
        Frame frame = SWT_AWT.new_Frame(composite);
        frame.add(new ChartPanel(createBarChart()));
        ti.setControl(composite);
        TabItem ti1 = new TabItem(tf, SWT.NULL);
        ti1.setText("项目组");
        Composite composite1 = new Composite(tf, SWT.NO_BACKGROUND
                | SWT.EMBEDDED);
        Frame frame1 = SWT_AWT.new_Frame(composite1);
        frame1.add(new ChartPanel(createGanttChart()));
        ti1.setControl(composite1);
        tf.setSelection(0);
        return tf;
    }

    /** *//**
     * 方法名称: 内容摘要:
     *
     * @return
     * @return JFreeChart
     * @throws
     */
    private JFreeChart createGanttChart() {
        String title = "Gantt测试";
        IntervalCategoryDataset dataset = createSampleDataset();

        JFreeChart jfc = ChartFactory.createGanttChart(title, "项目各阶段详细实施计划",
                "项目周期", dataset, false, false, false);

        return jfc;
    }

    /** *//**
      * 方法名称:
      * 内容摘要:创建gantt内容
      *
      * @return
      * @return IntervalCategoryDataset
      * @throws
     */
    private IntervalCategoryDataset createSampleDataset() {
        TaskSeries s1 = new TaskSeries("日程表");

        Task t1 = new Task("项目立项", date(1, Calendar.APRIL, 2001), date(5,
                Calendar.APRIL, 2001));
        t1.setPercentComplete(1.00);
       
        Task t2 = new Task("项目立项讨论", date(6, Calendar.APRIL, 2001), date(19,
                Calendar.APRIL, 2001));
       
        s1.add(t1);
        s1.add(t2);
       
       
        final Task t3 = new Task(
                "需求分析",
                date(10, Calendar.APRIL, 2001), date(5, Calendar.MAY, 2001)
            );
            final Task st31 = new Task(
                "需求1",
                date(10, Calendar.APRIL, 2001), date(25, Calendar.APRIL, 2001)
            );
            st31.setPercentComplete(1.0);
            final Task st32 = new Task(
                "需求2",
                date(1, Calendar.MAY, 2001), date(5, Calendar.MAY, 2001)
            );
            st32.setPercentComplete(1.0);
            t3.addSubtask(st31);
            t3.addSubtask(st32);
            s1.add(t3);
       
       
       
        final TaskSeriesCollection collection = new TaskSeriesCollection();
        collection.add(s1);

        return collection;
    }

    /** *//** */
    /** *//**
     * Utility method for creating Date objects.
     *
     * @param day
     *            日
     * @param month
     *            月
     * @param year
     *            年
     *
     * @return a date.
     */
    private static Date date(final int day, final int month, final int year) {

        final Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day);

        final Date result = calendar.getTime();
        return result;

    }

    /** *//**
     * 方法名称: 内容摘要:饼图测试
     *
     * @return
     * @return JFreeChart
     * @throws
     */
    private JFreeChart createBarChart() {
        String title = "空调2002年市场占有率";
        DefaultPieDataset piedata = new DefaultPieDataset();
        piedata.setValue("联想", 27.3);
        piedata.setValue("长城", 12.2);
        piedata.setValue("海尔", 5.5);
        piedata.setValue("美的", 17.1);
        piedata.setValue("松下", 9.0);
        piedata.setValue("科龙", 19.0);
        JFreeChart chart = ChartFactory.createPieChart(title, piedata, true,
                true, true);
        chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15)));
        chart.addSubtitle(new TextTitle("2002财年分析", new Font("隶书", Font.ITALIC,
                12)));
        chart.setBackgroundPaint(Color.white);
        PiePlot pie = (PiePlot) chart.getPlot();
        pie.setBackgroundPaint(Color.white);
        pie.setBackgroundAlpha(0.6f);
        pie.setForegroundAlpha(0.90f);
        return chart;
    }

    /** *//**
     * Launch the application
     *
     * @param args
     */
    public static void main(String args[]) {
        try {
            Test window = new Test();
            window.setBlockOnOpen(true);
            window.open();
            Display.getCurrent().dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** *//**
     * Configure the shell
     *
     * @param newShell
     */
    @Override
    protected void configureShell(Shell newShell) {
        super.configureShell(newShell);
        newShell.setText("New Application");
    }

    /** *//**
     * Return the initial size of the window
     */
    @Override
    protected Point getInitialSize() {
        return new Point(500, 375);
    }

}

转载于:https://www.cnblogs.com/wshsdlau/p/3460565.html

你可能感兴趣的:(AWT之—画图)