Java Code Examples for java.awt.Graphics#clipRect()

The following examples show how to use java.awt.Graphics#clipRect() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: blog-codes   File: mxGraphComponent.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 
 */
protected void paintBackground(Graphics g)
{
	Rectangle clip = g.getClipBounds();
	Rectangle rect = paintBackgroundPage(g);

	if (isPageVisible())
	{
		g.clipRect(rect.x + 1, rect.y + 1, rect.width - 1, rect.height - 1);
	}

	// Paints the clipped background image
	paintBackgroundImage(g);

	// Paints the grid directly onto the graphics
	paintGrid(g);
	g.setClip(clip);
}
 
Example 2
Source Project: netbeans   File: GlassPane.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Paints additional information about component constraints.
 * 
 * @param g graphics to use for painting.
 */
private void paintConstraints(Graphics g) {
    Point shift = fromComponentPane(new Point());
    Graphics gClip = g.create();
    Rectangle paneRect = fromComponentPane(new Rectangle(new Point(), componentPane.getSize()));
    gClip.clipRect(paneRect.x, paneRect.y, paneRect.width, paneRect.height);
    gClip.translate(shift.x, shift.y);
    for (Component comp : componentPane.getComponents()) {
        if (GridUtils.isPaddingComponent(comp)) {
            continue;
        }
        boolean selected = selection.contains(comp);
        gridInfo.paintConstraints(gClip, comp, selected);
    }
    gClip.dispose();
}
 
Example 3
Source Project: stendhal   File: MapPanel.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public void paintComponent(final Graphics g) {
	// Set this first, so that any changes made during the drawing will
	// flag the map changed
	controller.setNeedsRefresh(false);

	g.setColor(getBackground());
	g.fillRect(0, 0, getWidth(), getHeight());
	// The rest of the things should be drawn inside the actual map area
	g.clipRect(0, 0, width, height);
	// also choose the origin so that we can simply draw to the
	// normal coordinates
	g.translate(-xOffset, -yOffset);

	drawMap(g);
	drawEntities(g);

	g.dispose();
}
 
Example 4
Source Project: stendhal   File: StyleUtil.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Fill an area with the background sprite of a {@link Style}.
 *
 * @param style the style to be used
 * @param graphics
 * @param x left x coordinate
 * @param y top y coordinate
 * @param width width of the area
 * @param height height of the area
 */
static void fillBackground(Style style, Graphics graphics, int x,
		int y, int width, int height) {
	// Prepare clipping
	graphics = graphics.create();
	graphics.clipRect(x, y, width, height);

	Sprite image = style.getBackground();

	for (int i = x; i < x + width; i += image.getWidth()) {
		for (int j = y; j < y + height; j += image.getHeight()) {
			image.draw(graphics, i, j);
		}
	}
	graphics.dispose();
}
 
Example 5
Source Project: osp   File: Histogram.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 *  Draws this histogram in the drawing panel.
 *
 * @param  drawingPanel
 * @param  g
 */
public synchronized void draw(DrawingPanel drawingPanel, Graphics g) {
  if(bins.size()==0 || !visible) {
    return;
  }
  Shape oldClip = g.getClip();
  g.setColor(binFillColor);
  g.clipRect(0, 0, drawingPanel.getWidth(), drawingPanel.getHeight());
  for(Iterator<Integer> keys = bins.keySet().iterator(); keys.hasNext(); ) {
    Integer binNumber = keys.next();
    Double d = (bins.get(binNumber));
    if(d==null) {
      return;
    }
    double occurrences = d.doubleValue();
    if(normalizedToOne) {
      occurrences /= sum;
    }
    if(binStyle==DRAW_BIN) {
      drawBin(drawingPanel, g, binNumber.intValue(), occurrences);
    } else {
      drawPoint(drawingPanel, g, binNumber.intValue(), occurrences);
    }
  }
  g.setClip(oldClip);
}
 
