Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8834619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:03:52+00:00 2026-06-14T09:03:52+00:00

I have code to draw a pie chart with value and random colors. Now

  • 0

I have code to draw a pie chart with value and random colors. Now I would like to rotate the entire figure, not one piece of pie. Here is my code:

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);
    }
}

In row AA when I write rotate, it cannot work normally? Can you help me? How can I rotate the entire chart?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:03:53+00:00Added an answer on June 14, 2026 at 9:03 am

    The argument of Graphics2D.rotate is in radians.

    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

    If you want to rotate the whole chart, you should put the rotate code at the beginning of the drawPie method, not in the for loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code to create a candlestick chart in rebol. Now I'd like
Is there any way to draw pie chart in WP7? If not, is there
I have the following code to draw candlestick visualization. But the visualization is not
I have code supposes to draw ring in random position within JPanel but for
I have following code to draw my logistic distribution: x=seq(-2000,2000,length=1000) dat <- data.frame(x=x) dat$value
I have the code that draw a point inside a polygon. Each time I
I have this code to draw an ellipse in the screen but I don't
I have the following code to draw text centered vertically (and horizontally) in a
I have the following code for writing draw calls to a back buffer canvas,
I want to draw image in place where I click. i have this code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.