Issue regarding dynamically loading Images in loop using Java Swing - java

public String[] imagesArray = {Images.firstImage, Images.secondImage};
String imagesPath = "/testproject/images/";
for(int i = 0; i<imagesArray.length; i++) {
URL imageURL = this.getClass().getResource(imagesPath+imagesArray[i]);
ImageIcon orignalImageIcon = new ImageIcon(imageURL);
Image newImage = orignalImageIcon.getImage().getScaledInstance(100, 90, java.awt.Image.SCALE_SMOOTH);
ImageIcon newImageIcon = new ImageIcon(newImage);
JButton receiptButton = new JButton(newImageIcon);
receiptButton.setBorder((new EmptyBorder(0,0,0,0)));
toolBar.add(receiptButton);
add(toolBar);
}
Images not shown in my design layout?

The problem is most likely the asynchronous loading nature of using an ImageIcon to load the original images.
If that is the problem:
There is an easy way to test it. Add the orignalImageIcon to the button and see if they all appear.
There is an easy way to fix it. Load the images using ImageIO.read(URL) - a method that will block until the image is completely loaded.

Related

How to change a code of putting image within a label from desktop to project file

So I have this code It's work when the image at my desktop, I added the image at src file put I couldn't convert it can you tell me what is the problem? this the code it set the image to fit the label too:
public void ScalImage() {
ImageIcon image = new ImageIcon("C:\\Users\\HP\\Desktop\\ath3.png");
Image img = image.getImage();
Image imgScale = img.getScaledInstance(jLabel2.getWidth(), jLabel2.getHeight(), Image.SCALE_SMOOTH);
ImageIcon scaliedicon = new ImageIcon(imgScale);
jLabel2.setIcon(scaliedicon);
}
I tried to say: ImageIcon image = new ImageIcon("ath3.png");
didn't work
So I have solved it I wrote like that
ImageIcon image = new ImageIcon(getClass().getResource("ath3.png"));
It's work for me. I wish I'm right

display gifs in java jars using eclipse

Hi I am unsure on how to format my animated gif images to let them show on Jars created on eclipse.
try {
//ImageIcon titleIcon = new ImageIcon(ImageIO.read(getClass().getResource("title.gif")));
title = new ImagePicture (new ImageIcon(ImageIO.read(getClass().getResource("title.gif"))), 0, 0);
//title = new ImagePicture (new ImageIcon("title.gif"), 0, 0);
}//end try
catch (IOException e) {
}//end catch
//set title bounds
title.setBounds(260, 0, 400, 100);
That is my code right now for an animated GIF, Thank you for your input.
Hard to give a good example without more context, but you can try adding the ImageIcon to a JLabel like this.
URL url = this.getClass().getResource("title.gif");
Icon title = new ImageIcon(url);
JLabel titleLbl = new JLabel(title);
//You have to add titleLbl to a container after this, of course

Libgdx nine patch text button height issue

I am using a TextButton to create a dialog bubble effect in my game.
I have created the nine patch file as on the picture
everything seems to be fine until I try to use this bubble.9 with LibGDX, the TextButton is not stretching correctly based on the text height.
You can see that a multilines text extends the TextButton height but not correctly.
Here is the code
Load the bitmap font
FileHandleResolver resolver = new InternalFileHandleResolver();
assetManager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
assetManager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));
FreetypeFontLoader.FreeTypeFontLoaderParameter dialogFont = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
dialogFont.fontFileName = Conf.ASSET_FONT;
dialogFont.fontParameters.size = 8;
Initialize the TextButtonStyle
NinePatch patch = atlas.createPatch("bubble");
NinePatchDrawable dialogBoxPatch = new NinePatchDrawable(patch);
buttonStyle = new TextButton.TextButtonStyle();
buttonStyle.up = dialogBoxPatch;
buttonStyle.down = dialogBoxPatch;
buttonStyle.checked = dialogBoxPatch;
buttonStyle.over = dialogBoxPatch;
buttonStyle.font = dialogFont;
buttonStyle.fontColor = Color.BLACK;
actual rendering
dialogBox = new TextButton("some\ntext", buttonStyle);
dialogBox.draw(batch, 1);
Does someone see what the problem is?
I finally found the problem.
I have decided to test the ninepatch with another font and it worked like a charm. To understand what was going on, I have opened the two fonts with glyphrstudio. It appears that the problematic font is not correctly designed.
Bad font
Good font
You can see that the base line and capheight are not correctly setted on the bad font.
I hope this will help other people facing the same problem.

How to get Image (obj) from ImageIcon

I am trying to give my interface a new function, but I have encountered some obstacles. I want to enlarge image on JLabel when mouseEnters.
Here is how my JLabels looks:
int sacle = 50 //Size of my JLabel Icon
int zoom = 10 // How much the icon should enlarge
imageIcon = new ImageIcon(new ImageIcon(myClass.class.getResource(Picture))
.getImage().getScaledInstance(scale, scale, Image.SCALE_SMOOTH));
JLabel stackIsGreat = new JLabel();
stackIsGreat.setIcon(imageIcon);
//and I add multiple of such JLabels`
And the code goes on and on. I wanted to creat a function and add it to mouseListener, so all will behave the same. I wanted to achive that with:
//inside external method
activeLabel = (javax.swing.JLabel)(e.getSource());
ImageIcon temp = (ImageIcon) activeLabel.getIcon();
But there is no way I know I could get use of this, because java says I need Image to create my enlarged ImageIcon
ImageIcon enlarged = new ImageIcon((Image).getScaledInstance(scale + zoom, scale + zoom, Image.SCALE_SMOOTH))
How can I retrive the image used to crate the JLabel from code.
Any help would be appreciated.
I want to enlarge image on JLabel when mouseEnters.
Instead of creating your own MouseListener you could use a JButton to give you the rollover effect:
Something like:
JButton button = new JButton(...);
button.setBorderPainted( false );
ImageIcon icon = (ImageIcon)button.getIcon();
Image image = icon.getImage();
Image scaled = image.getScaledImage(...);
button.setRolloverIcon( new ImageIcon( scaled ) );

Write result image using jmagick

I want to crop part of the image and then write the result to another image file. My code looks like this
ImageInfo imageInfo = new ImageInfo("file path");
MagickImage image = new MagickImage(imageInfo );
Rectangle cropInfo = new Rectangle();
cropInfo.x = 20;
cropInfo.y = 20;
cropInfo.width = 300;
cropInfo.height = 300;
MagickImage result = image.cropImage(cropInfo);
result.setFileName("path to result file");
boolean s = result.writeImage(imageInfo);
The above code just works but why writeImage using the old ImageInfo? and the MagickImage.setFileName don't make sense to me. I think we should create a new ImageInfo object and then write to that ImageInfo. The following code make more sense but don't work as expected.
MagickImage result = image.cropImage(cropInfo);
ImageInfo resulInfo = new ImageInfo("path to new file");
boolean s = result.writeImage(imageInfo);
Does anyone experience with this ?
It was inconvenient for me to keep the original ImageInfo around, so I tried this and it also worked:
result.setFileName("path to result file");
boolean s = result.writeImage(new ImageInfo());
As to why the parameter is needed at all, the mystery remains. Null will not work.

Categories