1. Engineering
2. Computer Science
3. edit drawingjava to work as the comments within it direct...

# Question: edit drawingjava to work as the comments within it direct...

###### Question details

Edit Drawing.java to work as the comments within it direct. Create classes Triangle, Square, and Rectangle as the UML Class Diagram suggests. Uncomment code chunks within DrawDemo.java as you have the newly written classes ready. Do not change Drawable.java, DrawingComponent.java, Point.java, or Line.java.

Here are the UML and java files. Thx.

1.UML 2.DrawDemo.java

package Java;

import java.awt.event.*;

import javax.swing.*;

//import com.abc.draw.*;

//import com.abc.draw.geometry.*;

public class DrawDemo {

private static Drawing createDrawing() {

Drawing drawing = new Drawing();

Point p1 = new Point(50.0, 5.0);

Point p2 = new Point(300.0, 50.0);

Line line = new Line(p1, p2);

drawing.append(line);

/* Comment out until the class Triangle has been written

Triangle t1 = new Triangle(

new Point(10.0, 20.0),

new Point(10.0, 300.0),

new Point(50.0, 50.0));

drawing.append(t1);

Triangle t2 = new Triangle(

new Point(100.0, 100.0),

new Point(380.0, 170.0),

new Point(150.0, 300.0));

drawing.append(t2);

*/

/* Comment out until the class Square has been written

Square s = new Square(new Point(200.0, 200.0), 300.0);

s.setWidth(200.0); // just to see that this method works

drawing.append(s);

*/

/* Comment out until the class Rectangle has been written

Rectangle rect1 = new Rectangle(new Point(400.0, 50.0), 300.0, 1.0);

rect1.setHeight(130.0); // just to see that this method works

drawing.append(rect1);

*/

return drawing;

}

public static void main(String[] args) {

Drawing drawing = createDrawing();

DrawingComponent drawingComponent = new DrawingComponent(drawing);

JFrame.setDefaultLookAndFeelDecorated(true);

JFrame f = new JFrame("DrawDemo");

f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

f.setContentPane(drawingComponent);

f.setSize(800, 450);

f.setVisible(true);

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

}

}

3.Drawing.java

package Java;

import java.awt.*;

public class Drawing extends Object {

// FIXME - probably need a member variable or two...

private Drawable[] drawables;

private int drawableCount;

public Drawing() {

// FIXME - initialize

drawables = new Drawable;

drawableCount = 0;

}

public void drawAll(Graphics2D g2) {

// FIXME - Invoke draw(Graphics2D) on each of the Drawable's that

// have been added via append(Drawable).

for ( int i = 0; i < drawableCount; i++ ) {

Drawable d = drawables[i];

d.draw(g2);

}

}

public void append(Drawable drawable) {

// FIXME - Add the passed Drawable to the list of items that make up

// this Drawing.

drawables[drawableCount] = drawable;

drawableCount++ ;

}

//Classes for shapes.

public void Triangle(){

int p1;

int p2;

int p3;

}

public void Square(){

}

public void Rectangle(){

}

}

4.Drawble.java

package Java;

import java.awt.*;

public interface Drawable {

void draw(Graphics2D g2);

}

5.DrawingComponent.java

package Java;

import java.awt.*;

import javax.swing.*;

public class DrawingComponent extends JComponent {

private Drawing drawing;

public DrawingComponent(Drawing drawing) {

this.drawing = drawing;

}

protected void paintComponent(Graphics g) {

Graphics2D g2 = (Graphics2D) g;

g2.setPaint(Color.YELLOW);

Dimension size = getSize();

g2.fillRect(0, 0, size.width, size.height);

g2.setPaint(Color.BLUE);

drawing.drawAll(g2);

}

}

6.Point.java

package Java;

public class Point {

private double x;

private double y;

public Point(double x, double y) {

this.x = x;

this.y = y;

}

public double getX() {

return x;

}

public double getY() {

return y;

}

}

7.Line,java

package Java;

import java.awt.*;

//import com.abc.draw.*;

public class Line implements Drawable {

private Point p1;

private Point p2;

public Line(Point p1, Point p2) {

this.p1 = p1;

this.p2 = p2;

}

public Point getP1() {

return p1;

}

public Point getP2() {

return p2;

}

public void draw(Graphics2D g2) {

int x1 = (int) Math.round(p1.getX());

int y1 = (int) Math.round(p1.getY());

int x2 = (int) Math.round(p2.getX());

int y2 = (int) Math.round(p2.getY());

g2.drawLine(x1, y1, x2, y2);

}

}

###### Solution by an expert tutor 