Example 6
private static void draw(final Image img) {
    Graphics g = img.getGraphics();
    g.setColor(BACKGROUND);
    g.fillRect(0, 0, WIDTH, HEIGHT);
    g.setColor(COLOR1);
    g.drawRect(OFFSET, OFFSET, WIDTH - OFFSET * 2, HEIGHT - OFFSET * 2);
    g.setColor(COLOR2);
    g.clipRect(OFFSET, OFFSET, WIDTH - OFFSET * 2 + 1, HEIGHT - OFFSET * 2 + 1);
    ((Graphics2D) g).setStroke(dash);
    g.drawRect(OFFSET, OFFSET, WIDTH - OFFSET * 2, HEIGHT - OFFSET * 2);
    g.dispose();
}
 
Example 7
private static void draw(final Image img) {
    Graphics g = img.getGraphics();
    g.setColor(BACKGROUND);
    g.fillRect(0, 0, WIDTH, HEIGHT);
    g.setColor(COLOR1);
    g.drawRect(OFFSET, OFFSET, WIDTH - OFFSET * 2, HEIGHT - OFFSET * 2);
    g.setColor(COLOR2);
    g.clipRect(OFFSET, OFFSET, WIDTH - OFFSET * 2 + 1, HEIGHT - OFFSET * 2 + 1);
    ((Graphics2D) g).setStroke(dash);
    g.drawRect(OFFSET, OFFSET, WIDTH - OFFSET * 2, HEIGHT - OFFSET * 2);
    g.dispose();
}
 
Example 8
private static void draw(final Image img) {
    Graphics g = img.getGraphics();
    g.setColor(BACKGROUND);
    g.fillRect(0, 0, WIDTH, HEIGHT);
    g.setColor(COLOR1);
    g.drawRect(OFFSET, OFFSET, WIDTH - OFFSET * 2, HEIGHT - OFFSET * 2);
    g.setColor(COLOR2);
    g.clipRect(OFFSET, OFFSET, WIDTH - OFFSET * 2 + 1, HEIGHT - OFFSET * 2 + 1);
    ((Graphics2D) g).setStroke(dash);
    g.drawRect(OFFSET, OFFSET, WIDTH - OFFSET * 2, HEIGHT - OFFSET * 2);
    g.dispose();
}
 
Example 9
@Override
public void paint(Graphics g) {
    super.paint(g);
    //Color bkp = g.getColor();
    Rectangle r = this.getBounds();
    g.setColor(Color.BLACK);
    int re = 0;
    String bonito = "?";
    if (getAcaoTipo() == TipoDeAcao.tpAcaoDlgCor || getAcaoTipo() == TipoDeAcao.tpReadOnlyCor) {
        g.setColor(Color.BLACK);
        g.fillRect(3, 3, r.height - 7, r.height - 7);
        try {
            Color c = util.Utilidades.StringToColor(getTexto());
            g.setColor(c);
            bonito = getTexto();
        } catch (Exception e) {
        }
        g.fillRect(4, 4, r.height - 8, r.height - 8);
        //g.setColor(Color.BLACK);
        //g.drawRect(3, 3, r.height - 7, r.height - 7);
        re = r.height - 1;
    } else {
        bonito = getTexto().replaceAll("\n", " | ");
    }
    //g.setColor(bkp);
    
    Rectangle obkp = g.getClipBounds();

    g.setColor(Color.DARK_GRAY);
    g.setFont(new Font(this.getFont().getFontName(), Font.BOLD, getFont().getSize()));
    g.clipRect(re, 0, r.width - r.height -re - (re == 0? 4: 8), r.height);
    g.drawString(bonito, re + 2, (int) (r.height * 0.72) + 1);
    g.drawLine(0, 0, 0, getHeight());
    g.setClip(obkp);
}
 
Example 10
Source Project: pumpernickel   File: RowLayout.java    License: MIT License 5 votes vote down vote up
@Override
public void paint(Graphics g) {
	g = g.create();
	Rectangle clipping = (Rectangle) getClientProperty(CLIPPING);
	if (clipping != null) {
		clipping = SwingUtilities.convertRectangle(
				RowLayout.this.panel, clipping, this);
		g.clipRect(clipping.x, clipping.y, clipping.width,
				clipping.height);
	}
	super.paint(g);
	g.dispose();
}
 
