Java Code Examples for java.awt.geom.Rectangle2D#getBounds()

The following examples show how to use java.awt.geom.Rectangle2D#getBounds() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AddRulesTest.java    From pumpernickel with MIT License 7 votes vote down vote up
public BufferedImage render() {
	Rectangle2D bounds = getBounds();
	Rectangle r = bounds.getBounds();
	BufferedImage bi = new BufferedImage(r.width, r.height,
			BufferedImage.TYPE_INT_ARGB);
	Graphics2D g = bi.createGraphics();
	g.translate(-r.x, -r.y);
	g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
			RenderingHints.VALUE_ANTIALIAS_ON);
	for (int a = 0; a < shapes.size(); a++) {
		float hue = (a) / 11f;
		float bri = .75f + .25f * a / ((shapes.size()));
		Shape shape = shapes.get(a);
		g.setColor(new Color(Color.HSBtoRGB(hue, .75f, bri)));
		g.fill(shape);
	}
	g.dispose();
	return bi;
}
 
Example 2
Source File: DebugShape.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void doPaint(Graphics2D g) {
	if (shapeIsOutdated()) {
		return;
	}

	Color originalColor = g.getColor();
	g.setColor(getColor());

	g.draw(shape);

	g.setColor(Color.black);
	FontMetrics fontMetrics = g.getFontMetrics();
	Rectangle2D stringBounds = fontMetrics.getStringBounds(text, g);
	Point location = shape.getBounds().getLocation();
	location.y += shape.getBounds().height + stringBounds.getBounds().height;
	g.drawString(text, location.x, location.y);

	g.setColor(originalColor);
}
 
Example 3
Source File: RestSymbol.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
protected Params getParams (MusicFont font)
{
    MyParams p = new MyParams();

    // Rest symbol layout
    p.layout = getRestLayout(font);

    Rectangle2D rs = p.layout.getBounds();

    if (decorated) {
        // Define specific offset
        p.offset = new Point(0, (int) Math.rint(rs.getY() + (rs.getHeight() / 2)));

        // Lines layout
        p.linesLayout = font.layout(linesSymbol.getString());
        p.rect = p.linesLayout.getBounds().getBounds();
    } else {
        p.rect = rs.getBounds();
    }

    return p;
}
 
Example 4
Source File: Proofer.java    From AndroidDesignPreview with Apache License 2.0 6 votes vote down vote up
public ProoferClient() {
    try {
        this.robot = new Robot();
    } catch (AWTException e) {
        System.err.println("Error getting robot.");
        e.printStackTrace();
        System.exit(1);
    }

    GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] screenDevices = environment.getScreenDevices();

    Rectangle2D tempBounds = new Rectangle();
    for (GraphicsDevice screenDevice : screenDevices) {
        tempBounds = tempBounds.createUnion(
                screenDevice.getDefaultConfiguration().getBounds());
    }
    screenBounds = tempBounds.getBounds();
}
 
Example 5
Source File: FitGraphToViewJob.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private boolean scaleToFitViewer(VisualizationViewer<V, E> visualizationViewer,
		Rectangle2D graphBounds) {

	Dimension windowSize = visualizationViewer.getSize();
	Rectangle bounds = graphBounds.getBounds();

	Shape viewShape = translateShapeFromLayoutSpaceToViewSpace(bounds, visualizationViewer);
	Rectangle viewBounds = viewShape.getBounds();
	boolean fitsInView =
		viewBounds.width < windowSize.width && viewBounds.height < windowSize.height;
	if (onlyResizeWhenTooBig && fitsInView) {
		return false;
	}

	Double scaleRatio = getScaleRatioToFitInDimension(bounds.getSize(), windowSize);
	if (scaleRatio == null) {
		return true;
	}
	if (scaleRatio > 1.0) {
		scaleRatio = 1.0;
	}

	// add some padding and make it relative to the new scale
	int unscaledPaddingSize = 10;
	addPaddingToRectangle((int) (unscaledPaddingSize / scaleRatio), bounds);
	scaleRatio = getScaleRatioToFitInDimension(bounds.getSize(), windowSize);
	if (scaleRatio == null) {
		return true;
	}

	RenderContext<V, E> renderContext = visualizationViewer.getRenderContext();
	MultiLayerTransformer multiLayerTransformer = renderContext.getMultiLayerTransformer();
	MutableTransformer viewTransformer = multiLayerTransformer.getTransformer(Layer.VIEW);

	viewTransformer.setScale(scaleRatio, scaleRatio, new Point(0, 0));
	return true;
}
 
