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.


Drawing einterfaces Drawable +drawAll(g:Graphics2D) +append(d:Drawable) +drawg:Graphics2D) Square Add Square and Rectangle to this diagram. +init (upperLeft:Point, width:double) +getUpperLeft):Point +getWidth0:double +setWidth(w.double) +draw(g: Graphics2D) Triangle p1:Point -p2:Point -p3:Point +init (p1:Point, p2:Point, p3:Point) +getP10:Point +getP20 Point +getP30:Point +draw(g:Graphics2D) Rectangle +init (upperLeft:Point, width:double, height double) +getUpperLeft):Point +getWidth():double +setWidth(w.double) +getHeight() double +setHeight(h:double) +draw(g:Graphics2D Point iit(x:double, y:double) +getX():double +getY():double


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


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


Triangle t2 = new Triangle(

new Point(100.0, 100.0),

new Point(380.0, 170.0),

new Point(150.0, 300.0));



/* 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



/* 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



return drawing;


public static void main(String[] args) {

Drawing drawing = createDrawing();

DrawingComponent drawingComponent = new DrawingComponent(drawing);


JFrame f = new JFrame("DrawDemo");



f.setSize(800, 450);


f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {







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[1000];

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




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




package Java;

import java.awt.*;

public interface Drawable {

void draw(Graphics2D g2);



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;


Dimension size = getSize();

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






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;




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
Blurred Solution
This question has been solved
Subscribe to see this solution