Java Code Examples for java.awt.Rectangle.intersects()

The following are Jave code examples for showing how to use intersects() of the java.awt.Rectangle class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: UIUtils.java   View Source Code Vote up 7 votes
@Override
public void focusGained(FocusEvent e) {
    if (e.isTemporary()) {
        return;
    }
    Component cmp = e.getComponent();
    if(cmp instanceof JComponent) {
        JViewport vp = getViewport(container);
        if(vp == null) {
            return;
        }
        Rectangle vr = vp.getViewRect();
        Point p = SwingUtilities.convertPoint(cmp.getParent(), cmp.getLocation(), container);
        final Rectangle r = new Rectangle(p, cmp.getSize());
        if(vr.intersects(r)) {
            return; 
        }
        container.scrollRectToVisible(r);
    }
}
 
Example 2
Project: TrabalhoFinalEDA2   File: mxCurveLabelShape.java   View Source Code Vote up 6 votes
/**
 * Returns whether or not the rectangle passed in hits any part of this
 * curve.
 * @param rect the rectangle to detect for a hit
 * @return whether or not the rectangle hits this curve
 */
public boolean intersectsRect(Rectangle rect)
{
	// To save CPU, we can test if the rectangle intersects the entire
	// bounds of this label
	if ( (labelBounds != null
			&& (!labelBounds.getRectangle().intersects(rect)) )
			|| labelGlyphs == null )
	{
		return false;
	}

	for (int i = 0; i < labelGlyphs.length; i++)
	{
		if (labelGlyphs[i].visible
				&& rect.intersects(labelGlyphs[i].drawingBounds
						.getRectangle()))
		{
			return true;
		}
	}

	return false;
}
 
Example 3
Project: geomapapp   File: DBTableModel.java   View Source Code Vote up 6 votes
private boolean checkPolygonBounds(UnknownData d, Rectangle2D viewBounds, double wrap) {
	if (d.polyline == null) return false;
	Rectangle polyBounds = d.polyline.getBounds();
	polyBounds.x += d.x;
	polyBounds.y += d.y;

	while (true) {
		if (polyBounds.intersects(viewBounds))
			return true;

		if (wrap <= 0) break;
		if (polyBounds.x >= wrap) break;
		polyBounds.x += wrap;
	}
	return false;
}
 
Example 4
Project: rapidminer   File: SliderUI.java   View Source Code Vote up 6 votes
@Override
public void paint(Graphics g, JComponent c) {
	recalculateIfInsetsChanged();
	recalculateIfOrientationChanged();
	Rectangle clip = g.getClipBounds();

	if (!clip.intersects(trackRect) && slider.getPaintTrack()) {
		calculateGeometry();
	}

	if (slider.getPaintTrack() && clip.intersects(trackRect)) {
		paintTrack(g);
	}
	if (slider.hasFocus() && clip.intersects(focusRect)) {
		paintFocus(g);
	}

	// the ticks are now inside the track so they have to be painted each thumb movement
	paintTicks(g);
	// thumb is always painted due to value below thumb
	paintThumb(g);
}
 
Example 5
Project: jaer   File: CellStatsProber.java   View Source Code Vote up 6 votes
public void displayStats(GLAutoDrawable drawable) {
    if ((drawable == null) || (chip.getCanvas() == null)) {
        return;
    }
    canvas = chip.getCanvas();
    glCanvas = (GLCanvas) canvas.getCanvas();
    int sx = chip.getSizeX(), sy = chip.getSizeY();
    Rectangle chipRect = new Rectangle(sx, sy);
    GL2 gl = drawable.getGL().getGL2();
    if (selection != null && chipRect.intersects(selection)) {
        drawSelection(gl, selection, SELECT_COLOR);
    }
    stats.drawStats(drawable);
    stats.play();

}
 
