下面列出了java.awt.Image#getGraphics() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void draw(Shape clip, Shape shape, Image from, Image to) {
Graphics2D g2d = (Graphics2D) to.getGraphics();
g2d.setXORMode(Color.BLACK);
g2d.setClip(clip);
Rectangle toBounds = shape.getBounds();
g2d.drawImage(from, toBounds.x, toBounds.y, toBounds.width,
toBounds.height, null);
g2d.dispose();
}
private static long test(Image bi, Image vi, AffineTransform atfm) {
final Polygon p = new Polygon();
p.addPoint(0, 0);
p.addPoint(SIZE, 0);
p.addPoint(0, SIZE);
p.addPoint(SIZE, SIZE);
p.addPoint(0, 0);
Graphics2D g2d = (Graphics2D) vi.getGraphics();
g2d.clip(p);
g2d.transform(atfm);
g2d.setComposite(AlphaComposite.SrcOver);
final long start = System.nanoTime();
g2d.drawImage(bi, 0, 0, null);
final long time = System.nanoTime() - start;
g2d.dispose();
return time;
}
static void initImage(GraphicsConfiguration gc, Image image) {
Graphics g = image.getGraphics();
g.setColor(Color.RED);
int w = image.getWidth(null);
int h = image.getHeight(null);
g.fillRect(0, 0, w, h);
g.dispose();
// need to 'accelerate' the image
if (dstImage == null) {
dstImage =
gc.createCompatibleVolatileImage(TESTW, TESTH,
Transparency.OPAQUE);
}
dstImage.validate(gc);
g = dstImage.getGraphics();
g.drawImage(image, 0, 0, null);
g.drawImage(image, 0, 0, null);
g.drawImage(image, 0, 0, null);
}
/**
* Updates the window associated with the painter.
*
* @param repaint indicates if the window should be completely repainted
* to the back buffer using {@link java.awt.Window#paintAll} before update.
*/
public void updateWindow(boolean repaint) {
boolean done = false;
Image bb = getBackBuffer(repaint);
while (!done) {
if (repaint) {
Graphics2D g = (Graphics2D)bb.getGraphics();
try {
window.paintAll(g);
} finally {
g.dispose();
}
}
done = update(bb);
if (!done) {
repaint = true;
bb = getBackBuffer(true);
}
}
}
private void drawBGImage() {
Dimension d = getSize();
int w = d.width;
int h = d.height;
Image BGImage = createImage(w, h);
if (BGImage == null)
return;
Graphics g = BGImage.getGraphics();
g.setColor(Color.green.darker().darker());
g.fillRect(0, 0, w, h);
g.setColor(Color.green.darker());
g.fillRect(w - 2, 0, 2, h);
g.fillRect(0, h - 2, w, 2);
g.setColor(Color.green.darker().darker().darker());
g.fillRect(0, 0, w, 2);
g.fillRect(0, 0, 2, h);
g.dispose();
bgd.setImage(BGImage);
}
protected Image getPageErrorImage()
{
PrintPageFormat pageFormat = getPageFormat();
Image image = new BufferedImage(
(int) (pageFormat.getPageWidth() * realZoom) + 1,
(int) (pageFormat.getPageHeight() * realZoom) + 1,
BufferedImage.TYPE_INT_RGB
);
Graphics2D grx = (Graphics2D) image.getGraphics();
AffineTransform transform = new AffineTransform();
transform.scale(realZoom, realZoom);
grx.transform(transform);
drawPageError(grx);
return image;
}
static void initImage(GraphicsConfiguration gc, Image image) {
Graphics g = image.getGraphics();
g.setColor(Color.RED);
int w = image.getWidth(null);
int h = image.getHeight(null);
g.fillRect(0, 0, w, h);
g.dispose();
// need to 'accelerate' the image
if (dstImage == null) {
dstImage =
gc.createCompatibleVolatileImage(TESTW, TESTH,
Transparency.OPAQUE);
}
dstImage.validate(gc);
g = dstImage.getGraphics();
g.drawImage(image, 0, 0, null);
g.drawImage(image, 0, 0, null);
g.drawImage(image, 0, 0, null);
}
/**
* Updates the window associated with the painter.
*
* @param repaint indicates if the window should be completely repainted
* to the back buffer using {@link java.awt.Window#paintAll} before update.
*/
public void updateWindow(boolean repaint) {
boolean done = false;
Image bb = getBackBuffer(repaint);
while (!done) {
if (repaint) {
Graphics2D g = (Graphics2D)bb.getGraphics();
try {
window.paintAll(g);
} finally {
g.dispose();
}
}
done = update(bb);
if (!done) {
repaint = true;
bb = getBackBuffer(true);
}
}
}
private static long test(Image bi, Image vi, AffineTransform atfm) {
final Polygon p = new Polygon();
p.addPoint(0, 0);
p.addPoint(SIZE, 0);
p.addPoint(0, SIZE);
p.addPoint(SIZE, SIZE);
p.addPoint(0, 0);
Graphics2D g2d = (Graphics2D) vi.getGraphics();
g2d.clip(p);
g2d.transform(atfm);
g2d.setComposite(AlphaComposite.SrcOver);
final long start = System.nanoTime();
g2d.drawImage(bi, 0, 0, null);
final long time = System.nanoTime() - start;
g2d.dispose();
return time;
}
private void render(Image im, int type, String s) {
Graphics2D g2d = (Graphics2D) im.getGraphics();
clear(g2d, type, im.getWidth(null), im.getHeight(null));
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
Font f = new Font("Dialog", Font.BOLD, 40);// g2d.getFont().deriveFont(32.0f);
g2d.setColor(Color.white);
g2d.setFont(g2d.getFont().deriveFont(36.0f));
g2d.drawString(s, 10, im.getHeight(null) / 2);
}
private static void fill(final Image image) {
final Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setComposite(AlphaComposite.Src);
for (int i = 0; i < image.getHeight(null); ++i) {
graphics.setColor(new Color(i, 0, 0));
graphics.fillRect(0, i, image.getWidth(null), 1);
}
graphics.dispose();
}
/**
* Create background tile when MainDesktopPane is first displayed. Recenter
* logoLabel on MainWindow and set backgroundLabel to the size of
* MainDesktopPane.
*
* @param e the component event
*/
@Override
public void componentResized(ComponentEvent e) {
// If displayed for the first time, create background image tile.
// The size of the background tile cannot be determined during construction
// since it requires the MainDesktopPane be displayed first.
if (firstDisplay) {
ImageIcon baseImageIcon = ImageLoader.getIcon(Msg.getString("img.background")); //$NON-NLS-1$
Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
Image backgroundImage = createImage((int) screen_size.getWidth(), (int) screen_size.getHeight());
Graphics backgroundGraphics = backgroundImage.getGraphics();
for (int x = 0; x < backgroundImage.getWidth(this); x += baseImageIcon.getIconWidth()) {
for (int y = 0; y < backgroundImage.getHeight(this); y += baseImageIcon.getIconHeight()) {
backgroundGraphics.drawImage(baseImageIcon.getImage(), x, y, this);
}
}
backgroundImageIcon.setImage(backgroundImage);
backgroundLabel.setSize(getSize());
firstDisplay = false;
}
// Set the backgroundLabel size to the size of the desktop
backgroundLabel.setSize(getSize());
}
private static void draw(final Image img) {
float[] dashes = {10.0f, 10.0f};
BasicStroke bs = new BasicStroke(0.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 10.0f, dashes,
0.0f);
Graphics2D g = (Graphics2D) img.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, 200, 40);
Line2D line = new Line2D.Double(20, 20, 180, 20);
g.setColor(Color.BLACK);
g.setStroke(bs);
g.draw(line);
g.dispose();
}
private void drawArmorImage(Image im, int a) {
int x = 0;
int w = im.getWidth(null);
int h = im.getHeight(null);
Graphics g = im.getGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, w, h);
for (int i = 0; i < a; i++) {
x = i * 7;
g.setColor(Color.green.darker());
g.fillRect(x, 0, 5, 12);
}
}
private static void fill(final Image image) {
final Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setComposite(AlphaComposite.Src);
for (int i = 0; i < image.getHeight(null); ++i) {
graphics.setColor(new Color(i, 0, 0));
graphics.fillRect(0, i, image.getWidth(null), 1);
}
graphics.dispose();
}
private static void fill(final Image image) {
final Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setComposite(AlphaComposite.Src);
for (int i = 0; i < image.getHeight(null); ++i) {
graphics.setColor(new Color(i, 0, 0));
graphics.fillRect(0, i, image.getWidth(null), 1);
}
graphics.dispose();
}
/**
* Fills the whole image using different alpha for each row.
*
* @param image to fill
*/
private static void fill(final Image image, final int size) {
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setComposite(AlphaComposite.Src);
graphics.setColor(Color.GREEN);
graphics.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
int row = image.getHeight(null) / size;
for (int i = 0; i < size; ++i) {
graphics.setColor(new Color(23, 127, 189, i));
graphics.fillRect(0, i * row, image.getWidth(null), row);
}
graphics.dispose();
}
private static final Image clearImage(Image bb) {
Graphics2D g = (Graphics2D)bb.getGraphics();
int w = bb.getWidth(null);
int h = bb.getHeight(null);
g.setComposite(AlphaComposite.Src);
g.setColor(new Color(0, 0, 0, 0));
g.fillRect(0, 0, w, h);
return bb;
}
private static void init(final Image image) {
final Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setComposite(AlphaComposite.Src);
graphics.setColor(new Color(0, 0, 0, 0));
graphics.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
graphics.dispose();
}
private static final Image clearImage(Image bb) {
Graphics2D g = (Graphics2D)bb.getGraphics();
int w = bb.getWidth(null);
int h = bb.getHeight(null);
g.setComposite(AlphaComposite.Src);
g.setColor(new Color(0, 0, 0, 0));
g.fillRect(0, 0, w, h);
return bb;
}