Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class DashLine { public static void main ( String [ ] args ) { new FrDashLine ( ) ; } } class FrDashLine extends

public class DashLine
{ public static void main(String[] args){new FrDashLine();}
}
class FrDashLine extends Frame
{ CvDashLine cv = new CvDashLine();
FrDashLine()
{ super("Dashed lines");
addWindowListener
( new WindowAdapter()
{ public void windowClosing(WindowEvent e){System.exit(0);}
}
);
setSize(500,300);
add("Center", cv);
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
show();
}
}
class CvDashLine extends Canvas
{ int xA, yA, xB, yB;
int np =0;
CvDashLine()
{ addMouseListener
( new MouseAdapter()
{ public void mousePressed(MouseEvent evt)
{ int X = evt.getX(), Y = evt.getY();
if (np ==2) np =0;
np++;
if (np ==1){xA = X; yA = Y;} else {xB = X; yB = Y;}
repaint();
}
}
);
}
public void paint(Graphics g)
{ if (np <2) return;
g.drawRect(xA, yA, xB - xA, yB - yA);
int xP =(3* xA + xB)/4, yP =(3* yA + yB)/4,
xQ =(xA +3* xB)/4, yQ =(yA +3* yB)/4;
Lines.dashedLine(g, xP, yP, xQ, yP,20);
Lines.dashedLine(g, xQ, yP, xQ, yQ,20);
Lines.dashedLine(g, xQ, yQ, xP, yQ,20);
Lines.dashedLine(g, xP, yQ, xP, yP,20);
Lines.dashedLine(g, xA, yA, xP, yP,20);
Lines.dashedLine(g, xB, yA, xQ, yP,20);
Lines.dashedLine(g, xB, yB, xQ, yQ,20);
Lines.dashedLine(g, xA, yB, xP, yQ,20);
}
}
class Lines
{ static void dashedLine(Graphics g, int xA, int yA, int xB, int yB,
int dashLength)
{ float u1= xB - xA, u2= yB - yA,
L =(float)Math.sqrt(u1* u1+ u2* u2);
int n = Math.round((L/dashLength +1)/2);
float h1= u1/(2* n -1), h2= u2/(2* n -1);
for (int i=0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions