[英]How do I register two points in a mouselistener class in JPanel?
This one's been baffling me, I've a board and I want to be able to move a checker piece from one place to another.. I've set the frame to a grid layout, and each grid cell has a new JPanel that implements mouseListener.. how can I register the points start and end so that I can move the checker piece? 这个让我感到困惑,我有一块板子,我希望能够将一个棋子从一个地方移动到另一个地方。我已经将框架设置为网格布局,并且每个网格单元都有一个新的JPanel实现mouseListener ..我如何注册点开始和结束,以便我可以移动检查器块? I'm not able to save the point I'm at to a temp as it changes whenever I click another point.. 当我点击另一个点时,我无法保存我所处的温度点,因为它会发生变化。
Here's the code: 这是代码:
public class Tiles extends JPanel implements MouseListener {
Color c2, cc, tmp;
boolean isWhite, hasChecker, isHighlighted;
int i, j;
ArrayList<Point> al = new ArrayList<Point>();
ArrayList<Point> TempArray = new ArrayList<Point>();
Point start;
Point temp;
public Tiles(Color c, Point s){
this.setSize(75, 75);
this.setLayout(null);
this.addMouseListener(this);
this.c2 = c;
tmp = c2;
this.start = s;
}
public void setWhite(boolean isWhite){
this.isWhite = isWhite;
}
public void hasChecker(boolean hasChecker){
this.hasChecker = hasChecker;
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g2);
//if(isClicked == true)
// highlightPossibleMoves(start);
drawTile(g2);
if(hasChecker == true)
addCheckers(g2);
}
public void drawTile(Graphics2D g2){
g2.setColor(c2);
g2.fillRect(3, 3, 75, 75);
}
public void addCheckers(Graphics2D g2){
if(isWhite == true){
g2.setColor(Color.white);
}
else{
g2.setColor(Color.black);
}
Ellipse2D.Double circle = new Ellipse2D.Double(13, 11, 50, 50);
g2.fill(circle);
}
public boolean highlightPossibleMoves(Point start){
al = LOA.b.getPossibleMoves(start);
if(!al.isEmpty()){
for(int i = 0; i<al.size(); i++){
LOA.Jboard[al.get(i).getY()][al.get(i).getX()].c2 = Color.green;
LOA.Jboard[al.get(i).getY()][al.get(i).getX()].repaint();
}
return true;
}
return false;
}
public Point getThisPoint(){
return this.start;
}
public boolean isPossible(Point p){
if (TempArray.contains(p)){
System.out.println("contains");
return true;
}
return false;
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
if(isHighlighted = true){
LOA.initBoard();
}
if(highlightPossibleMoves(getThisPoint()) == true){
this.isHighlighted = true;
}
if(isPossible(getThisPoint()))
LOA.move(this.temp, getThisPoint());
else{
System.out.println("x =" + this.start.getX() + "y =" + this.start.getY());
this.temp = new Point(getThisPoint().getX(), getThisPoint().getY());
}
this.repaint();
this.validate();
}
}
I can give you business logic for the same: 我可以为您提供相同的业务逻辑:
This way you don't have to worry about saving a point but saving a selected component 这样您就不必担心保存点而是保存选定的组件
You need to have somewhere in your mousePressed(...) method, 你需要在mousePressed(...)方法中的某个地方,
if (start != null) { // second press
// second point pressed
//.... do some junk
start = null; then set start to null
} else { // first press
start = ...;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.