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

  • SEARCH
  • Home
  • 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 8845319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:42:38+00:00 2026-06-14T11:42:38+00:00

I would like to rotate pie chart about his center, how can I do

  • 0

I would like to rotate pie chart about his center, how can I do this? I would like to rotate whole pie chart. This is my code:

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import javax.swing.JComponent;
import javax.swing.JFrame;

class Slice{

    double value;
    Color color;
    public Slice(double _value){
        Random numGen = new Random();
        int R =  numGen.nextInt(256);
        int G = numGen.nextInt(256);
        int B = numGen.nextInt(256);
        this.setColor(new Color(R,G,B));
        this.value = _value;    
    }
    public void setColor(Color _color){
        this.color = _color;

    }
}



class Component extends JComponent {

    int movx = 0;
    int movy = 0;
    private double angle = 0;
    private double radius = 0.0D;
    private  Random numGen;
    private int w = 500;
    private int h = 500;



    Slice[] slice = {new Slice(5),new Slice(20),new Slice(33),new Slice(42)};
    public Component(){
        numGen = new Random();
        Listener listener = new Listener();
        addMouseListener(listener);
        addMouseMotionListener(listener);
    }

    class Listener extends MouseAdapter{
        private double LastAngle;
        @Override
        public void mousePressed(MouseEvent e) {
            LastAngle = getAngle(e.getPoint());
        }
        @Override
        public void mouseDragged(MouseEvent e) {
            double curAngle = getAngle(e.getPoint());
            double deltaAngle = curAngle - LastAngle;
            angle += deltaAngle;
            repaint();
            LastAngle = curAngle;
        }
        private double getAngle(Point point) {
            double x = point.getX() - 0.5 * 500;
            double y = point.getY() - 0.5 * 500;
            return Math.atan2(y, x);
        }

    }

    public void paint(Graphics g){
        Graphics2D g2 = (Graphics2D)g;
       //g2.translate(250, 250);
        g2.rotate(angle,w/2-5,h/2-5);
        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;
       radius = total/2*Math.PI;
        double curentValue = 0.0D;
        int startAngle = 0; 
        for(int i = 0;i<s.length;i++){

            //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(angle);//row AA
           for(int j=1;j>0;j--){
            g.fillArc(j, j, w, h, startAngle, arcAngle);   

           }
            curentValue+=s[i].value;

        }
    }


}

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(600,600);
        frame.setVisible(true);
    }
}

How can I do this. I write w/2,h/2, because in fillArc I write width and heght 500, but it not rotate about the center.

  • 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-14T11:42:40+00:00Added an answer on June 14, 2026 at 11:42 am

    You are using

    g2.rotate(angle,w/2-5,h/2-5);
    

    which will rotate the pie chart about an off-center point. You could use:

    g2.rotate(angle, (w / 2) + 1, (h / 2) + 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a java method to rotate an image like this , I would
I have a TShape on my form. and would like to rotate it. Can
I would like to build turret, that can rotate in all axis. So the
I would like to rotate an image in 360 degrees, like this site .
I rotate a UILabel 30 degree. In this case, I would like to get
I would like to know is this method the right way to go about
I've got a div that I would like to rotate about its y-axis (with
I would like to spin/rotate my Logo(Image) on the Z axis. This is the
I would like to rotate a rectangle around its center point and it should
I would like to rotate a histogram in R, plotted by hist(). The question

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.