Example 11
Source Project: pumpernickel   File: AnimationController.java    License: MIT License 5 votes vote down vote up
@Override
public void paintBorder(Component c, Graphics g, int x, int y,
		int width, int height) {
	if (c.isFocusOwner()) {
		g.clipRect(x, y, width, height);
		Rectangle r = new Rectangle(x - 1, y - 1, width + 1, height + 1);
		PlafPaintUtils.paintFocus((Graphics2D) g, r, 2);
	}
}
 
Example 12
Source Project: pumpernickel   File: AbstractSearchHighlight.java    License: MIT License 5 votes vote down vote up
@Override
protected void paintComponent(Graphics g) {
	Graphics2D g2 = (Graphics2D) g;

	Rectangle clipping = getClipping(highlightInfo.jc, this);
	if (clipping.width == 0 || clipping.height == 0)
		return;

	g.clipRect(clipping.x, clipping.y, clipping.width, clipping.height);

	Point2D absCenter = SwingUtilities.convertPoint(highlightInfo.jc,
			center.x, center.y, this);
	g2.translate(absCenter.getX(), absCenter.getY());
	AffineTransform transform = (AffineTransform) getClientProperty(
			"transform");
	if (transform != null)
		g2.transform(transform);
	g2.translate(-imageCenter.x, -imageCenter.y);
	g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
			RenderingHints.VALUE_ANTIALIAS_ON);
	g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
			RenderingHints.VALUE_INTERPOLATION_BILINEAR);

	Number opacity = (Number) getClientProperty("opacity");
	if (opacity != null) {
		g2.setComposite(AlphaComposite.getInstance(
				AlphaComposite.SRC_OVER, opacity.floatValue()));
	}

	g2.drawImage(image, 0, 0, null);
}
 
Example 13
private static void draw(final Image img) {
    Graphics g = img.getGraphics();
    g.setColor(BACKGROUND);
    g.fillRect(0, 0, WIDTH, HEIGHT);
    g.setColor(COLOR1);
    g.drawRect(OFFSET, OFFSET, WIDTH - OFFSET * 2, HEIGHT - OFFSET * 2);
    g.setColor(COLOR2);
    g.clipRect(OFFSET, OFFSET, WIDTH - OFFSET * 2 + 1, HEIGHT - OFFSET * 2 + 1);
    ((Graphics2D) g).setStroke(dash);
    g.drawRect(OFFSET, OFFSET, WIDTH - OFFSET * 2, HEIGHT - OFFSET * 2);
    g.dispose();
}
 
Example 14
@Override
public void paintBorder(Graphics g) {
	Graphics graphics = g.create();
	graphics.clipRect(0, getHeight() - insets.bottom, getWidth(), getHeight());
	/*
	 * Adjust the width, so that the drawn border does not get corners
	 * that may look ugly
	 */
	getBorder().paintBorder(this, graphics, -insets.left, 0,
			getWidth() + insets.left + insets.right, getHeight());
	graphics.dispose();
}
 
Example 15
Source Project: osp   File: EpsGraphics2D.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns an EpsGraphics2D object based on this
 * Graphics object, but with a new translation and clip
 * area.
 */
public Graphics create(int x, int y, int width, int height) {
  Graphics g = create();
  g.translate(x, y);
  g.clipRect(0, 0, width, height);
  return g;
}
 
Example 16
Source Project: seaglass   File: SeaGlassSplitPaneUI.java    License: Apache License 2.0 5 votes vote down vote up
private void paintDragDivider(Graphics g, int x, int y, int w, int h) {
    SeaGlassContext context = getContext(splitPane, Region.SPLIT_PANE_DIVIDER);
    context.setComponentState(((context.getComponentState() | MOUSE_OVER) ^ MOUSE_OVER) | PRESSED);
    Shape oldClip = g.getClip();
    g.clipRect(x, y, w, h);
    context.getPainter().paintSplitPaneDragDivider(context, g, x, y, w, h, splitPane.getOrientation());
    g.setClip(oldClip);
    context.dispose();
}
 
Example 17
Source Project: niftyeditor   File: ImageModeCanvas.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.clearRect(0, 0, this.image.getWidth(null), this.image.getHeight(null));
    g.drawImage(this.image, 0, 0, null);
    g.clipRect(0, 0, this.image.getWidth(null), this.image.getHeight(null));
    painter.paint(g);
   
}
 
