Lab 7

advertisement
Lab 7: Tree & Forest
Drawing a Forest
Created by Emily Hill & Jerry Alan Fails
Drawing a Forest
Step 1: Create classes for Rectangle & Triangle (done)
Step 2: Create a Tree class that can draw a tree at
any: x, y position; width, height; color
Step 3: Create a Forest class with at least 3 Trees
Created by Emily Hill & Jerry Alan Fails
Step 2: Tree class
• Create a new Tree class
• Has 2 fields: a triangle for the top & a rectangle for the trunk
• Has 3 constructors:
– Default
– One with 4 parameters: x, y, width, & height
– One with 6 parameters: x, y, width, height, topColor, trunkColor
• Getters/setters for topColor, trunkColor
• Has a public setBounds method that takes: x, y, width, height
– (Might want to add this method to Triangle and Rectangle, but you don’t
need to as you can use setX, setY, setWidth, and setHeight)
– Tree should start at x, y and extend to width & height
– Top and trunk should be relative to one another and to size of tree:
• Trunk 1/4 height of tree, 1/5 width
• Top 3/4 height of tree, full width
• Has a paintComponent method that has a Graphics parameter and
calls the corresponding paintComponent methods for the 2 fields
• Test: add Tree(s) to Picture class, use different constructors,
change the size of the Tree in the constructor or init method and
Created by Emily Hill & Jerry Alan Fails
test that it works
Step 3: Forest class
•
•
•
•
Create a new Forest class
Has at least 3 Tree fields
Has a constructor that takes 4 parameters: x, y, width, height
Has a draw method that has a Graphics parameter and calls
the corresponding draw methods for the 3 fields
• Test: replace Tree field in Picture with a Forest
• Modify position/sizes in PictureApplet.init() or the Picture
constructor
• Look at how easy it is to aggregate components!
Created by Emily Hill & Jerry Alan Fails
Homework
• Finish lab exercise (due after exam)
Created by Emily Hill & Jerry Alan Fails
General Review
•
•
•
•
•
Lecture notes
Class exercises
Labs
CodingBat
Chapters 1-4
– Where
• Chapter 1
• Chapter 2
• Chapter 3 (except 3.7-3.8, 3.10-3.11)
• Chapter 4 (except 4.8-4.9)
– What:
• Key concepts
• Concept summary
• Section headings
• For extra help look at the exercises
Chapter 1: Computer Basics
•
•
•
•
•
•
Computer processing
Hardware components
Networks
The Java programming language
Program development
Object-oriented programming
Chapter 2: Data & Expressions
•
•
•
•
•
•
•
Character strings
Variables and assignment
Primitive data types
Expressions
Data conversions
Interactive programs – the Scanner class
Graphics, Applets, drawing shapes
Chapter 3: Using Classes and Objects
•
•
•
•
Creating objects
The String class
Packages – import, Java API
Classes: Random, Math
Chapter 4: Writing Classes
• Classes and objects
• Anatomy of a class
– Three main components of a class
– What each item does
– How to write it in code
• Encapsulation
Download