Workshop: Baking a Pie Graph

To draw this hour to a close, you will create PiePanel, a graphical user interface component that displays a pie graph. This component will be a subclass of JPanel, a simple Swing container that's useful as a place to draw something.

One way to begin creating a class is to define the way objects of the class will be created. Programs that use the PiePanel class must undertake the following steps:

  • Create a PiePanel object by using the constructor method PiePanel(int). The integer specified as an argument is the number of slices the pie graph will contain.
  • Call the object's addSlice(Color, float) method to give a slice the designated color and value.

The value of each slice in PiePanel is the quantity represented by ...

Get SAMS Teach Yourself Programming with Java™ in 24 Hours, FOURTH EDITION now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.