public Rectangle2D getBounds2D() { StringTokenizer tokens = new StringTokenizer(getRenderString(), "\n"); int noLines = tokens.countTokens(); double height = (theFont.getSize2D() * noLines) + 5; double width = 0; while (tokens.hasMoreTokens()) { double l = theFont.getSize2D() * tokens.nextToken().length() * (5.0 / 8.0); if (l > width) width = l; } double parX; double parY; if (parent instanceof State) { parX = ((State) parent).getX(); parY = ((State) parent).getY(); } else if (parent instanceof Transition) { parX = ((Transition) parent).getMiddle().getX(); // dummy parY = ((Transition) parent).getMiddle().getY(); // dummy } else { parX = 0; parY = 0; } double mx = parX + offsetX; double my = parY + offsetY - 10; double tx = parX + width + offsetX; double ty = parY + height + offsetY - 10; return new Rectangle2D.Double(mx, my, tx - mx, ty - my); }
public void workOutMinsAndMaxs() { StringTokenizer tokens = new StringTokenizer(getRenderString(), "\n"); int noLines = tokens.countTokens(); double height = (theFont.getSize2D() * noLines) + 5; double width = 0; while (tokens.hasMoreTokens()) { double l = theFont.getSize2D() * tokens.nextToken().length() * (5.0 / 8.0); if (l > width) width = l; } double parX; double parY; if (parent instanceof State) { parX = ((State) parent).getX(); parY = ((State) parent).getY(); } else if (parent instanceof Transition) { parX = ((Transition) parent).getMiddle().getX(); // dummy parY = ((Transition) parent).getMiddle().getY(); // dummy } else { parX = 0; parY = 0; } minX = parX + offsetX - 5; minY = parY + offsetY - 25; maxX = parX + width + offsetX + 5; maxY = parY + height + offsetY - 5; }
/** * A rendering method to draw this label to the given Graphics2D object, with the additional * option of allowing the "long" lines to be drawn. Due to the fact that the relative drawing * point of a state is its x and y co-ordinates and for a Transition there is a workOutMiddle() * method, the relative x and y values must be supplied to this method. Usually this method will * be called from inside a Transition render method or a State render method. * * @param g2 the Graphics2D component upon which to draw this label. * @param x the x position upon which to make relative co-ordinates exact. * @param y the y position upon which to make relative co-ordinates exact. * @param longLines flag to determine whether the long version of this label should be drawn. */ public void render(Graphics2D g2, double x, double y, boolean longLines) { intersects(new Rectangle2D.Double(0, 0, 1, 1)); StringTokenizer tokens = new StringTokenizer(getRenderString(), "\n"); if (selected) { g2.setColor(Color.green); } else { g2.setColor(theColour); } g2.setFont(theFont); int i = 0; boolean doneLong = false; while (tokens.hasMoreTokens()) { if (doneLong) g2.drawString( tokens.nextToken(), (float) (x + offsetX), (float) (y + offsetY + ((i * (theFont.getSize() + 2))))); else { if (!longLines) g2.drawString( tokens.nextToken().trim(), (float) (x + offsetX), (float) (y + offsetY + ((i * (theFont.getSize()))) + 2)); else g2.drawString( getName() + ": " + tokens.nextToken().trim(), (float) (x + offsetX), (float) (y + offsetY + ((i * (theFont.getSize()))) + 2)); } i++; doneLong = true; } /*if(intersects != null) { g2.setColor(Color.magenta); for(int j = 0; j < intersects.size(); j++) { Rectangle2D rect = (Rectangle2D)intersects.get(j); g2.draw(rect); } }*/ }
/** * This method calculates whether a given "Hot area" rectangle intersects with this label and * returns the result. The aim of this method is that it can be used to detect whether the * position of the mouse is colliding with the label. * * @param rect A "Hot area" which we are looking for the collision to lie in. * @return the result of the collision. */ public boolean intersects(Rectangle2D rect) { intersects = new ArrayList(); if (!getString().equals("")) { double parX; double parY; if (parent instanceof State) { parX = ((State) parent).getX(); parY = ((State) parent).getY(); } else if (parent instanceof Transition) { parX = ((Transition) parent).getMiddle().getX(); // dummy parY = ((Transition) parent).getMiddle().getY(); // dummy } else { parX = 0; parY = 0; } double x = parX + offsetX; double y = parY + offsetY - 5; StringTokenizer tokens; String pre = ""; if (lineLabels) { pre = getName() + ": "; } tokens = new StringTokenizer(pre + getRenderString(), "\n"); int i = 0; boolean collides = false; while (tokens.hasMoreTokens() && !collides) { String str = tokens.nextToken().trim(); // System.out.println("str = \""+str+"\""); double height = theFont.getSize2D(); double width = theFont.getSize2D() * str.length() * (5.0 / 8.0); intersects.add(new Rectangle2D.Double(x, y + (i * (height + 1.75) - 2), width, height)); collides = (new Rectangle2D.Double(x, y + (i * (height + 1.75) - 2), width, height)) .intersects(rect); i++; } return collides; } else return false; }