1 / 16

CS 2302, Fall 2014

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

rshults
Download Presentation

CS 2302, Fall 2014

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. Graphics Concepts CS 2302, Fall 2014

  2. Drawing Paths

  3. 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

  4. 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

  5. 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

  6. Many Random Diamonds • Modify the diamond view to draw many diamonds at random • The number of diamonds is another resource

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. Drawing a Diamond • Modify the drawDiamond method to take a Diamond object instead of the separate parameters • Change the references in the body

  15. OnDraw Changes • Change onDraw to • Check if the list of Diamond objects should be initialzied • Draw the list

  16. Change Listener • The listener will remove a Diamond from the list • Check if the list is empty before doing that!

More Related