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);
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 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".
I have this function where I receive String[][], f (rows), and c (columns). I've got to adapt my labels occupying all of my frame (1270,750).
For example, if I receive:
f=3 and c=3, the images will have: (423.333,250.) pixels
f=10 and c=10, the images will have (127,75) pixels.
I've proved with all the functions which are in the example, but it doesn't work. Any idea how to do it?
public void inicialitzamatriu(String[][] arraystrings,int f,int c) {
ff=f;
cc=c;
compsToExperiment=new JPanel();
GridLayout experimentLayout = new GridLayout(ff,cc);
compsToExperiment.setLayout(experimentLayout);
this.setContentPane(compsToExperiment);
for (int filas=0;filas<ff;filas++){
for (int columnas=0;columnas<cc;columnas++){
if (arraystrings[filas][columnas].equals("gat")){
JLabel cat2 = new JLabel();
cat2.resize(1270/cc, 750/cc);
cat2.setIcon(new ImageIcon("cat.png"));
compsToExperiment.add(cat2);
/*cat2.getSize();
//cat2.getWidth();
//cat2.getHeight();
cat2.resize(width, height);
cat2.setBounds(x, y, width, height)
cat2.setSize(width, height)
cat2.
*/
}
if (arraystrings[filas][columnas].equals("rat")){
JLabel rat2 = new JLabel();
rat2.setSize(1270/cc, 750/cc);
rat2.setIcon(new ImageIcon("raton.png"));
compsToExperiment.add(rat2);
}
if (arraystrings[filas][columnas].equals("menjar")){
JLabel comida2 = new JLabel();
comida2.setSize(1270/cc, 750/cc);
comida2.setIcon(new ImageIcon("comida.png"));
compsToExperiment.add(comida2);
}
if (arraystrings[filas][columnas].equals("res")){
JLabel nada2 = new JLabel();
nada2.setSize(1270/cc, 750/cc);
nada2.setIcon(new ImageIcon("nada.png"));
compsToExperiment.add(nada2);
}
}
}
this.repaint();
//compsToExperiment.repaint();
compsToExperiment.setVisible(true);
//this.setVisible(true);
//this.setVisible(false);
}
You need to do custom painting to scale the image to the space available in the grid.
You can try using Darryl's Stretch Icon (or maybe the Shrink Icon).
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.
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()