Java: Image as toggle button - java

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);
}

Related

Problem with setting background image using JLabel

I trying to make mini game and this game have option to choose map. Diffrent chooice, choose diffrent map. Every choice have own map ( background image ). For background image I useing JLabel and method: setIcon().
My problem is that when I set image, all my components get hide. This is picuture: http://prntscr.com/qi5m8a ( You can see that only image can be seen ).
For map choose I use this structure, here is picture: http://prntscr.com/qi5n4c There is Play button with event like this:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(mapOneRadioButton.isSelected())
{
this.dispose();
GameWindow game = new GameWindow();
game.setVisible(true);
game.setLocationRelativeTo(null);
backgroundLabelGameWin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/main/earthProject.gif")));
}else if(mapTwoRadioButton.isSelected())
{
}else if(mapTreeRadioButton.isSelected())
{
}else if(mapFourRadioButton.isSelected())
{
}else if(mapFiveRadioButton.isSelected())
{
}
}
I useing JPanel where I add JLabel for background image and I useing JLayeredPanel for rest of components.
My problem/question is, How can I change/set image without breaking order in my frame ?? I mean, I want to change/set image in background so all my components can be seen.
Please help me, I have no idea how to fix this.
For background image I useing JLabel and method: setIcon(). My problem is that when I set image, all my components get hide.
Two options:
add the components to the JLabel. Then the components will paint on top of the image. The restriction of this approach is that the components must fit inside the image, since the image is always painted at its actual size.
do custom painting of the image on a JPanel by overring the paintComponent(...) method of the panel and then invoke the Grapics.drawImage(...) method. Then add your components to the panel. See Background Panel for a class that provides this support. This provides more flexibility as you can scale the image to fill the panel.
It will be better if you change the background of the JPanel instead of using a JLabel for image. You can't directly change the background of a JPanel. But you can refer following link to do it
http://www.java2s.com/Code/Java/Swing-JFC/Panelwithbackgroundimage.htm

Java Swing : best way to set background image clickable

I try to find the best way to make an image clickable with different action depending on the area you click. For now, my window display an EFTPOS (image found on google image) using :
getContentPane().add(new JPanelBackground("./img/background.png", "./img/eftposAboveBackground.png", this.getWidth(), this.getHeight()));
Now, I would like to click on every EFTPOS's buttons. I was thinking of create some transparent buttons at the buttons's positions but :
First, I don't want to use an absolute layer
Second, the Swing GUI design editor (Netbeans) doesn't display my background image since I used the code above.
I'm pretty sure there is another way than setting my eftpos on the background, maybe using the ImageIcon class..
Any help would be greatly appreciated!
You can to create an ArrayList of Rectangles (or Shapes) to represent the area of each button on the image.
You need to add a MouseListener to the image.
In the mousePressed() event you need to iterate through all the Rectangle in the ArrayList using the Rectangle.contains(mouse point) method to see if the user clicked in a Rectangle.
When you find the Rectangle that was clicked you execute your custom code.

Libgdx label rotation

Is it not possible to rotate a Label? It seems the API has that function but it doesn't seems to work? Are there anymore ways to rotate text?
Label nameLabel = new Label( "Test", skin);
nameLabel.setRotation( 90 );
stage.addActor( nameLabel );
You can wrap your label inside another Actor and rotate the parent Actor. So you will indirectly rotate the label, but the visible result is the same.
So you could create a parent actor for example like this:
public class LetterActor extends Group { //..
then for example in the constructor you add a Label to it:
this.addActor(someLabel);
then add a rotate action (or any other action!) to it:
this.addAction(Actions.rotateBy(90));
you may also need to set a height/width & origin for this parent actor
I've found it's not possible to rotate Labels, or Buttons or anything with text in libGDX. You can make an image and rotate it as a workaround.

Making an image show up on a JButton

I'm trying to make a chessboard (8x8 grid) and fill it with background squares, but it always appears as a grid of empty squares. The image is called emptysquare.jpg but it does have a background color.
How to I correctly add an image to a JButton on the grid I have?
The reason that your chessboard is empty is due to the fact that you call JFrame#setVisible before adding your ChessSquare buttons to your panel. Ensure all component have been added before making this call.
Also set the Icon like this in ChessSquare
ImageIcon empty = ...
setIcon(empty);
You need to call setIcon( empty ) in your constructor.
It's allowed-but-awkward to call setVisible( ) before the squares are added, but if you do you may need to repack to get the layout right.
The call to setLayout( ) must be moved before the loop.

Is there a container that takes shape of an image or icon?

I have an ImageIcon which is an image of a router. I added this ImageIcon to a label. But labels are either rectangles or squares. I want such a component that takes the shape of the ImageIcon when added to it. Does such a component exist? Is there any other way of doing it?
Yes label is rectangular but it is transparent by default( call setOpaque( false ) if it isn't ). Isn't that what you want?

Categories

Resources