Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In java how do you iterate over an array of line segments to draw them on a canv

ID: 3878964 • Letter: I

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 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);

                }

}

Explanation / Answer


I have updated the code with a for loop to iterate over the array and call the draw() method in LineSeqgment. I hope this helps. Not sure about what is needed for setMeanColor() method.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i


import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;

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());

for(int i = 0; i < array.length; i++)
array[i].draw(g, xoffset, yoffset, getHeight());

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote