簡體   English   中英

如何在java中旋轉餅圖

[英]How can I rotate pie chart in java

我有代碼繪制一個餅圖值和隨機顏色。 現在我想旋轉整個數字,而不是一塊餡餅。 這是我的代碼:

class Slice{

    double value;
    Color color;
    public Slice(double _value){
        this.value = _value;    
    }
    public void setColor(Color _color){
        this.color = _color;

    }
}

class Component extends JComponent implements MouseListener{

    int movx = 0;
    int movy = 0;

    Slice[] slice = {new Slice(5),new Slice(20),new Slice(33),new Slice(55)};
    public Component(){
        addMouseListener(this);
    }

    public void paint(Graphics g){
        Graphics2D g2 = (Graphics2D)g;
        drawPie(g2, getBounds(), slice);

    }
    public void drawPie(Graphics2D g, Rectangle area, Slice[] s){
        double total = 0.0D;
        //calculate total value
        for(int i=0;i<s.length;i++)
            total+=s[i].value;

        double curentValue = 0.0D;
        int startAngle = 0;
        for(int i = 0;i<s.length;i++){
            Random numGen = new Random();
            s[i].setColor(new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256)));
            startAngle = (int)((curentValue*360)/total);
            int arcAngle = (int)((s[i].value*360)/total) ;
            g.setColor(s[i].color);
            g.rotate(30);//row AA
            g.fillArc(area.x, area.y, area.width, area.height, startAngle, arcAngle);   

            curentValue+=s[i].value;

        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        movx = e.getX();
        movy = e.getY();
        repaint();
    }

    // unimplemented Mouse methods removed
}

public class PieChart {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame frame = new JFrame();
        frame.getContentPane().add(new Component());
        frame.setSize(300,200);
        frame.setVisible(true);
    }
}

在我寫rotate行AA中,它無法正常工作? 你能幫助我嗎? 如何旋轉整個圖表?

Graphics2D.rotate的參數是弧度。

http://docs.oracle.com/javase/6/docs/api/java/awt/Graphics2D.html#rotate%28double%29

http://en.wikipedia.org/wiki/Radian#Conversion_between_radians_and_degrees

如果要旋轉整個圖表,則應將旋轉代碼放在drawPie方法的開頭,而不是for循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM