下面列出了java.awt.Graphics2D#drawRenderedImage ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void renderTest(Graphics2D g2d, int w, int h) {
g2d.setColor(Color.yellow);
g2d.fillRect(0, 0, w, h);
BufferedImage image = getTestImage();
// draw original image
g2d.drawRenderedImage(image, null);
// draw image with custom composite
g2d.translate(175, 25);
Composite currentComposite = g2d.getComposite();
g2d.setComposite(new TestComposite());
g2d.drawRenderedImage(image, null);
g2d.setComposite(currentComposite);
// draw image with XOR
g2d.translate(175, 25);
g2d.setXORMode(Color.red);
g2d.drawRenderedImage(image, null);
System.out.println("Painting is done...");
}
public static void renderTest(Graphics2D g2d, int w, int h) {
g2d.setColor(Color.yellow);
g2d.fillRect(0, 0, w, h);
BufferedImage image = getTestImage();
// draw original image
g2d.drawRenderedImage(image, null);
// draw image with custom composite
g2d.translate(175, 25);
Composite currentComposite = g2d.getComposite();
g2d.setComposite(new TestComposite());
g2d.drawRenderedImage(image, null);
g2d.setComposite(currentComposite);
// draw image with XOR
g2d.translate(175, 25);
g2d.setXORMode(Color.red);
g2d.drawRenderedImage(image, null);
System.out.println("Painting is done...");
}
public static void renderTest(Graphics2D g2d, int w, int h) {
g2d.setColor(Color.yellow);
g2d.fillRect(0, 0, w, h);
BufferedImage image = getTestImage();
// draw original image
g2d.drawRenderedImage(image, null);
// draw image with custom composite
g2d.translate(175, 25);
Composite currentComposite = g2d.getComposite();
g2d.setComposite(new TestComposite());
g2d.drawRenderedImage(image, null);
g2d.setComposite(currentComposite);
// draw image with XOR
g2d.translate(175, 25);
g2d.setXORMode(Color.red);
g2d.drawRenderedImage(image, null);
System.out.println("Painting is done...");
}
public static void renderTest(Graphics2D g2d, int w, int h) {
g2d.setColor(Color.yellow);
g2d.fillRect(0, 0, w, h);
BufferedImage image = getTestImage();
// draw original image
g2d.drawRenderedImage(image, null);
// draw image with custom composite
g2d.translate(175, 25);
Composite currentComposite = g2d.getComposite();
g2d.setComposite(new TestComposite());
g2d.drawRenderedImage(image, null);
g2d.setComposite(currentComposite);
// draw image with XOR
g2d.translate(175, 25);
g2d.setXORMode(Color.red);
g2d.drawRenderedImage(image, null);
System.out.println("Painting is done...");
}
public static void renderTest(Graphics2D g2d, int w, int h) {
g2d.setColor(Color.yellow);
g2d.fillRect(0, 0, w, h);
BufferedImage image = getTestImage();
// draw original image
g2d.drawRenderedImage(image, null);
// draw image with custom composite
g2d.translate(175, 25);
Composite currentComposite = g2d.getComposite();
g2d.setComposite(new TestComposite());
g2d.drawRenderedImage(image, null);
g2d.setComposite(currentComposite);
// draw image with XOR
g2d.translate(175, 25);
g2d.setXORMode(Color.red);
g2d.drawRenderedImage(image, null);
System.out.println("Painting is done...");
}
public static void renderTest(Graphics2D g2d, int w, int h) {
g2d.setColor(Color.yellow);
g2d.fillRect(0, 0, w, h);
BufferedImage image = getTestImage();
// draw original image
g2d.drawRenderedImage(image, null);
// draw image with custom composite
g2d.translate(175, 25);
Composite currentComposite = g2d.getComposite();
g2d.setComposite(new TestComposite());
g2d.drawRenderedImage(image, null);
g2d.setComposite(currentComposite);
// draw image with XOR
g2d.translate(175, 25);
g2d.setXORMode(Color.red);
g2d.drawRenderedImage(image, null);
System.out.println("Painting is done...");
}
@Override
protected void paintComponent(Graphics g) {
if (image == null) {
return;
}
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
int x = (w - imageWidth) / 2;
int y = (h - imageHeight) / 2;
AffineTransform at = AffineTransform.getTranslateInstance(x, y);
g2.drawRenderedImage(image, at);
g2.setPaint(Color.LIGHT_GRAY);
g2.draw(clip);
}
@Override
public void render (Graphics2D g)
{
// Display the dewarped image
g.drawRenderedImage(image, identity);
// Display also the Destination Points
renderWarpGrid(g, false);
}
public static BufferedImage rotate(BufferedImage _origImg, int angle, Color bgColor) {
// Convert the degrees to radians
double radians = Math.toRadians(angle);
// Determine the sin and cos of the angle
double sin = Math.abs(Math.sin(radians));
double cos = Math.abs(Math.cos(radians));
// Store the original width and height of the image
int w = _origImg.getWidth();
int h = _origImg.getHeight();
// Determine the width and height of the rotated image
int neww = (int) Math.floor(w * cos + h * sin);
int newh = (int) Math.floor(h * cos + w * sin);
// Create a BufferedImage to store the rotated image
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.OPAQUE);
// Render the rotated image
Graphics2D g = result.createGraphics();
g.setBackground(bgColor);
g.clearRect(0, 0, neww, newh);
g.translate((neww - w) / 2, (newh - h) / 2);
g.rotate(radians, w / 2, h / 2);
g.drawRenderedImage(_origImg, null);
g.dispose();
// Return the rotated image
return result;
}
/** Paints the image. */
@Override public void paint(final Graphics graphics) {
super.paint(graphics);
final Graphics2D gr = (Graphics2D) graphics;
gr.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
final double width = image.getWidth();
final double height = image.getHeight();
final double scale = min(getWidth() / width, getHeight() / height);
final AffineTransform gridToPanel = new AffineTransform(
scale, 0, 0, scale,
0.5*(getWidth() - scale*width),
0.5*(getHeight() - scale*height));
gr.drawRenderedImage(image, gridToPanel);
}
private void doRender (Graphics2D g,
boolean input,
boolean output,
boolean voice,
RunTable table)
{
final Color oldColor = g.getColor();
g.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_OFF);
// Render the picture image (either initial or binary)
if (input) {
Picture picture = sheet.getPicture();
BufferedImage initial = picture.getInitialImage();
if (initial != null) {
g.drawRenderedImage(initial, null);
} else if (table != null) {
table.render(g, new Point(0, 0));
}
}
// Render the recognized score entities?
if (output) {
final boolean mixed = input;
final boolean coloredVoices = mixed ? false : voice;
g.setColor(mixed ? Colors.MUSIC_PICTURE : Colors.MUSIC_ALONE);
new SheetResultPainter(sheet, g, coloredVoices, true, false).process();
}
g.setColor(oldColor);
}
/**
* Creates a buffered image compatible with the given graphics configuration,
* using the given buffered image as a source. If gc is null, the default
* graphics configuration is used.
*/
public static BufferedImage makeCompatible(final BufferedImage image,
GraphicsConfiguration gc)
{
if (gc == null) gc = getDefaultConfiguration();
final int w = image.getWidth(), h = image.getHeight();
final int trans = image.getColorModel().getTransparency();
final BufferedImage result = gc.createCompatibleImage(w, h, trans);
final Graphics2D g2 = result.createGraphics();
g2.drawRenderedImage(image, null);
g2.dispose();
return result;
}
public cfData execute( cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException{
cfImageData im = getImage( _session, argStruct );
float opacity = 0.4f;
float fadeHeight = 0.001f;
int imageWidth = im.getWidth();
int imageHeight = im.getHeight();
int width = imageWidth;
int height = (imageHeight * 2);
BufferedImage newImage = new BufferedImage( width, height, im.getImage().getType() );
Graphics2D g2 = newImage.createGraphics();
g2.setPaint( new GradientPaint(0,0, Color.black, 0, height, Color.darkGray ) );
g2.fillRect(0, 0, width, height );
g2.translate( (width-imageWidth)/2, height/2-imageHeight );
g2.drawRenderedImage( im.getImage(), null );
g2.translate( 0, 2*imageHeight);
g2.scale( 1, -1 );
BufferedImage reflection = new BufferedImage( imageWidth, imageHeight, im.getImage().getType() );
Graphics2D rg = reflection.createGraphics();
rg.drawRenderedImage( im.getImage(), null);
rg.setPaint( new GradientPaint(
0, imageHeight * fadeHeight, new Color(1.0f,0.0f,0.0f,0.0f),
0, imageHeight, new Color(0.0f,0.0f,0.0f,opacity)
) );
rg.fillRect(0,0,imageWidth,imageHeight);
rg.dispose();
g2.drawRenderedImage(reflection, null);
g2.dispose();
im.setImage(newImage);
return cfBooleanData.TRUE;
}
/** Copies the source image into the target, applying scaling. */
public static BufferedImage copyScaled(final BufferedImage source,
final BufferedImage target, Object hint)
{
if (hint == null) hint = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
final Graphics2D g2 = target.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
final double scalex = (double) target.getWidth() / source.getWidth();
final double scaley = (double) target.getHeight() / source.getHeight();
final AffineTransform xform = AffineTransform.getScaleInstance(scalex,
scaley);
g2.drawRenderedImage(source, xform);
g2.dispose();
return target;
}
public void paintComponent(Graphics g)
{
// For background
super.paintComponent(g);
// Meant for visual check
if (image != null) {
Graphics2D g2 = (Graphics2D) g;
g2.drawRenderedImage (image, scaleXform);
//g2.drawImage (image, 1, 1, this);
}
}
/** Print the page */
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
System.err.println("[GanttPrintable] print(): reduceFactor=" + reduceFactor);
System.err.println("[GanttPrintable] print(): image: w=" + image.getWidth() + " h=" + image.getHeight());
System.err.println("[GanttPrintable] print(): page=" + pageIndex);
int pagesPerRow = (int) (image.getWidth() / reduceFactor / pageFormat.getImageableWidth() + 1);
int numRows = (int) (image.getHeight() / reduceFactor / pageFormat.getImageableHeight() + 1);
System.err.println("[GanttPrintable] print(): numrows=" + numRows + " pagesPerRow=" + pagesPerRow);
int totalPages = pagesPerRow * numRows;
if (pageIndex >= totalPages) {
return Printable.NO_SUCH_PAGE;
}
int currentRow = pageIndex / pagesPerRow;
int currentColumn = pageIndex - currentRow * pagesPerRow;
System.err.println("[GanttPrintable] print(): curentpage=" + currentColumn + " current row=" + currentRow);
int leftx = (int) (currentColumn * (pageFormat.getImageableWidth() * reduceFactor - 2 / 3 * pageFormat.getImageableX()));
int topy = (int) (currentRow * pageFormat.getImageableHeight() * reduceFactor);
System.err.println("[GanttPrintable] print(): leftx=" + leftx + " topy=" + topy);
Graphics2D g2d = (Graphics2D) graphics;
g2d.setClip((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY(),
(int) pageFormat.getImageableWidth(), (int) pageFormat.getImageableHeight());
AffineTransform transform = AffineTransform.getScaleInstance(1 / reduceFactor, 1 / reduceFactor);
transform.translate(pageFormat.getImageableX() - leftx, pageFormat.getImageableY() - topy);
g2d.drawRenderedImage(image, transform);
return Printable.PAGE_EXISTS;
}
private static BufferedImage scale ( BufferedImage source, double ratio ) {
int w = (int) (source.getWidth() * ratio);
int h = (int) (source.getHeight() * ratio);
BufferedImage bi = getCompatibleImage( w, h );
Graphics2D g2d = bi.createGraphics();
double xScale = (double) w / source.getWidth();
double yScale = (double) h / source.getHeight();
AffineTransform at = AffineTransform.getScaleInstance( xScale, yScale );
g2d.drawRenderedImage( source, at );
g2d.dispose();
return bi;
}
private static BufferedImage scaleImage(final BufferedImage bsrc, final int width, final int height) {
BufferedImage bdest = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g = bdest.createGraphics();
AffineTransform at = AffineTransform.getScaleInstance((double) bdest.getWidth() / bsrc.getWidth(),
(double) bdest.getHeight() / bsrc.getHeight());
g.drawRenderedImage(bsrc, at);
g.dispose();
return bdest;
}
/**
* Draw the icon at the specified location. Icon implementations may use the Component argument to get properties
* useful for painting, e.g. the foreground or background color.
*/
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2D = (Graphics2D) g;
g2D.drawRenderedImage(_image, AffineTransform.getTranslateInstance(x, y));
}
/**
* Returns a *copy* of a subimage of image. This avoids the performance problems associated with BufferedImage.getSubimage.
* @param image the image
* @param x the x position
* @param y the y position
* @param w the width
* @param h the height
* @return the subimage
*/
public static BufferedImage getSubimage( BufferedImage image, int x, int y, int w, int h ) {
BufferedImage newImage = new BufferedImage( w, h, BufferedImage.TYPE_INT_ARGB );
Graphics2D g = newImage.createGraphics();
g.drawRenderedImage( image, AffineTransform.getTranslateInstance(-x, -y) );
g.dispose();
return newImage;
}