java code to create a yardstick needed.

Oct 26, 2011
Reaction score
hey everyone, I have a project due in the morning but need some help in finishing my project:
it goes as follows

Project Description
Java’s class Graphics2D extends the Graphics class has the following method to draw a shape: public void draw(Shape). java.awt.geom.Line2D.Double implements the Shape interface and represents a line segment specified with double coordinates. Its constructor, Line2D.Double(double X1, double Y1, double X2, double Y2), constructs and initializes a Line2D from the specified coordinates. Graphics uses a coordinate system that measures points from the top left corner. You are to use these classes (Graphics2D and Line2D.Double) to draw the image for this project.
Write a recursive method that draws a picture of a section of a yardstick. Mark inches, half inches, quarter inches, eighth inches, and sixteenth inches. Mark the half inches with marks that are smaller than those that mark the inches. Mark the quarter inches with marks that are smaller than those that mark the half inches, and so on. Your graphic is not intended to show an entire yardstick (which is 3 ft. long). It should look something like this,

Hint: Draw a mark in the middle of the graphic and then draw marks to the left and right of the mark. Don’t worry if the middle mark is slightly larger than its equivalence – I’m not looking for perfection.

Follow all commenting conventions mentioned in class. Your program must have a comment block at the top of the main method describing the program purpose, input, and output. Name your class containing the main method and your class that does the drawing is where you create a frame and then you add an instance of to the frame.

here is my code for the driver:

import javax.swing.JFrame;

public class UseYardStick

public static void main(String[]args)

JFrame jf = new JFrame("A Yardstick");
jf.add(new Yardstick());
jf.setSize(600, 300);


and here is the code for the yardstick

import javax.swing.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;

public class Yardstick extends JComponent
private int height;
private int mid;
Line2D.Double verticalLineMarks = new Line2D.Double();

public void paintComponent(Graphics g)

Graphics2D g2 = (Graphics2D) g;
Line2D.Double line = new Line2D.Double(0,150,600,150);
g2.draw(line) ; //Display the horizontal Line

drawMarks(g, 115, mid, height); //the recursive call

private void drawMarks(Graphics g, int x, int y, int height)
if (height<1)
int mid = (x + y)/2;
Line2D.Double verticalLine = new Line2D.Double(mid, 150, mid, 150 - height*5);
drawMarks(g, x, mid, height-1);
drawMarks(g, mid, y, height-1);



i need help with using recursion in completing this project.


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question