I have a Button with text and an ImageView as icon. The image might be of any resolution. Currently, the button will adapt its size to fit the image, e.g. if the image is 400x400, the button will be huge and the text will be tiny next to it.
What I want though, is the image to fit into the button. It should always be as tall as the text is.
ImageView img = new ImageView(image);
button = new Button("Some text", img);
button.setStyle("-fx-background-radius: 6em;");
Wouldn't have thought the solution to this is so easy:
img.setPreserveRatio(true);
img.setFitHeight(button.getFont().getSize());
does the trick for me. JavaFX is weirdly unintuitive sometimes. If the font size may change (e.g. via CSS styling), use a binding instead:
img.fitHeightProperty().bind(Bindings.createDoubleBinding(() -> button.getFont().getSize(), button.fontProperty()));
You can set your image height via
image_view.getLayoutParams().height = YOURHEIGHT
as discussed here.
I would suggest setting your text size to an appropriate value and use the above command to scale the image to the same height.
Test around whether you can size the image with em, otherwise use cm/inch.
If there a problems with image distortion when setting height, refer to this thread.
Related
First of all, this is my first project in JavaFX, so if my general approach is wrong, I very much appreciate feedback on how else to implement this.
I'm trying to set up a hexagon grid of clickable tiles, similar to Settlers of Catan. I did this by creating a hex class that includes an imageview. For scaling purposes, these imageviews are scaled by a common parameter, where I tried both the .setScaleX/Y and setFitWidth/Heigth functions.
For what it matters, each hex is combined into a StackPane object and then all the hexes are combined into a Group.
To detect mouseclicks, I'm using the .setOnMouseClicked() function.
The problem I've been facing is that when the scaling parameter gets small, the imageviews seem to overlap their clickable areas. The graphic, however, scales properly. Since the setOnMouseClicked() function only responds non-transparent parts, this means there is no response when clicking on tiles overlapped by others.
How can I solve this problem?
Is there a way to scale the clickable area using my global scale parameter?
Can I somehow define the clickable area using some kind of path along the border?
Would it work to define a transparent hexagon shaped button and place on top of my image?
I tried using different approaches to scaling, such as setScaleX and setFitWidth.
I tried using setMaxSize() on the StackPane.
I tried using viewport and clip, but I don't think they do what I'm looking for.
public Hex(Point2D pos, double sizeScale){
...
Image img = new Image( "images/hex.png" );
m_size = new Point2D(img.getWidth(), img.getHeight());
m_image = new ImageView( img );
...
m_image.setFitWidth( m_sizeScale * m_size.getX() );
m_image.setFitHeight( m_sizeScale * m_size.getY() );
...
}
Expected result:
Each hex is clickable, giving different responses based on which hex is clicked.
Actual result:
Hexes added before added hexes are not clickable if positioned to the right or below. (Anchor point is top-left)
I have customized a cursor on a label component called ageLabel using the following code
ageLabel.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(new javax.swing.ImageIcon(getClass().getResource("/images/image1.jpg")).getImage(),new Point(5,5),"custom cursor"));
It works fine anyway but what I want to do is increase the size of the customized cursor. I have tried changing the point to (10,10) but the cursor size wouldn't change. I also tried changing the dimensions of the image that I used ,it still wouldn't change. I have searched through the internet but to no avail. Is It possible to resize the cursor in anyway? If it is how do I do that?
Thanks in advance for all helps.
Java's Image class has a built in function for scaling.
Documentation: https://docs.oracle.com/javase/7/docs/api/java/awt/Image.html#getScaledInstance(int,%20int,%20int)
My approach to the problem:
Split the line up into many to make it look a bit nicer (not necessary).
Add an extra method call to scale the image up.
Potential Solution:
URL imageResource = getClass().getResource("/images/image1.jpg");
ImageIcon imageIcon = new ImageIcon(imageResource);
Image image = imageIcon.getImage();
// This is the important line that scales the image up!
Image scaledImage = image.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT);
Toolkit toolkit = Toolkit.getDefaultToolkit();
Cursor cursor = toolkit.createCustomCursor(scaledImage, new Point(5, 5), "Custom Cursor");
ageLabel.setCursor(cursor);
Afterthoughts:
Remember to replace newWidth and newHeight with the values you want.
After you get it working, I'd even go further and extract some of this code into methods.
You might need to tweak your imports a bit if it isn't compiling.
I haven't tried running this code so I don't know if it works.
As the title says, how can I do to remove the background from an imageIcon?
Let's say.. I've a JTree (but it can be every JComponent) and I would like to set an icon for its leaf nodes.
myDefaultTreeCellRendere.setLeafIcon(new ImageIcon(icon));
The icon has the white background and I would like removing that. Is that possible?
I read some answers on Stackoverflow but there a BufferedImage is used and the icon is retrieved from the web and stored in an URL object... My icon is local, instead.
You need to use an image format that supports transparency, such as GIF or PNG. You can either look for a new icon with transparent background, or you can modify your current image with an image editor (Photoshop or Gimp will do).
What you could do is to give to the ImageIcon object the same size as that of the image. For example, if your image has a sioze of 40x40, you do
ImageIcon img = new ImageIcon("path_to_image");
img.setPreferredSize(new Dimension(40, 40));
myDefaultTreeCellRendere.setLeafIcon(img);
+1 if it works :D
I have a few issues with my drag and drop. I have a resized imageview (obj) that I can drag and drop. The problem is when I drag it the cursor is in the middle. I want to change that. I'v tried db.setDragViewOffsetX(); but it doesn't work. Also the image displayed is full size, and since my imageview is resized I want it to match the imageview size but there seem to be no function to resize Image type...
Dragboard db = obj.startDragAndDrop(TransferMode.MOVE);
ClipboardContent content = new ClipboardContent();
Image sourceImage = obj.getImage();
content.putImage(sourceImage);
db.setContent(content);
Thanks for helping
I had the same problem using the Drag/Drop for precise image movements in my application. Also the effect, that for large images, the shown Drag/Drop image is scaled down was annoying too. So I decided to move images not using Drag/Drop within my application and change into Drag/Drop transfer mode if the movement left my application area.
See How to delay the Drag-Start to combine move and Drag-And-Drop dynamically for that approach.
For a solution on the mouse anchor during Drag/Drop:there also is a comment from José Perera about more control on the Drag/Drop image mouse anchor during move...
I have an ImageIcon that I used for a button to open this skillsFrame. This size of the image is 100x100px. As you can see in the screenshot if I just put the image it is too large(which is expected).
My question is about how to scale the image to 16x16(or whatever the default size is) so that I don't need to manually create a smaller sized version(for multiple reasons).
In case they make windows bigger in the future
Multiple operating system support
I'm sure I don't need to go on naming more...
skillsFrame = new JInternalFrame("Skills", true, true, false, false);
skillsFrame.setFrameIcon(new ImageIcon("images/gui/button_skills.png"));
And now for the image... the bar at the tops is the buttons to click to open the various JInternalFrames.
how to scale the image to 16x16(or whatever the default size is)
you have look at Image getScaledInstance(int width, int height, int hints)