2
\$\begingroup\$

Task

You should create an abstract Shape class that contains the stuff that would be similar across all shapes, and change Line and Rectangle so that they extend Shape. Then, in the DrawingModel class, you should be able to create a single array of Shape objects and add both lines and rectangles to that list. In your DrawingCanvas class, you should be able to iterate through this single ArrayList of shapes and draw all the shapes, in the order they were added.

Code:

DRAWING CANVAS:

package drawingprogram;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import javax.swing.JPanel;

/**
* This class creates a drawing canvas and extends JPanel
* We do this so that we can override JPanel's paintComponent method,
* so we can control what is drawn
* @author clatulip
*/
public class DrawingCanvas extends JPanel {

private DrawingModel myModel;

/**
* Constructor
* Set the background of the canvas to white
*/
public DrawingCanvas () {
this.setBackground(Color.WHITE);
}

/**
* Need this so we can pass in the model
* @param model
*/
public void setModel(DrawingModel model) {
myModel = model;
}


protected void paintComponent(Graphics g) {
// first, check to make sure we have a model,
// otherwise there is nothing to draw
if (myModel != null) {
super.paintComponent(g);
// get the graphics context for drawing (don't change this)
Graphics2D g2d = (Graphics2D) g.create();

// get the Line ArrayList from the model, and
// iterate through it, telling each Line object to draw itself
ArrayList lines = myModel.getLines();
for (Line l : lines) {
l.draw(g2d);
}

// get the Recatngle ArrayList from the model, and
// iterate through it, telling each Rectangle object to draw itself
ArrayList rects = myModel.getRects();
for (Rectangle r : rects) {
r.draw(g2d);
}

// see if user's mouse is down and is currently drawing
// this is to show a shape while the user is drawing
// interactive feedback is important
// you can comment this section out and see what happens if you
// don't believe how important this is...
if (myModel.isMouseDown()) {

switch (myModel.getCurrentShape()) {
case LINE:
Line.draw(g2d,
myModel.getStartX(),
myModel.getStartY(),
myModel.getCurrentX(),
myModel.getCurrentY(),
myModel.getLineColor()
);
break;
case RECT:
Rectangle.draw(g2d,
myModel.getStartX(),
myModel.getStartY(),
myModel.getCurrentX(),
myModel.getCurrentY(),
myModel.getLineColor(),
myModel.getFillColor()
);

break;
}
}
// done with drawing, get rid of graphics context (don't change this)
g2d.dispose();
}
}

public void update() {
// this calls the parent class repaint method,
// which in turn forces the paintComponent method to be called
repaint();
}
}

DRAWING MODEL

package drawingprogram;

import java.awt.Color;
import java.io.File;
import java.util.ArrayList;

