下面列出了java.awt.Graphics2D#create ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void paintGraph(Graphics graphics, int pixWidth, int pixHeight) {
Graphics2D g = (Graphics2D) graphics;
Graphics2D scaled = (Graphics2D) g.create();
scaled.translate(0, pixHeight + 1 + this.updatePanelHeight + 50);
// prepare data
prepareData();
scaled.scale(1, -1);
g.setColor(Color.black);
drawPoints(scaled, pixWidth, pixHeight);
scaled.dispose();
// x-axis label
String xAxisLabel = "sorted k-distances";
Rectangle2D stringBounds = SCALED_LABEL_FONT.getStringBounds(xAxisLabel, g.getFontRenderContext());
g.drawString(xAxisLabel, MARGIN + (float) (pixWidth / 2.0d - stringBounds.getWidth() / 2.0d), MARGIN
+ (float) (pixHeight - 2.0d * stringBounds.getHeight()) + 3);
// y-axis label
String yAxisLabel = "k-distance value";
stringBounds = LABEL_FONT.getStringBounds(yAxisLabel, g.getFontRenderContext());
g.drawString(yAxisLabel, MARGIN, (int) (MARGIN + stringBounds.getHeight() + 6));
}
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();
}
/**
* Draws the background annoations.
*/
private void draw(final ExecutionUnit process, final Graphics2D g2, final ProcessRendererModel rendererModel,
final boolean printing) {
if (!visualizer.isActive()) {
return;
}
// background annotations
WorkflowAnnotations annotations = rendererModel.getProcessAnnotations(process);
if (annotations != null) {
for (WorkflowAnnotation anno : annotations.getAnnotationsDrawOrder()) {
// selected is drawn by highlight decorator
if (anno.equals(model.getSelected())) {
continue;
}
// paint the annotation itself
Graphics2D g2P = (Graphics2D) g2.create();
drawer.drawAnnotation(anno, g2P, printing);
g2P.dispose();
}
}
}
@Override
public void render(JasperReportsContext jasperReportsContext, Graphics2D grx, Rectangle2D rectangle) throws JRException
{
ensureSvg(jasperReportsContext);
AffineTransform transform = ViewBox.getPreserveAspectRatioTransform(
new float[]{0, 0, (float) documentSize.getWidth(), (float) documentSize.getHeight()},
SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_NONE, true,
(float) rectangle.getWidth(), (float) rectangle.getHeight());
Graphics2D graphics = (Graphics2D) grx.create();
try
{
graphics.translate(rectangle.getX(), rectangle.getY());
graphics.transform(transform);
// CompositeGraphicsNode not thread safe
synchronized (rootNode)
{
rootNode.paint(graphics);
}
}
finally
{
graphics.dispose();
}
}
protected void paintBackground(Graphics2D g, GeneralPath bodyOutline) {
Paint background = getBackground();
if (background != null) {
if (isShadowActive()) {
Graphics2D g2 = (Graphics2D) g.create();
double dy = 0;
Stroke stroke = getStroke();
if (stroke != null) {
dy = (ShapeBounds.getBounds(
stroke.createStrokedShape(bodyOutline)).getHeight() - ShapeBounds
.getBounds(bodyOutline).getHeight()) / 2;
}
g2.translate(0, dy);
g2.setColor(getBackgroundShadowColor());
g2.translate(0, 1);
g2.fill(bodyOutline);
g2.translate(0, 1);
g2.fill(bodyOutline);
g2.dispose();
}
g.setPaint(background);
g.fill(bodyOutline);
}
}
@Override
public void render(Graphics2D g, ScreenTransform transform) {
if (start == null || end == null) {
return;
}
Graphics2D graphics = (Graphics2D)g.create();
graphics.setColor(colour);
double x1 = start.getX();
double x2 = end.getX();
double y1 = start.getY();
double y2 = end.getY();
if (useWorldCoords) {
x1 = transform.xToScreen(x1);
x2 = transform.xToScreen(x2);
/*
double temp = transform.yToScreen(y2);
y2 = transform.yToScreen(y1);
y1 = temp;
*/
y1 = transform.yToScreen(y1);
y2 = transform.yToScreen(y2);
}
graphics.drawLine((int)x1, (int)y1, (int)x2, (int)y2);
}
@Override
protected void drawDrawing(Graphics2D gr) {
if (getDrawing() == null) {
return;
}
Graphics2D g2d = (Graphics2D) gr.create();
AffineTransform tx = g2d.getTransform();
tx.translate(getDrawingToViewTransform().getTranslateX(),
getDrawingToViewTransform().getTranslateY());
tx.scale(getScaleFactor(), getScaleFactor());
g2d.setTransform(tx);
if (blocksVisible) {
drawBlocks(g2d);
}
drawDriveOrderElements(g2d);
getDrawing().setFontRenderContext(g2d.getFontRenderContext());
try {
getDrawing().draw(g2d);
}
catch (ConcurrentModificationException e) {
LOG.warn("Exception from JHotDraw caught while calling DefaultDrawing.draw(), continuing.");
// TODO What to do when it is catched?
}
g2d.dispose();
}
public void paint(BoxContainerPanel bcp, Graphics2D g) {
for (Handle handle : handles) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER,
bcp.getUI().getHandleOpacity(bcp, handle)));
handle.paint(bcp, g2);
g2.dispose();
}
}
/**
* Draws operator backgrounds and then calls all registered {@link ProcessDrawDecorator}s for
* the annotations render phase.
*
* @param process
* the process to draw the operator backgrounds for
* @param g2
* the graphics context to draw upon
* @param printing
* if {@code true} we are printing instead of drawing to the screen
*/
public void drawOperatorBackgrounds(final ExecutionUnit process, final Graphics2D g2, final boolean printing) {
Graphics2D gBG = (Graphics2D) g2.create();
// draw background of operators
for (Operator op : process.getOperators()) {
Rectangle2D frame = model.getOperatorRect(op);
if (frame == null) {
continue;
}
// only draw background if operator is visisble
Rectangle2D opBounds = new Rectangle2D.Double(frame.getX() - 10, frame.getY(), frame.getWidth() + 20,
frame.getHeight());
if (g2.getClipBounds() != null && !g2.getClipBounds().intersects(opBounds)) {
continue;
}
renderOperatorBackground(op, gBG);
}
// draw connections background for all operators
for (Operator operator : process.getOperators()) {
renderConnectionsBackground(operator.getInputPorts(), operator.getOutputPorts(), gBG);
}
// draw connections background for process
renderConnectionsBackground(process.getInnerSinks(), process.getInnerSources(), gBG);
gBG.dispose();
// let decorators draw
drawPhaseDecorators(process, g2, RenderPhase.OPERATOR_BACKGROUND, printing);
}
static void clear(Graphics2D g, int type, int w, int h) {
Graphics2D gg = (Graphics2D) g.create();
if (type > OPAQUE) {
gg.setColor(new Color(0, 0, 0, 0));
gg.setComposite(AlphaComposite.Src);
} else {
gg.setColor(new Color(0xAD, 0xD8, 0xE6));
}
gg.fillRect(0, 0, w, h);
}
/**
* Draws indicator in case the annotation text overflows on the y axis.
*
* @param g
* the graphics context to draw upon
* @param loc
* the location of the annotation
* @param printing
* if we are currently printing
*/
private void drawOverflowIndicator(final Graphics2D g, final Rectangle2D loc, final boolean printing) {
if (printing) {
// never draw them for printing
return;
}
Graphics2D g2 = (Graphics2D) g.create();
int size = 20;
int xOffset = 10;
int yOffset = 10;
int stepSize = size / 4;
int dotSize = 3;
int x = (int) loc.getMaxX() - size - xOffset;
int y = (int) loc.getMaxY() - size - yOffset;
GradientPaint gp = new GradientPaint(x, y, Color.WHITE, x, y + size * 1.5f, Color.LIGHT_GRAY);
g2.setPaint(gp);
g2.fillRect(x, y, size, size);
g2.setColor(Color.BLACK);
g2.drawRect(x, y, size, size);
g2.fillOval(x + stepSize, y + stepSize * 2, dotSize, dotSize);
g2.fillOval(x + stepSize * 2, y + stepSize * 2, dotSize, dotSize);
g2.fillOval(x + stepSize * 3, y + stepSize * 2, dotSize, dotSize);
g2.dispose();
}
/**
* Shadow non valid drop targets while dragging annotations around.
*
* @param g
* the graphics context to draw upon
* @param anno
* the current annotation to draw
* @param printing
* if we are currently printing
*/
private void shadowOperatorsWhileDragging(final Graphics2D g, final WorkflowAnnotation anno, final boolean printing) {
if (printing) {
// never draw them for printing
return;
}
AnnotationDragHelper dragged = model.getDragged();
// only shadow if we are actually dragging and the operator annotation is unsnapped
if (!dragged.isUnsnapped() || !dragged.isDragInProgress()) {
return;
}
Graphics2D g2 = (Graphics2D) g.create();
// shadow operators which are not a valid drop target
for (Operator op : anno.getProcess().getOperators()) {
if (anno instanceof OperatorAnnotation) {
if (op.equals(((OperatorAnnotation) anno).getAttachedTo())) {
continue;
}
}
WorkflowAnnotations annotations = rendererModel.getOperatorAnnotations(op);
if (annotations != null && !annotations.isEmpty()) {
overshadowRect(rendererModel.getOperatorRect(op), g2);
}
}
g2.dispose();
}
static void clear(Graphics2D g, int type, int w, int h) {
Graphics2D gg = (Graphics2D) g.create();
if (type > OPAQUE) {
gg.setColor(new Color(0, 0, 0, 0));
gg.setComposite(AlphaComposite.Src);
} else {
gg.setColor(new Color(0xAD, 0xD8, 0xE6));
}
gg.fillRect(0, 0, w, h);
}
private void paintForegroundEnabledAndVertical(Graphics2D g, int width, int height) {
Graphics2D g2 = (Graphics2D) g.create();
g2.translate(0, height);
g2.rotate(Math.toRadians(-90));
paintForegroundEnabled(g2, height, width);
}
@Override
protected void paintWidget() {
Graphics2D g = this.getGraphics();
if (xPoints.length == 0 || Math.abs(xPoints[0] - xPoints[xPoints.length - 1]) > 2000) {
return;
}
//g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
//g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
//g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
DiagramScene ds = (DiagramScene) this.getScene();
boolean shouldHide = false;//ds.getShouldHide(this);
Composite oldComposite = null;
if (shouldHide) {
Color c = new Color(255 - (255 - color.getRed()) / WHITE_FACTOR, 255 - (255 - color.getGreen()) / WHITE_FACTOR, 255 - (255 - color.getBlue()) / WHITE_FACTOR);
g.setPaint(c);
} else {
g.setPaint(color);
}
if (split) {
for (int i = 1; i < controlPoints.size(); i++) {
Point prev = controlPoints.get(i - 1);
Point cur = controlPoints.get(i);
if (cur == null || prev == null) {
continue;
}
g.drawLine(prev.x, prev.y, cur.x, cur.y);
}
} else {
g.drawPolyline(xPoints, yPoints, pointCount);
}
/*for(int i=0; i<xPoints.length; i++) {
int x = xPoints[i];
int y = yPoints[i];
g.fillOval(x - 2, y - 2, 4, 4);
}*/
if (xPoints.length >= 2) {
Graphics2D g2 = (Graphics2D) g.create();
int xOff = xPoints[xPoints.length - 2] - xPoints[xPoints.length - 1];
int yOff = yPoints[yPoints.length - 2] - yPoints[yPoints.length - 1];
if (xOff == 0 && yOff == 0 && yPoints.length >= 3) {
xOff = xPoints[xPoints.length - 3] - xPoints[xPoints.length - 1];
yOff = yPoints[yPoints.length - 3] - yPoints[yPoints.length - 1];
}
g2.translate(xPoints[xPoints.length - 1], yPoints[yPoints.length - 1]);
g2.rotate(Math.atan2(yOff, xOff));
g2.scale(0.55, 0.80);
AnchorShape.TRIANGLE_FILLED.paint(g2, false);
}
}
private void calcFontMetrics( Graphics2D g2d, int w, int h ) {
FontMetrics fm;
Graphics2D g2 = (Graphics2D)g2d.create();
/// ABP
if ( g2Transform != NONE && textToUse != FILE_TEXT ) {
g2.setFont( g2.getFont().deriveFont( getAffineTransform( g2Transform )) );
fm = g2.getFontMetrics();
}
else {
fm = g2.getFontMetrics();
}
maxAscent = fm.getMaxAscent();
maxDescent = fm.getMaxDescent();
if (maxAscent == 0) maxAscent = 10;
if (maxDescent == 0) maxDescent = 5;
if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) {
/// Give slight extra room for each character
maxAscent += 3;
maxDescent += 3;
gridWidth = fm.getMaxAdvance() + 6;
gridHeight = maxAscent + maxDescent;
if ( force16Cols )
numCharAcross = 16;
else
numCharAcross = ( w - 10 ) / gridWidth;
numCharDown = ( h - 10 ) / gridHeight;
canvasInset_X = ( w - numCharAcross * gridWidth ) / 2;
canvasInset_Y = ( h - numCharDown * gridHeight ) / 2;
if ( numCharDown == 0 || numCharAcross == 0 )
throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );
if ( !isPrinting )
resetScrollbar( verticalBar.getValue() * numCharAcross );
}
else {
maxDescent += fm.getLeading();
canvasInset_X = 5;
canvasInset_Y = 5;
/// gridWidth and numCharAcross will not be used in this mode...
gridHeight = maxAscent + maxDescent;
numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight;
if ( numCharDown == 0 )
throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );
/// If this is text loaded from file, prepares the LineBreak'ed
/// text layout at this point
if ( textToUse == FILE_TEXT ) {
if ( !isPrinting )
f2dt.fireChangeStatus( "LineBreaking Text... Please Wait", false );
lineBreakTLs = new Vector();
for ( int i = 0; i < fileText.length; i++ ) {
AttributedString as =
new AttributedString( fileText[i], g2.getFont().getAttributes() );
LineBreakMeasurer lbm =
new LineBreakMeasurer( as.getIterator(), g2.getFontRenderContext() );
while ( lbm.getPosition() < fileText[i].length() )
lineBreakTLs.add( lbm.nextLayout( (float) w ));
}
}
if ( !isPrinting )
resetScrollbar( verticalBar.getValue() );
}
}
private void calcFontMetrics( Graphics2D g2d, int w, int h ) {
FontMetrics fm;
Graphics2D g2 = (Graphics2D)g2d.create();
/// ABP
if ( g2Transform != NONE && textToUse != FILE_TEXT ) {
g2.setFont( g2.getFont().deriveFont( getAffineTransform( g2Transform )) );
fm = g2.getFontMetrics();
}
else {
fm = g2.getFontMetrics();
}
maxAscent = fm.getMaxAscent();
maxDescent = fm.getMaxDescent();
if (maxAscent == 0) maxAscent = 10;
if (maxDescent == 0) maxDescent = 5;
if ( textToUse == RANGE_TEXT || textToUse == ALL_GLYPHS ) {
/// Give slight extra room for each character
maxAscent += 3;
maxDescent += 3;
gridWidth = fm.getMaxAdvance() + 6;
gridHeight = maxAscent + maxDescent;
if ( force16Cols )
numCharAcross = 16;
else
numCharAcross = ( w - 10 ) / gridWidth;
numCharDown = ( h - 10 ) / gridHeight;
canvasInset_X = ( w - numCharAcross * gridWidth ) / 2;
canvasInset_Y = ( h - numCharDown * gridHeight ) / 2;
if ( numCharDown == 0 || numCharAcross == 0 )
throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );
if ( !isPrinting )
resetScrollbar( verticalBar.getValue() * numCharAcross );
}
else {
maxDescent += fm.getLeading();
canvasInset_X = 5;
canvasInset_Y = 5;
/// gridWidth and numCharAcross will not be used in this mode...
gridHeight = maxAscent + maxDescent;
numCharDown = ( h - canvasInset_Y * 2 ) / gridHeight;
if ( numCharDown == 0 )
throw new CannotDrawException( isPrinting ? CANT_FIT_PRINT : CANT_FIT_DRAW );
/// If this is text loaded from file, prepares the LineBreak'ed
/// text layout at this point
if ( textToUse == FILE_TEXT ) {
if ( !isPrinting )
f2dt.fireChangeStatus( "LineBreaking Text... Please Wait", false );
lineBreakTLs = new Vector();
for ( int i = 0; i < fileText.length; i++ ) {
AttributedString as =
new AttributedString( fileText[i], g2.getFont().getAttributes() );
LineBreakMeasurer lbm =
new LineBreakMeasurer( as.getIterator(), g2.getFontRenderContext() );
while ( lbm.getPosition() < fileText[i].length() )
lineBreakTLs.add( lbm.nextLayout( (float) w ));
}
}
if ( !isPrinting )
resetScrollbar( verticalBar.getValue() );
}
}
protected void paintPage(Graphics2D grx)
{
if (pageError)
{
paintPageError(grx);
return;
}
try
{
if (exporter == null)
{
exporter = getGraphics2DExporter();
}
else
{
exporter.reset();
}
exporter.setExporterInput(new SimpleExporterInput(viewerContext.getJasperPrint()));
SimpleGraphics2DReportConfiguration configuration = new SimpleGraphics2DReportConfiguration();
configuration.setPageIndex(viewerContext.getPageIndex());
configuration.setZoomRatio(realZoom);
configuration.setOffsetX(1); //lblPage border
configuration.setOffsetY(1);
exporter.setConfiguration(configuration);
SimpleGraphics2DExporterOutput output = new SimpleGraphics2DExporterOutput();
Graphics2D g = (Graphics2D)grx.create();
output.setGraphics2D(g);
exporter.setExporterOutput(output);
try
{
exporter.exportReport();
}
finally
{
g.dispose();
}
}
catch(Exception e)
{
if (log.isErrorEnabled())
{
log.error("Page paint error.", e);
}
pageError = true;
paintPageError(grx);
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
JOptionPane.showMessageDialog(JRViewerPanel.this, viewerContext.getBundleString("error.displaying"));
}
});
}
}
/**
* Prepares this Graphics2D to paint this BlockLetter at the given (x,y)
* coordinate.
*
* @param g
* the Graphics2D to paint to
* @param x
* the x-coordinate to paint to
* @param y
* the y-coordinate to paint to
* @return a cloned Graphics2D to paint to. When complete you should dispose
* this Graphics2D.
*/
protected Graphics2D prep(Graphics2D g, float x, float y) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.translate(x, y);
float dx = (float) (depth * Math.cos(angle));
float dy = (float) (depth * Math.sin(angle));
g2.translate(-dx, -dy);
return g2;
}
/**
* Draws the process(es) foreground based on the {@link ProcessRendererModel} data and then
* calls all registered {@link ProcessDrawDecorator}s for the foreground render phase.
*
* @param process
* the process to draw the foreground for
* @param g2
* the graphics context to draw upon
* @param printing
* if {@code true} we are printing instead of drawing to the screen
*/
public void drawForeground(final ExecutionUnit process, final Graphics2D g2, final boolean printing) {
Graphics2D gBG = (Graphics2D) g2.create();
renderForeground(process, gBG, printing);
gBG.dispose();
}