Example 18
Source Project: osp   File: CellLatticeOSX.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 *  Draws the lattice and the grid.
 *
 * @param  panel
 * @param  g
 */
public void draw(DrawingPanel panel, Graphics g) {
  if(!visible) {
    return;
  }
  double ymax = this.ymax;
  double xmin = this.xmin;
  if(panel.getXMax()<panel.getXMin()) { // x axis is flipped
    xmin = (dx<0) ? this.xmin-dx : this.xmin+dx;
  }
  if(panel.getYMax()<panel.getYMin()) { // yaxis is flipped
    ymax = (dy<0) ? this.ymax+dy : this.ymax-dy;
  }
  double x = (dx<0) ? xmin+dx : xmin;
  double y = (dy<0) ? ymax-dy : ymax;
  int x1pix = panel.xToPix(x);
  int y1pix = panel.yToPix(y);
  int x2pix, y2pix;
  Shape clipShape = g.getClip();
  Rectangle r = getBounds(panel);
  g.clipRect(r.x, r.y, r.width, r.height);
  for(int ix = 0; ix<nx; ix++) {
    x += dx;
    x2pix = panel.xToPix(x);
    for(int iy = ny-1; iy>=0; iy--) { // start at top
      y -= dy;
      y2pix = panel.yToPix(y);
      int val = data[ix][iy]&0xFF;
      g.setColor(colors[val]);
      g.fillRect(x1pix, y1pix, Math.abs(x2pix-x1pix)+1, Math.abs(y1pix-y2pix)+1);
      y1pix = y2pix;
    }
    x1pix = x2pix;
    y = (dy<0) ? ymax-dy : ymax;
    y1pix = panel.yToPix(y);
  }
  g.setClip(clipShape);
  super.draw(panel, g); // draw the grid
}
 
Example 19
Source Project: zap-extensions   File: JScrollPopupMenu.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void paintChildren(Graphics g){
    Insets insets = getInsets();
    g.clipRect(insets.left, insets.top, getWidth(), getHeight() - insets.top - insets.bottom);
    super.paintChildren(g);
}
 
Example 20
Source Project: pdfxtk   File: MultiColumnListUI.java    License: Apache License 2.0 4 votes vote down vote up
/**
  * Paint the rows that intersect the Graphics objects clipRect.  This
  * method calls paintCell as necessary.  Subclasses
  * may want to override these methods.
  *
  * @see #paintCell
  */
 public void paint(Graphics g, JComponent c)
 {
   maybeUpdateLayoutState();
   
   ListCellRenderer   renderer  = list.getCellRenderer();
   ListModel          dataModel = list.getModel();
   ListSelectionModel selModel  = list.getSelectionModel();
   
   if ((renderer == null) || (dataModel.getSize() == 0)) {
     return;
   }
   
   /* Compute the area we're going to paint in terms of the affected
    * rows (firstPaintRow, lastPaintRow), and the clip bounds.
    */
   
   Rectangle paintBounds   = g.getClipBounds();
   int       firstPaintRow = locationToIndex(paintBounds.x, paintBounds.y);
   int       lastPaintRow  = locationToIndex((paintBounds.x + paintBounds.width) - 1, (paintBounds.y + paintBounds.height) - 1);
   
   if (firstPaintRow == -1) {
     firstPaintRow = 0;
   }
   if (lastPaintRow == -1) {
     lastPaintRow = dataModel.getSize() - 1;
   }
   
   Rectangle rowBounds = getCellBounds(list, firstPaintRow, lastPaintRow);
   if (rowBounds == null) {
     return;
   }
   
   int leadIndex = list.getLeadSelectionIndex();
   
   for(int row = firstPaintRow; row <= lastPaintRow; row++) {
     /* Set the clip rect to be the intersection of rowBounds
      * and paintBounds and then paint the cell.
      */
     
     g.setClip(rowBounds.x, rowBounds.y, rowBounds.width, rowBounds.height);
     g.clipRect(paintBounds.x, paintBounds.y, paintBounds.width, paintBounds.height);
     
     Rectangle bounds = getCellBounds(row);
     if(bounds != null)
paintCell(g, row, bounds, renderer, dataModel, selModel, leadIndex);
   }
 }