Question: In java how do you iterate over an array of line segments to draw them on a canvas given the code below: class PlotCanvas extends

In java how do you iterate over an array of line segments to draw them on a canvas given the code below:

class PlotCanvas extends Canvas {

// lines for plotting axes and mean color locations

LineSegment x_axis, y_axis;

LineSegment red, green, blue;

boolean showMean = false;

public PlotCanvas() {

x_axis = new LineSegment(Color.BLACK, -10, 0, 256+10, 0);

y_axis = new LineSegment(Color.BLACK, 0, -10, 0, 200+10);

}

// set mean image color for plot

public void setMeanColor(LineSegment[] array) {

//main program calls to here with an array of line segments

}

// redraw the canvas

public void paint(Graphics g, LineSegment[] array) {

// draw axis

int xoffset = (getWidth() - 256) / 2;

int yoffset = (getHeight() - 200) / 2;

x_axis.draw(g, xoffset, yoffset, getHeight());

y_axis.draw(g, xoffset, yoffset, getHeight());

}

}

// LineSegment class defines line segments to be plotted

class LineSegment {

// location and color of the line segment

int x0, y0, x1, y1;

Color color;

// Constructor

public LineSegment(Color clr, int x0, int y0, int x1, int y1) {

color = clr;

this.x0 = x0; this.x1 = x1;

this.y0 = y0; this.y1 = y1;

}

public void draw(Graphics g, int xoffset, int yoffset, int height) {

g.setColor(color);

g.drawLine(x0+xoffset, height-y0-yoffset, x1+xoffset, height-y1-yoffset);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!