Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

**** This queston is for Computer graphic class**** Write a program that draws a

ID: 670278 • Letter: #

Question

**** This queston is for Computer graphic class****

Write a program that draws a square ABCD. The points A and B are arbitrarily specified by the user by clicking the mouse button. The orientation of the points A, B, C and D should be counter-clockwise. Answers After rotating the vector v = (v1, v2) through 90 degree counter-clockwise, we obtain the vector (- v2, v1). Setting v = (v1, v2)= (xB - xA, yB - yA) we 030 therefore find the points D and C by adding (-v2, v1) to the coordinates of A and B, respectively.

Explanation / Answer

01       import java.awt.*; 02 import java.awt.event.*; 03 04 public class DefSquare extends Frame 05 {   public static void main(String[] args){new DefSquare();} 06 07     DefSquare() 08     {   super("Define a Square"); 09         addWindowListener(new WindowAdapter() 10             {public void windowClosing(WindowEvent e){System.exit(0);}}); 11         setSize(500,300); 12         add("Center", new CvDefSquare()); 13         setCursor(Cursor.getPredefinedCursor(CROSSHAIR_CURSOR)); 14         show(); 15     } 16 } 17 18 class CvDefSquare extends Canvas 19 {   int centerX, centerY; 20     float x1,y1,x2,y2; 21 22     CvDefSquare() 23     {   addMouseListener(new MouseAdapter() 24         {   public void mousePressed(MouseEvent click) 25             {   float xP=fx(click.getX()), yP=fy(click.getY()); 26                 x1 = xP; y1 = yP; 27                 float xP2=fx(click.getX()), yP2=fy(click.getY()); 28                 x2=xP2; y1=yP2; 29                 repaint(); 30             } 31         }); 32     } 33 }