Example 6
Source File: AugmentationSymbol.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected BasicSymbol.Params getParams (MusicFont font)
{
    MyParams p = new MyParams();

    // Augmentation symbol layout
    p.layout = font.layout(getString());

    Rectangle2D rs = p.layout.getBounds(); // Symbol bounds

    if (decorated) {
        // Head layout
        p.headLayout = font.layout(head.getString());

        // Use a rectangle twice as wide as note head
        Rectangle2D hRect = p.headLayout.getBounds();
        p.rect = new Rectangle(
                (int) Math.ceil(2 * hRect.getWidth()),
                (int) Math.ceil(hRect.getHeight()));

        // Define specific offset
        p.offset = new Point((int) Math.rint(dxRatio * p.rect.width), 0);
    } else {
        p.rect = rs.getBounds();
    }

    return p;
}
 
Example 7
Source File: RepeatDotSymbol.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected Params getParams (MusicFont font)
{
    // offset: if decorated, offset of symbol center vs decorated image center
    // layout: dot layout
    // rect:   global image (= other dot + dot if decorated, dot if not)
    Params p = new Params();

    // Dot layout
    p.layout = font.layout(getString());

    Rectangle2D rs = p.layout.getBounds(); // Symbol bounds

    if (decorated) {
        // Use a rectangle yTimes times as high as dot
        p.rect = new Rectangle(
                (int) Math.ceil(rs.getWidth()),
                (int) Math.ceil(yRatio * rs.getHeight()));

        // Define specific offset
        p.offset = new Point(0, (int) Math.rint(rs.getHeight() * ((yRatio - 1) / 2)));
    } else {
        p.rect = rs.getBounds();
    }

    return p;
}
 
Example 8
Source File: ArticulationSymbol.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected Params getParams (MusicFont font)
{
    MyParams p = new MyParams();

    // Articulation symbol layout
    p.layout = font.layout(getString());

    Rectangle2D rs = p.layout.getBounds(); // Symbol bounds

    if (decorated) {
        // Head layout
        p.headLayout = font.layout(head.getString());

        // Use a rectangle 'yRatio' times as high as note head
        Rectangle2D rh = p.headLayout.getBounds(); // Head bounds
        p.rect = new Rectangle(
                (int) Math.ceil(Math.max(rh.getWidth(), rs.getWidth())),
                (int) Math.ceil(yRatio * rh.getHeight()));

        // Define specific offset
        p.offset = new Point(0, (int) Math.rint(dyRatio * p.rect.height));
    } else {
        p.rect = rs.getBounds();
    }

    return p;
}
 
Example 9
Source File: TemplateSymbol.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected MyParams getParams (MusicFont font)
{
    final MyParams p = new MyParams();
    final int interline = font.getStaffInterline();

    final TextLayout fullLayout = layout(font);
    final Rectangle2D fullRect2d = fullLayout.getBounds();
    final Rectangle fullRect = fullRect2d.getBounds();

    if (isSmall) {
        p.layout = font.layout(getString(), smallAt);
    } else {
        p.layout = fullLayout;
    }

    final Rectangle2D r2d = p.layout.getBounds();
    final Rectangle r = r2d.getBounds();
    final int symWidth = r.width;
    final int symHeight = r.height;

    // Choose carefully the template rectangle, with origin at (0,0), around the symbol
    // For full size symbol, add some margin on each direction of the symbol
    int dx = 2;
    int dy = 2;
    p.rect = new Rectangle(
            isSmall ? fullRect.width : (symWidth + dx),
            isSmall ? interline : (symHeight + dy));

    // Bounds of symbol within template rectangle
    p.symbolRect = new Rectangle(
            (p.rect.width - symWidth) / 2,
            (p.rect.height - symHeight) / 2,
            symWidth,
            symHeight);

    return p;
}
 
Example 10
Source File: TextBoxPaintable.java    From pumpernickel with MIT License 5 votes vote down vote up
public TextBoxPaintable(String string, Font font, float maxWidth,
		float insets, Paint paint) {
	if (paint == null)
		paint = SystemColor.textText;
	this.insets = insets;
	this.text = string;
	this.paint = paint;

	List<String> lines = new ArrayList<String>();
	Map<TextAttribute, Object> attributes = new HashMap<TextAttribute, Object>();
	attributes.put(TextAttribute.FONT, font);
	AttributedString attrString = new AttributedString(string, attributes);
	LineBreakMeasurer lbm = new LineBreakMeasurer(attrString.getIterator(),
			frc);
	TextLayout tl = lbm.nextLayout(maxWidth);
	float dy = 0;
	GeneralPath path = new GeneralPath();
	int startIndex = 0;
	while (tl != null) {
		path.append(
				tl.getOutline(AffineTransform.getTranslateInstance(0, dy)),
				false);
		dy += tl.getAscent() + tl.getDescent() + tl.getLeading();
		int charCount = tl.getCharacterCount();
		lines.add(text.substring(startIndex, startIndex + charCount));
		startIndex += charCount;
		tl = lbm.nextLayout(maxWidth);
	}
	this.lines = lines.toArray(new String[lines.size()]);
	untransformedShape = path;
	Rectangle2D b = ShapeBounds.getBounds(untransformedShape);
	b.setFrame(b.getX(), b.getY(), b.getWidth() + 2 * insets, b.getHeight()
			+ 2 * insets);
	untransformedBounds = b.getBounds();
}
 