Example 6
Project: jaer   File: CellStatsProber_cochlea.java   View Source Code Vote up 6 votes
public void displayStats(GLAutoDrawable drawable) {
    if ((drawable == null) || (selection == null) || (chip.getCanvas() == null)) {
        return;
    }
    canvas = chip.getCanvas();
    glCanvas = (GLCanvas) canvas.getCanvas();
    int sx = chip.getSizeX(), sy = chip.getSizeY();
    Rectangle chipRect = new Rectangle(sx, sy);
    GL2 gl = drawable.getGL().getGL2();
    if (!chipRect.intersects(selection)) {
        return;
    }
    drawSelection(gl, selection, SELECT_COLOR);
    stats.drawStats(drawable);
    stats.play();

}
 
Example 7
Project: incubator-netbeans   File: RemoteFXScreenshot.java   View Source Code Vote up 5 votes
@Override
public ComponentInfo findAt(int x, int y) {
    Rectangle bounds = getWindowBounds();
    if (!bounds.contains(x, y)) {
        return null;
    }

    ComponentInfo[] subComponents = getSubComponents();
    if (subComponents != null) {
        Rectangle tempRect = null;
        ComponentInfo tempRslt = null;
        for (int i = 0; i < subComponents.length; i++) {
            Rectangle sb = subComponents[i].getWindowBounds();
            if (sb.contains(x, y)) {
                tempRect = sb;
                tempRslt = subComponents[i];
                ComponentInfo tci = subComponents[i].findAt(x, y);
                if (tci != null) {
                    Rectangle tbounds = tci.getWindowBounds();
                    if (tempRect.intersects(tbounds)) {
                        tempRect = tbounds;
                        tempRslt = tci;
                    }
                }
            }
        }
        return tempRslt;
    }
    return this;
}
 
Example 8
Project: AWGW   File: GridPanel.java   View Source Code Vote up 5 votes
/**
 * Draw one occupant object. First verify that the object is actually
 * visible before any drawing, set up the clip appropriately and use the
 * DisplayMap to determine which object to call upon to render this
 * particular Locatable. Note that we save and restore the graphics
 * transform to restore back to normalcy no matter what the renderer did to
 * to the coordinate system.
 *
 * @param g2    the Graphics2D object to use to render
 * @param xleft the leftmost pixel of the rectangle
 * @param ytop  the topmost pixel of the rectangle
 * @param obj   the Locatable object to draw
 */
private void drawOccupant(Graphics2D g2, int xleft, int ytop, Object obj) {
    Rectangle cellToDraw = new Rectangle(xleft, ytop, cellSize, cellSize);

    // Only draw if the object is visible within the current clipping
    // region.
    if (cellToDraw.intersects(g2.getClip().getBounds())) {
        Graphics2D g2copy = (Graphics2D) g2.create();
        g2copy.clip(cellToDraw);
        // Get the drawing object to display this occupant.
        Display displayObj = displayMap.findDisplayFor(obj.getClass());
        displayObj.draw(obj, this, g2copy, cellToDraw);
        g2copy.dispose();
    }
}
 
Example 9
Project: WordnetLoom   File: SpanTableUI.java   View Source Code Vote up 5 votes
/**
 * narysowanie kolumny
 *
 * @param col - indeks kolumny
 * @param g - kontekst graficzny
 */
private void paintCol(int col, Graphics g) {
    Rectangle rect = g.getClipBounds();
    SpanTable spanTable = (SpanTable) table;
    for (int row = 0; row < table.getRowCount(); row++) {
        Rectangle cellRect = table.getCellRect(row, col, true);
        if (cellRect.intersects(rect)) // czy jest widoczny
        {
            int rowIndex = spanTable.visibleRow(row, col);
            paintCell(rowIndex, col, g, cellRect);
            row += spanTable.rowSpan(rowIndex, col) - 1;
        }
    }
}
 
