I try to resize IUManager icon. But i cant do it correct. My code looks like:
// label
ErrorDetails = new javax.swing.JLabel();
// icon
Icon icon = UIManager.getIcon("OptionPane.informationIcon");
BufferedImage bi = new BufferedImage(
205,
250,
BufferedImage.SCALE_DEFAULT);
Graphics2D g = bi.createGraphics();
g.scale(205,205);
// paint new graphics
icon.paintIcon(null,g,250,250);
g.dispose();
// set resized UIManage icon
ErrorDetails.setIcon(icon);
but icon have still same size
You are attempting to paint the Icon onto the BufferedImage. Therefore you would need to create a new Icon using the BufferedImage>
ImageIcon scaled = new ImageIcon(bi);
ErrorDetails.setIcon(scaled);
Also follow Java naming conventions. Variable names should NOT start with an upper case character. "ErrorDetails" should be "errorDetails".
Related
I'm applying an image to a JLabel using the command .setIcon() and then adding the label to a panel
public browser() throws IOException {
JLabel lblimg;
Image img;
ImageIcon ico;
img = ImageIO.read(new File("<FilePath>"));
ico = new ImageIcon(img);
lblimg.setIcon(ico);
lblimg.setBounds(300,90,120,120);
add(lblimg);
}
but this doesn't resize the image inside the label, this way just a slice of the image will appear if it is bigger than the label size.
Does anyone knows a method to insert an image to a label background, resizing the image into it?
Create a BufferedImage and get scaled instance, set its width and height to the width and height to that of the label. Now if you even resize the label, the image will cover the label.
For Example: lblimg.setBounds(300, 90, 300, 120);
BufferedImage bimg =ImageIO.read(new File("file path"));
ico = new ImageIcon(bimg.getScaledInstance(lblimg.getWidth(), lblimg.getHeight(), Image.SCALE_SMOOTH));
lblimg.setIcon(ico);
The following image has been scaled according to the label's width and height.
lblimg.setBounds(300, 90, 100, 50);
Here's a method I wrote some years back to resize an image in Java.
public ImageIcon picturePrep(ImageIcon icon) {
final int DESIRED_WIDTH = 880;
double imageWidth = icon.getIconWidth();
int imageHeight = icon.getIconHeight();
imageWidth = DESIRED_WIDTH/imageWidth;
imageHeight = (int) (imageWidth * imageHeight);
Image img = icon.getImage();
return new ImageIcon(img.getScaledInstance(DESIRED_WIDTH, imageHeight, Image.SCALE_SMOOTH));
}
Use a appropriate layout manager to take care it for you automatically. See Laying Out Components Within a Container for more details.
If, for some bizarre reason, you can't use a layout manager, then you should probably be considering a solution based around custom painting, you could make use of the components preferred size to provide it better information when settings it's bounds
BufferedImage img = ImageIO.read(new File("<FilePath>"));
JLabel lblimg = new JLabel(new ImageIcon(img));
lblimg.setBounds(new Rectangle(new Point(300, 90), lblimg.getPreferredSize()));
Then, you don't need to make guesses
I have made my own customized GUI where it displays images that are opened and read by ImageJ. I am able to display the images but I can't display the subtitle that is displayed in the ImageJ window before the image.
This is what I have been trying but it doesn't seem to display anything. Is there something simple that I am missing?
JPanel panel2 = new JPanel();
ImageCanvas ic = new ImageCanvas(image);
StackWindow sw = new StackWindow(image,ic);
Insets insets = sw.getInsets();
BufferedImage bi = image.getBufferedImage();
Graphics2D g = (Graphics2D)bi.getGraphics();
//g.drawImage(image.getBufferedImage(), 0, 0, null);
g.drawString(sw.createSubtitle(), insets.left+5, insets.top+10);
//image.draw();
sw.drawInfo(g);
panel2.add(sw.getContentPane());
frame.add(panel2);
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?
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()
I have two ImageIcons and I want to create a third ImageIcon which has nr 2 drawn upon nr 1.
How would I best do that?
The following code takes an Image from two ImageIcons and creates a new ImageIcon.
The image from the second ImageIcon is drawn on top of the image from the first, then the resulting image is used to make a new ImageIcon:
Image img1 = imageIcon1.getImage();
Image img2 = imageIcon2.getImage();
BufferedImage resultImage = new BufferedImage(
img1.getWidth(null), img1.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = resultImage.createGraphics();
g.drawImage(img1, 0, 0, null);
g.drawImage(img2, 0, 0, null);
g.dispose();
ImageIcon resultImageIcon = new ImageIcon(resultImage);
Edit
(Fixed some errors, added transparency support.)
For allowing transparency, the BufferedImage.TYPE_INT_ARGB can be used for the image type in the constructor, rather than BufferedImage.TYPE_INT_RGB which does not have an alpha channel.