free web hosting | free hosting | Business Hosting | Free Website Submission | shopping cart | Promoter Online | php hosting
affordable web hosting Pets web page hosting web hosting website hosting web hosting service web hosting web host
Drawing Measures in Java

Drawing Measures in Java

This page describes how to draw a measure that indicates the distance between two points on an image. Click here to have a look at the measure on the Plexippus setipes image. A measure here, is a line with two edges on both ends.

So, what kinds of equation would hold for the coordinates of the edges? Here are two equations that should represent the edge lines. The first equation defines the point where the distance from the end point is a constant, say r. The second equation represents the line that runs vertical to the original line, which also should go through the end point.

Equations 1. Edge Line Equations

The figure below depicts the above relations.

Figure 1. Geometric Relations of the Edge Line

Now, let's solve the equation. Let's replace the variables to X, Y, s as defined below for convenience.

Equations 2. X, Y, and s

Let's solve for x now. Equations 3 show the Equations 1 with X, Y and s as defined in Equations 2.

Equations 3. Solving for x

Equations 4 below shows x as functions of s and r.

Equations 4. Solving for x

Equations 5 below shows y as functions of x, s and x0.

Equations 5. Solving for y

Now, let's take a look at how those can be coded in Java langauge. The function drawEdge(int, int, int, int, Graphics) draws the edge line, which runs perpendicular to the original 'measure' line, where the length of the line is 2r.

    int r=15;

    public void drawEdge(int x0, int y0, int x1, int y1, Graphics g)
    {
        int xx0,yy0,xx1,yy1;
	if(y0==y1)
	    g.drawLine(x0,(int)(y0-r),x0,(int)(y0+r));
	else
	    {
		double s=(double)-(x0-x1)/(y0-y1);
		double A=r/Math.sqrt(1+s*s);
		xx0=(int)(x0+A);
		xx1=(int)(x0-A);
		yy0=(int)(y0+s*((x0+A)-x0));
		yy1=(int)(y0+s*((x0-A)-x0));
		g.drawLine(xx0,yy0,xx1,yy1);
	    }
    }


Return to Home
Erica Asai
Last Modified: Tue Dec 14 01:51:44 2004