Example 10
Project: Spark   File: Collision.java   View Source Code Vote up 5 votes
public static final Rectangle PixelPointCollision(int PointX, int PointY,
		BufferedImage Img1, int X, int Y, int width, int height, int SrcX,
		int SrcY)
{
	Rectangle rct = new Rectangle();

	rct.x = PointX;
	rct.y = PointY;
	rct.width = 1;
	rct.height = 1;

	Rectangle rct2 = new Rectangle();

	rct2.x = X;
	rct2.y = Y;
	rct2.width = width;
	rct2.height = height;

	if (rct.intersects(rct2) == true)
	{
		Rectangle intersection = rct.intersection(rct2);

		int Image1StartX = SrcX + (intersection.x - X);
		int Image1StartY = SrcY + (intersection.y - Y);
		int pixel1 = Img1.getRGB(Image1StartX, Image1StartY);
		int alpha1 = (pixel1 >> 24) & 0xff;

		if (alpha1 > 0)
		{
			return intersection;
		}
	}

	return null;
}
 
Example 11
Project: Spark   File: Collision.java   View Source Code Vote up 5 votes
public static final Rectangle PixelPointWeaponCollision(int PointX, int PointY,
		BufferedImage Img1, int X, int Y, int width, int height, int SrcX,
		int SrcY, int Tolerance)
{
	Rectangle rct = new Rectangle();

	rct.x = PointX;
	rct.y = PointY;
	rct.width = 1;
	rct.height = 1;

	Rectangle rct2 = new Rectangle();

	rct2.x = X + Tolerance;
	rct2.y = Y + Tolerance;
	rct2.width = width - Tolerance;
	rct2.height = height - Tolerance;

	if (rct.intersects(rct2) == true)
	{
		Rectangle intersection = rct.intersection(rct2);

		int Image1StartX = SrcX + (intersection.x - rct2.x);
		int Image1StartY = SrcY + (intersection.y - rct2.y);
		int pixel1 = Img1.getRGB(Image1StartX, Image1StartY);
		int alpha = (pixel1 >> 24) & 0xff;
		int red = (pixel1 >> 16) & 0xff;
		int green = (pixel1 >> 8) & 0xff;
		int blue = (pixel1) & 0xff;

		if (alpha > 0 && !(red == 255 && green == 0 && blue == 0))
		{
			return intersection;
		}
	}

	return null;
}
 
Example 12
Project: Tarski   File: mxEdgeLabelLayout.java   View Source Code Vote up 5 votes
/**
 * 
 */
protected void avoid(mxCellState edge, mxCellState vertex) {
  mxIGraphModel model = graph.getModel();
  Rectangle labRect = edge.getLabelBounds().getRectangle();
  Rectangle vRect = vertex.getRectangle();

  if (labRect.intersects(vRect)) {
    int dy1 = -labRect.y - labRect.height + vRect.y;
    int dy2 = -labRect.y + vRect.y + vRect.height;

    int dy = (Math.abs(dy1) < Math.abs(dy2)) ? dy1 : dy2;

    int dx1 = -labRect.x - labRect.width + vRect.x;
    int dx2 = -labRect.x + vRect.x + vRect.width;

    int dx = (Math.abs(dx1) < Math.abs(dx2)) ? dx1 : dx2;

    if (Math.abs(dx) < Math.abs(dy)) {
      dy = 0;
    } else {
      dx = 0;
    }

    mxGeometry g = model.getGeometry(edge.getCell());

    if (g != null) {
      g = (mxGeometry) g.clone();

      if (g.getOffset() != null) {
        g.getOffset().setX(g.getOffset().getX() + dx);
        g.getOffset().setY(g.getOffset().getY() + dy);
      } else {
        g.setOffset(new mxPoint(dx, dy));
      }

      model.setGeometry(edge.getCell(), g);
    }
  }
}
 
Example 13
Project: brModelo   File: FormaElementar.java   View Source Code Vote up 4 votes
public boolean IntersectPath(Rectangle recsel) {
    return recsel.intersects(getBounds());
}
 
