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

📄 bar.java

📁 自己写的轮询提问的小程序 新手刚学 多多指教 合乎哈
💻 JAVA
字号:
/*Bar is just a simple bar as in bar graph.Copyright (C) 2005-2006  Igor Partola, Michael J. Krikonis, Clark UniversityThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.*/package SharedClasses;import java.beans.*;import java.io.Serializable;import java.awt.*;import javax.swing.*;import java.awt.font.*;import java.awt.geom.*;public class Bar extends JComponent implements Serializable {    private int VoteCount = 0;    private int GreatestVote = 0;    private int TotalVoteCount = 0;    private Color BarColor = Color.RED;    private String Name = "";    private boolean DisplayPercentage = false;        private int X = 0;    private int Y = 0;    private int Width = 0;    private int MaxHeight = 0;        private Font TextFont = new Font("Tahoma", Font.PLAIN, 11);        public Bar() {        this(Color.RED, "");    }        public Bar(Color c, String s) {        super();        BarColor = c;        Name = s;        setBounds(X, Y, 50, 60);        repaint();    }        public void setFont(Font f) {        TextFont = f;        repaint();    }        public void paint(Graphics g) {        super.paint(g);        int Height = 0;        int p = 0;                g.setFont(TextFont);                if (GreatestVote > 0) Height = MaxHeight * VoteCount / GreatestVote;                g.setColor(BarColor);        g.fillRect(X, Y - Height, Width, Height + 1);                g.setColor(Color.BLACK);                g.drawRect(0, 0, this.getSize().width - 1, this.getSize().height - 1);                if (!DisplayPercentage)             paintHorizontallyCenteredText((Graphics2D) g, Integer.toString(VoteCount), X + 24*Width/50, Y - Height - 5);                    else {            if (TotalVoteCount == 0) p = 0;            else p = (int) (100*VoteCount/TotalVoteCount);            paintHorizontallyCenteredText((Graphics2D) g, Integer.toString(p) + "%", X + 24*Width/50, Y - Height - 5);        }        paintHorizontallyCenteredText((Graphics2D) g, Name, X + 25*Width/50, Y + 25);    }        public void setBounds(int x, int y, int width, int height) {        int w = width;        int h = height;                if (width < 20) w = 20;        if (height < 60) h = 60;                super.setBounds(x, y, w, h);        X = w/10;        MaxHeight = h - 50;        Y = h - 30;        Width = 8*w/10;                  repaint();     }            public void setColor(Color c) {        BarColor = c;        repaint();    }        public void setLabel(String s) {        int i = s.length();        int numOfLetters = Width/7;        if (i > numOfLetters) i = numOfLetters;        Name = s.substring(0, i);        repaint();    }        public void setVoteCount(int v, int gv, int tvc) {        VoteCount = v;        GreatestVote = gv;        TotalVoteCount = tvc;        repaint();    }        public void setDisplayPercentage(boolean b) {        DisplayPercentage = b;        repaint();    }        private void paintHorizontallyCenteredText(Graphics2D g2, String s, float centerX, float baselineY) {        FontRenderContext frc = g2.getFontRenderContext();        Rectangle2D bounds = g2.getFont().getStringBounds(s, frc);        float width = (float) bounds.getWidth();        g2.drawString(s, centerX - width / 2, baselineY);    }}

⌨️ 快捷键说明

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