So i have a GUI program called Safe25. Basically, if you press the buttons in the right order which is "15032018" the program closes itself.
If you input a correct number, lets say you press 1 at the start, the buttons should change their backgroundcolor to green like this:
If you press a wrong button, the buttons should change their color to red.
But the logic of my code is irrelevant for my problem.
As i said, i want to change the buttons backgroundcolor like in the linked image. My problem is that it changes the backgroundcolor of the frame instead like this
The important line is 75, i commented this one.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Safe25 extends Frame implements ActionListener {
JButton[] buttons;
Safe25() { // Konstruktor
setSize(250, 300);
setLocation(300, 300);
setTitle("Safe25");
buttons = new JButton[10];
for (int i = 0; i < 10; i++) { // 10 Knöpfe im Array
buttons[i] = new JButton("" + i);
buttons[i].setFont(new Font("Courier", Font.BOLD, 34));
buttons[i].addActionListener(this); //
}
Panel panel0 = new Panel();
panel0.setLayout(new GridLayout(1, 1));
panel0.add(buttons[0]);
Panel panelRest = new Panel();
panelRest.setLayout(new GridLayout(3, 3));
setLayout(new GridLayout(2, 1));
for (int i = 1; i < 10; i++) {
panelRest.add(buttons[i]);
}
add(panel0); // Panel mit 0-Knopf
add(panelRest);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent wv) {
System.exit(0);
}
});
setVisible(true);
}
int s = 0;
public void actionPerformed(ActionEvent evt) {
// 0 1 2 3 4 5 6 7 8 Zustände ...
// 1-5-0-3-2-0-1-8 ist richtige Kombination
switch (Integer.parseInt(evt.getActionCommand())) {
case 0:
s = (s == 2 || s == 5) ? s + 1 : 0;
break;
case 1:
s = (s == 0 || s == 6) ? s + 1 : 1;
break;
case 2:
s = (s == 4) ? s + 1 : 0;
break;
case 3:
s = (s == 3) ? s + 1 : 0;
break;
case 5:
s = (s == 1) ? s + 1 : s == 7 ? 2 : 0;
break;
case 8:
s = (s == 7) ? s + 1 : 0;
break;
default:
s = 0;
}
Color col;
if (s == 0) {
col = Color.red;
} else { // richtiger Weg
col = Color.green;
}
if (s == 8) {
System.exit(0);
}
for (Component c : getComponents()) // line 75, i want this one
c.setBackground(col); // to change the buttons backgroundcolor
repaint(); // but it changes the frames backgroundcolor instead
}
public static void main(String[] args) {
Safe25 we = new Safe25();
}
}
have you red the javadoc for JButton?
edit:
Sorry i looked over your code to quickly. What your doing right now is setting the background color of every component in the current container.
While your buttons array is global you could simply loop trough that collection again to get the correct components "the buttons" and setting the background color like so:
for (JButton b : buttons) // line 75, i want this one
b.setBackground(col); // to change the buttons backgroundcolor
repaint(); // but it changes the frames backgroundcolor instead
The answer is, no, not really - or at least not as you might expect.
The button's content is provided by the look and feel delegate, most of which ignore things like the background property (or at least don't use it in ways you might think it should).
Instead, you need to remove these decorations and do a little of the work yourself
For example...
buttons = new JButton[10];
for (int i = 0; i < 10; i++) { // 10 Knöpfe im Array
buttons[i] = new JButton("" + i);
buttons[i].setFont(new Font("Courier", Font.BOLD, 34));
buttons[i].setContentAreaFilled(false);
buttons[i].setOpaque(true);
buttons[i].setBorder(new EtchedBorder(EtchedBorder.LOWERED));
buttons[i].setBackground(Color.RED);
buttons[i].addActionListener(this); //
}
This disables the area filling, replaces the border and makes the component transparent, which produces something along the lines of
Related
I have the weirdest bug ever.
I have this puzzle game that moves puzzle pieces (which really are buttons with images attached to them).
Everything worked fine until I tried to change the text of some label (to indicate how many steps the player has done).
Everytime I call someControl.setText("text");, the puzzle pieces that moved are set back to the their first position. I have no idea why, but they just do.
Here's my window:
It consists of two panels, each uses a GridBagLayout.
The main frame uses a gridBagLayout as well, which consists of the two panels.
I know it's weird as hell, but I can't figure out what may cause this GUI bug. Any idea?
The pieces of code:
increaseSteps which is called everytime I click a puzzle button
void increaseSteps() {
_steps++;
_lblSteps.setText("Steps: " + _steps);
}
Creation of the puzzle panel (the left panel)
private JPanel puzzlePanel() {
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
for (int i = 0; i < _splitImage.getSize(); i++)
for (int j = 0; j < _splitImage.getSize(); j++) {
int valueAtPos = _board.getMatrix()[i][j];
if (valueAtPos == 0)
continue;
int imageRow = _board.getImageRowFromValue(valueAtPos);
int imageCol = _board.getImageColFromValue(valueAtPos);
ImageIcon imageIcon = new ImageIcon(_splitImage.getImages()[imageRow][imageCol]);
JButton btn = new JButton(imageIcon);
_tileButtons[i][j] = new TileButton(btn, i, j);
btn.setPreferredSize(new Dimension(_splitImage.getImages()[i][j].getWidth(null),
_splitImage.getImages()[i][j].getHeight(null)));
// add action listener
btn.addActionListener(this);
btn.addKeyListener(this);
gbc.gridx = j;
gbc.gridy = i;
panel.add(_tileButtons[i][j].getButton(), gbc);
}
return panel;
}
actionPerformed:
#Override
public void actionPerformed(ActionEvent e) {
if (!(e.getSource() instanceof JButton))
return;
JButton btn = (JButton) e.getSource();
TileButton tile = getTileButtonFromBtn(btn);
if (tile == null)
return;
// check if we can move the tile
String moveDir = _board.canMoveTile(tile.getRow(), tile.getCol());
if (moveDir.equals("no"))
return;
increaseSteps();
int dirx = 0;
int diry = 0;
if (moveDir.equals("left")) {
dirx = -1;
_board.move("left", true);
tile.setCol(tile.getCol() - 1);
} else if (moveDir.equals("right")) {
dirx = 1;
_board.move("right", true);
tile.setCol(tile.getCol() + 1);
} else if (moveDir.equals("up")) {
diry = -1;
_board.move("up", true);
tile.setRow(tile.getRow() - 1);
} else { // down
diry = 1;
_board.move("down", true);
tile.setRow(tile.getRow() + 1);
}
moveButton(btn, dirx, diry, MOVE_SPEED);
if (_board.hasWon())
win();
}
moveButton: (moves the button in a seperate thread, calling btn.setLocation())
private void moveButton(JButton btn, int dirx, int diry, int speed) {
Point loc = btn.getLocation();
// get start ticks, calculate distance etc...
StopWatch stopper = new StopWatch();
int distance;
if (dirx != 0)
distance = _splitImage.getImages()[0][0].getWidth(null) * dirx;
else
distance = _splitImage.getImages()[0][0].getHeight(null) * diry;
if (speed > 0) {
// run the animation in a new thread
Thread thread = new Thread() {
public void run() {
int currentTicks;
int elapsed;
do {
int newX = loc.x;
int newY = loc.y;
elapsed = stopper.getElapsed();
int moved = (int) ((double) distance * (double) (elapsed / (double) speed));
if (dirx != 0)
newX += moved;
else
newY += moved;
btn.setLocation(newX, newY);
} while (elapsed <= MOVE_SPEED);
// make sure the last location is exact
btn.setLocation(loc.x + (dirx == 0 ? 0 : distance), loc.y + (diry == 0 ? 0 : distance));
}
};
thread.start();
}
else
btn.setLocation(loc.x + (dirx == 0 ? 0 : distance), loc.y + (diry == 0 ? 0 : distance));
}
You're trying to set the absolute position of a component via setLocation(...) or setBounds(...), one that is held by a container that uses a layout manager. This may work temporarily, but will fail if the container's layout manager is triggered to re-do the layout of its contained components. When that happens, the GridBagConstraints will take over and the components will move to their gridbag constraints assigned location.
The solution is to not do this, and instead to place the location of your components in concert with the layout managers used.
Another problem is that your current code is not Swing thread-safe since you're making Swing state changes from within a background thread. This won't always cause problems, but since it's a threading issue, risks causing intermittent hard to debug problems (ones that usually only occur when your boss or instructor are trying to run your code).
Possible solutions:
For a grid of images, you could use a grid of JLabels (or JButtons if you must) held in a container that uses GridLayout. When you need to reposition components, remove all components held by that JPanel, and then re-add, using the order of addition to help you position the components.
Easiest though would be to use a grid of non-moving JLabels, give them MouseListeners, and instead of moving the JLabels, remove and add Icons to them, including a blank Icon.
If you need to do Swing animation, use a Swing Timer to drive the animation. This will allow your code to make repetitive calls with delay between the calls, and with these calls being made on the Swing event thread, the EDT (event dispatch thread).
Demo proof of concept example code that shows swapping icons, but without animation, and without test of solution yet:
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.*;
#SuppressWarnings("serial")
public class ImageShuffle extends JPanel {
private static final int SIDES = 3;
public static final String IMG_PATH = "https://upload.wikimedia.org/wikipedia/commons/"
+ "thumb/5/5a/Hurricane_Kiko_Sep_3_1983_1915Z.jpg/"
+ "600px-Hurricane_Kiko_Sep_3_1983_1915Z.jpg";
private List<Icon> iconList = new ArrayList<>(); // shuffled icons
private List<Icon> solutionList = new ArrayList<>(); // in order
private List<JLabel> labelList = new ArrayList<>(); // holds JLabel grid
private Icon blankIcon;
public ImageShuffle(BufferedImage img) {
setLayout(new GridLayout(SIDES, SIDES, 1, 1));
fillIconList(img); // fill array list with icons and one blank one
Collections.shuffle(iconList);
MyMouseListener myMouse = new MyMouseListener();
for (Icon icon : iconList) {
JLabel label = new JLabel(icon);
label.addMouseListener(myMouse);
add(label);
labelList.add(label);
}
}
private class MyMouseListener extends MouseAdapter {
#Override
public void mousePressed(MouseEvent e) {
JLabel selectedLabel = (JLabel) e.getSource();
if (selectedLabel.getIcon() == blankIcon) {
return; // don't want to move the blank icon
}
// index variables to hold selected and blank JLabel's index location
int selectedIndex = -1;
int blankIndex = -1;
for (int i = 0; i < labelList.size(); i++) {
if (selectedLabel == labelList.get(i)) {
selectedIndex = i;
} else if (labelList.get(i).getIcon() == blankIcon) {
blankIndex = i;
}
}
// get row and column of selected JLabel
int row = selectedIndex / SIDES;
int col = selectedIndex % SIDES;
// get row and column of blank JLabel
int blankRow = blankIndex / SIDES;
int blankCol = blankIndex % SIDES;
if (isMoveValid(row, col, blankRow, blankCol)) {
Icon selectedIcon = selectedLabel.getIcon();
labelList.get(selectedIndex).setIcon(blankIcon);
labelList.get(blankIndex).setIcon(selectedIcon);
// test for win here by comparing icons held by labelList
// with the solutionList
}
}
private boolean isMoveValid(int row, int col, int blankRow, int blankCol) {
// has to be on either same row or same column
if (row != blankRow && col != blankCol) {
return false;
}
// if same row
if (row == blankRow) {
// then columns must be off by 1 -- they're next to each other
return Math.abs(col - blankCol) == 1;
} else {
// or else rows off by 1 -- above or below each other
return Math.abs(row - blankRow) == 1;
}
}
public void shuffle() {
Collections.shuffle(iconList);
for (int i = 0; i < labelList.size(); i++) {
labelList.get(i).setIcon(iconList.get(i));
}
}
}
private void fillIconList(BufferedImage img) {
// get the width and height of each individual icon
// which is 1/3 the image width and height
int w = img.getWidth() / SIDES;
int h = img.getHeight() / SIDES;
for (int row = 0; row < SIDES; row++) {
int y = (row * img.getWidth()) / SIDES;
for (int col = 0; col < SIDES; col++) {
int x = (col * img.getHeight()) / SIDES;
// create a sub image
BufferedImage subImg = img.getSubimage(x, y, w, h);
// create icon from the image
Icon icon = new ImageIcon(subImg);
// add to both icon lists
iconList.add(icon);
solutionList.add(icon);
}
}
// create a blank image and corresponding icon as well.
BufferedImage blankImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
blankIcon = new ImageIcon(blankImg);
iconList.remove(iconList.size() - 1); // remove last icon from list
iconList.add(blankIcon); // and swap in the blank one
solutionList.remove(iconList.size() - 1); // same for the solution list
solutionList.add(blankIcon);
}
private static void createAndShowGui(BufferedImage img) {
JFrame frame = new JFrame("ImageShuffle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ImageShuffle(img));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
URL imgUrl = null;
BufferedImage img;
try {
imgUrl = new URL(IMG_PATH);
img = ImageIO.read(imgUrl);
SwingUtilities.invokeLater(() -> createAndShowGui(img));
} catch (IOException e) {
e.printStackTrace();
}
}
}
If I wanted animation, again, I'd raise the icon into the JFrame's glasspane, animate it to the new position using a Swing Timer, and then place the icon into the new JLabel. I'd also disable the MouseListener using a boolean field, a "flag", until the animation had completed its move.
I made a GUI TicTacToe game years ago and wanted to redo it since I now have more programming skills. I was able to shrink the code from 600 lines to around 150 lines.
While I used the same scheme, I ran into some problems that I couldn't solve myself, so please help me out.
The program consists of two classes, the main class TTTMain:
public class TTTMain {
public static void main(String[] args) {
TTTFrame tttf = new TTTFrame(0,0);
/*Tic Tac Toe Field:
* 0 1 2
* 3 4 5
* 6 7 8
*/
}}
And TTTFrame:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TTTFrame extends JFrame implements ActionListener {
private Button[] btnPlayButton;
private Button btnRestart;
private int buttonCounter;
private int xScore;
private int oScore;
private Label Olabel, Xlabel;
TTTFrame(int xScore, int oScore) {
this.xScore = xScore;
this.oScore = oScore;
btnPlayButton = new Button[9];
for (int i = 0; i < 9; i++) {
btnPlayButton[i] = new Button("" + i);
btnPlayButton[i].setBackground(Color.white);
btnPlayButton[i].setForeground(Color.white);
btnPlayButton[i].addActionListener(this);
this.add(btnPlayButton[i]);
}
Xlabel = new Label("X: " + this.xScore);
Xlabel.setFont(new Font("Arial", Font.BOLD, 24));
Xlabel.setForeground(Color.white);
Xlabel.setBackground(Color.black);
this.add(Xlabel);
btnRestart = new Button("Restart");
btnRestart.setActionCommand("Restart");
btnRestart.setFont(new Font("Arial", Font.PLAIN, 18));
btnRestart.addActionListener(this);
this.add(btnRestart);
Olabel = new Label("O: " + this.oScore);
Olabel.setFont(new Font("Arial", Font.BOLD, 24));
Olabel.setForeground(Color.white);
Olabel.setBackground(Color.black);
this.add(Olabel);
this.setLayout(new GridLayout(4, 3));
this.pack();
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Tic Tac Toe");
this.setSize(300, 400);
this.getContentPane().setBackground(Color.black);
this.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Restart")) {
System.out.println("Restarted");
for (int i = 0; i < 9; i++) {
btnPlayButton[i].setLabel("" + i);
btnPlayButton[i].setForeground(Color.white);
btnPlayButton[i].setBackground(Color.white);
btnPlayButton[i].addActionListener(this);
this.buttonCounter = 0;
}
} else {
((Button) e.getSource()).setFont(new Font("Arial", Font.BOLD, 48));
((Button) e.getSource()).setForeground(Color.black);
System.out.println(buttonCounter);
if (buttonCounter % 2 == 0) {
((Button) e.getSource()).setLabel("X");
((Button) e.getSource()).removeActionListener(this);
} else {
((Button) e.getSource()).setLabel("O");
((Button) e.getSource()).removeActionListener(this);
}
buttonCounter++;
CheckField();
}
}
private void CheckField() {
if (ButtonsWithIdenticalLabels(0, 1, 2)) {
Deactivatebuttons();
}
if (ButtonsWithIdenticalLabels(3, 4, 5)) {
Deactivatebuttons();
}
if (ButtonsWithIdenticalLabels(6, 7, 8)) {
Deactivatebuttons();
}
if (ButtonsWithIdenticalLabels(0, 3, 6)) {
Deactivatebuttons();
}
if (ButtonsWithIdenticalLabels(1, 4, 7)) {
Deactivatebuttons();
}
if (ButtonsWithIdenticalLabels(2, 5, 8)) {
Deactivatebuttons();
}
if (ButtonsWithIdenticalLabels(0, 4, 8)) {
Deactivatebuttons();
}
if (ButtonsWithIdenticalLabels(2, 4, 6)) {
Deactivatebuttons();
}
}
private boolean ButtonsWithIdenticalLabels(int i, int j, int k) {
if (btnPlayButton[i].getLabel() == btnPlayButton[j].getLabel()
&& btnPlayButton[j].getLabel() == btnPlayButton[k].getLabel()) {
btnPlayButton[i].setBackground(Color.red);
btnPlayButton[j].setBackground(Color.red);
btnPlayButton[k].setBackground(Color.red);
if (btnPlayButton[i].getLabel().equals("X")) {
xScore++;
Xlabel.setText("X: " + xScore);
} else {
oScore++;
Olabel.setText("O: " + oScore);
}
return true;
} else {
return false;
}
}
private void Deactivatebuttons() {
for (int i = 0; i < 9; i++) {
btnPlayButton[i].removeActionListener(this);
}
}
}
Now let me explain how the program works. The 3x3 playing field is made of the ButtonArray btnPlayButton. The buttons are compared by their Labels, so to not have matching labels at the start of the game the buttons are labeled from 1 to 9 right when they are created. Here:
for (int i = 0; i < 9; i++) {
btnPlayButton[i] = new Button("" + i); // Right here
btnPlayButton[i].setBackground(Color.white);
btnPlayButton[i].setForeground(Color.white);
btnPlayButton[i].addActionListener(this);
this.add(btnPlayButton[i]);
}
Whenever you click a btnPlayButton , the program jumps into the actionPerformed method. Since the btnPlayButtons don't have an ActionCommand , it jumps right into the else section of the method. Here, int buttonCounter gets greater by 1. Wether buttonCounter is even or odd, the btnPlayButton that got clicked gets relabeled with "X" or "O". Since buttonCounter gets +1 with every click, the X and Os are alternating.
Here is said section:
else {
((Button) e.getSource()).setFont(new Font("Arial", Font.BOLD, 48));
((Button) e.getSource()).setForeground(Color.black);
System.out.println(buttonCounter);
if (buttonCounter % 2 == 0) {
((Button) e.getSource()).setLabel("X");
((Button) e.getSource()).removeActionListener(this);
} else {
((Button) e.getSource()).setLabel("O");
((Button) e.getSource()).removeActionListener(this);
}
buttonCounter++;
CheckField();
}
The ActionListener of the clicked Button are removed to prevent cheating. With every buttonpress, the playing field is checked for a winning combination. This happens in CheckField().
In CheckField(), or to be more precisely, ButtonsWithIdenticalLabels(x, y, z) the labels of btnPlayButtons[x], btnPlayButtons[y], btnPlayButtons[z] are taken and compared, if they are identical it returns true.
Since the btnPlayButton are ordered like this:
0 1 2
3 4 5
6 7 8
the winning combinations are: 012,345,678,036,147,258,045 and 246
so, for example, when btnPlayButton[0], btnPlayButton[1] and btnPlayButton[2] all have the same label. ButtonsWithIdenticalLabels is true and the program jumps into Deactivatebuttons() where all the btnPlayButton are getting disabled meaning a winning combination was found and the game is over. If the label of btnPlayButton[1] is "X" then int xScore gets 1 added to it. Also btnPlayButton[0], btnPlayButton[1] and btnPlayButton[2] get painted red for aesthetics.
With the Restart button you jump into a for loop that relabels the btnPlayButton again and adds them the ActionListener that is implemented into TTTFrame. buttonCounter is getting resetted to 0 as well. The relabeling is the same as the one in the beginning of the class:
if (e.getActionCommand().equals("Restart")) {
System.out.println("Restarted");
for (int i = 0; i < 9; i++) {
btnPlayButton[i].setLabel("" + i);
btnPlayButton[i].setForeground(Color.white);
btnPlayButton[i].setBackground(Color.white);
btnPlayButton[i].addActionListener(this);
this.buttonCounter = 0;
}
Now the problem is that I have is, that after a few restarts, the labeling of X and O isn't alternating anymore. Sometimes there are 3 Os in a row and sometimes even Fields like this are getting recognized as a win
Picture
If someone knows how to fix this bug I'd be really happy.
Thanks in advance,
Fihdi
The problem here is: When you restart the game, a new ActionListener is added to every button. However, it's only removed when you either click it or when someone wins the game. That means when you restart a game before anyone won, every non-clicked button gets a second ActionListener, so the click will be registered twice and this bug appears. Try to call DeactivateButtons() before you reset the board.
I am attempting to overlay the text on a JButton over an ImageIcon that is behind it. However, when the imageIcon is added, the text dissapears. Is there any way to specify the order in which it displays?
Below, i have tried to separately add the images and text to see if that would affect it, but no luck.
Can anyone help me out?
private void initButtons() {
int locationX = 0, locationY = 525;
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
boardArray[x][y] = new ChessButton();
boardArray[x][y].setSize(75, 75);
boardArray[x][y].setLocation(locationX, locationY);
boardArray[x][y].setXAndY(x, y);
if ((x % 2 == 0 && y % 2 == 1) || (x % 2 == 1 && y % 2 == 0)) {
boardArray[x][y].setColour("white");
boardArray[x][y].setIcon(new ImageIcon("Assets/white_null_null.png"));
} else {
boardArray[x][y].setColour("black");
boardArray[x][y].setIcon(new ImageIcon("Assets/black_null_null.png"));
}
//this adds the images in an alternating pattern
chessFrame.add(boardArray[x][y]);
locationX = locationX + 75;
}
locationX = 0;
locationY = locationY - 75;
}
}
void initPieces() {
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
if ((x % 2 == 0 && y % 2 == 1) || (x % 2 == 1 && y % 2 == 0)) {
boardArray[x][y].setFont(new Font("Arial Unicode MS", Font.PLAIN, 40));
boardArray[x][y].setText("\u2654");//sets a particular chess piece as text, just testing it now.
} else {
boardArray[x][y].setFont(new Font("Arial Unicode MS", Font.PLAIN, 40));
boardArray[x][y].setText("\u2654");//sets a particular chess piece as text, just testing it now.
//this is suposed to overlay the image over the text, but it is not.
}
}
}
}
First of all, I see you're calling this method: boardArray[x][y].setLocation(locationX, locationY);
.setLocation(...) indicates you're using a null layout, please read Null layout is evil and Why is it frowned upon to use a null layout in Swing? to know why you should avoid its use.
For creating a Chess board, I'd be using GridLayout, please read how to use the different layout managers
Now, to overlay text over the icon, you only need to call JButton#setHorizontalAlignment(...) method and pass SwingConstants.CENTER as the parameter.
For example:
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
public class ButtonWithImageAndText {
private JFrame frame;
private JButton button;
private Icon icon;
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new ButtonWithImageAndText().createAndShowGui());
}
private void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
try {
icon = new ImageIcon(ImageIO.read(getClass().getResource("up.jpg")));
} catch (IOException e) {
e.printStackTrace();
}
button = new JButton();
button.setIcon(icon);
button.setText("Click me!");
button.setHorizontalTextPosition(SwingConstants.CENTER);
button.setFont(new Font("Arial", Font.PLAIN, 40));
button.setForeground(Color.RED);
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Which gives the following output:
For your future questions, please read how to make a valid Minimal, Complete and Verifiable Example (MCVE) that demonstrates your issue and your best try to solve it yourself, the code I posted is a good example of it, as you can copy-paste it and see the same result as I
Use the setComponentZOrder(...) method of the Container class. This will enable you to set the Z-index of the components to set them in what ever order you like. Look also this
I have successfully got a checkerboard to be put together using colored panels, but only when the user will input odd numbers for the rows and columns. Otherwise when inputing even numbers it just shows alternating colored columns. I'm struggling to figure out how to write a short segment that checks to see if it's odd or even by using the %2=0, with the result of even changing the color. Below is my code. Thanks, and take it easy on me I'm very new to programming! :-)
Also, I've created a separate ColorPanel class to build the colored panel, and then pull into into my main program. I didn't bother putting that code below.
import javax.swing.*;
import java.awt.*;
public class Checkerboard extends JPanel{
public static void main(String[] args) {
JFrame chBoard = new JFrame();
chBoard.setTitle("Checkerboard");
chBoard.setSize(800,800);
chBoard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String inputStr = JOptionPane.showInputDialog("Number of rows", "5");
if (inputStr == null) return;
int row = Integer.parseInt(inputStr);
inputStr = JOptionPane.showInputDialog("Number of columns", "5");
if (inputStr == null) return;
int col = Integer.parseInt(inputStr);
Container pane = chBoard.getContentPane();
pane.setLayout(new GridLayout(row, col));
Color BoxColor = Color.red;
for ( int counter = 1; counter <= row * col; counter++ )
{
if (BoxColor == Color.red)
BoxColor = Color.black;
else
BoxColor = Color.red;
ColorPanel panel = new ColorPanel(BoxColor);
pane.add(panel);
}
chBoard.setVisible(true);
}
}
Change your loop to:
for ( int x = 0; x < row; x++ ) {
for(int y = 0; y < col; y++) {
if((x + y)%2 == 0) {
BoxColor = Color.red;
} else {
BoxColor = Color.black;
}
...
}
}
Like I said, I'm new to programming but I'm really enjoying the learning experience. I hope this helps other people in their learning experience.
Anyways, I suppose I created more work for myself with the separate ColorPanel class. So instead of creating a separate ColorPanel class to build the colored panel, I just changed it to use the preexisting JPanel class to create the panel inside the main program. So instead of:
ColorPanel panel = new ColorPanel(BoxColor);
+ the ColorPanel class...
I put:
JPanel panel = new JPanel();
panel.setBackground(BoxColor);
within the main program and deleted the additional ColorPanel class.
Sorry for the redundancy, just wanting to explain myself clearly.
Also, thanks to Jason he really helped me figure out the idea of using the two
int x & y
to count the
row & col
and then add them together which enabled me to use the
%2=0
to figure out whether I was on the odd or even panel.
Hope this helps someone! :-)
Final code looks like this:
import javax.swing.*;
import java.awt.*;
public class Checkerboard extends JPanel{
public static void main(String[] args) {
JFrame chBoard = new JFrame();
chBoard.setTitle("Checkerboard");
chBoard.setSize(800,800);
chBoard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String inputStr = JOptionPane.showInputDialog("Number of rows", "5");
if (inputStr == null) return;
int row = Integer.parseInt(inputStr);
inputStr = JOptionPane.showInputDialog("Number of columns", "5");
if (inputStr == null) return;
int col = Integer.parseInt(inputStr);
Container pane = chBoard.getContentPane();
pane.setLayout(new GridLayout(row, col));
Color BoxColor = Color.red;
for ( int x = 0; x < row; x++ ) {
for(int y = 0; y < col; y++) {
if((x + y)%2 == 0) {
BoxColor = Color.red;}
else{
BoxColor = Color.black;}
JPanel panel = new JPanel();
panel.setBackground(BoxColor);
pane.add(panel);
}
chBoard.setVisible(true);
}
}
}
I'm fairly new to GUI programming with swing and am having what I'm sure is a noob problem.
I've created a JFrame with a JPanel inside of it. Then I'm trying to add a JLabel for each element in an array. The problem is that the elements are not appearing on the panel. I've checked to make sure that the array elements are registering using a println statement, so that's not the problem. I'm guessing that I'm missing a statement somewhere... please advise.
Here's my code:
public class MazeFrame extends javax.swing.JFrame {
Maze m;
/**
* Creates new form MazeFrame
*/
public MazeFrame(Maze m) {
this.m = m;
setSize(new Dimension(800, 600));
JPanel pan = new JPanel();
add(pan);
setVisible(true);
// pan.setBackground(Color.yellow);
pan.setLayout(new GridLayout(m.width, m.height));
for (int curr = 0; curr < m.height; curr++){
for (Cell c: m.maze[curr]){
JLabel lab = new JLabel();
switch (c.state){
case border:
lab.setBackground(Color.black);
System.out.println("addedborder");
break;
case wall:
lab.setBackground(Color.DARK_GRAY);
System.out.println("addedwall");
break;
case open:
lab.setBackground(Color.LIGHT_GRAY);
System.out.println("addedopen");
break;
case travelled:
lab.setBackground(Color.RED);
}
lab.setSize(new Dimension(50, 50));
lab.setVisible(true);
pan.add(lab);
// System.out.println("added");
}
}
pan.revalidate();
pan.repaint();
}
}
Here's the maze class:
package robots;
import java.util.Random;
public class Maze {
public Cell[][] maze;
final int width;
final int height;
public Maze(){
width = 20;
height = 20;
maze = new Cell[width][height];
for (int row = 0; row < height; row++){
for (int col = 0; col < width; col++){
maze[row][col] = new Cell(row, col);
}
}
// set borders
for (int curr = 0; curr < height; curr++) {
maze[0][curr].setState("border");
maze[curr][0].setState("border");
maze[height - 1][curr].setState("border");
maze[curr][width - 1].setState("border");
}
// initially mark all cells as walls
for (int row = 1; row < height - 1; row++) {
for (int col = 1; col < width - 1; col++) {
maze[row][col].setState("wall");
}
}
}
private boolean isValidTurn(int row, int col) {
if (row >= 0 && col < width && col > 0 &&
row < 20 && (!this.maze[row][col].getState().matches("open"))) {
return true;
}
return false;
}
public void makeRoute() {
Random r = new Random();
int row = 0;
int col = r.nextInt(width);
maze[row][col].setState("open");
row = row+1;
maze[row][col].setState("open");
// System.out.println(this);
while (row < (this.height - 1)) {
// Assuming the mouse moves in only 3 directions left right or down
// in the maze. 0 indicates left turn 1 indicates right turn and
// 2 indicates down movement in the maze.
int nextDir = r.nextInt(3);
switch (nextDir) {
case 0: // left turn
if (this.isValidTurn(row, (col - 1))) {
--col;
this.maze[row][col].setState("open");
}
break;
case 1: // right turn
if (this.isValidTurn(row, (col + 1))) {
++col;
this.maze[row][col].setState("open");
}
break;
case 2: // down movement
++row;
this.maze[row][col].setState("open");
break;
}
System.out.println("turn : " + nextDir);
// System.out.println(this);
}
System.out.println(this);
}
}
class Cell {
int row;
int col;
int above, below, toLeft, toRight;
enum state {border, wall, open, travelled};
state state;
public Cell(int row, int col){
this.row = row;
this.col = col;
above = row + 1;
below = row -1;
toLeft = col -1;
toRight = col +1;
}
#Override
public String toString(){
String out = new String();
if (state == state.border) {
out = "0";
}
if (state == state.wall) {
out = "#";
}
if (state == state.open) {
out = ".";
}
if (state == state.open) {
out = "-";
}
return out;
}
public void setState(String toSet){
switch (toSet){
case "border":
state = state.border;
break;
case "wall":
state = state.wall;
break;
case "open":
state = state.open;
break;
case "travelled":
state = state.travelled;
break;
}
}
public String getState() {
return state.toString();
}
}
But, as I said, I know that the maze class works fine, because it outputs to the console perfectly when I run it. Also, the println statements in the MazeFrame class show that each cell is registering with its respective state.
See comments in the code:
public class MazeFrame extends javax.swing.JFrame {
Maze m;
/**
* Creates new form MazeFrame
*/
public MazeFrame(Maze m) {
this.m = m;
// Don't manually set the size of a frame. Let the preferred size of you components determine the size.
// This is done by invoking pack() after all components have been added to the frame.
// setSize(new Dimension(800, 600));
JPanel pan = new JPanel();
add(pan);
// setVisible(true); // do after all components added.
// pan.setBackground(Color.yellow);
pan.setLayout(new GridLayout(m.width, m.height));
for (int curr = 0; curr < m.height; curr++){
for (Cell c: m.maze[curr]){
JLabel lab = new JLabel();
lab.setOpaque(true); // as suggested by MadProgrammer
switch (c.state){
case border:
lab.setBackground(Color.black);
System.out.println("addedborder");
break;
case wall:
lab.setBackground(Color.DARK_GRAY);
System.out.println("addedwall");
break;
case open:
lab.setBackground(Color.LIGHT_GRAY);
System.out.println("addedopen");
break;
case travelled:
lab.setBackground(Color.RED);
}
// Set the preferred size so layout managers can do there job
// lab.setSize(new Dimension(50, 50));
lab.setPreferredSize(new Dimension(50, 50));
// Not required. This is the default for all components except top level containers like JFrame, JDialog
// lab.setVisible(true);
pan.add(lab);
// System.out.println("added");
}
}
// No neeed to revalidate or repaint because the frame is not visible yet
// pan.revalidate();
// pan.repaint();
pack(); // let the layout manager determine the size of the frame
setVisible(); // show the frame
}
}
Note: Normally you don't even need to set the preferred size of a component because each component has a preferred size. But in this case you didn't add text or an Icon to the label so it won't have a preferred size.
I was able to get something to work (as to if it's correct is another matter)
Basically all I did (apart from moving setVisible to the end of the constructor) was make the lab opaque...
public TestMaze(Maze m) {
this.m = m;
setSize(new Dimension(800, 600));
JPanel pan = new JPanel();
add(pan);
pan.setLayout(new GridLayout(m.width, m.height));
for (int curr = 0; curr < m.height; curr++) {
for (Cell c : m.maze[curr]) {
JLabel lab = new JLabel();
lab.setOpaque(true); // <-- Add me...
switch (c.state) {
case border:
lab.setBackground(Color.black);
break;
case wall:
lab.setBackground(Color.DARK_GRAY);
break;
case open:
lab.setBackground(Color.LIGHT_GRAY);
break;
case travelled:
lab.setBackground(Color.RED);
}
lab.setSize(new Dimension(50, 50));
lab.setVisible(true);
pan.add(lab);
// System.out.println("added");
}
}
setVisible(true);
}
As i know you should call revalidate/repaint when you add components to any visible container, so you could move your setVisible & adding panel to frame to last statement:
// update panel
pan.revalidate();
pan.repaint();
// adding panel to frame
this.add(pan);
this.pack();
this.setVisible(true);
the other thing you don't need to call JLabel.setVisible(true) because its the default, also to change the background of JLabel you need to add (because its transparent by default):
lab.setOpaque(true);