Example 14
Project: incubator-netbeans   File: TaskListTableUI.java   View Source Code Vote up 4 votes
/** Paint a representation of the <code>table</code> instance
    * that was set in installUI().
    * 
    * (copy & paste from BasicTableUI)
    */
   public void paint(Graphics g, JComponent c) {
       Rectangle clip = g.getClipBounds();

       Rectangle bounds = table.getBounds();
       // account for the fact that the graphics has already been translated
       // into the table's bounds
       bounds.x = bounds.y = 0;

if (table.getRowCount() <= 0 || table.getColumnCount() <= 0 ||
               // this check prevents us from painting the entire table
               // when the clip doesn't intersect our bounds at all
               !bounds.intersects(clip)) {

    return;
}

Point upperLeft = clip.getLocation();
Point lowerRight = new Point(clip.x + clip.width - 1, clip.y + clip.height - 1);
       int rMin = table.rowAtPoint(upperLeft);
       int rMax = table.rowAtPoint(lowerRight);
       // This should never happen (as long as our bounds intersect the clip,
       // which is why we bail above if that is the case).
       if (rMin == -1) {
    rMin = 0;
       }
       // If the table does not have enough rows to fill the view we'll get -1.
       // (We could also get -1 if our bounds don't intersect the clip,
       // which is why we bail above if that is the case).
       // Replace this with the index of the last row.
       if (rMax == -1) {
    rMax = table.getRowCount()-1;
       }

       boolean ltr = table.getComponentOrientation().isLeftToRight();
       int cMin = table.columnAtPoint(ltr ? upperLeft : lowerRight); 
       int cMax = table.columnAtPoint(ltr ? lowerRight : upperLeft);        
       // This should never happen.
       if (cMin == -1) {
    cMin = 0;
       }
// If the table does not have enough columns to fill the view we'll get -1.
       // Replace this with the index of the last column.
       if (cMax == -1) {
    cMax = table.getColumnCount()-1;
       }

       // Paint the grid.
       paintGrid(g, rMin, rMax, cMin, cMax);

       // Paint the cells.
paintCells(g, rMin, rMax, cMin, cMax);
   }
 
Example 15
Project: sumo   File: MultiThumbSlider.java   View Source Code Vote up 4 votes
public void paint(Graphics g, JComponent c) {

        Rectangle clip = g.getClipBounds();
        thumbRect = zeroRect;

        super.paint(g, c);

        int thumbNum = additonalUi.getThumbNum();
        Rectangle[] thumbRects = additonalUi.getThumbRects();

        for (int i = thumbNum - 1; 0 <= i; i--) {
            if (clip.intersects(thumbRects[i])) {
                thumbRect = thumbRects[i];

                paintThumb(g);

            }
        }
    }
 
Example 16
Project: VASSAL-src   File: AbstractTileOpImpl.java   View Source Code Vote up 4 votes
/**
 * {@inheritDoc}
 *
 * @throws IllegalArgumentException if <code>rect == null</code>.
 */
public Point[] getTileIndices(Rectangle rect) {
  if (rect == null) throw new IllegalArgumentException();
  return rect.intersects(new Rectangle(size)) ?
    new Point[]{new Point(0,0)} : new Point[0];
}
 
Example 17
Project: VASSAL-src   File: Region.java   View Source Code Vote up 4 votes
public void draw(Graphics g, Rectangle bounds, Rectangle visibleRect, double scale, boolean reversed, int xOffset, int yOffset) {
  if (!bounds.intersects(visibleRect)) {
    return;
  }

  final int labelOffset = 7;

  int size = (int) (scale * myGrid.getFontSize() + 0.5);
  Font f = new Font("Dialog", Font.PLAIN, size); //$NON-NLS-1$

  Color fg = selected ? Color.white : Color.black;
  Color bg = selected ? Color.black : Color.white;

  Rectangle region = bounds.intersection(visibleRect);

  Shape oldClip = g.getClip();
  if (oldClip != null) {
    Area clipArea = new Area(oldClip);
    clipArea.intersect(new Area(region));
    g.setClip(clipArea);
  }

  int posX = (int) (scale * origin.x + 0.5) + bounds.x - 1 + xOffset;
  int posY = (int) (scale * origin.y + 0.5) + bounds.y - 1 + yOffset;

  Color saveColor = g.getColor();

  g.setColor(bg);
  g.fillRect(posX, posY, 3, 3);
  g.setColor(fg);
  g.drawRect(posX, posY, 3, 3);

  g.setColor(saveColor);

  Labeler.drawLabel(g, getLocalizedConfigureName(), posX, posY + labelOffset, f, Labeler.CENTER,
                    Labeler.TOP, fg, bg, fg);
  g.setClip(oldClip);

  // Calculate and store the selection rectangle
  int width = g.getFontMetrics().stringWidth(getConfigureName() + "  ")+1; //$NON-NLS-1$
  int height = g.getFontMetrics().getHeight()+1;

  selectionRect.setLocation(posX - (width / 2), posY - 1);
  selectionRect.setSize(width, height + labelOffset + 1);

}
 
