Draw over an image

Hi again. I need some help. I need to draw a white rectangle over a jpg file, loaded in the program. The white rectangle must be at the bottom right corner of the image file, and it has a fixed size. I'd like to get some help here. Thank you all
[253 byte] By [dantevaza] at [2007-11-27 5:38:30]
# 1

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

import javax.swing.*;

public class DrawingOnAnImage {

private JPanel getContent(BufferedImage image) {

JPanel panel = new JPanel(new GridLayout(1,0));

panel.add(new JLabel(new ImageIcon(image)));

panel.add(new JLabel(new ImageIcon(getImage(image))));

return panel;

}

private BufferedImage getImage(BufferedImage src) {

int w = src.getWidth();

int h = src.getHeight();

int s = 75;

BufferedImage dest = new BufferedImage(w, h, src.getType());

Graphics2D g2 = dest.createGraphics();

g2.drawImage(src, 0, 0, null);

g2.setPaint(Color.white);

int x = w - s;

int y = h - s;

g2.fillRect(x,y,s,s);

g2.dispose();

return dest;

}

public static void main(String[] args) throws IOException {

String path = "images/cougar.jpg";

BufferedImage image = ImageIO.read(new File(path));

DrawingOnAnImage test = new DrawingOnAnImage();

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test.getContent(image));

f.pack();

f.setVisible(true);

}

}

crwooda at 2007-7-12 15:12:11 > top of Java-index,Security,Cryptography...
# 2

Thanks a lot. And just one more. About the code you posted in the other topic (the one that merges two images,one with text - getImage() method - and the other named "cougar.jpg"), the result is a background image with some transparency. How can I get the same result but with not transparency? I want the background image resulting in an image identical to the source,and just some text over it. Thanks a lot.

dantevaza at 2007-7-12 15:12:11 > top of Java-index,Security,Cryptography...
# 3

I want the background image resulting in an image identical to the source,and just some

text over it.

To make an image to use is easy enough. Use a method like the getImage method shown

above and draw your text into the image after you draw the background image. You can use

the font/string drawing code from the getImage method in ImageMerge for this. So

private BufferedImage getImage(BufferedImage src) {

int w = src.getWidth();

int h = src.getHeight();

int type = BufferedImage.TYPE_INT_RGB;

// src.getType();

BufferedImage dest = new BufferedImage(w, h, type);

Graphics2D g2 = dest.createGraphics();

// Copy src image into dest.

g2.drawImage(src, 0, 0, null);

// Draw your text on top.

Font font = g2.getFont().deriveFont(16f);

g2.setFont(font);

FontRenderContext frc = g2.getFontRenderContext();

String text = "hello world";

float width = (float)font.getStringBounds(text, frc).getWidth();

LineMetrics lm = font.getLineMetrics(text, frc);

float height = lm.getAscent() + lm.getDescent();

// Locate text, this will draw it centered

float x = (w - width)/2;

float y = (h + height)/2 - lm.getDescent();

g2.setPaint(Color.red);

g2.drawString(text, x, y);

g2.dispose();

return dest;

}

If you want to save this image to file

String ext = "jpg"; // png, bmp (j2se 1.5+), gif (j2se 1.6+) okay

File file = new File("fileName." + ext);

ImageIO.write(image, ext, file);

crwooda at 2007-7-12 15:12:11 > top of Java-index,Security,Cryptography...