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

📄 barchart.java

📁 使用MVC 的模型画 BAR CHART, 便于理解MVC的内涵.
💻 JAVA
字号:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Collections;
import java.util.Random;
import java.util.Vector;
import java.awt.Font;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class BarChart extends JComponent
{

public 
    int 
        top, 
        bottom, 
        left, 
        right, 
        padding = 4, 
        min, 
        max, 
        nBarDis = 0,
        x2Bottom = 4 * padding;
    String
        title = null;
        
    List<XYData> XYDatas = null;
    public BarChart()
    {
        min = 10;
        max = 100;
        title="";
        XYDatas = new ArrayList<XYData>();
    }

    public void setTitle(String title)
    {
        this.title = title;
    }
    
    public void setRange(int min, int max)
    {
        this.min = min;
        this.max = max;
    }
    
    public void sortByX()
    {
        Collections.sort(XYDatas, new XComparator());
        repaint();
    }
    
    public void sortByY()
    {
        Collections.sort(XYDatas, new YComparator());
        repaint();
    }
    
    public void updateXYDatas(List<XYData> datas)
    {
        XYDatas = datas;
    }
            
    public void setBounds(int x, int y, int width, int height)
    {
        super.setBounds(x, y, width, height);
        top = 10*padding;
        bottom = (int) (getSize().getHeight()) - padding - x2Bottom;
        left = padding*8;
        right = (int) (getSize().getWidth()) - padding;
    }// End setBounds

    private void drawHeading(Graphics g)
    {
        Font
            headingFont = new Font("SansSerif",Font.BOLD,24),
            currentFont = g.getFont();
        Color
            headingColor = Color.red,
            currentColor = g.getColor();
        int
            x = (right-left)/2 - 20,
            y= top-padding;
            
        g.setFont(headingFont);
        g.setColor(headingColor);
        g.drawString(title,x,y);
        
        // set default color and font back
        g.setFont(currentFont);
        g.setColor(currentColor);
            
    }//End drawHeading
    
    private void drawYLabels(Graphics g)
    {
       // Draw Y Labels
       int
        nYScales = 10,                              // number of scales in y
        yValueScale = (max-min)/nYScales,           // Y Value scale 
        yPostionScale = (bottom - top) / nYScales,  // Y Postion scale
        yValue =0,                                  // Y value    
        yPos = bottom,                              // Y position
        xPos = padding;                             // x position
       for(int i=0; i<nYScales; i++)
       { 
           g.drawString(String.valueOf(yValue),xPos,yPos);
           yPos = yPos - yPostionScale;
           yValue = yValue + yValueScale;
        }//End for
    }//End drawYLabels
    
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        // Draw X y Lines
        g.drawLine(left, top, left, bottom);
        g.drawLine(left, bottom, right, bottom);
        g.setColor(Color.BLACK);

        // Draw Y Labels
        drawYLabels(g);
       
        //Draw Headings
        drawHeading(g);
        
        if( XYDatas.size() == 0)
        {
           return;
        }
        
        int adjustedValue, nValue, position, increment;
        Random r = new Random();
        int red = r.nextInt(256), green = r.nextInt(256), blue = r.nextInt(256);
        increment = (right - left) / (XYDatas.size());
        position = left;

        for (XYData data : XYDatas)
        {
            Float oValue = (Float) data.getY();
            nValue = oValue.intValue();
            adjustedValue = bottom - (nValue - min) * (bottom - top)
                    / (max - min);
            g.setColor(new Color(red, green, blue));

            g.fillRect(position, adjustedValue, increment, bottom
                    - adjustedValue);
            g.drawString(oValue.toString(), position + 1, adjustedValue - 1);
            g.drawString(data.getX() + "", position + 1, (int) (getSize()
                    .getHeight())
                    - 2 * padding);
            position = position + increment + nBarDis;

            red = r.nextInt(256);
            green = r.nextInt(256);
            blue = r.nextInt(256);
        }   
    }

    
    
}// End Class Definition

⌨️ 快捷键说明

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