Example 18
Project: VASSAL-src   File: SquareGrid.java   View Source Code Vote up 4 votes
/** Draw the grid even if not marked visible */
public void forceDraw(Graphics g, Rectangle bounds, Rectangle visibleRect, double scale, boolean reversed) {
  if (!bounds.intersects(visibleRect) || color == null) {
    return;
  }

  Graphics2D g2d = (Graphics2D) g;
  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                       RenderingHints.VALUE_ANTIALIAS_ON);

  Rectangle region = bounds.intersection(visibleRect);

  Shape oldClip = g2d.getClip();
  if (oldClip != null) {
    Area clipArea = new Area(oldClip);
    clipArea.intersect(new Area(region));
    g2d.setClip(clipArea);
  }

  double deltaX = scale * dx;
  double deltaY = scale * dy;

  double xmin = reversed ? bounds.x + scale * origin.x + bounds.width - deltaX * round((bounds.x + scale * origin.x + bounds.width - region.x) / deltaX) + deltaX / 2
      : bounds.x + scale * origin.x + deltaX * round((region.x - bounds.x - scale * origin.x) / deltaX) + deltaX / 2;
  double xmax = region.x + region.width;
  double ymin = reversed ? bounds.y + scale * origin.y + bounds.height - deltaY * round((bounds.y + scale * origin.y + bounds.height - region.y) / deltaY) + deltaY / 2
      : bounds.y + scale * origin.y + deltaY * round((region.y - bounds.y - scale * origin.y) / deltaY) + deltaY / 2;
  double ymax = region.y + region.height;

  Point p1 = new Point();
  Point p2 = new Point();
  g2d.setColor(color);
  // x is the location of a vertical line
  for (double x = xmin; x < xmax; x += deltaX) {
    p1.move((int) round(x), region.y);
    p2.move((int) round(x), region.y + region.height);
    g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
  }
  for (double y = ymin; y < ymax; y += deltaY) {
    g2d.drawLine(region.x, (int) round(y), region.x + region.width, (int) round(y));
  }
  if (dotsVisible) {
    xmin = reversed ? bounds.x + scale * origin.x + bounds.width - deltaX * round((bounds.x + scale * origin.x + bounds.width - region.x) / deltaX)
        : bounds.x + scale * origin.x + deltaX * round((region.x - bounds.x - scale * origin.x) / deltaX);
    ymin = reversed ? bounds.y + scale * origin.y + bounds.height - deltaY * round((bounds.y + scale * origin.y + bounds.height - region.y) / deltaY)
        : bounds.y + scale * origin.y + deltaY * round((region.y - bounds.y - scale * origin.y) / deltaY);
    for (double x = xmin; x < xmax; x += deltaX) {
      for (double y = ymin; y < ymax; y += deltaY) {
        p1.move((int) round(x - 0.5), (int) round(y - 0.5));
        g2d.fillRect(p1.x, p1.y, 2, 2);
      }
    }
  }
  g2d.setClip(oldClip);
}
 
