Large Image on JButton not showing - java

is there a way to show an image larger than the small imageIcon?
I have a 64x64 png image that I am trying to show on a button (take up the whole button).
but when I use
ImageIcon icon = new ImageIcon("somePath");
JButton btnBuildStructure = new JButton(icon);
the image that is shown is only the 16x16 in the top left corner of the image I want shown
how do I do this?
I want it to look like this

The JButton should naturally size to an optimal preferred size that shows the entire icon. If this is not happening, then you are likely restricting the size of the JButton artificially. The solution: use the layout managers well, call pack() after adding all components and before setting the GUI visible, avoid null layouts, call revalidate() and repaint() on containers if you change any components, or their sizes or preferred sizes (such as if you change an icon while a program runs).

Related

MigLayout JLabels and fixing their size

So my program creates a MigLayout GUI and has a bunch of JLabels that are given images on creation. Here you can see what the final result is right now:
The images are too big and go over the size of the JPanel they're contained in. It is simply creating it to be the size that the original image was and then display that, however, it's obviously an issue. Is there a way I can fix the size so it will try to fit every component in the GUI?
For starters, I want to ensure that the blue portion does not get shifted like that, I want it to keep to the size it was set as (half the height). I also want the images to take up the maximum size they can with keeping the aspect ratio of the original image.
I've tried setting the JLabel's size and preferred size, I've tried some attempts with MigLayout giving it paramaters when I tell it to add the JLabel, neither have worked for me. I'm entertaining the idea of extending JLabel and writing something to fix it, but not entirely sure how to go about that. I know I could overwrite paint but not sure what I'd have to actually do.
My JPanel is set as follows:
JPanel jpCards = new JPanel(new MigLayout("insets 0, gapx 0, gapy 0, wrap 7", "grow"));
My cards are being added without additional parameters and created just by giving it pictures of the size they were displayed.
Just to clarify, I need the actual JLabel's size to be changed, not just what's contained inside, because they will have mouse listeners later on. If only the image changed, I'll have problems with the mouse listener detecting itself inside a JLabel's that it doesn't look as if it should
I solved this by instead changing the size of the icon before making the JLabel.
I've found that in Java, when you create a JLabel given some sort of image, it will fit the size of the JLabel to be that of the image you give it. So instead of creating the JLabel given the original image, I scale it first using the dimensions of the screen, then create a JLabel out of it.
There's still some work to be done since I want the user to be able to resize the screen and have the image resize. I'll have to spend some time figuring out how I can get it to resize, but it's not a major concern.

How to auto adjust window size in Swing

I need to display an image in a JFrame. How can I make the window size to be automatically adjusted based on the size of the image.
1.in the case that you put image as Icon / ImageIcon to the JLabel then
have to test for MaximumSize for JFrame that returned Toolkit for concrete monitor
if PreferedSize is lower than MaximumSize size then call JFrame#pack()
otherwise have to call setSize()
2.in the case that you put image as Icon / ImageIcon by using Custom Painting to the JComponent, JPanel, JLabel e.i. then
then this JComponent must to returns PreferredSize
a) call JFrame#pack() if PreferedSize is lower than MaximumSize,
b) otherwise have to call JFrame#setSize()
c) by assume that you don't use Image#getScalledInstance
3.I'd be to use Icon in the JLabel, there is only one issue that image can be smaller then expected size on the screen, but no issue with that, is pretty possible to centering image to the JLabel.CENTER to the JLabel

Netbeans null layout causing border around background when resizable unchecked

I'm making a simple JFrame with the GUI editor in netbeans with a background image set as an icon in a label as suggested by the netbeans site, with a label and a button centered. I was having a very hard time centering them without using the null layout and setting the pixels to center them. I have an 800X600 image as the background, and I don't want the window to be resizeable. So I unchecked resizeable in the properties, and on the code tab I have designer size set to 800, 600, generate size is checked, and the form size automatically sets to 816, 638. This then gives me a border around the right and bottom sides of a few pixels. If I change the Form Size to 800, 600, then the background image is cut off by a few pixels. One other thing that I set that may impact that is in the properties=>bounds set to 800, 600, 800, 600.
Any advice on how to get rid of the border without allowing the window to be resizeable as well as any on whether a different layout can help with centering would be greatly appreciated. I did find some information that Grid Bag layout would help, but I wasn't quite able to get it working correctly. I suppose that writing out the code instead of using the GUI editor may also be a better alternative, but I'm pretty new so any advice on that would be great as well.
Don't use null layout when you can center components quite easily if you use the correct layout or combination of layouts. For instance if you want a JLabel next to a JButton and have them centered in a JPanel, put the JLabel and JButton into their own JPanel first (make sure to have this JPanel's opaque property set to false) and then have the containing JPanel use GridBagLayout. If you add one component (the inner JPanel) without GridBagConstraints, the component is centered automatically, even if the containing JPanel is resized. It's almost idiot-proof, whereas null layout is a recipe for difficult hard to maintain code.

JToolBar buttons with different size images-NetBeans

I'm having problem in my JToolBar. I'm using images of different size.
Tool bar is not looking good. How can I use different size images in the JToolbar buttons?
How can I show the button label below each image?
How can all the buttons be aligned from the top, left corner?
For example,
java.net.URL imageURL2 = cldr.getResource("Images/report2.jpg");
ImageIcon aceOfDiamonds1 = new ImageIcon(imageURL2);
btnReport = new JButton(aceOfDiamonds1);
btnReport.setMaximumSize(new Dimension(49, 43));
btnReport.addActionListener(this);
jToolBar1.add(btnReport);
I'm using images of different size.
Resize them. Either once at time of build (recommended), or at run-time.
And I want to show the button label below each image
This is close.
newJButton(String,Icon);
And i want to show the button label below to the each image?
With the setVerticalTextPosition and setHorizontalTextPosition methods of JButton:
// Place text below icon
button.setVerticalTextPosition(SwingConstants.BOTTOM);
button.setHorizontalTextPosition(SwingConstants.CENTER);
How to show all this buttons align from the left top corner
As Andrew Thompson said, the images all have to be the same size.

How to rotate an image properly in JPanel (Java)

I'm working on rotating a loaded image. I set the graphics on a JPanel and then use standard AffineTransform in order to rotate it, say, 45 degrees.
Unfortunately, the image is being cut, if it exceeds the panel area.
How may I force JPanel to add scrolls to itself (while loading an image file, I would like to adjust the size of JPanel by adding the scrolls, without adjusting the size of JFrame).
Or, in other words, how to correctly rotate the whole image?
Thank you in advance for the reply!
Use a JScrollPane instead.
You need to put the panel in a jscrollPane and then make sure you have the preferredsize set correctly so that the scroll will be available.
Well, I would suggest that you should not be rotating the image in the paintComponent() method of your panel since this will result in the preferred size of the panel changing and you should not be setting the preferred size in the paintComponent() method.
Instead you should probably have a rotate(...) method. This method will create a BufferedImage of the desired rotation. Then your paintComponent() method simply paints the buffered image. Then you would set the preferred size based on the rotation.
Now regarding the preferred size of the rotated image check out this example.

Categories

Resources