/**
* This model holds everything that the user has added to the canvas,
* and keeps track of the current state (i.e. selected color and shape, etc.)
* @author clatulip, for ITIS 1213 @ UNC Charlotte
*/
public class DrawingModel {
// arrays to hold the shapes
private ArrayList lines = new ArrayList<>();
private ArrayList rects = new ArrayList<>();
private ArrayList<Shape>shapeList=new ArrayList<>();

// enumerated type that defines the different shapes that can be drawn
public enum ShapeType {LINE, RECT}

// currently selected shape and color
private ShapeType currentShape;
private Color lineColor;
private Color fillColor;

// variables to make drawing real-time interactive
private boolean mouseDown; // is the mouse pressed down?
private int startX; // x coord where mouse pressed down
private int startY; // y coord where mouse pressed down
private int currentX; // currentX pos
private int currentY; // currentY pos


//constructor
public DrawingModel() {
currentShape = ShapeType.LINE;
lineColor = Color.BLACK;
fillColor = Color.WHITE;
mouseDown = false;
currentX = 0;
currentY = 0;
}

/**
* Adds a new Line object to the drawing model
* @param line Line object
*/
public void addNewLine(Line line) {
lines.add(line);
}

/**
* Adds a new Rectangle object to the drawing model
* @param rect Rectangle object
*/
public void addNewRect(Rectangle rect) {
rects.add(rect);
}

/**
* Gives you the list of lines in the model
* @return ArrayList
*/
public ArrayList getLines() {
return lines;
}

/**
* Gives you the list of rectangles in the model
* @return ArrayList
*/
public ArrayList getRects() {
return rects;
}

/**
* Removes all shapes the user has drawn
*/
public void clearModel() {
lines.clear();
rects.clear();
}

/**
* Returns the currently selected shape type (what the user would draw if they
* clicked and dragged).
* @return ShapeType
*/
public ShapeType getCurrentShape() {
return currentShape;
}

/**
* Sets the current shape type (what the user will draw if they click and drag)
* @param currentShape
*/
public void setCurrentShape(ShapeType currentShape) {
this.currentShape = currentShape;
}

/**
* Returns the current lineColor for drawing
* @return Color
*/
public Color getLineColor() {
return lineColor;
}

/**
* Sets the current lineColor for drawing
* @param lineColor Color
*/
public void setLineColor(Color lineColor) {
this.lineColor = lineColor;
}

/**
* Allows you to check whether user has their mouse pressed down
* @return boolean true if mouse is down
*/
public boolean isMouseDown() {
return mouseDown;
}

/**
* Sets the mouseDown to true or false
* @param mouseDown boolean true if mouse button is pressed down
*/
public void setMouseDown(boolean mouseDown) {
this.mouseDown = mouseDown;
}

/**
* Returns current x coordinate of cursor
* @return int x coord
*/
public int getCurrentX() {
return currentX;
}

/**
* Sets the current X coord of cursor
* @param currentX int x coord
*/
public void setCurrentX(int currentX) {
this.currentX = currentX;
}

/**
* Returns current y coordinate of cursor
* @return int y coord
*/
public int getCurrentY() {
return currentY;
}

/**
* Sets the current y coordinate of cursor
* @param currentY int y coord
*/
public void setCurrentY(int currentY) {
this.currentY = currentY;
}

/**
* Returns the x coordinate where the mouse was pressed down
* @return int x coord
*/
public int getStartX() {
return startX;
}

/**
* Set the x coordinate of where the mouse was pressed down
* @param startX int x coordinate
*/
public void setStartX(int startX) {
this.startX = startX;
}

/**
* Returns the y coordinate of where the mouse was pressed down
* @return int y
*/
public int getStartY() {
return startY;
}

/**
* Set the y coordinate of where the mouse was pressed down
* @param startY int y coord
*/
public void setStartY(int startY) {
this.startY = startY;
}

/**
* Returns the current fill color for drawing
* @return Color fillColor
*/
public Color getFillColor() {
return fillColor;
}

/**
* Sets the current fill color for drawing
* @param fillColor Color
*/
public void setFillColor(Color fillColor) {
this.fillColor = fillColor;
}




}

DRAWING PROGRAM

package drawingprogram;

/**
* This is the harness that runs the ITIS 1213 Assignment4 Drawing Program
* @author clatulip
*/
public class DrawingProgram {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Create a DrawingModel class to store all the stuff being drawn
DrawingModel myModel = new DrawingModel();
// Create the GUI window, pass it the model, so it knows what to draw
DrawingWindow myWindow = new DrawingWindow(myModel);
// Make the window visible
myWindow.setVisible(true);
}

}

LINE

package drawingprogram;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

/**
* This is the class for line drawing
* @author clatulip
*/
public class Line {
private int startX;
private int startY;
private int endX;
private int endY;
private Color lineColor;

/**
* Constructor
* @param startX int x coordinate at first end
* @param startY int y coordinate at first end
* @param endX int x coordinate at the other end
* @param endY int y coordinate at the other end
* @param color Color for the line
*/
public Line(int startX, int startY, int endX, int endY, Color color) {
this.startX = startX;
this.startY = startY;
this.endX = endX;
this.endY = endY;
this.lineColor = color;
}

/**
* Draws the line
* @param g2d The graphics context needed for drawing
*/
public void draw(Graphics2D g2d) {
g2d.setColor(lineColor);
g2d.drawLine(startX, startY, endX, endY);
}

/**
* This static method is used to draw a line without creating a line object
* It is used while the user is dragging the mouse, in order to show the line in real-time
* @param g2d The graphics context needed for drawing
* @param x1 int x coordinate at one end
* @param y1 int y coordinate at one end
* @param x2 int x coordinate at the other end
* @param y2 int y coordinate at the other end
* @param color Color for line
*/
public static void draw(Graphics2D g2d, int x1, int y1, int x2, int y2, Color color) {
g2d.setColor(color);
g2d.drawLine(x1, y1, x2, y2);
}
}

