problem with the MouseListener
I built a game that built an matrix of labels i add the labels an mouselistener and The problem is that when I press event on a
particular label, say there is map[0][0] It create an event no matter what I click .... How do I create a matrix of labels when I do certain conditions on each label was only then that event will happen
sorry about my english :)
this is my code:
Code :
public class Board
{
private int x;
private int y;
private Point location;
private ImageIcon picture;
private JLabel label;
private Piece[][] map;
public Board()
{
this.x = 47;
this.y = 45;
this.location = new Point();
this.map = new Piece[9][12];
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 12; j++)
{
this.map[i][j] = new Piece(new Point(), null, new JLabel(), 0, false);
}
}
}
public Piece[][] getMap()
{
return this.map;
}
public void setMap(Piece[][] map)
{
this.map = map;
}
public void draw(Panelmenu panel)
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 12; j++)
{
this.location.setX(this.x);
this.location.setY(this.y);
System.out.println("[" + i + ", " + j + "] = [X=" + this.x +", Y=" + this.y + "]");
this.map[i][j].setLocation(this.location);
System.out.println("[" + i + ", " + j + "] = [Point = " + this.map[i][j].getLocation() + "]");
this.picture = new ImageIcon(getClass().getResource("images/stone.png"));
this.map[i][j].setPicture(this.picture);
this.label = this.map[i][j].getLabel();
this.label.setText("");
this.label.setIcon(this.map[i][j].getPicture());
this.label.setHorizontalAlignment(JLabel.CENTER);
this.label.setSize(58, 58);
this.label.setLocation(this.x, this.y);
this.map[i][j].setLabel(this.label);
this.map[i][j].setType(0);
this.map[i][j].setStatus(true);
this.x += 62;
if (j == 11)
{
this.x = 47;
this.y += 59;
}
this.map[i][j].draw(panel);
}
}
}
}
this is the Piece class:
Code :
public class Piece implements MouseListener
{
private Point location;
private ImageIcon picture;
private JLabel label;
private int type;
private boolean status;
public Piece(Point location, ImageIcon picture, JLabel label, int type, boolean status)
{
this.location = location;
this.picture = picture;
this.label = label;
this.type = type;
this.status = status;
this.label.addMouseListener(this);
}
public Point getLocation()
{
return this.location;
}
public void setLocation(Point location)
{
this.location = location;
System.out.println("Piece = " + this.location);
}
public ImageIcon getPicture()
{
return this.picture;
}
public void setPicture(ImageIcon picture)
{
this.picture = picture;
}
public JLabel getLabel()
{
return this.label;
}
public void setLabel(JLabel label)
{
this.label = label;
}
public int getType()
{
return this.type;
}
public void setType(int type)
{
this.type = type;
}
public boolean getStatus()
{
return this.status;
}
public void setStatus(boolean status)
{
this.status = status;
}
public void draw(JPanel panel)
{
panel.add(this.label);
}
@Override
public void mouseClicked(MouseEvent e)
{
System.out.println("[" + this.location.getX() + ", " + this.location.getY() + "]");
}
Re: problem with the MouseListener
Your English is fine, but what exactly is the problem?
You're adding a MouseListener to every JLabel, so it's going to print out the location no matter what. If you only want certain JLabels to respond to the MouseListener, either don't add the MouseListener to them or add some logic to the mouseClicked() method.
Also, since each JLabel's MouseListener is doing the same thing, why do you create a new instance for each JLabel? Why not just use a single instance of your MouseListener?
Re: problem with the MouseListener
my game is like the game exagon know that game?
so i want that the board will be a matrix... i added printers because i want to know what is the pointers in evry piece and piece...
but i write me all the time the last x and y that changed...
a add an "if " that say if i`m clicking on map[0][0].jlabel write "bobo" ... but he does not refer to that "if"
if i am clicking on the label in map[0][1].jlabel he will also print "bobo"
how can i do that it will consider in the metrix and the clicks will happend depend the map[][]
Re: problem with the MouseListener
i added print because i want to print the points in evry piece... but he doesnt print the point... he prints me all the time the last x and y
of the last piece (map[8][11])
Re: problem with the MouseListener
Where do you get the x,y values you are printing?
Make sure you get a new Point object for each Piece. Do not continue to use only one Point object.
Re: problem with the MouseListener
i get the same value in evry labels
Code :
public class Board implements MouseListener
{
private int x;
private int y;
private Point location;
private ImageIcon picture;
private JLabel label;
private Piece[][] map;
public Board()
{
this.x = 47;
this.y = 45;
this.location = new Point();
this.map = new Piece[9][12];
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 12; j++)
{
this.map[i][j] = new Piece(new Point(), null, new JLabel(), 0, false);
}
}
}
public Piece[][] getMap()
{
return this.map;
}
public void setMap(Piece[][] map)
{
this.map = map;
}
public void draw(Panelmenu panel)
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 12; j++)
{
this.location.setX(this.x);
this.location.setY(this.y);
System.out.println("[" + i + ", " + j + "] = [X=" + this.x +", Y=" + this.y + "]");
this.map[i][j].setLocation(this.location);
System.out.println("[" + i + ", " + j + "] = [Point = " + this.map[i][j].getLocation() + "]");
this.picture = new ImageIcon(getClass().getResource("images/stone.png"));
this.map[i][j].setPicture(this.picture);
this.label = this.map[i][j].getLabel();
this.label.setText("");
this.label.setIcon(this.map[i][j].getPicture());
this.label.setHorizontalAlignment(JLabel.CENTER);
this.label.setSize(58, 58);
this.label.setLocation(this.x, this.y);
this.label.addMouseListener(this);
this.map[i][j].setLabel(this.label);
this.map[i][j].setType(0);
this.map[i][j].setStatus(true);
this.x += 62;
if (j == 11)
{
this.x = 47;
this.y += 59;
}
this.map[i][j].draw(panel);
}
}
}
@Override
public void mouseClicked(MouseEvent arg0)
{
System.out.println("[" + map[0][0].getLocation().getX() + ", " +map[0][0].getLocation().getY()+ "]");
}
the first point is in map[0][0].getloction().getx() is x=47
but when i clicking on im i print that the x value in map[0][0]= 729
the last value x gets....
he does not give me the corect x
map[0][0].getLocation().getX() need to print 47 not 729
evry x and y on the map array is different so when i clicking on label i want that he will print the value that i set to the label.
Re: problem with the MouseListener
Quote:
i get the same value in evry labels
That sounds like all the Piece objects are using the SAME Point object.
Check your code to be sure that each Piece has it own UNIQUE Point object.
One way to check is to print out the reference to the Point object EVERY time it is set and changed.
The output should look something like: Point@e43541
The output for all of them should be different. If they are all the same, then all your Piece objects are sharing ONE Point object.
One easy way to test for this case, is to add a method to the Piece class that prints out the Point object. Write a loop after all the Piece objects have been created and filled with data that calls that method for all the Piece objects and look at what is printed. If all of them print the same value, them you know that you need to check why your code is giving the SAME Point object to each Piece object.
Re: problem with the MouseListener
i check`t the points and you right! all the points are the same...
where is the problem in may code because i adding the pieces to the panel ... way the draw is on the same point?
this is my code:
Code :
public class Board implements MouseListener
{
private int x;
private int y;
private Point location;
private ImageIcon picture;
private JLabel label;
private Piece[][] map;
public Board()
{
this.x = 47;
this.y = 45;
this.location = new Point();
this.map = new Piece[9][12];
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 12; j++)
{
this.map[i][j] = new Piece(new Point(), null, new JLabel(), 0, false);
}
}
}
public Piece[][] getMap()
{
return this.map;
}
public void setMap(Piece[][] map)
{
this.map = map;
}
public void draw(Panelmenu panel)
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 12; j++)
{
this.location.setX(this.x);
this.location.setY(this.y);
// System.out.println("[" + i + ", " + j + "] = [X=" + this.x +", Y=" + this.y + "]");
this.map[i][j].setLocation(this.location);
// System.out.println("[" + i + ", " + j + "] = [Point = " + this.map[i][j].getLocation() + "]");
this.picture = new ImageIcon(getClass().getResource("images/stone.png"));
this.map[i][j].setPicture(this.picture);
this.label = this.map[i][j].getLabel();
this.label.setText("");
this.label.setIcon(this.map[i][j].getPicture());
this.label.setHorizontalAlignment(JLabel.CENTER);
this.label.setSize(58, 58);
this.label.setLocation(this.x, this.y);
this.label.addMouseListener(this);
this.map[i][j].setLabel(this.label);
this.map[i][j].setType(0);
this.map[i][j].setStatus(true);
this.x += 62;
if (j == 11)
{
this.x = 47;
this.y += 59;
}
this.map[i][j].draw(panel);
}
}
check();
}
public void check()
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 12; j++)
{
System.out.println("[" + i + ", " + j + "] = [Point = " + this.map[i][j].getLocation() + "]");
}
}
}
the metod check is prints the points
this is the results:
0, 0] = [Point = Point [x=729, y=517]]
[0, 1] = [Point = Point [x=729, y=517]]
[0, 2] = [Point = Point [x=729, y=517]]
[0, 3] = [Point = Point [x=729, y=517]]
[0, 4] = [Point = Point [x=729, y=517]]
[0, 5] = [Point = Point [x=729, y=517]]
[0, 6] = [Point = Point [x=729, y=517]]
[0, 7] = [Point = Point [x=729, y=517]]
[0, 8] = [Point = Point [x=729, y=517]]
[0, 9] = [Point = Point [x=729, y=517]]
[0, 10] = [Point = Point [x=729, y=517]]
[0, 11] = [Point = Point [x=729, y=517]]
[1, 0] = [Point = Point [x=729, y=517]]
[1, 1] = [Point = Point [x=729, y=517]]
[1, 2] = [Point = Point [x=729, y=517]]
[1, 3] = [Point = Point [x=729, y=517]]
[1, 4] = [Point = Point [x=729, y=517]]
[1, 5] = [Point = Point [x=729, y=517]]
[1, 6] = [Point = Point [x=729, y=517]]
[1, 7] = [Point = Point [x=729, y=517]]
[1, 8] = [Point = Point [x=729, y=517]]
[1, 9] = [Point = Point [x=729, y=517]]
[1, 10] = [Point = Point [x=729, y=517]]
[1, 11] = [Point = Point [x=729, y=517]]
[2, 0] = [Point = Point [x=729, y=517]]
[2, 1] = [Point = Point [x=729, y=517]]
[2, 2] = [Point = Point [x=729, y=517]]
[2, 3] = [Point = Point [x=729, y=517]]
[2, 4] = [Point = Point [x=729, y=517]]
[2, 5] = [Point = Point [x=729, y=517]]
[2, 6] = [Point = Point [x=729, y=517]]
[2, 7] = [Point = Point [x=729, y=517]]
[2, 8] = [Point = Point [x=729, y=517]]
[2, 9] = [Point = Point [x=729, y=517]]
[2, 10] = [Point = Point [x=729, y=517]]
[2, 11] = [Point = Point [x=729, y=517]]
[3, 0] = [Point = Point [x=729, y=517]]
[3, 1] = [Point = Point [x=729, y=517]]
[3, 2] = [Point = Point [x=729, y=517]]
[3, 3] = [Point = Point [x=729, y=517]]
[3, 4] = [Point = Point [x=729, y=517]]
[3, 5] = [Point = Point [x=729, y=517]]
[3, 6] = [Point = Point [x=729, y=517]]
[3, 7] = [Point = Point [x=729, y=517]]
[3, 8] = [Point = Point [x=729, y=517]]
[3, 9] = [Point = Point [x=729, y=517]]
[3, 10] = [Point = Point [x=729, y=517]]
[3, 11] = [Point = Point [x=729, y=517]]
[4, 0] = [Point = Point [x=729, y=517]]
[4, 1] = [Point = Point [x=729, y=517]]
[4, 2] = [Point = Point [x=729, y=517]]
[4, 3] = [Point = Point [x=729, y=517]]
[4, 4] = [Point = Point [x=729, y=517]]
[4, 5] = [Point = Point [x=729, y=517]]
[4, 6] = [Point = Point [x=729, y=517]]
[4, 7] = [Point = Point [x=729, y=517]]
[4, 8] = [Point = Point [x=729, y=517]]
[4, 9] = [Point = Point [x=729, y=517]]
[4, 10] = [Point = Point [x=729, y=517]]
[4, 11] = [Point = Point [x=729, y=517]]
[5, 0] = [Point = Point [x=729, y=517]]
[5, 1] = [Point = Point [x=729, y=517]]
[5, 2] = [Point = Point [x=729, y=517]]
[5, 3] = [Point = Point [x=729, y=517]]
[5, 4] = [Point = Point [x=729, y=517]]
[5, 5] = [Point = Point [x=729, y=517]]
[5, 6] = [Point = Point [x=729, y=517]]
[5, 7] = [Point = Point [x=729, y=517]]
[5, 8] = [Point = Point [x=729, y=517]]
[5, 9] = [Point = Point [x=729, y=517]]
[5, 10] = [Point = Point [x=729, y=517]]
[5, 11] = [Point = Point [x=729, y=517]]
[6, 0] = [Point = Point [x=729, y=517]]
[6, 1] = [Point = Point [x=729, y=517]]
[6, 2] = [Point = Point [x=729, y=517]]
[6, 3] = [Point = Point [x=729, y=517]]
[6, 4] = [Point = Point [x=729, y=517]]
[6, 5] = [Point = Point [x=729, y=517]]
[6, 6] = [Point = Point [x=729, y=517]]
[6, 7] = [Point = Point [x=729, y=517]]
[6, 8] = [Point = Point [x=729, y=517]]
[6, 9] = [Point = Point [x=729, y=517]]
[6, 10] = [Point = Point [x=729, y=517]]
[6, 11] = [Point = Point [x=729, y=517]]
[7, 0] = [Point = Point [x=729, y=517]]
[7, 1] = [Point = Point [x=729, y=517]]
[7, 2] = [Point = Point [x=729, y=517]]
[7, 3] = [Point = Point [x=729, y=517]]
[7, 4] = [Point = Point [x=729, y=517]]
[7, 5] = [Point = Point [x=729, y=517]]
[7, 6] = [Point = Point [x=729, y=517]]
[7, 7] = [Point = Point [x=729, y=517]]
[7, 8] = [Point = Point [x=729, y=517]]
[7, 9] = [Point = Point [x=729, y=517]]
[7, 10] = [Point = Point [x=729, y=517]]
[7, 11] = [Point = Point [x=729, y=517]]
[8, 0] = [Point = Point [x=729, y=517]]
[8, 1] = [Point = Point [x=729, y=517]]
[8, 2] = [Point = Point [x=729, y=517]]
[8, 3] = [Point = Point [x=729, y=517]]
[8, 4] = [Point = Point [x=729, y=517]]
[8, 5] = [Point = Point [x=729, y=517]]
[8, 6] = [Point = Point [x=729, y=517]]
[8, 7] = [Point = Point [x=729, y=517]]
[8, 8] = [Point = Point [x=729, y=517]]
[8, 9] = [Point = Point [x=729, y=517]]
[8, 10] = [Point = Point [x=729, y=517]]
[8, 11] = [Point = Point [x=729, y=517]]
way? i dont know way
need help....
this is the piece class:
Code :
public class Piece
{
private Point location;
private ImageIcon picture;
private JLabel label;
private int type;
private boolean status;
public Piece(Point location, ImageIcon picture, JLabel label, int type, boolean status)
{
this.location = location;
this.picture = picture;
this.label = label;
this.type = type;
this.status = status;
//this.label.addMouseListener(this);
}
public Point getLocation()
{
return this.location;
}
public void setLocation(Point location)
{
this.location = location;
// System.out.println("Piece = " + this.location);
}
public ImageIcon getPicture()
{
return this.picture;
}
public void setPicture(ImageIcon picture)
{
this.picture = picture;
}
public JLabel getLabel()
{
return this.label;
}
public void setLabel(JLabel label)
{
this.label = label;
}
public int getType()
{
return this.type;
}
public void setType(int type)
{
this.type = type;
}
public boolean getStatus()
{
return this.status;
}
//System.out.println("[" + this.location.getX() + ", " + this.location.getY() + "]");
public void setStatus(boolean status)
{
this.status = status;
}
public void draw(JPanel panel)
{
panel.add(this.label);
}
}
Re: problem with the MouseListener
Where do you change the value of the Piece's Point? Look at those places. At one of them you are setting them to be the same. Remember that assigning the value of a reference to a object to another reference does not create a new object. It makes the two references point to the SAME object. Where in your code are you NOT creating a new Point object before using it to set a reference?