下面列出了java.awt.Graphics2D#clipRect ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
// prevent the clipping from applying to the lines
Graphics2D another = (Graphics2D)g2d.create();
g2d.scale(get(zoom), get(zoom));
g2d.clipRect(get(cropLeft) + 10, get(cropTop) + 10, getWidth() / get(zoom) - get(cropLeft) - get(cropRight) - 20,
getHeight() / get(zoom) - get(cropTop) - get(cropBottom) - 20);
BufferedImage img = wiz.getImage();
g2d.drawImage(img, 10, 10, null);
another.scale(get(zoom), get(zoom));
another.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
another.drawLine(0, get(top) + 10, getWidth() / get(zoom), get(top) + 10);
another.drawLine(0, getHeight() / get(zoom) - get(bottom) - 10, getWidth() / get(zoom), getHeight() / get(zoom) - get(bottom) - 10);
another.drawLine(get(left) + 10, 0, get(left) + 10, getHeight() / get(zoom));
another.drawLine(getWidth() / get(zoom) - get(right) - 10, 0, getWidth() / get(zoom) - get(right) - 10, getHeight() / get(zoom));
another.dispose();
}
@Override
protected void paintIcon(Component c, Graphics2D g, int width, int height, double scaling) {
final Color color;
if (UIManager.getBoolean("nb.dark.theme")) {
// Foreground brightness level taken from the combobox dropdown on Darcula.
color = disabled ? new Color(90, 90, 90, 255) : new Color(187, 187, 187, 255);
} else {
color = disabled ? new Color(201, 201, 201, 255) : new Color(86, 86, 86, 255);
}
g.setColor(color);
final double overshoot = 2.0 / scaling;
final double arrowWidth = width + overshoot * scaling;
final double arrowHeight = height - 0.2 * scaling;
final double arrowMidX = arrowWidth / 2.0 - (overshoot / 2.0) * scaling;
g.clipRect(0, 0, width, height);
Path2D.Double arrowPath = new Path2D.Double();
arrowPath.moveTo(arrowMidX - arrowWidth / 2.0, 0);
arrowPath.lineTo(arrowMidX, arrowHeight);
arrowPath.lineTo(arrowMidX + arrowWidth / 2.0, 0);
arrowPath.closePath();
g.fill(arrowPath);
}
@Override
public void draw(Graphics2D g2) {
int hgap = (height - upButton.height - downButton.height) / 3;
upButton.x = width - upButton.width /* - gaps */;
downButton.x = width - downButton.width /* - gaps */;
upButton.y = hgap;
downButton.y = hgap * 2 + upButton.height;
Shape save0 = g2.getClip();
g2.clipRect(0, 0, width, height);
super.draw(g2);
if (borderColor != 0) {
g2.setColor(new Color(borderColor, true));
g2.drawRect(0, 0, width - 1, height - 1);
}
g2.setClip(save0);
}
@Override
public void paint( final Graphics2D g, final Rect bounds, final Rect visibleRect ) {
super.paint( g, bounds, visibleRect );
drawingRect = bounds.applyInsets( INSETS );
if ( drawingRect.isEmpty() ) {
g.setColor( ChartsCanvas.COLOR_MESSAGE );
g.drawString( "Not enough space to draw chart!", visibleRect.x1 + 5, visibleRect.y1 + 12 );
return;
}
// Things that may be inside or outside of the drawing rectangle
paintAxesLabels();
// Things that must be inside of the drawing rectangle
final Rectangle oldClipBounds = g.getClipBounds();
g.clipRect( drawingRect.x1, drawingRect.y1, drawingRect.width, drawingRect.height );
paintAxes();
paintData();
g.setClip( oldClipBounds );
// Things that may be inside or outside of the drawing rectangle
paintTitles();
}
/**
* Paint an extra shadow on top of the track. I wish there were an
* easier way to do this, but I looked through the WindowsSliderUI and
* didn't see a way to customize the track color.
*/
protected void paintDarkTrack(Graphics g0) {
Graphics2D g = (Graphics2D) g0;
VectorImage img = new VectorImage();
Graphics2D w = img.createGraphics();
w.setRenderingHints(g.getRenderingHints());
w.clipRect(0, 0, getWidth(), getHeight());
super.paintComponent(w);
for (Operation op : img.getOperations()) {
op.paint((Graphics2D) g);
if (op instanceof ImageOperation) {
Rectangle r = op.getBounds().getBounds();
if (r.width > getWidth() * .8) {
g.setColor(new Color(0, 0, 0, 40));
((Graphics2D) g).fill(op.getBounds());
}
}
}
}
/**
* Paint the middle part (the un-bordered part) of this Background.
* The dimensions provided here should be for the entire area.
*/
public void paintContents(Component c, Graphics g, int x, int y, int width,
int height) {
Insets insets = getBorderInsets(c);
Rectangle insetRect = new Rectangle(x + insets.left, y + insets.top,
width - insets.left - insets.right, height - insets.top
- insets.bottom);
if (insetRect.width > 0 && insetRect.height > 0) {
Graphics2D g2 = (Graphics2D) g.create(x, y, width, height);
g2.clipRect(insetRect.x, insetRect.y, insetRect.width,
insetRect.height);
paint(c, g2, 0, 0, width, height);
g2.dispose();
}
}
/**
* Draw this Dataset in the drawing panel.
*
* @param drawingPanel the drawing panel
* @param g the graphics
*/
public void draw(DrawingPanel drawingPanel, Graphics g) {
super.draw(drawingPanel, g);
Graphics2D g2 = (Graphics2D) g;
int offset = getMarkerSize()+4;
int edge = 2*offset;
// increase the clip to include the entire highlight
Shape clipShape = g2.getClip();
g2.setClip(drawingPanel.leftGutter-offset-1, drawingPanel.topGutter-offset-1, drawingPanel.getWidth()-drawingPanel.leftGutter-drawingPanel.rightGutter+2+2*offset, drawingPanel.getHeight()-drawingPanel.bottomGutter-drawingPanel.topGutter+2+2*offset);
Rectangle viewRect = drawingPanel.getViewRect();
if(viewRect!=null) { // decrease the clip if we are in a scroll pane
g2.clipRect(viewRect.x, viewRect.y, viewRect.x+viewRect.width, viewRect.y+viewRect.height);
}
hitShapes = new Shape[index];
double[] xValues = getXPoints();
double[] yValues = getYPoints();
if (screenCoordinates[0]==null || screenCoordinates[0].length!=index) {
screenCoordinates[0] = new double[index];
screenCoordinates[1] = new double[index];
}
for(int i = 0; i<index; i++) {
if(Double.isNaN(yValues[i])) {
screenCoordinates[1][i] = Double.NaN;
continue;
}
double xp = drawingPanel.xToPix(xValues[i]);
double yp = drawingPanel.yToPix(yValues[i]);
screenCoordinates[0][i] = xp;
screenCoordinates[1][i] = yp;
hitShapes[i] = new Rectangle2D.Double(xp-offset, yp-offset, edge, edge);
if(!isHighlighted(i)) {
continue;
}
g2.setColor(highlightColor);
g2.fill(hitShapes[i]);
}
g2.setClip(clipShape); // restore the original clip
}
private void paintContent(Graphics g) {
if (contentBuffer == null ||
contentBuffer.getWidth() != contentPane.getWidth() ||
contentBuffer.getHeight() != contentPane.getHeight()) {
if (contentBuffer != null) {
contentBuffer.flush();
contentGraphics.dispose();
}
contentBuffer =
GraphicsUtilities.createCompatibleTranslucentImage(
contentPane.getWidth(), contentPane.getHeight());
contentGraphics = contentBuffer.createGraphics();
}
Graphics2D g2 = contentGraphics;
g2.clipRect(contentPane.getX(), contentPane.getY(),
contentPane.getWidth(), contentPane.getHeight());
g2.setComposite(AlphaComposite.Clear);
Rectangle clip = g.getClipBounds();
g2.fillRect(clip.x, clip.y, clip.width, clip.height);
g2.setComposite(AlphaComposite.SrcOver);
g2.setColor(g.getColor());
g2.setFont(g.getFont());
super.paint(g2);
g.drawImage(contentBuffer, 0, 0, null);
}
@Override
protected void paintBase(Graphics2D g) {
if (endOFF) {
return;
}
setArea(null);
Rectangle r = this.getBounds();
g.setColor(Color.GRAY);
g.drawRoundRect(0, 0, r.width - 1, r.height - 1, 10, 10);
g.setColor(Color.lightGray);
g.drawRoundRect(5, 5, r.height - 10, r.height - 10, 4,4);
int met = (r.height - 11) / 2;
g.setColor(Color.black);
g.drawLine(7, 6 + met, r.height - 7, 6 + met);
if ('+' == getEstado()) {
g.drawLine(6 + met, 7, 6 + met, r.height - 7);
}
g.setColor(Color.BLACK);
Rectangle bkp = g.getClipBounds();
g.clipRect(0, 0, r.width - 1, r.height);
if (isSelecionado()) {
g.setFont(new Font(this.getFont().getFontName(), Font.BOLD, getFont().getSize()));
g.drawRoundRect(0, 0, r.width - 1, r.height - 1, 10, 10);
}
int tmp = (r.width - g.getFontMetrics().stringWidth(getTexto())) / 2;
g.drawString(getTexto(), tmp, (int) (r.height * 0.72));
g.setClip(bkp);
}
/**
* This tests clipping different Graphics2Ds, drawing arcs, a SrcOut
* composite to clear pixels.
*/
public void testContext1() throws Exception {
RenderTest t = new RenderTest() {
@Override
public void paint(Graphics2D g) {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setStroke(new BasicStroke(15));
g.clipRect(20, 20, 160, 160);
g.setColor(Color.cyan);
g.fillRect(0, 0, 200, 200);
Graphics2D g2 = (Graphics2D) g.create(40, 40, 100, 100);
g2.setColor(Color.pink);
g2.drawArc(20, 20, 120, 120, 0, 190);
Graphics2D g3 = (Graphics2D) g.create(60, 0, 100, 100);
g3.setComposite(AlphaComposite.SrcOut);
g3.setColor(Color.magenta);
g3.drawArc(10, 10, 120, 120, 180, 340);
}
};
t.test();
}
/**
* Paints the charts.
*
* @param g graphics context in which to paint
*/
private void paintCharts( final Graphics2D g ) {
if ( chartList.isEmpty() )
return;
// Calculate charts sizes
// Use full width
final int chartWidth = getWidth() - INSETS.left - INSETS.right;
// Vertical space available for a chart:
final int chartVSpace = getHeight() / chartList.size(); // Size > 0
final int chartHeight = chartVSpace - INSETS.top - INSETS.bottom;
if ( chartHeight <= 0 )
return;
final Rect visibleRect = new Rect( getVisibleRect() );
final Rectangle oldClipBounds = g.getClipBounds();
int y = 0;
for ( final Chart< ? > chart : chartList ) {
final Rect r = new Rect( INSETS.left, y + INSETS.top, chartWidth, chartHeight );
g.setClip( oldClipBounds );
g.clipRect( r.x1, r.y1, r.width, r.height );
chart.paint( g, r, visibleRect.intersection( r ) );
y += chartVSpace;
}
g.setClip( oldClipBounds );
}
/**
* Paints all the drawable objects in the panel.
* @param g
*/
protected void paintDrawableList(Graphics g, ArrayList<Drawable> tempList) {
if(tempList==null) {
return;
}
Graphics2D g2 = (Graphics2D) g;
Iterator<Drawable> it = tempList.iterator();
Shape clipShape = g2.getClip();
int w = getWidth()-leftGutter-rightGutter;
int h = getHeight()-bottomGutter-topGutter;
if((w<0)||(h<0)) {
return;
}
if(clipAtGutter) {
g2.clipRect(leftGutter, topGutter, w, h);
}
if(!tempList.isEmpty()&&(tempList.get(0) instanceof False3D)) {
tempList.get(0).draw(this, g2);
} else {
while(it.hasNext()) {
if(!validImage) {
break; // abort drawing
}
Drawable drawable = it.next();
drawable.draw(this, g2);
}
}
g2.setClip(clipShape);
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
Graphics2D g2 = (Graphics2D) g.create();
Point viewPosition = scrollPane.getViewport().getViewPosition();
g2.clipRect(x, y, width, height);
g2.translate(-viewPosition.x, -viewPosition.y);
renderer.paintComponent(g2);
g2.dispose();
}
@Override
protected void paintChildren(Graphics g0) {
Graphics2D g = (Graphics2D) g0.create();
g.setClip(null);
g.transform(getRotation().createTransform(getWidth(), getHeight()));
g.clipRect(0, 0, getHeight(), getWidth());
super.paintChildren(g);
g.dispose();
}
@Override
public void draw(Graphics2D g2) {
base.height = height - 38;
RenderTools.darkenAround(base, width, height, g2, 0.5f, true);
g2.drawImage(commons.equipment().panelTop, base.x, base.y, null);
RenderTools.fill(g2, base.x, base.y + commons.equipment().panelTop.getHeight(), base.width, base.y + base.height - commons.equipment().panelBottom.getHeight(), commons.equipment().panelMiddle);
g2.drawImage(commons.equipment().panelBottom, base.x, base.y + base.height - commons.equipment().panelBottom.getHeight(), null);
RenderTools.fill(g2, leftPanel, commons.equipment().leftStars);
RenderTools.fill(g2, rightPanel, commons.equipment().rightStars);
update();
if (planetVisible) {
Shape save0 = g2.getClip();
g2.clipRect(leftPanel.x, leftPanel.y, leftPanel.width, leftPanel.height);
BufferedImage planet = planet().type.equipment;
g2.drawImage(planet, leftPanel.x, leftPanel.y + (leftPanel.height - planet.getHeight()) / 2, null);
g2.setClip(save0);
}
super.draw(g2);
if (innerEquipmentVisible) {
g2.setColor(new Color(0, 0, 0, 128));
g2.fillRect(innerEquipment.x, innerEquipment.y, innerEquipment.width - 1, innerEquipment.height - 1);
g2.setColor(new Color(0xFF4D7DB6));
g2.drawRect(innerEquipment.x, innerEquipment.y, innerEquipment.width - 1, innerEquipment.height - 1);
drawAgain(g2, innerEquipmentName);
drawAgain(g2, innerEquipmentSeparator);
drawAgain(g2, innerEquipmentValue);
}
if (secondary == null) {
if (!fleetListing.visible()) {
if (configure.item != null && configure.item.count > 0) {
drawDPS(g2, configure.item);
} else
if (configure.type != null) {
Player o;
int cnt;
if (player().selectionMode == SelectionMode.PLANET || fleet() == null) {
o = planet().owner;
cnt = planet().inventoryCount(configure.type, o);
} else {
o = fleet().owner;
cnt = fleet().inventoryCount(configure.type);
}
if (cnt > 0) {
InventoryItem ii = new InventoryItem(world().newId(), o, configure.type);
ii.count = cnt;
ii.init();
drawDPS(g2, ii);
}
}
}
drawTotalDPS(g2);
if (upgradeVisible && secondary == null && !fleetListing.visible()
&& configure.selectedSlot == null) {
drawUpgrades(g2);
}
}
}
private void drawBreakSymbol(ColorBreak aCB, Rectangle rect, boolean selected, Graphics2D g) {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
float aSize;
PointF aP = new PointF(0, 0);
float width, height;
aP.X = rect.x + rect.width / 2;
aP.Y = rect.y + rect.height / 2;
//Draw selected back color
if (selected) {
g.setColor(Color.lightGray);
g.fill(new Rectangle(_symbolWidth, rect.y, _valueWidth + _labelWidth, rect.height));
}
//Draw symbol
switch (aCB.getBreakType()) {
case PointBreak:
PointBreak aPB = (PointBreak) aCB;
aSize = aPB.getSize();
if (aPB.isDrawShape()) {
if (aPB.getMarkerType() == MarkerType.Character) {
Draw.drawPoint(aP, aPB, g);
} else {
Draw.drawPoint(aP, aPB, g);
}
}
break;
case PolylineBreak:
PolylineBreak aPLB = (PolylineBreak) aCB;
aSize = aPLB.getWidth();
width = rect.width / 3 * 2;
height = rect.height / 3 * 2;
Draw.drawPolylineSymbol(aP, width, height, aPLB, g);
break;
case PolygonBreak:
PolygonBreak aPGB = (PolygonBreak) aCB;
width = rect.width / 3 * 2;
height = rect.height / 5 * 4;
if (aPGB.isDrawShape()) {
Draw.drawPolygonSymbol(aP, width, height, aPGB, g);
}
break;
case ColorBreak:
width = rect.width / 3 * 2;
height = rect.height / 3 * 2;
Draw.drawPolygonSymbol(aP, aCB.getColor(), Color.black, width,
height, true, true, g);
break;
}
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
//Draw value and label
int sX = _symbolWidth;
Font aFont = new Font("Simsun", Font.PLAIN, 13);
String str = aCB.getCaption();
FontMetrics metrics = g.getFontMetrics(aFont);
Dimension size = new Dimension(metrics.stringWidth(str), metrics.getHeight());
aP.X = sX;
aP.Y = rect.y + rect.height * 3 / 4;
g.setFont(aFont);
g.setColor(Color.black);
if (_legendScheme.getLegendType() == LegendType.SingleSymbol) {
g.drawString(str, aP.X, aP.Y);
} else {
//Label
aP.X += _valueWidth;
g.drawString(str, aP.X, aP.Y);
//Value
if (String.valueOf(aCB.getStartValue()).equals(
String.valueOf(aCB.getEndValue()))) {
str = String.valueOf(aCB.getStartValue());
} else {
str = String.valueOf(aCB.getStartValue()) + " - " + String.valueOf(aCB.getEndValue());
}
//size = new Dimension(metrics.stringWidth(str), metrics.getHeight());
aP.X = sX;
Rectangle clip = g.getClipBounds();
g.clipRect(sX, rect.y, this._valueWidth - 5, rect.height);
g.drawString(str, aP.X, aP.Y);
g.setClip(clip.x, clip.y, clip.width, clip.height);
}
}
protected void paintRealBackground(Graphics g0) {
Graphics g = g0.create();
Insets i = getComponent().getInsets();
g.translate(i.left, i.top);
if (editor.isOpaque()) {
g.setColor(editor.getBackground());
g.fillRect(0, 0, editor.getWidth(), editor.getHeight());
}
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
if (editor.hasFocus()) {
PlafPaintUtils.paintFocus(g2, path, focusPadding);
}
g2.setColor(editor.getBackground());
g2.fill(path);
Shape oldClip = g2.getClip();
g2.clipRect(0, 0, editor.getWidth(), editor.getHeight() / 2);
g2.translate(0, 1);
g2.setPaint(new Color(0xBBBBBB));
g2.draw(path);
g2.translate(0, -1);
g2.setClip(oldClip);
if (editor.hasFocus() == false) {
g2.clipRect(0, editor.getHeight() / 2, editor.getWidth(),
editor.getHeight() / 2);
g2.translate(0, 1);
g2.setPaint(new Color(0x66FFFFFF, true));
g2.draw(path);
g2.translate(0, -1);
g2.setClip(oldClip);
}
Rectangle editorRect = getVisibleEditorRect();
g2.setPaint(PlafPaintUtils.getVerticalGradient("roundTextField",
editorRect.height, focusPadding, gradientPositions,
gradientColors));
g2.draw(path);
if (includeSearchIcon()) {
searchIcon.paintIcon(
editor,
g0,
editorRect.x - searchIcon.getIconWidth() - 4,
editorRect.y + 1 + editorRect.height / 2
- searchIcon.getIconHeight() / 2);
}
}
@Override
protected void paintBase(Graphics2D g) {
Rectangle r = this.getBounds();
int esq = (int) (r.width * Criador.getDivisor());
setArea(new Rectangle(esq -2, 0, 4, r.height - 1));
//int dir = r.width - esq;
if (!isSelecionado()) {
g.setColor(Color.GRAY);
g.drawRoundRect(0, 0, r.width - 1, r.height - 1, 10, 10);
g.drawLine(esq, 0, esq, r.height - 1);
g.setColor(Color.BLACK);
getCorParaTexto(g);
Rectangle bkp = g.getClipBounds();
g.clipRect(0, 0, esq - 1, r.height);
g.drawString(getTexto(), (Criador.espaco * 2) + 1, (int) (r.height * 0.72));
g.setClip(bkp);
int re = esq + r.height -5;
g.setColor(CanEdit() ? Color.BLACK : Color.LIGHT_GRAY);
g.fillRect(esq + 4, 4, r.height - 9, r.height - 9);
try {
Color c = util.Utilidades.StringToColor(getTransValor());//new Color(Integer.parseInt(getTransValor()));
g.setColor(c);
} catch (Exception e) {
}
Color tmpc = g.getColor();
String bonito = Integer.toString(tmpc.getRed()) + ", " + Integer.toString(tmpc.getGreen()) + ", " +Integer.toString(tmpc.getBlue())+ ", " +Integer.toString(tmpc.getAlpha());
if (CanEdit()) {
g.fillRect(esq + 5, 5, r.height - 10, r.height -10);
}
// g.setColor(CanEdit() ? Color.BLACK : Color.LIGHT_GRAY);
// g.drawRect(tmp + 4, 4, r.height - 9, r.height -9);
g.clipRect(re, 0, esq - 1, r.height);
getCorParaTexto(g);
g.drawString(bonito, re + (Criador.espaco * 2) + 1, (int) (r.height * 0.72));
g.setClip(bkp);
} else {
super.paintBase(g);
}
}
/**
* Print the table into a PDF file
*/
private void print() {
Document document = new Document(PageSize.A4.rotate());
try {
PdfWriter writer =
PdfWriter.getInstance(document, PdfTestBase.getOutputStream( "jTable.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
// Create the graphics as shapes
cb.saveState();
Graphics2D g2 = cb.createGraphicsShapes(500, 500);
// Print the table to the graphics
Shape oldClip = g2.getClip();
g2.clipRect(0, 0, 500, 500);
table.print(g2);
g2.setClip(oldClip);
g2.dispose();
cb.restoreState();
document.newPage();
// Create the graphics with pdf fonts
cb.saveState();
g2 = cb.createGraphics(500, 500);
// Print the table to the graphics
oldClip = g2.getClip();
g2.clipRect(0, 0, 500, 500);
table.print(g2);
g2.setClip(oldClip);
g2.dispose();
cb.restoreState();
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
document.close();
}
/**
* This paints the text in the image.
*
* @param g
* the graphics to paint to.
* @param textRect
* the rectangle in g the text is painted in.
* @param textRectInTextComponent
* the rectangle relative to the text component.
*/
protected void paintHighlightForeground(Graphics2D g, Rectangle textRect,
Rectangle textRectInTextComponent) {
int tx = -textRectInTextComponent.x + textRect.x;
int ty = -textRectInTextComponent.y + textRect.y;
g.translate(tx, ty);
g.clipRect(textRectInTextComponent.x, textRectInTextComponent.y,
textRectInTextComponent.width, textRectInTextComponent.height);
paintOnlyText(g);
}