⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chart.java

📁 这是一个十分有用的java图形工具包
💻 JAVA
字号:
package net.turbochen.graph;

import java.awt.*;
import javax.swing.*;
import java.util.*;
/**
 * @author Turbo Chen
 */
public class Chart extends JComponent implements ChartWidget
{
    
    protected XAxis xAxis;
    protected YAxis yAxis;
    protected ArrayList plots;
    protected Color background;

    public Chart(XAxis xAxis,YAxis yAxis,Plot plot)
    {
        plots = new ArrayList();
        background = Color.WHITE;
        this.xAxis = xAxis;
        this.yAxis = yAxis;
        plots.add(plot);
    }

    public void paint(Graphics g)
    {
        draw(g);
    }
    
    /**
     * @return XAxis
     */
    public XAxis getXAxis()
    {
        return xAxis;
    }

    /**
     * @return YAxis
     */
    public YAxis getYAxis()
    {
        return yAxis;
    }

    /**
     * Sets the xAxis.
     * @param xAxis The xAxis to set
     */
    public void setXAxis(XAxis xAxis)
    {
        this.xAxis = xAxis;
    }

    /**
     * Sets the yAxis.
     * @param yAxis The yAxis to set
     */
    public void setYAxis(YAxis yAxis)
    {
        this.yAxis = yAxis;
    }
    
    public void addPlot(Plot plot)
    {
        plots.add(plot);
    }
    
    public void removePlot(Plot plot)
    {
        plots.remove(plot);
    }
    
    public void draw(Graphics g)
    {
        initCoordinate(g);
        g.setColor(background);
        g.fillRect(0,0,this.getWidth(),this.getHeight());
        xAxis.draw(g);
        yAxis.draw(g);
        for (int i = 0;i<plots.size();i++)
            ((Plot)plots.get(i)).draw(g);
    }
    
    protected void initCoordinate(Graphics g)
    {
        int leftMargin = yAxis.calculateTickLabelSize(g);
        int bottomMargin = xAxis.calculateTickLabelSize(g);
        int rightMargin = leftMargin;
        int topMargin = bottomMargin;
        
        xAxis.x = leftMargin;
        xAxis.y = this.getHeight()-bottomMargin;
        xAxis.width = this.getWidth() - leftMargin-rightMargin;
        xAxis.height = bottomMargin;
        xAxis.peerAxis = yAxis;
        xAxis.scale.screenMin = leftMargin;
        xAxis.scale.screenMax = this.getWidth()-leftMargin;
        
        yAxis.x = 0;
        yAxis.y = topMargin;
        yAxis.width = leftMargin;
        yAxis.height = this.getHeight()- topMargin-bottomMargin;
        yAxis.peerAxis = xAxis;
        yAxis.scale.screenMax = topMargin;
        yAxis.scale.screenMin = this.getHeight()-bottomMargin;
        
        for (int i = 0;i<plots.size();i++)
        {
            Plot plot = ((Plot)plots.get(i));
            plot.x = leftMargin;
            plot.y = topMargin;
            plot.width = xAxis.width;
            plot.height = yAxis.height;
        } 
        
    }


    /**
     * @return Color
     */
    public Color getBackground()
    {
        return background;
    }

    /**
     * Sets the background.
     * @param background The background to set
     */
    public void setBackground(Color background)
    {
        this.background = background;
    }

}

⌨️ 快捷键说明

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