I'm trying to create a button that will scale when pushed.
textureAtlas = assetManager.get("btn.txt", TextureAtlas.class);
skin = new Skin(textureAtlas);
btnStyle = new Button.ButtonStyle();
btnStyle.up = howToBtn_skin.getDrawable("button");
btnStyle.down = howToBtn_skin.getDrawable("button_pushed");
myButton = new Button(btnStyle);
howToBtn.setPosition((Constant.WIDTH / 2) - 41, (Constant.HEIGHT / 2 - 150));
"button" - is the smaller one
"button_pushed" - is the larger one
The button switches image when clicked, but it doesn't scale based on the image size. So when the user click the button, it looks like nothing happened because the size of the button remains the same.
Size of the button remains same(normal state and pressed state) and you need some UI interaction touch feel.
Three options. 1st and 2nd are in similar context.
Take two button state image (button-up,button-down),with same dimension but different tint value. like this -
If you want same tint value, make image smaller for down state button image but dimension should be same. canvas size for both state should be same and you've to scale down by center.
Scale button by yourself when you touch button. You can also add Action on your button that will give you better effect. You've to enable transform on your button.
button.setTransform(true);
button.setScale(0.5f);
Related
I want to create buttons in the corners of the screen for the controls of my game.
It used to work with just creating a skin, adding that skin to a new ImageButton(skin) and setting the size and position of that button to what is desired. I now use box2d and if i now set the size of the button to (1, 1) the height is correct but the width is always about 2/3 of the screen, no matter what i change it to.
I tried using a table and doing this:
table = new Table();
table.setFillParent(true);
stage.addActor(table);
table.setDebug(true);
table.add(boostButtonn).width(1).height(1).bottom().right();
But I have no clue how to draw it in the corner of the screen, also the button is drawn in the middle of the screen with the correct size using this table, but the skin is stretched out of the button. Viewport width and height are 40,24 respectively
My skin and button code:
Skin skin = new Skin(Gdx.files.internal("skin/flath-earth-ui.json"));
Button bootsButton = new ImageButton(skin);
And then i add it to the table
Problem is not in how you align or arrange your widget. Problem is why button/widget or even text is stretched and looks blurry.
Why stretched ?
Problem is in your resources. Your resources should be compatible with your viewport. If you're using small viewport then we need your resources(images and fonts) in small size.
You're using skin, that used to create Widget.
Skin depends on .json file that contains information about your Widget object and .atlas files having information of your image file.
You're using 40 as width of viewport then button image that used as drawable should be in range of approx. 10.
So what is solution now :
If you already have all resources ready then use some higher viewport dimension according to your resources else create create resources according to viewport.
I have button on frame. On that button I put an image, which as you see that is a search icon. I remove the visible border on the button. But still I have two questions:
when I put the mouse on the button, the image and also the invisible button border is clickable, what I need is restrict it just on the icon.
I want to make the icon focusable by clicking like click on the simple button.
Search Icon:
What sort of image file format are you using? If it's a format with transparency support, you'll have to implement an algorithm that checks whether the click coordinate is inside of the non-transparent pixels of the icon you're using. You'll have to offset the rgb checks on the image pixels by the position of the image on your frame as well. Use this as a reference - Java Image Processing
I want my program to do the following:
In frame there is a button and some image. When button is clicked I want this image move to the left until it will not be visible on frame (be outside of frame) and at the same time next image should come from outside of the frame to the center of it and stops there until the button is clicked again.
Should I inflate some ImageView everytime the button is clicked and change their x coord. though property animation? Or I can make two ImageView on frame and one of them would be transparent while other will be visible? And then change their positions and transparency level ("alpha") when button is clicked?
Which is the right way to do it?
A ViewFlipper might do what you want. From the javadoc:
Simple ViewAnimator that will animate between two or more views that have been
added to it. Only one child is shown at a time. If requested, can automatically
flip between each child at a regular interval.
Presumably, you can add two ImageView instances as children of the ViewFlipper and then animate between them with a slide animation. There are some answers on SO detailing this. Try searching for ViewFlipper.
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 can I create in a Swing interface a toggle image button?
I have two images, imageon.jpg and imageoff.jpg,and I basically want a clickable element that toggles the images and fires an event.
Update: Is there a way to override the usual 'chrome' around the image? I would prefer a simple image to a button with an image inside.
Load the images with ImageIcon. Create a JToggleButton. Then apply the icons with AbstractButton.setIcon/setPressedIcon/setSelectedIcon. Remove the border with AbstractButton.setBorderPainted(false).
How about JToggleButton? You can subclass it and override paint() to paint the correct image based on whether it is selected or not.
Another way would be to subclass JPanel and catch mouse clicks, overriding paintComponent() to draw the correct image. This way the only thing that gets drawn is the actual image (unlike the JToggleButton option).
I had the same problem with JButtons. Try this:
result = new JButton( icon );
result.setBorderPainted( false );
result.setContentAreaFilled( false );
width = icon.getIconWidth();
height = icon.getIconHeight();
result.setPreferredSize( new Dimension( width, height ) );
It is necessary to set the preferred size to get rid of additional space around the button. This worked for me on Windows 7 and Mac OS X 10.6.
Your best bet is to subclass AbstractButton, and set properties like border and background (in your constructor).
MyButton() {
setBorder(null);
setBackground(null);
}