Example 19
Project: Spark   File: Collision.java   View Source Code Vote up 4 votes
public static final boolean PixelCollision(BufferedImage Img1, int X, int Y,
		int width, int height, int SrcX, int SrcY, BufferedImage Img2,
		int X2, int Y2, int width2, int height2, int SrcX2, int SrcY2)
{
	Rectangle rct = new Rectangle();

	rct.x = X;
	rct.y = Y;
	rct.width = width;
	rct.height = height;

	Rectangle rct2 = new Rectangle();

	rct2.x = X2;
	rct2.y = Y2;
	rct2.width = width2;
	rct2.height = height2;

	// If this is true, falls within bounds for pixel checking
	if (rct.intersects(rct2) == true)
	{
		Rectangle intersection = rct.intersection(rct2);

		int Image1StartX = SrcX + (intersection.x - X);
		int Image1StartY = SrcY + (intersection.y - Y);

		int Image2StartX = SrcX2 + (intersection.x - X2);
		int Image2StartY = SrcY2 + (intersection.y - Y2);

		for (int j = 0; j < intersection.height; j++)
		{
			for (int i = 0; i < intersection.width; i++)
			{
				int pixel1 = Img1
						.getRGB(Image1StartX + i, Image1StartY + j);
				int alpha1 = (pixel1 >> 24) & 0xff;

				int pixel2 = Img2
						.getRGB(Image2StartX + i, Image2StartY + j);
				int alpha2 = (pixel2 >> 24) & 0xff;

				if (alpha1 > 0 && alpha2 > 0)
				{
					return true;
				}
			}
		}
	}

	return false;
}
 
Example 20
Project: openjdk-jdk10   File: SynthTableUI.java   View Source Code Vote up 4 votes
/**
 * Paints the specified component.
 *
 * @param context context for the component being painted
 * @param g the {@code Graphics} object used for painting
 * @see #update(Graphics,JComponent)
 */
protected void paint(SynthContext context, Graphics g) {
    Rectangle clip = g.getClipBounds();

    Rectangle bounds = table.getBounds();
    // account for the fact that the graphics has already been translated
    // into the table's bounds
    bounds.x = bounds.y = 0;

    if (table.getRowCount() <= 0 || table.getColumnCount() <= 0 ||
            // this check prevents us from painting the entire table
            // when the clip doesn't intersect our bounds at all
            !bounds.intersects(clip)) {

        paintDropLines(context, g);
        return;
    }

    boolean ltr = table.getComponentOrientation().isLeftToRight();

    Point upperLeft = clip.getLocation();

    Point lowerRight = new Point(clip.x + clip.width - 1,
                                 clip.y + clip.height - 1);

    int rMin = table.rowAtPoint(upperLeft);
    int rMax = table.rowAtPoint(lowerRight);
    // This should never happen (as long as our bounds intersect the clip,
    // which is why we bail above if that is the case).
    if (rMin == -1) {
        rMin = 0;
    }
    // If the table does not have enough rows to fill the view we'll get -1.
    // (We could also get -1 if our bounds don't intersect the clip,
    // which is why we bail above if that is the case).
    // Replace this with the index of the last row.
    if (rMax == -1) {
        rMax = table.getRowCount()-1;
    }

    int cMin = table.columnAtPoint(ltr ? upperLeft : lowerRight);
    int cMax = table.columnAtPoint(ltr ? lowerRight : upperLeft);
    // This should never happen.
    if (cMin == -1) {
        cMin = 0;
    }
    // If the table does not have enough columns to fill the view we'll get -1.
    // Replace this with the index of the last column.
    if (cMax == -1) {
        cMax = table.getColumnCount()-1;
    }

    // Paint the cells.
    paintCells(context, g, rMin, rMax, cMin, cMax);

    // Paint the grid.
    // it is important to paint the grid after the cells, otherwise the grid will be overpainted
    // because in Synth cell renderers are likely to be opaque
    paintGrid(context, g, rMin, rMax, cMin, cMax);

    paintDropLines(context, g);
}