I am building a Sudoku game. I have drawn a grid so far and programmed the selection of a field, but my chosen picture for selection does not appear. My Class for the selector is:
package com.brendenbunker;
import javax.swing.*;
public class Selection {
public JLabel boxSelected;
public ImageIcon selected;
int x, y;
public Selection(){
x = 0;
y = 0;
selected = new ImageIcon(getClass().getResource("/Selected.png"));
boxSelected = new JLabel("");
boxSelected.setIcon(selected);
boxSelected.setBounds((x * (selected.getIconWidth() + 4) + (x / 3) * 4) + 4, (y * (selected.getIconWidth() + 4) + (y / 3) * 4) + 4, selected.getIconWidth(), selected.getIconHeight());
}
public Selection(int x, int y){
this.x = x;
this.y = y;
selected = new ImageIcon(getClass().getResource("/Selected.png"));
boxSelected = new JLabel("");
boxSelected.setIcon(selected);
boxSelected.setBounds((x * (selected.getIconWidth() + 4) + (x / 3) * 4) + 4, (y * (selected.getIconWidth() + 4) + (y / 3) * 4) + 4, selected.getIconWidth(), selected.getIconHeight());
}
public void setNewSelection(int x, int y) {
this.x = x;
this.y = y;
boxSelected.setBounds((x * (selected.getIconWidth() + 4) + (x / 3) * 4) + 4, (y * (selected.getIconWidth() + 4) + (y / 3) * 4) + 4, selected.getIconWidth(), selected.getIconHeight());
}
}
The code which displays everything is:
package com.brendenbunker;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class ScreenGenerator extends JFrame{
//Intro Components
//JLabel temp;
JLabel[] gridLabel, numbLabel, numbBackLabel;
JLabel[][] numbDisp;
ImageIcon gridPic, numbPic, numbBackPic;
Rectangle[][] boxArea;
Selection selection;
Random random;
//intro Vars
public ScreenGenerator() {
setLayout(null);
random = new Random();
selection = new Selection();
gridPic = new ImageIcon(getClass().getResource("/Grid_Unified.png"));
numbBackPic = new ImageIcon(getClass().getResource("/Square.png"));
gridLabel = new JLabel[9];
numbLabel = new JLabel[9];
numbBackLabel = new JLabel[9];
boxArea = new Rectangle[9][9];
numbDisp = new JLabel[9][9];
for (int i=0; i<9; i++) {
gridLabel[i] = new JLabel("");
gridLabel[i].setBounds(((i+1)%3)*gridPic.getIconWidth(),Math.round(i/3)*gridPic.getIconHeight(),gridPic.getIconWidth(),gridPic.getIconHeight());
numbBackLabel[i] = new JLabel("");
numbBackLabel[i].setBounds(i*numbBackPic.getIconWidth()+1,gridPic.getIconHeight()*3,numbBackPic.getIconWidth(),numbBackPic.getIconHeight());
numbLabel[i] = new JLabel("");
numbLabel[i].setBounds(i*numbBackPic.getIconWidth(),gridPic.getIconHeight()*3,numbBackPic.getIconWidth(),numbBackPic.getIconHeight());
for (int j=0; j<9; j++) {
numbDisp[i][j] = new JLabel("");
numbDisp[i][j].setBounds((j * (selection.selected.getIconWidth() + 4) + (j / 3) * 4) + 4, (i * (selection.selected.getIconWidth() + 4) + (i / 3) * 4) + 4, selection.selected.getIconWidth(), selection.selected.getIconHeight());
boxArea[j][i] = new Rectangle((j*(selection.selected.getIconWidth()+4)+(j/3)*4)+4,(i*(selection.selected.getIconWidth()+4)+(i/3)*4)+4,selection.selected.getIconWidth(),selection.selected.getIconHeight());
add(numbDisp[i][j]);
}
}
for (int i=0; i<9; i++) {
numbPic = new ImageIcon(getClass().getResource("/numb_" + (i+1) + ".png"));
numbLabel[i].setIcon(numbPic);
gridLabel[i].setIcon(gridPic);
numbBackLabel[i].setIcon(numbBackPic);
add(selection.boxSelected);
add(gridLabel[i]);
add(numbLabel[i]);
add(numbBackLabel[i]);
}
setBoxNumb(random.nextInt(9)+1,random.nextInt(9)+1,random.nextInt(9)+1);
selection.setNewSelection(1,2);
}
public void setBoxNumb(int x, int y, int numb){
numbPic = new ImageIcon(getClass().getResource("/numb_" + numb + ".png"));
numbDisp[x - 1][y - 1].setIcon(numbPic);
}
}
So what I am trying to ask is why the image I want to be displayed if a field is selected does not appear ? Does anyone know how to fix this ?
What I found is that the layout layers are backwards to what i would expect. The Label you add first will always stay on top. That label won't be drawn over by JComponents add after the original is on. So basically, the earlier in the code you add a component to a JFrame, the Higher the priority that Component has.