Public abstract class abstractdrawable implements

This preview shows page 6 - 9 out of 9 pages.

public abstract class AbstractDrawable implements Drawable {private final Rectangle bounds;private final State state;public AbstractDrawable(State state) {bounds = new Rectangle();this.state = state;}public State getState() {return state;}public abstract Shape getShape();public void setLocation(Point location) {bounds.setLocation(location);}public void setSize(Dimension size) {bounds.setSize(size);}public Rectangle getBounds() {return bounds;}public void paint(JComponent parent, Graphics2D g2d) {Shape shape = getShape();State state = getState();Rectangle bounds = getBounds();final float dash1[] = {state.lineWidth};final BasicStroke dashed = new BasicStroke(state.dashLength,
BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);if (bounds.width > 0 && bounds.height > 0) {if (state.isGradient()) {if (state.isDashed()) {Point2D startPoint = new Point2D.Double(bounds.x, bounds.y);Point2D endPoint = new Point2D.Double(bounds.x + bounds.width,bounds.y + bounds.height);LinearGradientPaint gp = new LinearGradientPaint(startPoint,endPoint, new float[]{0f, 1f}, new Color[]{state.getForeground(), state.getBackground()});g2d.setPaint(gp);g2d.setStroke(dashed);g2d.draw(shape);} else {Point2D startPoint = new Point2D.Double(bounds.x, bounds.y);Point2D endPoint = new Point2D.Double(bounds.x + bounds.width,bounds.y + bounds.height);LinearGradientPaint gp = new LinearGradientPaint(startPoint,endPoint, new float[]{0.2f, 0.6f}, new Color[]{state.getForeground(), state.getBackground()});g2d.setPaint(gp);g2d.fill(shape);}} else if (state.isFilled()) {if (bounds.width > 0 && bounds.height > 0) {g2d.setPaint(state.getForeground());g2d.fill(shape);}} else if (state.isDashed()) {g2d.setStroke(dashed);g2d.setPaint(state.getForeground());g2d.draw(shape);} else {BasicStroke stroke = new BasicStroke(state.dashLength);g2d.setStroke(stroke);g2d.setPaint(state.getForeground());g2d.draw(shape);}}}}public class Square extends AbstractDrawable {public Square(State state) {super(state);}//returns a square or rectanglepublic Shape getShape() {return getBounds();}}
public class Circle extends AbstractDrawable {public Circle(State state) {super(state);}//Returns a Circlepublic Shape getShape() {Rectangle bounds = getBounds();return new Ellipse2D.Float(bounds.x, bounds.y, bounds.width, bounds.height);}}public class Line extends AbstractDrawable {public Line(State state) {super(state);}//Returns a linepublic Shape getShape() {Rectangle bounds = getBounds();return new Line2D.Float(bounds.x, bounds.y, bounds.x + bounds.width,bounds.y + bounds.height);}}public class drawPanel extends JPanel {public drawPanel() {itemsDrawn = new ArrayList<Drawable>();}public Dimension getPreferredSize() {return new Dimension(500, 600);}protected void paintComponent(Graphics g) {super.paintComponent(g);Graphics2D g2d = (Graphics2D) g.create();for (Drawable d : itemsDrawn) {d.paint(this, g2d);}g2d.dispose();}public void addDrawable(Drawable drawable) {itemsDrawn.add(drawable);}}Point start, end;public void mouseMoved(MouseEvent arg0) {}public void mouseClicked(MouseEvent arg0) {}public void mouseEntered(MouseEvent arg0) {}public void mouseExited(MouseEvent arg0) {
}}

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture