CPS 1 - Spring - Jenkins 3/23
Java Graphics: Fractals and Animation
- Intro to Fractals
- Definition
- History
- Why study them?
- Image compression
- Study of nature
- Mathmatical properties
- Art - they're fun to make and pleasing to the eye
- Examples
- The Java you need to know to make your own fractals
- Graphics coordinate system
- (0, 0) is upper left corner
- (width-1, height-1) is lower right corner
- y coordinate is reverse from math coordinate plane - y decreases as you move up and increases as you move down
- Canvas class - area used for drawing
- Canvas drawingArea=new Canvas();
- drawingArea.resize(width, height);
- add(drawingArea);
- Graphics class - used for drawing on Canvas
- You can only get a Graphics instance after you have added the Canvas
- Graphics brush=drawingArea.getGraphics();
- brush.drawLine(x0, y0, x1, y1);
- brush.clearRect(x0, y0, width, height);
- Point class - used to story coordinates
- Point start=new Point(x0, y0);
- start.x is the integer x coordinate
- start.y is the integer y coordinate
- brush.drawLine(start.x, start.y, end.x, end.y);
- Color class
- Color c=new Color(percentRed, percentGreen, percentBlue);
- Color c=Color.blue;
- Available colors are: black, blue, cyan, darkGray, gray,
green, lightGray, magenta, orange, pink, red, white, & yellow
- Color c=Color.orange.brighter();
- Color c=Color.red.darker();
- brush.setColor(c);
- Example code
- Run the example applet
- Animation
- Why study it? Because it's:
- Useful for modeling/simulating
- Good for creating special effects in films
- Cool to show off animations on your web page
- Naive algorithm for animation
- Draw figure
- Erase figure
- Alter figure's position
- Redraw figure in its new position
- Repeat above until done
- Better algorithm
- Draw figure
- Alter figure's position
- Draw figure in new position
- Erase non-overlapping part of old figure
- Repeat above until done
- Ideal algorithm
- Draw image on screen
- Alter figure's position
- Draw new figure in an offscreen buffer
- Display new figure by putting the offscreen buffer on the screen
- Repeat above until done
- The Java you need to know to make animations
- Graphics ability from creating fractals
- Graphics method copyArea(x0, y0, width, height, dx, dy)
- (x0, yo) is the upper left corner of area to copy
- width and height describe how much to copy
- dx - amount to move right (or left if dx is negative)
- dy - amount to move down (or up if dy is negative)
- Must have "cushion" around area sufficient to erase non-overlapping part of previous draw
- Must know how to use while loops to alter coordinates
- Understand how machine speed affects animation speed
- Example of a simple bouncing ball program