3 Replies - 1077 Views - Last Post: 22 April 2012 - 07:32 PM Rate Topic: -----

#1 princessjess86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-April 12

java Drawing Panel not compiling, abstract methods Q

Posted 22 April 2012 - 04:19 PM

Hi there,

I am working on a java project that involves a drawing panel that we are to render different shapes to. I have been provided with starter code from our teacher that we are to add to. I have created the "class DrawingArea extends DrawingAreaBase" code, which implements the different methods that are in DrawingAreaBase. I am getting a compiler error, "ShapePanel.java:238: DrawingArea is not abstract and does not override abstract method removeAllShapes() in DrawingArea" I don't understand why I am getting this error, since I thought that I DID implement that method in my method.

Any help would be appreciated - for now, I am just trying to get this code to COMPILE so I can work with it from there.

Thanks!

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

interface Shape {

    // this method should render your Shape. If the shape is in the "hot" state,
    // meaning the mouse is over the shape, it should render fully opaque, with
    // the color specified by setColor. If the shape is not in the hot state, it
    // should render a white background (fill) and just a border in the color specified
    // by setColor.
    public void render(Graphics g);

    // determine if the given point is within the shape. Keep track of if the
    // shape is currently hit tested or not, and call listener.OnHitTestChange()
    // whenever the state changes. You will need this state to determine if it should be
    // rendered in the "hot" state or not.
    public boolean hitTest(Point pt, HitTestChangeListener listener);

    // allow the outline color to be set (or fill color while in the "hot" state).
    public void setColor(Color color);

    // calculate the area of the shape.
    public double getArea();

    // get/set the position of the shape. This point can be anywhere on (or even off)
    // the shape that you want, but you must be consistent and accept a change to
    // setPosition().
    public Point getPosition();

    public void setPosition(Point point);
}

class Rectangle implements Shape {
	private int width;
	private int height;
	private int x;
	private int y;
	
	public Rectangle(int width, int height, int y, int x) {
		this.width = width;
		this.height = height;
		this.x = x;
		this.y = y;
	}

	public double getWidth() {
		return width;
	}
	
	public double getHeight() {
		return height;
	}
	
	public void render(Graphics g) {
		g.drawRect(x, y, width, height);
	}
	
	public boolean hitTest(Point pt, HitTestChangeListener listener) {
		return true;
	}
	
	public void setColor(Color color) {
		setColor(Color.BLACK);
	}
	
	public double getArea() {
		return width * height;
	}
	
	 public Point getPosition() {
		Point p = new Point();
		return p;
	}
	
	public void setPosition(Point point) {
		System.out.println(point);
	}
}

// The ShapeFactory will create shapes of the specified type, sized reasonably for the given
// panel with and height
class ShapeFactory {
    private enum Shapes {
        RECTANGLE,
        SQUARE,
        CIRCLE,
        OVAL,
        TRIANGLE
        /* HEXAGON  Bonus */
        /* OCTAGON  Bonus */};

    public static String[] getSupportedShapes() {
        // have the enumeration provide strings for each enum value
        String[] ret = new String[Shapes.values().length];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = Shapes.values()[i].toString();
        }
        return ret;
    }

    public static Shape createShape(String shapeString, int panelWidth, int panelHeight) {
        Shapes shape = Shapes.valueOf(shapeString);
        Random random = new Random();
		//switch(shape) {
        //case RECTANGLE:
            Rectangle rect = new Rectangle(10, 10, 20, 10);
            return rect;
		//}
        // TODO: Implement this method, to create the specified shape. No shape should be larger
        // than 25% of the panel, and should be initially positioned at least partially on screen.
    }
}


//This class will be created on mouse down to keep track of the selected shape, the mouse
//down point, and the original location of the selected shape. On subsequent mouse drag events
//we will move the selected shape appropriately.
class DragData {
    private Point mouseDragStart;
    private Point shapeStart;
    private Shape shape;

    public DragData(Point mouseDragStart, Shape shape) {
        this.mouseDragStart = mouseDragStart;
        this.shapeStart = shape.getPosition();
        this.shape = shape;
    }

    public void handleMouseDrag(MouseEvent e) {
        int deltaX = e.getPoint().x - mouseDragStart.x; 
        int deltaY = e.getPoint().y - mouseDragStart.y;
        shape.setPosition(new Point(shapeStart.x + deltaX, shapeStart.y + deltaY));
    }
}

// callback interface so that the drawing panel can know to repaint if a shape used to be
// hit and is no longer hit, or vice-versa.
interface HitTestChangeListener {
    public void OnHitTestChange();
}

// Abstract class for your drawing area, you need to extend this and implement
// all the abstract methods to work with an ArrayList of Shape objects
abstract class DrawingAreaBase extends JPanel implements HitTestChangeListener {
    private DragData dragData = null;
    private Color colorSelection = Color.BLACK;

    // compute the cummulative area of all shapes
    abstract public double getTotalShapeArea();

    // render all shapes
    abstract public void renderShapes(Graphics g);

    // find the first shape hit by the given point
    abstract public Shape findFirstShapeHit(Point point);

    // handle a click event on the current shape as per functional requirements
    abstract public void handleShapeClicked(Shape shape);

    // perform hit testing on all shapes
    abstract public void hitTestAllShapes(Point point);

    // add a shape to the drawing area
    abstract public void addShape(Shape s);

    // remove all shapes from the drawing area
    abstract public void removeAllShapes();

    public DrawingAreaBase() {
        setPreferredSize(new Dimension(500, 500));

        enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
        enableEvents(AWTEvent.MOUSE_EVENT_MASK);
    }

