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

📄 chartpanel.java

📁 一个用JAVA 写的程序
💻 JAVA
字号:

import java.awt.*;
import javax.swing.*;

public class ChartPanel extends JPanel
{
    // instance variables
    private CandidateList candidates;
    
    private int xAxisStart = 50;   // start point of x axis is 50
    private int yAxisStart;         // start point of y axis
    private int border = 20;        // border margin is 20
    
    private int lowerBorder = 40;   // lower margin is 80
    private int xAxisLen;           // length of x axis
    private int yAxisLen;           // length of y axis
    private double barSpace;        // space between 2 bars
    double yScale;                  // scale of y axis
    
    int max = 25;
    
    // initialise card list
    public ChartPanel(CandidateList aCL)
    {
        candidates = aCL;        
    }
    
    // initialise instance variables and draw different parts of bar chart
    public void paintComponent(Graphics g)
    {
        yAxisStart = getHeight() - lowerBorder;
        
        xAxisLen = getWidth() - xAxisStart - border;
        yAxisLen = getHeight() - lowerBorder - border;
        
        barSpace = (double)xAxisLen/(candidates.getNumberOfCandidates());
                
        yScale = (double)yAxisLen / max;
        super.paintComponent(g);
        g.setFont(new Font("Monospaced", Font.BOLD, 14));
                        
        drawAxes(g);
        drawXLabels(g);
        drawYLabels(g);
        drawChartBlocks(g);
		  g.setColor(Color.blue);
				        
    }
    
    // draw axises
    public void drawAxes(Graphics g)
    {        
        //  draw x axis
        g.drawLine(xAxisStart, yAxisStart, getWidth() - border, yAxisStart);
        // draw y axis
        g.drawLine(xAxisStart, yAxisStart, xAxisStart, border);     
    }
    
    // draw lables of x axis
    public void drawXLabels(Graphics g)
    {
        // start point of first label of x axis
        int xPos = xAxisStart + (int)Math.round(barSpace / 4);
        int yPos = yAxisStart + 30;
        
        // draw labels one by one
        for (int i = 0; i < candidates.getNumberOfCandidates(); i ++)
        {
            Candidate candidate = candidates.getCandidate(i);
            String xLabel = String.valueOf(candidate .getId());
            g.drawString(xLabel, xPos, yPos);
            xPos += (int)Math.round(barSpace);
        }
    }
    
    // draw lables of y axis
    public void drawYLabels(Graphics g)
    {
        // start point of first label of y axis
        int xPos = xAxisStart - 30;
        int yPos = yAxisStart;
        
        // draw labels one by one
        for (int i = 0; i < 11; i ++)
        {
            String yLabel = String.valueOf(i * 5);
            g.drawString(yLabel, xPos, yPos);
            yPos -= (int)Math.round(yAxisLen / 5);
        }
    }
    
    // draw bars
    public void drawChartBlocks(Graphics g)
    {
        // start point of first bar
        int xPos = xAxisStart + (int)Math.round(barSpace / 4);
        int yPos = 0;
        
        // draw bars one by one
        for (int i = 0; i < candidates.getNumberOfCandidates(); i ++)
        {
            Candidate candidate = candidates.getCandidate(i);
            int fisrtVotesNumber = candidate.getVotesNumber();
            int otherVotesNumber = candidate.getOtherVotesNumber();
            int totalVotesNumber = candidate.getVotesNumber()+candidate.getOtherVotesNumber();    
            
           // int height = (int)Math.round(totalVotesNumber * yScale);   // height of bar
            int height = (int)Math.round(fisrtVotesNumber * yScale);   // height of bar
            yPos = yAxisStart - height;      // y position of bar
            
            Color col = new Color(0,255,255);
				col = Color.green;
            GRectangle rect = new GRectangle(xPos, yPos, (int)barSpace/2, height, col);
            rect.displayFilled(g);
            
             height = (int)Math.round(otherVotesNumber * yScale); 
            yPos -= height;
            col = Color.orange; 
            
            GRectangle rect2 = new GRectangle(xPos, yPos, (int)barSpace/2, height, col);
            rect2.displayFilled(g);

            xPos += (int)Math.round(barSpace);     // next x position of bar
        }
    }
}

⌨️ 快捷键说明

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