• <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之行人
            看起來還不錯。  回復  更多評論
              

            久久www免费人成看片| 久久精品亚洲一区二区三区浴池| 青草国产精品久久久久久| 久久国产精品无码网站| 久久99精品国产99久久6男男| 久久精品国产只有精品66 | 久久精品国产亚洲精品| 欧美日韩精品久久久免费观看| 久久久无码精品亚洲日韩蜜臀浪潮| 久久亚洲高清观看| 日产精品久久久久久久性色| 久久久久亚洲精品男人的天堂| 99久久无色码中文字幕人妻| 久久精品视频91| 久久夜色精品国产亚洲| 日韩av无码久久精品免费| 久久久久青草线蕉综合超碰| 久久久WWW成人免费毛片| 狠色狠色狠狠色综合久久| 久久综合精品国产二区无码| 欧美激情一区二区久久久| 精品国产91久久久久久久a | 色综合久久最新中文字幕| 色综合久久综合中文综合网| 亚洲色欲久久久久综合网| 九九热久久免费视频| 国产精自产拍久久久久久蜜| 99久久99久久精品国产片| 亚洲国产成人久久精品影视| 国产精品久久久天天影视| 99久久精品国内| 欧美伊香蕉久久综合类网站| 色综合久久中文字幕无码| 国产午夜免费高清久久影院| 久久亚洲AV成人出白浆无码国产| 97久久国产露脸精品国产| 精品国产乱码久久久久久呢| 亚洲国产精品无码久久98| 精品久久久久久久久午夜福利 | 91精品婷婷国产综合久久| 丁香久久婷婷国产午夜视频|