RECTANGLE

package drawingprogram;

import java.awt.Color;
import java.awt.Graphics2D;

/**
*
* @author clatulip
*/
public class Rectangle {
private int startX;
private int startY;
private int endX;
private int endY;
private int topLeftX;
private int topLeftY;
private int width;
private int height;
private Color lineColor;
private Color fillColor;

/**
* Constructor to build a rectangle
* @param startX The top left X coordinate
* @param startY The top left Y coordinate
* @param width The width of the rectangle
* @param height The height of the rectangle
* @param color The outline color of the rectangle
*/
public Rectangle(int startX, int startY, int endX, int endY, Color color, Color fillColor) {
this.startX = startX;
this.startY = startY;
this.endX = endX;
this.endY = endY;
this.lineColor = color;
this.fillColor = fillColor;
// calculations for the rectangle, since it needs to know top left and width/height
topLeftX = Math.min(startX, endX);
topLeftY = Math.min(startY, endY);
width = Math.abs(startX - endX);
height = Math.abs(startY - endY);

}

/**
* Draws the rectangle
* @param g2d The graphics context needed for drawing
*/
public void draw(Graphics2D g2d) {
// draw solid rectangle first
g2d.setColor(fillColor);
g2d.fillRect(topLeftX, topLeftY, width, height);
// draw outline rectangle on top
g2d.setColor(lineColor);
g2d.drawRect(topLeftX, topLeftY, width, height);
}

/**
* This static method is used to draw a rectangle before an actual rectangle object is created
* It is used to give the user interactive feedback as they are dragging the cursor
* @param g2d The graphics context needed for drawing
* @param x1 int x coordinate from where mouse pressed down
* @param y1 int y coordinate from where mouse pressed down
* @param x2 int x coordinate from where mouse released
* @param y2 int y coordinate from where mouse released
* @param lc Color for line color
* @param fc Color for fill color
*/
public static void draw(Graphics2D g2d, int x1, int y1, int x2, int y2, Color lc, Color fc) {
// draw solid rectangle first
g2d.setColor(fc);
g2d.fillRect(
Math.min(x1,x2),
Math.min(y1, y2),
Math.abs(x1 - x2),
Math.abs(y1 - y2));
// draw outline on top
g2d.setColor(lc);
g2d.drawRect(
Math.min(x1,x2),
Math.min(y1, y2),
Math.abs(x1 - x2),
Math.abs(y1 - y2));

}
}

ABSTRACT SHAPE:

package drawingprogram;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
/**
 *
 * @author mackenziemilligan
 */
public abstract class Shape {
    int left, top;      // Position of top left corner of rectangle that bounds this shape.
        int width, height;  // Size of the bounding rectangle.
        Color color = Color.white;  // Color of this shape.

        void reshape(int left, int top, int width, int height) {
                // Set the position and size of this shape.
            this.left = left;
            this.top = top;
            this.width = width;
            this.height = height;
        }

        void moveBy(int dx, int dy) {
                // Move the shape by dx pixels horizontally and dy pixels vertically
                // (by changing the position of the top-left corner of the shape).
            left += dx;
            top += dy;
        }

        void setColor(Color color) {
                // Set the color of this shape
            this.color = color;
        }

        boolean containsPoint(int x, int y) {
                // Check whether the shape contains the point (x,y).
                // By default, this just checks whether (x,y) is inside the
                // rectangle that bounds the shape.  This method should be
                // overridden by a subclass if the default behavior is not
                // appropriate for the subclass.
            if (x >= left && x < left+width && y >= top && y < top+height)
                return true;
            else
                return false;
        }

        abstract void draw(Graphics g);  
            // Draw the shape in the graphics context g.
            // This must be overriden in any concrete subclass.
}
\$\endgroup\$
  • 2
    \$\begingroup\$ The given task states you should let Line and Rectangle extend Shape. So this is not yet finished? \$\endgroup\$ – Koekje Apr 27 '18 at 1:22
  • 1
    \$\begingroup\$ Indent your code for readability \$\endgroup\$ – ndrone Apr 27 '18 at 2:47
  • \$\begingroup\$ Please say a few words about the code by yourself. What kind of advice are you looking for? Do you have some worries about the code, or do you think it's perfect? \$\endgroup\$ – Rene Saarsoo Apr 27 '18 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.