lecture_21_s2005

# Lecture_21_s2005 - 1.00 Lecture 21 2D API 2D Transformations Reading for next time Numerical Recipes p 32-36 Just read the text dont worry about

This preview shows pages 1–4. Sign up to view the full content.

1.00 Lecture 21 2D API 2D Transformations Reading for next time: Numerical Recipes p. 32-36 Just read the text; don’t worry about reading the C code Clock, revisited We’ll use the model-view-controller version of the clock and draw with the 2D API (application programming interface): Download ClockController, ClockModel, ClockView

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Clock View with 2D API import java.awt.*; import javax.swing.*; import java.awt.geom.*; public class ClockView extends JPanel { private ClockModel model; private static final double CD= 200; // Clock diameter private static final double X= 100; // Dist from upper lh corner private static final double Y= 50; // Dist from upper lh corner private static final double XC= X + CD/2; // Clock center x private static final double YC= Y + CD/2; // Clock center y private static final double HR= 0.3F*CD; // Size of hour hand private static final double MI= 0.45F*CD; // Size of minute hand public ClockView(ClockModel cm) { model = cm; } // Continued Clock View with 2D API, p.2 public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; // Cast g to g2 context double minutes= model.getMinutes(); double hourAngle = 2*Math.PI * (minutes - 3 * 60) / (12 * 60); double minuteAngle = 2*Math.PI * (minutes - 15) / 60; Ellipse2D.Double e = new Ellipse2D.Double(X, Y, CD, CD); Line2D hr= new Line2D.Double(XC, YC, XC+(HR*Math.cos(hourAngle)), YC+ (HR * Math.sin(hourAngle)) ); Line2D mi= new Line2D.Double(XC, YC, XC+ (MI* Math.cos(minuteAngle)), YC+ (MI * Math.sin(minuteAngle)) ); g2.setPaint(Color.BLUE); BasicStroke bs= new BasicStroke(5.0F, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); g2.setStroke(bs); g2.draw(e); g2.draw(hr); g2.draw(mi); } }
Exercise Add the two lines and arc in paintComponent() to create the picture shown in the first slide – Line2D.Double(double x0, double y0, double x1, double y1) Draws a line from (x0, y0) to (x1, y1) Make your line length = clock diameter / 4 – Arc2D.Double(double x, double y, double w, double h, double start, double extent, int type) Draws an arc with upper left hand corner (x,y), width w and height h. These first 4 arguments are the same as the ellipse or circle arguments Start is the start angle, in degrees Extent is the angle of the arc, in degrees • Type is a style; use Arc2D.OPEN Optional: Draw the hour and minute hands in different colors and different line widths. Affine Transformations The 2D API provides strong support for affine transformations . Affine means linear ( of the form y= ax +b) An affine transformation maps 2D coordinates so that the straightness and parallelism of lines are preserved.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 11/29/2011 for the course CIVIL 1.00 taught by Professor Georgekocur during the Spring '05 term at MIT.

### Page1 / 14

Lecture_21_s2005 - 1.00 Lecture 21 2D API 2D Transformations Reading for next time Numerical Recipes p 32-36 Just read the text dont worry about

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online