160 likes | 180 Views
Graphics Concepts. CS 2302, Fall 2014. Drawing Paths. Random Diamonds. Create a method that will draw a random diamond The method will get a Canvas The class will have a static random number generator defined The center and width and height of the diamond will be randomly computed
E N D
Graphics Concepts CS 2302, Fall 2014
Random Diamonds • Create a method that will draw a random diamond • The method will get a Canvas • The class will have a static random number generator defined • The center and width and height of the diamond will be randomly computed • The fill and outline colors will be randomly chosen • The color value will be in a specified range
Random Coordinates • The width and height of the View used for drawing can be obtained with getWidth() and getHeight(), repsectively • The width times a random float (in the range 0 to 1) will give the x coordinate of the center • The y coordinate is similar, but uses the height • To determine the diamond's width • Compute the distance of the center from the left and right edges • Multiply the minimum by a float and then by 2 • The diamond' height is similar
Random Colors • Using RGB to pick random colors does not give very good colors • Using HSV to pick colors gives better results • The Value will be in the range from .25 to .75 • Specified in the diamonds resource file • The Hue and saturation will be randomly chosen over their entire range
Many Random Diamonds • Modify the diamond view to draw many diamonds at random • The number of diamonds is another resource
Interactive Diamonds View • This is the same as the basic diamond view except that every time the view is clicked, the number of diamonds is reduced by 1 • The view will contain a listener registered to 'this' • Once the number of diamonds has been reduced, the view must be redrawn • Note that, right now, clicking the view causes no visible change • Write a message to the log file (visible in the logcat view) to show that the listener is actually active
Interactive Diamonds View • This is the same as the basic diamond view except that every time the view is clicked, the number of diamonds is reduced by 1 • Copy the diamond view class and use the new class as a component • Define an onClick listener • Register the listener to 'this' • The Listener will reduce the number of diamonds • Note that, right now, clicking the view causes no visible change • Write a message to the log file (visible in the logcat view) to show that the listener is actually active
Forcing Redrawing • There is an internal change but not a visible change • The onDraw method must be executed somehow • However, we don't have a Canvas, necessary to call onDraw • There's no good way to create a useful one either • The system must call onDraw • So, we ask the system to call onDraw for us • This is what the method 'invalidate' does • It tells the system that the current state of the view is not valid, so it must be redrawn
Redrawing Restarts • Notice that the collection of diamonds displayed changes each time the number is reduced • To keep the same set of diamonds, the diamonds have to be stored so that they can be redrawn as they were • This requires • Keeping a list of diamonds • Representing diamonds
Representing Diamonds • Create a class that holds the essential values • Colors: fill and outline • Coordinates: left, top, right, bottom • This will be a private class inside the View so • The instance variables will be final and public • A constructor will set the instance variables
List of Diamonds • Create a list of Diamond objects in the View • The list will be initially null • Create a method to initialize the list to a random list of diamonds • Scavenge the code used to draw random diamonds
When to Initialize • Setting up the list is tricky • The constructor for the View is too soon since the dimensions of the View are not known then • The onDraw method can initialize the list, but should only do it once • OnDraw checks if the list is null and initializes the list if it is null
Drawing a Diamond • Modify the drawDiamond method to take a Diamond object instead of the separate parameters • Change the references in the body
OnDraw Changes • Change onDraw to • Check if the list of Diamond objects should be initialzied • Draw the list
Change Listener • The listener will remove a Diamond from the list • Check if the list is empty before doing that!