90 likes | 296 Views
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
E N D
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
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;
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; }
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)
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
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
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
Homework • Finish lab exercise • Read Chapters 2 and 3