Results 1 to 4 of 4
  1. #1
    ubuntu_jdk is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Explanation of Java code

    I have this code that I found on the internet, but there isnt any explanation for it. Now before I use it in my projects I want to understand what it does. I am trying to make pie chart.

    The code:

    Java Code:
     import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    
    class Slice {
       double value;
       Color color;
       public Slice(double value, Color color) {  
          this.value = value;
          this.color = color;
       }
    }
    class MyComponent extends JComponent {
    	StatButtons sb = new StatButtons();
    	int var1 = 3;
    	int var2 = 2;
    	
    	
       Slice[] slices = { new Slice(var1, Color.red), 
       new Slice(var2, Color.green) };
       MyComponent() {}
       public void paint(Graphics g) {
          drawPie((Graphics2D) g, getBounds(), slices);
       }
       void drawPie(Graphics2D g, Rectangle area, Slice[] slices) {
          double total = 0.0D;
          for (int i = 0; i < slices.length; i++) {
             total += slices[i].value;
          }
          double curValue = 0.0D;
          int startAngle = 0;
          for (int i = 0; i < slices.length; i++) {
             startAngle = (int) (curValue * 360 / total);
             int arcAngle = (int) (slices[i].value * 360 / total);
             g.setColor(slices[i].color);
             g.fillArc(area.x, area.y, area.width, area.height, 
             startAngle, arcAngle);
             curValue += slices[i].value;
          }
       }
    }
    Can someone explain why we need the Rectangle class? What does getBounds() and Rectangel area do?

    What is the diffrence between Graphics class and Graphics2D class? (java.awt.Graphics and java.awt.Graphics2D)

    I have read the JAVA API and tried to search on google, but I couldnt get closer to a explanation. I am new to Java, so maybe there is some simple terms I havent understood yet. But anyways any help would be greatly apreciated.
    Last edited by ubuntu_jdk; 05-29-2013 at 09:29 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Why do we need the Rectangle class?

    I think of a Rectangle class as a convenience class. You can use it for setBounds and also in SwingUtilities in finding intersections and unions. It is also used in repainting clip regions.

    Graphics2D is a subclass of Graphics with more functionality. It was added in JDK 1.1. In most cases since 1.1 you can cast a Graphics instance to a Graphics2D instance.

    Edit: Actually, upon further "reflection," no pun intended, the Rectangle class is probably a necessity. One can easily set the values in a method call with four parameters. But a Rectangle class is really necessary to retrieve them efficiently.

    Regards,
    Jim
    Last edited by jim829; 05-30-2013 at 01:20 AM. Reason: Additional info
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    ubuntu_jdk is offline Member
    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Default Re: Explanation of Java code

    Thanks for the reply. So what does getBounds do exactly?
    And why do I call arguments for fillArC with area.x, area.y, area.width, area.height from Rectangle?

    Is this coordinates as where to draw the pie?
    Last edited by ubuntu_jdk; 05-29-2013 at 09:44 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Explanation of Java code

    getBounds returns the bounding rectangle of the component (its location and extent in its parent's coordinate system).

    For fillArc, checkout Graphics (Java Platform SE 7 )

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Rectangle Class
    By RiterHeng in forum New To Java
    Replies: 1
    Last Post: 05-03-2012, 07:10 PM
  2. Rectangle class again
    By toppcon in forum New To Java
    Replies: 2
    Last Post: 05-05-2011, 03:25 AM
  3. Rectangle class with no argument constructor
    By toppcon in forum New To Java
    Replies: 1
    Last Post: 05-05-2011, 01:37 AM
  4. Wrong with Rectangle res = new Rectangle(0,0,0,0);???
    By jiapei100 in forum AWT / Swing
    Replies: 3
    Last Post: 09-25-2010, 03:39 PM
  5. help with rectangle class
    By darkgt in forum New To Java
    Replies: 7
    Last Post: 11-14-2007, 06:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •