• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            逛奔的蝸牛

            我不聰明,但我會很努力

               ::  :: 新隨筆 ::  ::  :: 管理 ::

            package test;


            import java.awt.Color;

            import java.awt.Graphics;

            import java.awt.Graphics2D;

            import java.awt.RenderingHints;

            import java.awt.Toolkit;

            import java.awt.geom.Arc2D;

            import java.awt.geom.Area;

            import java.awt.geom.GeneralPath;

            import java.awt.geom.Point2D;

            import java.util.ArrayList;

            import java.util.List;


            import javax.swing.JFrame;

            import javax.swing.JPanel;


            /**

             * 繪制帶陰影的餅圖

             * 

             * @author Biao

             */

            @SuppressWarnings("serial")

            public class Pie3D extends JPanel {

                // 餅圖中每一個部分的百分比

                private double[] percents = { 10.72, 15.38, 3.74, 10.26, 6.56, 5.69, 10.72, 15.38, 6.15, 18.0 };

                // 餅圖的顏色

                private Color[] pieColors;

                

                public Pie3D() {

                    List<Color> colors = new ArrayList<Color>();

                    colors.add(getColorFromHex("#169800"));

                    colors.add(getColorFromHex("#00E500"));

                    colors.add(getColorFromHex("#D0F15A"));

                    colors.add(getColorFromHex("#FF7321"));

                    colors.add(getColorFromHex("#E2FF55"));

                    colors.add(getColorFromHex("#AA6A2D"));

                    colors.add(getColorFromHex("#BFDD89"));

                    colors.add(getColorFromHex("#D718A5"));

                    colors.add(getColorFromHex("#00DBFF"));

                    colors.add(getColorFromHex("#00FF00"));

                    

                    pieColors = colors.toArray(new Color[0]);

                }

                

                public static Color getColorFromHex(String hex) {

                    try {

                        return new Color(Integer.valueOf(hex.substring(1), 16));

                    } catch (Exception e) {

                        return Color.BLACK;

                    }

                }


                @Override

                protected void paintComponent(Graphics g) {

                    super.paintComponent(g);

                    Graphics2D g2d = (Graphics2D) g;

                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                    int w = 380;

                    int h = (int) (w * 0.618);

                    fillPies(g2d, percents, 30, 30, w, h);

                }


                private void fillPies(Graphics2D g2d, double[] percents, int x, int y, int w, int h) {

                    int colorIndex = 0;

                    double startAngle = 0;

                    double arcAngle = 0;


                    // 一次性繪制完陰影,然后再繪制餅圖的效果比繪制餅圖的同時繪制它的陰影好

                    for (int i = 0; i < percents.length; ++i) {

                        arcAngle = percents[i] * 3.6;


                        if (i + 1 == percents.length) {

                            arcAngle = 360 - startAngle;

                        }


                        Arc2D.Double arcTop = new Arc2D.Double(x, y, w, h, startAngle, arcAngle, Arc2D.PIE);


                        g2d.setColor(pieColors[colorIndex].darker());

                        g2d.fill(buildShadow(arcTop));

                        // g2d.setColor(pieColors[colorIndex]);

                        // g2d.fill(arcTop);


                        colorIndex = (colorIndex + 1) % pieColors.length;

                        startAngle += arcAngle;

                    }


                    colorIndex = 0;

                    startAngle = 0;

                    for (int i = 0; i < percents.length; ++i) {

                        arcAngle = percents[i] * 3.6;


                        if (i + 1 == percents.length) {

                            arcAngle = 360 - startAngle;

                        }


                        Arc2D.Double arcTop = new Arc2D.Double(x, y, w, h, startAngle, arcAngle, Arc2D.PIE);


                        g2d.setColor(pieColors[colorIndex]);

                        g2d.fill(arcTop);


                        colorIndex = (colorIndex + 1) % pieColors.length;

                        startAngle += arcAngle;

                    }

                }


                private Area buildShadow(Arc2D arc) {

                    int shadowDapth = 10;

                    Arc2D arcBottom = new Arc2D.Double(arc.getX(), arc.getY() + shadowDapth, arc.getWidth(),

                        arc.getHeight() + 0, arc.getAngleStart(), arc.getAngleExtent(), Arc2D.CHORD);

                    Point2D topLeft = new Point2D.Double(arc.getStartPoint().getX(), arc.getStartPoint().getY());

                    Point2D topRight = new Point2D.Double(arc.getEndPoint().getX(), arc.getEndPoint().getY());

                    Point2D bottomLeft = new Point2D.Double(arcBottom.getStartPoint().getX(), arcBottom

                        .getStartPoint().getY());

                    Point2D bottomRight = new Point2D.Double(arcBottom.getEndPoint().getX(), arcBottom

                        .getEndPoint().getY());


                    double[] xpoints = { topLeft.getX(), topRight.getX(), bottomRight.getX(), bottomLeft.getX() };

                    double[] ypoints = { topLeft.getY(), topRight.getY(), bottomRight.getY(), bottomLeft.getY() };


                    GeneralPath path = new GeneralPath();

                    path.moveTo(xpoints[0], ypoints[0]);

                    path.lineTo(xpoints[1], ypoints[1]);

                    path.lineTo(xpoints[2], ypoints[2]);

                    path.lineTo(xpoints[3], ypoints[3]);

                    path.closePath();


                    Area area = new Area(arcBottom);

                    area.add(new Area(path));


                    return area;

                }


                private static void createGuiAndShow() {

                    JFrame frame = new JFrame("Pie with Shadow");

                    frame.getContentPane().add(new Pie3D());


                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                    int sw = Toolkit.getDefaultToolkit().getScreenSize().width;

                    int sh = Toolkit.getDefaultToolkit().getScreenSize().height;

                    int w = 500;

                    int h = 400;

                    int x = (sw - w) / 2;

                    int y = (sh - h) / 2 - 40;

                    x = x > 0 ? x : 0;

                    y = y > 0 ? y : 0;

                    frame.setBounds(x, y, w, h);

                    frame.setVisible(true);

                }


                public static void main(String[] args) {

                    createGuiAndShow();

                }

            }

            posted on 2010-12-31 17:02 逛奔的蝸牛 閱讀(941) 評論(1)  編輯 收藏 引用 所屬分類: Java

            評論

            # re: Java:繪制有立體感的3D餅圖 2011-01-03 10:24 Code之行人
            看起來還不錯。  回復  更多評論
              

            久久久久无码精品| 亚洲国产综合久久天堂| 97久久久精品综合88久久| 久久99久久99精品免视看动漫| 中文字幕一区二区三区久久网站| 久久精品国产99国产精品| 久久天天躁狠狠躁夜夜躁2O2O| 亚洲狠狠久久综合一区77777| 青青热久久国产久精品| 看久久久久久a级毛片| 久久亚洲精品无码播放| 秋霞久久国产精品电影院| 久久精品国产2020| 欧美久久一区二区三区| 久久国产一区二区| 精品无码久久久久久尤物| 久久夜色精品国产噜噜亚洲a| 亚洲一区二区三区日本久久九| 久久夜色精品国产噜噜亚洲AV| 无码精品久久一区二区三区| 国产亚洲成人久久| 99久久精品国产麻豆| 青青草原精品99久久精品66| 久久最新免费视频| 品成人欧美大片久久国产欧美| 国色天香久久久久久久小说| 欧美亚洲国产精品久久| 精品国产一区二区三区久久蜜臀| 久久精品国产只有精品2020| 久久99国产乱子伦精品免费| 天天爽天天狠久久久综合麻豆| 伊人久久大香线蕉av一区| 漂亮人妻被中出中文字幕久久| 亚洲欧洲久久久精品| 亚洲精品第一综合99久久| 久久精品国产久精国产果冻传媒| 一极黄色视频久久网站| 国产精品一区二区久久精品涩爱| 国产精品成人久久久| 色妞色综合久久夜夜| 国产成人精品白浆久久69|