Example 11
Source File: CompositePaintable.java    From pumpernickel with MIT License 5 votes vote down vote up
private void refreshBounds() {
	Rectangle2D sum = null;
	Rectangle t = new Rectangle();
	Rectangle2D t2 = new Rectangle2D.Double();
	for (int a = 0; a < paintables.size(); a++) {
		t.x = 0;
		t.y = 0;
		t.width = paintables.get(a).getWidth();
		t.height = paintables.get(a).getHeight();
		ShapeBounds.getBounds(t, transforms.get(a), t2);
		if (sum == null) {
			sum = new Rectangle2D.Double(t2.getX(), t2.getY(),
					t2.getWidth(), t2.getHeight());
		} else {
			sum.add(t2);
		}
	}
	if (sum == null) {
		bounds = new Rectangle(0, 0, 0, 0);
	} else {
		AffineTransform translation = AffineTransform.getTranslateInstance(
				-sum.getX(), -sum.getY());
		for (int a = 0; a < transforms.size(); a++) {
			transforms.get(a).preConcatenate(translation);
		}
		bounds = sum.getBounds();
		bounds.x = 0;
		bounds.y = 0;
	}
}
 
Example 12
Source File: MMDGraphEditor.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void onEnsureVisibilityOfTopic(@Nonnull final MindMapPanel source, @Nonnull final Topic topic) {
  mindMapPanel.doLayout();

  final AbstractElement element = (AbstractElement) topic.getPayload();
  if (element == null) {
    return;
  }

  final Rectangle2D orig = element.getBounds();
  final int GAP = 30;

  final Rectangle bounds = orig.getBounds();
  bounds.setLocation(Math.max(0, bounds.x - GAP), Math.max(0, bounds.y - GAP));
  bounds.setSize(bounds.width + GAP * 2, bounds.height + GAP * 2);

  final JViewport viewport = mainScrollPane.getViewport();
  final Rectangle visible = viewport.getViewRect();

  if (visible.contains(bounds)) {
    return;
  }

  bounds.setLocation(bounds.x - visible.x, bounds.y - visible.y);

  mainScrollPane.revalidate();
  SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
      viewport.scrollRectToVisible(bounds);
      mainScrollPane.repaint();
    }
  });
}
 
Example 13
Source File: CFont.java    From ForgeHax with MIT License 4 votes vote down vote up
protected BufferedImage generateFontImage(
    Font font, boolean antiAlias, boolean fractionalMetrics, CharData[] chars) {
  int imgSize = (int) this.imgSize;
  BufferedImage bufferedImage = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_ARGB);
  Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
  g.setFont(font);
  g.setColor(new Color(255, 255, 255, 0));
  g.fillRect(0, 0, imgSize, imgSize);
  g.setColor(Color.WHITE);
  g.setRenderingHint(
      RenderingHints.KEY_FRACTIONALMETRICS,
      fractionalMetrics
          ? RenderingHints.VALUE_FRACTIONALMETRICS_ON
          : RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
  g.setRenderingHint(
      RenderingHints.KEY_TEXT_ANTIALIASING,
      antiAlias
          ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON
          : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
  g.setRenderingHint(
      RenderingHints.KEY_ANTIALIASING,
      antiAlias ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
  FontMetrics fontMetrics = g.getFontMetrics();
  int charHeight = 0;
  int positionX = 0;
  int positionY = 1;
  
  for (int i = 0; i < chars.length; i++) {
    char ch = (char) i;
    CharData charData = new CharData();
    Rectangle2D dimensions = fontMetrics.getStringBounds(String.valueOf(ch), g);
    charData.width = (dimensions.getBounds().width + 8);
    charData.height = dimensions.getBounds().height;
    
    if (positionX + charData.width >= imgSize) {
      positionX = 0;
      positionY += charHeight;
      charHeight = 0;
    }
    
    if (charData.height > charHeight) {
      charHeight = charData.height;
    }
    
    charData.storedX = positionX;
    charData.storedY = positionY;
    
    if (charData.height > this.fontHeight) {
      this.fontHeight = charData.height;
    }
    
    chars[i] = charData;
    g.drawString(String.valueOf(ch), positionX + 2, positionY + fontMetrics.getAscent());
    positionX += charData.width;
  }
  
  return bufferedImage;
}