Using Dr.Java

Objective:

Write a bunch of classes that will draw a bunch of lines! Don’t worry you don’t have to write any graphics, as that part is provided in the driver. Each line is drawn based on a math function that takes in a given x coordinate and will return its y coordinate.

First download the driver and put it in your project

DO NOT ALTER THE DRIVER!

**Write an interface called Line**

Create the following method definition

getYPoint: This takes in a decimal value and returns a decimal value depending on the type of line.

**Write a class called SlopedLine**

This should implement Line

Instance variable

slope: a decimal value corresponding to the line’s slope

Create the following Constructors

Default

Parameterized Constructor

Accessors and Mutators for each variable

Create the following Methods

getYPoint – this method takes in a decimal value corresponding to a x-coordinate and returns the y-coordinate based on the slope equation (y = slope*x)

**Write a class called ExponentialLine**

This should implement Line

Instance variable

exponent: a decimal value corresponding to the line’s exponent

Create the following Constructors

Default

Parameterized Constructor

Accessors and Mutators for each variable

Create the following Methods

getYPoint – this method takes in a decimal value corresponding to a x-coordinate and returns the y-coordinate based on the slope equation (y = x^exponent)

**Write a class called SineLine**

This should implement Line

Instance variable

amplitude: a decimal value corresponding to the line’s
amplitude

frequency: a decimal value corresponding to the line’s
frequency

Create the following Constructors

Default

Parameterized Constructor

Accessors and Mutators for each variable

Create the following Methods

getYPoint – this method takes in a decimal value corresponding to a x-coordinate and returns the y-coordinate based on a sine wave equation (y = amplitude*sin(x*frequency))

**Write a class called SawLine**

This should implement Line

Instance variable

modValue: a decimal value corresponding to the modulo peak of the wave

Create the following Constructors

Default

Parameterized Constructor

Accessors and Mutators for each variable

Create the following Methods

getYPoint – this method takes in a decimal value corresponding to a x-coordinate and returns the y-coordinate based on the equation (y = x mod modValue)

**Write a class called StaircaseLine**

This should implement Line

Instance variable

width: a decimal value corresponding to the stair’s width

height: a decimal value corresponding to the stair’s height

Create the following Constructors

Default

Parameterized Constructor

Accessors and Mutators for each variable

Create the following Methods

getYPoint – this method takes in a decimal value corresponding to a x-coordinate and returns the y-coordinate based the width and height. HINT(using integer division and multiplying that by the height will achieve the effect).

HINT: If the lines are looking weird it may be a good idea to print out each of the coordinates and observing what is going on in the method getYPoint

**//The Driver**

/*

*

*/

import java.applet.*; //Needed to create the window

import java.awt.*; //Needed for drawing

import java.util.*;

public class DrawingLines extends Applet{//When it extends applet it creates a window and calls certain methods

private Image display; //Used as the flat image

private Graphics drawingArea; //Used to draw item in the image

/*

*This is called by the parent Applet, as its an overridden method, and it initializes all of the instance variables.

* Think of this as a variation of a constructor, but called by another, hidden piece of code

*/

public void init()

{

//get the height and width from the Applet

int height = getSize().height;

int width = getSize().width;

//Creates an image using the height and width in the applet

display = createImage(width,height);

//Sets up the drawing area for the image above to be drawn on

drawingArea = display.getGraphics();

//This draws the lines

drawLines(drawingArea);

}

/*

* This is also called by

* the applet as it is an overridden method.

*/

public void paint(Graphics g)

{

g.drawImage(display,0,0,null);

}

/*

* This Draws line!

*/

public void drawLines(Graphics g)

{

Color[] colors = {Color.black,Color.blue,Color.red,Color.green,Color.darkGray};

Line[] lines = new Line[5];

lines[0] = new SlopedLine(1);

lines[1] = new SineLine(100,0.25);

lines[2] = new ExponentialLine(1.25);

lines[3] = new StaircaseLine(20,20);

lines[4] = new SawLine(50);

final int X_POINTS = 1000;

for(int i=0;i<X_POINTS;i++)

{

for(int j=0;j<lines.length;j++)

{

if(lines[j]==null)

continue;

g.setColor(colors[j]);

g.drawLine(i, getSize().height - (int)lines[j].getYPoint(i), i+1, getSize().height - (int)lines[j].getYPoint(i+1));

}

}

}

}

**Example Dialog**

Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.

Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.

Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.