I'm writing checkers. So, every cell is JButton, it has image (ImageIcon).
I want JButton that has background image and backlighting (for example, blue of red), because I want to show user available turns. Is it possible?
Now if I create new JButton(Icon icon) and then setBackground(Color.BLUE);
background is ignored.
You should set its border color to BLUE or RED (I believe this is what you are looking for as an answer)
Try this statement:
chessButton.setBorder(BorderFactory.createLineBorder(Color.GREEN));
Painting JButton's border is not effected by image icon, therefore if you want to create a highlight effect and set border's color you should use BorderFactory. You can also have various effects for the border through BorderFactory
If you want complete background highlighted or some special effect then have two separate image of each cell i.e. one normal image and one highlighted image. When you want to show user its available turns, simply update the respective cells with their corresponding highlighted image
Related
I'm creating an AbstractColorChooserPanel for recent colours (in a 4 * 4 grid) and while setting the background colour for the recent colours it only appears as a border to the button instead of filling it.
According to this the code below should work:
button.setBackground(Color.RED);
button.setOpaque(true);
Ive also tried adding
button.setBorderPainted(false);
but all that displays is a grey (standard colour) button with a red border.
I have tried putting the code on a button outside the JColorChooser and received the same effect. example
How do I make it so the entire button is filled with the red colour instead of just the border?
EDIT: The problem turned out to be the UIManager (default system look and feel)
To solve this I modified the code used in this solution.
Try adding this :
button.setContentAreaFilled( false );
I want to be able to select part of an image in an ImageIcon on JLabel and fill it with color.
Is this possible I have become a little confused as I have read that an ImageIcon is not selectable but I am not sure if that means I have to find another way of displaying the image?
Possible, yes, difficult, yes.
You need to start with a BufferedImage, which you can then wrap in a ImageIcon and apply to a JLabel.
You would then need to register a MouseMotionListener and MouseListener to the label to detect the area which was selected, you would then modify the BufferedImage accordingly and repaint everything.
Having said that, I wouldn't use a JLabel, as you can't accurately calculate the location that the label renders the icon, instead, I'd make myself a custom component, extending from JPanel and encapsulate the functionality within it and use custom painting to paint the image (and the selection area)
Start by having a look at How to Write a Mouse Listener, Performing Custom Painting, 2D Graphics and possibly Reading/Loading an Image, Writing/Saving an Image
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 have a column of JPanel instances that has content in it, that when it is clicked, the selected Panel is set to have a border (in order to distinguish it), and only 1 at a time has the border.
The problem is that when it sets the border, it sets the outer section of the panel to the border, and shrinks the content inside. Although it seems minor it is not very professional, and I would much rather have it add more like an overlay, where the content will not shrink.
I am thinking maybe there is some method of graphics that will let me do this? I haven't been able to find any way of doing this.
Start by setting all the components to have a EmptyBorder set to a single pixel inset.
When you select a panel, simply set the newly selected panel's border as you are (presumably using a LineBorder) and the set the previously selected panel's border to the single pixel EmptyBorder.
If you're clever, you could get away with a single instance of EmptyBorder ;)
I want to colour the same cell of a table using 2 colours. I still have to display some text in the cell also and I want to do it by keeping the current structure.
I've tried with labels, panels, but I can't get it to work properly. Also, the colours have to updated based on the status on the cell (update the colour of the first half while keeping the colour of the second one).
You can write your own extension of a Component / JPanel that knows how to display a text and knows how to paint the background in 2 colors.
This would mean that you have to overwrite paint or paintComponent in your custom component.
Then, the getTableCellRendererComponent would return instances of this custom component.