How can I generate text image same as the JLabel label? - java

I would like to generate text image same as the JLabel label without displaying JLabel.
I tried same Font, same drawing method.
But generated image is not same as JLabel.
My sourcecode is below.
* 'super.paintComponent(g)' has been commented out for clarity that it is the same way. Output image is same.
* Below drawing by 'View.paint' method, but I'm tried 'SwingUtilities2.drawString' too. Two results are the same.
/* Label */
JLabel label = new JLabel(text) {
#Override
public void paintComponent(Graphics g) {
//super.paintComponent(g);
View v = BasicHTML.createHTMLView(this, getText());
v.paint(g, new Rectangle(0, 0, getWidth(), getFontMetrics(
getFont()).getAscent()));
}
};
label.setFont(new Font("Consolas", Font.PLAIN, 13));
/* Image */
FontMetrics fm = label.getFontMetrics(font);
BufferedImage image = new BufferedImage(fm.stringWidth(text),
fm.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
g2d.setFont(label.getFont());
// Clear background.
g2d.setPaint(label.getBackground());
g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
// Draw string.
g2d.setClip(new Rectangle(0, 0, image.getWidth(), image.getHeight()));
View v = BasicHTML.createHTMLView(label, text);
v.paint(g2d, new Rectangle(0, 0, image.getWidth(),
g2d.getFontMetrics().getAscent()));
// ... output image to file ...
Result image is following.
[JLabel]
[Generated image]
Generated image is slightly thin-faced, as compared to JLabel's capture.
How can I generate text image same as the JLabel label?
Thank you for your consideration.

I'm not sure, but you might need to create a compatible buffered image (compatible to the display)
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
// Create an image that does not support transparency
BufferedImage bimage = gc.createCompatibleImage(100, 100, Transparency.OPAQUE);
This will at least get you on a close with the graphics been used to render to the screen
You might also like to pay around with the rendering quality as well
Kleopatra did a post on a similar question awhile ago, you might to try and hunt it down

Why do you use BasicHTML.createView if you want to have the same as JLabel?
You could use the JLabel directly (if you only want the text and not the background, set opaque to false and the border to null)
or you can use g2d.drawString()

Related

java graphics - text color does not render correclty

I want to add some white text to a dark background image and send that image for download. Here is my code:
BufferedImage image = ImageIO.read(new File("path\to\my\image.png"));
Font font = new Font("Tahoma", Font.PLAIN, 18);
Graphics g = image.getGraphics();
Graphics2D g2d = (Graphics2D) g;
g2d.setFont(font);
g2d.setColor(Color.WHITE);
g2d.drawString("This is a test!", 20, 700);
//here is some codes for output result image
I have set text color to white as you can see, but weird problem is the generated text on image has other color than white!
This is part of my output image:
Can anyone help me to find out the problem?

Why is my text-to-image method returning a fully-black image?

I'm trying to make a method that converts a given String (and Font) into a BufferedImage. However, every time I run it, it returns a fully black image. The exact size of the image seems to be correct, but all pixels are perfectly black.
Here's the code:
static final GraphicsEnvironment GE;
static
{
GE = GraphicsEnvironment.getLocalGraphicsEnvironment();
}
public static BufferedImage textToImage(String text, Font font)
{
if (font == null)
font = Font.getFont("Courier New");
FontRenderContext frc = new FontRenderContext(new AffineTransform(), false, false);
Rectangle2D bounds = font.getStringBounds(text, frc);
BufferedImage bi = new BufferedImage(
(int)(bounds.getWidth() + .5),
(int)(bounds.getHeight() + .5),
BufferedImage.TYPE_BYTE_BINARY
);
Graphics2D g = GE.createGraphics(bi);
g.setFont(font);
g.drawString(text, 0, 0);
return bi;
}
Here's "Hello World" in the default JOptionPane font displayed as a JOptionPane's icon:
The simple solution would be to change the color...
g.setColor(Color.WHITE);
g.fillRect(0, 0, bi.getWidth(), bi.getHeight());
g.setColor(Color.BLACK);
g.setFont(font);
g.drawString(text, 0, 0);
g.dispose();
Text, generally, isn't rendered from the y position down, it draw's up AND down from the y position, so you'll need to use something like...
FontMetrics fm = g.getFontMetrics();
g.drawString(text, 0, fm.getAscent());
to get the text to appear correctly...
Also font = Font.getFont("Courier New"); isn't doing what you think it is, this won't return the font named "Courier New", but will instead attempt to load the font file called "Courier New".
Try using something like...
if (font == null) {
font = new Font("Courier New", Font.PLAIN, 12);
}
instead...
You might like to take a closer look at Working with Text APIs for more details

How do I ignore the alpha content when loading an Image off disk into a BufferedImage

Currently, when I load an image off disk,
BufferedImage img = ImageIO.read(new File("myfile.png");
The resulting color space is one of ARGB. What I'd prefer is a plain old RGB, but without having to do the filtering myself.
Is there a way to open an image in a certain color mode?
I'm not aware of a way to open it with a specific color format, but you can create a new BufferedImage of the desired format and draw the old image on to it:
BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D g = img2.createGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
Any transparent parts in the original image will be drawn over a black background in the new image. If you'd prefer a different background, you can insert these lines before the drawImage call:
g.setColor(Color.white); // or whichever
g.fillRect(0, 0, img.getWidth(), img.getHeight());

display an image in awt

I have a frame and a buffer values of the image. I couldn't display the image in the frame. The code that I used is as follows:
byte [] payload = new byte[payload_length];
rtp_packet.getpayload(payload);
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.createImage(payload, 0, payload_length);
icon = new ImageIcon(image);
iconLabel.setIcon(icon);
I also tried adding it directly to the frame using the code :
f.setIconImage(image);
now how to display the image? and why it is not working?
You want to use the graphics object. Your awt frame should already have one and you call your graphics object via ...
BufferedImage img = javaImage; // You replace this with your image
Graphics g = this.getGraphics(); // this is what you need to call
g.drawImage(img, 0, 0, null); // you then call draw image
In your case you simply do
g.drawImage(image, 0, 0, null); // you can look up the parameters
That should do it for you.

Keeping array of ImageIcon or BufferedImage?

My Java program loads a bunch of image on startup. For now for each image I call this piece of code. Eventually I get an array of ImageIcon, which would be used in user interface.
InputStream is = getClass().getClassLoader().getResourceAsStream(imagePath);
bimage = ImageIO.read(is);
is.close();
Image img = bimage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
return new ImageIcon(img);
But now I want to implement a method which would get an object from array and add title in specified language. So I have to use Graphics2D.drawString(). But ImageIcon does not have createGraphics() method. Is there a way I can get Graphics from ImageIcon?
Graphics2D g = bimage.createGraphics();
g.setColor(Color.red);
g.setFont(new Font( "SansSerif", Font.BOLD, 25 ));
g.drawString("TEST", 25, 25);
Now is the question: should I keep array of ImageIcon or BufferedImage?

Categories

Resources