Skip to main content

Extending an abstract class [closed]



The following code for RectangleNode extends an abstract class called GraphElement, this is needed for my program to draw rectangles where necessary. My question is regarding the abstract method in GraphElement.





Rectangle Node:







import java.awt.Graphics2D;

import java.awt.geom.Rectangle2D;

import java.util.ArrayList;

import java.awt.Color;



public class RectangleNode extends GraphElement

{

private double width = 20;

private double length = 40;

private Rectangle2D.Double rect;



public RectangleNode(int x, int y)

{

super(x,y);

rect = new Rectangle2D.Double(getXPos(), getYPos(), length, width);

}



public RectangleNode()

{

super();

rect = null;

}



public boolean isSelected(double x, double y)

{

return super(x,y);

}



public void draw(Graphics2D g2)

{

if (rect != null)

g2.draw(rect);



}



}







Graph Element:







import java.awt.Graphics2D;



abstract public class GraphElement

{

private double xPos;

private double yPos;

protected String label;



public GraphElement()

{

xPos = 0;

yPos = 0;

}



public GraphElement(double x, double y)

{

xPos = x;

yPos = y;

}



public final double getXPos()

{

return xPos;

}



public final double getYPos()

{

return yPos;

}



public void moveTo (double xLoc, double yLoc)

{

xPos = xLoc;

yPos = yLoc;

}



public String toString()

{

String str = "(X,Y) Position: (" + xPos + "," + yPos + ")\n";

return str;

}



abstract void draw(Graphics2D g2);

abstract boolean isSelected(double x, double y);



boolean applyLabel()

{

return true;

}



public String getLabel()

{

return label;

}



public void setLabel(String label)

{

this.label = label;

}

}







How do i deal with the abstract isSelected method in RectangleNode? isSelected() returns true or false depending on if the mouseclick was on a rectangle thats already printed.





Thanks.


Comments

  1. I assume that you are getting x,y point of mouse pointer as parameters to isSelected() method. You have your current width, breadth of rectangle and x,y coordinate of one of its corner. So given this data you can figure it out whether given mouse pointer is inside the rectangle or not.

    ReplyDelete
  2. Is the (x, y) of GraphElement maybe the center of the element? And is the (x, y) for the RectangleNode the corner?

    For isSelected the parameters would better be named mouseX and mouseY. And they should reside inside the rectangle to yield true.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.