1 / 9

Lab 4: Circle Outline

Lab 4: Circle Outline. Basic class structure. public class ClassName { Fields Constructors Methods }. Three major components of a class: Fields – store data for the object to use Constructors – allow the object to be set up properly when first created

gilles
Download Presentation

Lab 4: Circle Outline

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab 4: Circle Outline

  2. Basic class structure public class ClassName { Fields Constructors Methods } Three major components of a class: • Fields – store data for the object to use • Constructors – allow the object to be set up properly when first created • Methods – implement the behavior of the object

  3. Fields store values for an object. They are also known as instance variables. Fields define the state of an object. Fields public class Square { private int x; private int y; private int size; private Color fillColor; // Further details omitted. } type visibility modifier variable name private int size;

  4. Constructors • Constructors initialize an object. • They have the same name as their class. • They store initial values into the fields. • They often receive external parameter values for this. public Square(int x, int y) { this.x = x; this.y = y; size = 50; fillColor = Color.blue; }

  5. Methods /** * Gets the size of the square. */ method header/signature return type visibility modifier method name parameter list (empty) public int getSize() { return size; } return statement start and end of method body (block)

  6. Method & Field Practice • Modify the Circle class to display a colored outline: • Add a field outlineColor, plus get & set methods • Add another constructor that takes the outline color as a parameter • What should the outline color be set to in the original constructors? • Update paintComponent to draw the outline AND the filled oval (hint: use drawOval) • Use your Picture applet to test and make sure the outline is showing up correctly

  7. Extra: Change to Square & Triangle • Make similar changes to Square and Triangle that you did to Circle • Add a constructor that accepts as parameters the initial x and y positions, radius, and color • Add a field outlineColor, plus get & set methods • Add another constructor that takes the outline color as a parameter • What should the outline color be set to in the original constructors? • Update paintComponent to draw the outline AND the filled oval • Use your Picture applet to test and make sure the outline is showing up correctly

  8. What we did today … • Concepts practiced • Constructors • Methods • Classes • Modified the Circle, Square, and Triangle classes: • Added the ability to have a different colored outline

  9. Homework • Finish lab exercise • Read Chapters 2 and 3

More Related