Reputation: 77
So, essentially I'm making memory. I have a tile class, which at this point will only have a number, and a boolean (to show if it is flipped or not). I made duplicates of all the numbers as Tile objects and randomized them, and in turn added them to my layout.
Basically, I need them to show up on my screen as JLabels, and I'm not exactly sure how to do this? Should I be extending something else, or do something special in my Tile class? Or is it a problem with my logic elsewhere? Here's my Tile class at this point (very little)
PS. I also need to use a timer for the flippy flop
class Tile extends JLabel implements ActionListener{
boolean flipped;
int imageNum;
ImageIcon image;
JLabel card;
Tile(int num, boolean f)
{
imageNum=num;
flipped=f;
card=new JLabel(""+imageNum);
}
public void flipCard()
{
}
public void actionPerformed(ActionEvent a)
{
}
Any help would be greatly appreciated! EDIT:
Here's the main class where I try to add my tiles
JPanel gridPanel = new JPanel(new GridLayout(gridSize, gridSize));
Tile[][]tiles=new Tile[(gridSize*gridSize)/2][2];
boolean[][]tilePlaced=new boolean[(gridSize*gridSize)/2][2];
JLabel[][] cardsOnGrid = new JLabel[gridSize][gridSize];
for(int i=0;i<gridSize;i++)
{
for(int j=0;j<gridSize;j++)
cardsOnGrid[i][j]=new JLabel("");
}
for(int i=0; i<((gridSize*gridSize)/2);i++)
{
tiles[i][0]= new Tile(i, true);
tiles[i][1]= new Tile(i, true);
tilePlaced[i][0]=false;
tilePlaced[i][1]=false;
}
for(int i=0; i<gridSize;i++)
{
for(int j=0;j<gridSize;j++)
{
int tileNum = tileRandom(gridSize);
int tileNumVer = (int)Math.floor(Math.random()*2);
while(tilePlaced[tileNum][tileNumVer]==true)
{
tileNum = tileRandom(gridSize);
tileNumVer = (int)Math.floor(Math.random()*2);
}
gridPanel.add(tiles[tileNum][tileNumVer]);
tilePlaced[tileNum][tileNumVer]=true;
}
:'(
Upvotes: 0
Views: 1778
Reputation: 22972
Tile(int num, boolean f){
imageNum=num;
flipped=f;
card=new JLabel(""+imageNum);//1. Remove this line and card Label
super.setText(String.valueOf(num));//ADD This line
}
Just create Object
of Tile
Tile myLabel = new Tile(10,false);//this will create lable
Because your Tile
class is subClass of JLabel
and no need to create Label
in Tile
class as Tile
itself is a Label
.Apart from that you won't get action events for JLabel
.It's not a clickable element use MouseListener
.
Upvotes: 1