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.
Related
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
I'm trying to build a program that displays random shapes and random text. This is my code inside my class that extends JPanel.
#Override
protected void paintComponent(Graphics g) {
// draw all shapes
super.paintComponent(g); // clears the background
this.add(new JLabel(new RandomText().getRandomSentence()));
for (RandomShape shape : shapes) {
shape.draw(g);
}
}
I use a JPanel instead of g.drawString() to in the future display HTML code. I also have a button that "regenerates" the window, by calling the repaint() method. However, when I push the regenerate button, thus calling the repaint() method, the shapes change (like they're supposed to), the text however does not. What I find even stranger is when I resize the window new labels are continously added to my panel (with random text like they're supposed to).
Does super.paintCompnent(g) not clear components added to the panel? If so, why does calling repaint() myself not add extra labels, like resizing the window does?
Resizing the window also forces validate() to be called which lays out the components on the panel again - simply repainting does not.
However, in your painting method you're adding a new JLabel each time and not removing any, which almost certainly isn't what you want to do. You should keep track of the JLabel in a separate variable and either change it's position when you repaint (preferably) or remove it before adding a new one.
This adds a new label to the panel at every repaint:
this.add(new JLabel(new RandomText().getRandomSentence()));
Instead, you should add one label once to the panel, perhaps in the constructor of your class. Then setText(aRandomSentence) for it, when the button is pressed. Do not modify components in paintComponent().
when super.paintComponent(..); is called it wipes out the graphics elements. It however does not change the objects that have been added to it. You will be adding a new label to the panel every time repaint is called.
When you call repaint(); yourself, it's not guarenteed to call the paintComponent of all components. But when you resize it HAS to since everything is going to be different.
This happens because you're constantly adding new JLabels to the JPanel.
Keep track of your JLabel and change it's text instead of adding new JLabels.
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 can't find anyone who has this same problem, so it might just be a simple fix, but I'm creating a Jeopardy game in Netbeans. I created a JPanel Form to design each of the individual squares, and then I added 36 of them (for the 6x6 board) to another JPanel Form which represents the board. That works great, but the problem is that after I add all 36 squares to the board, they are stuck in the state at which I added them. If I change the position or color of a label in the Question JPanel Form, it does not update in the Jeopardy Board JPanel Form. I don't want to re-add 36 Questions and rename all the variables every time I make a change to a sub-component. Is there a way to keep it all in sync?
If I change the position or color of a label in the Question JPanel Form,
1). labels are non opaque which means changing the background color will have no effect. You need to make the label opaque when you create it when you want to alter the background:
label.setOpaque( true );
2) Once you set up your jeopardy board you should have no need to change the position of the label. Once the question is answer, I would just clear the text in the label by using:
label.setText("");
For each new round of jeopardy again all you need to do is reset the background for every label and then reset the text for every label. There should be no need to recreate all the labels.
You could try calling the revalidate() method of the JPanel.
Consider refactoring and renaming all folders you personally created in the project. This should resolve the issue.
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);
}