    public void refreshBackgroundColorByShapeArea() {
        double shapeArea = getTotalShapeArea();
        double totalArea = getWidth() * getHeight();
        int channelColor = Math.max(0,  255 - (int)(shapeArea * 255.0 / totalArea));
        setBackground(new Color(channelColor, channelColor, channelColor));
    }

    public void setColorSelection(Color color) { colorSelection = color; }
    public Color getColorSelection() { return colorSelection; }

    @Override public void paintComponent(Graphics g) {
        super.paintComponent(g);  // Ask parent to paint background.
        renderShapes(g);
    }

    @Override public void processMouseMotionEvent(MouseEvent e) {
        switch (e.getID()) {
        case MouseEvent.MOUSE_MOVED:
            hitTestAllShapes(e.getPoint());
            break;
        case MouseEvent.MOUSE_DRAGGED:
            if (dragData != null) {
                dragData.handleMouseDrag(e);
                repaint();
            }
            break;
        }
    }

    @Override public void processMouseEvent(MouseEvent e) {
        Shape firstShape = null;
        switch (e.getID()) {
        case MouseEvent.MOUSE_CLICKED:
            firstShape = findFirstShapeHit(e.getPoint());
            if (firstShape != null) {
                handleShapeClicked(firstShape);
                firstShape.setColor(colorSelection);
                this.repaint();
            }						
            break;
        case MouseEvent.MOUSE_PRESSED:
            firstShape = findFirstShapeHit(e.getPoint());
            if (firstShape != null) {
                dragData = new DragData(e.getPoint(), firstShape);
            }
            break;
        case MouseEvent.MOUSE_RELEASED:
            dragData = null;
            break;
        }
    }

    public void OnHitTestChange() {
        repaint();
    }
}

class DrawingArea extends DrawingAreaBase {
	ArrayList<Shape> shapelist = new ArrayList<Shape>();
	
	abstract public double getTotalShapeArea();
	
	public void renderShapes(Graphics g) {
        for (int i = 0; i <= shapelist.size() - 1; i++) {
			shapelist.get(i).render(g);
		}
    }
	
	abstract public Shape findFirstShapeHit(Point point); 
	
	abstract public void handleShapeClicked(Shape shape);
	
	abstract public void hitTestAllShapes(Point point);
	
	public void addShape(Shape s) {
        shapelist.add(s);
	}
	
	abstract public void removeAllShapes();
}

public class ShapePanel extends JFrame {

    private DrawingArea panel;
    private ShapeFactory factory;

    public ShapePanel() {
        panel = new DrawingArea();
        panel.setBackground(Color.white);

        factory = new ShapeFactory();

        JPanel content = new JPanel();
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));

        // for all supported shapes, add a button to add a new instance of that shape
        JPanel buttons = new JPanel();
        content.add(buttons);
        buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));       
        for (final String shapeType : factory.getSupportedShapes()) {        	
            JButton button = new JButton("New " + shapeType.toLowerCase());
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    Shape s = factory.createShape(shapeType, panel.getWidth(), panel.getHeight());
                    if (s.getArea() > panel.getWidth() * panel.getHeight() * 0.25) {
                        throw new RuntimeException("Shape should not be larger than 25% of panel");
                    }
                    s.setColor(panel.getColorSelection());
                    panel.addShape(s);
                    panel.refreshBackgroundColorByShapeArea();
                }
            });
            buttons.add(button);
        }

        JButton buttonClear = new JButton("Clear");
        buttonClear.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {                	
                panel.removeAllShapes();
                panel.refreshBackgroundColorByShapeArea();
            }
        });
        buttons.add(buttonClear);

        // TODO (Bonus) - create a color toolbar that will allow the user to change the color of the next clicked on shape
        //Color[] colors = {Color.BLACK, Color.RED, Color.GREEN, Color.BLUE, Color.CYAN, Color.MAGENTA, Color.ORANGE, Color.YELLOW, Color.PINK};
        content.add(panel);

        //... Set window characteristics
        setContentPane(content);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Shape Panel");
        setLocationRelativeTo(null);  // Center window.		
        pack();
    }

    public static void main(String[] args) {
        JFrame window = new ShapePanel();
        window.setVisible(true);
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: java Drawing Panel not compiling, abstract methods Q

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: java Drawing Panel not compiling, abstract methods Q

Posted 22 April 2012 - 05:50 PM

class DrawingArea extends DrawingAreaBase {

has abstract methods, it should be decared abstract

public class ShapePanel extends JFrame {

public classes should be defined in their own file so, in your case, ShapePanel class should be defined/declares in ShapePanel.java
Was This Post Helpful? 0
  • +
  • -

#3 princessjess86  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-April 12

Re: java Drawing Panel not compiling, abstract methods Q

Posted 22 April 2012 - 05:53 PM

Thanks you -

However, when I make DrawingArea abstract, I get this error:


c:\Users\owner\Desktop\Java>javac ShapePanel.java
ShapePanel.java:268: DrawingArea is abstract; cannot be instantiated
panel = new DrawingArea();
^
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 851
  • View blog
  • Posts: 2,599
  • Joined: 29-July 11

Re: java Drawing Panel not compiling, abstract methods Q

Posted 22 April 2012 - 07:32 PM

You cannot instantiate an abstract class. DrawingArea is the panel upon which you draw, so you must implement the abstract methods of DrawingAreaBase by providing a body for those methods. You can solve your immediate problem by NOT declaring DrawingArea abstract and providing a body for removeAllShapes() e.g.

public class DrawingArea extends DrawingAreaBase {
    public void removeAllShapes() {
        //add method implementation here.
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1