问题遇到的现象和发生背景
swing斗地主,发牌以后,排序重新设置坐标玩,重新绘制图片会堆叠,运行没问题
问题相关代码,请勿粘贴截图
package game.poker.Thread;
import java.awt.Container;
import java.awt.Point;
import java.util.Collections;
import java.util.List;
import game.poker.model.GamePokerCardType;
import game.poker.model.GamePokerPlayer;
import game.poker.view.GamePokerMainJPanel;
public class GamePokerDealThread extends Thread {
GamePokerMainJPanel mainJPanel;
GamePokerPlayer player1;
GamePokerPlayer player2;
GamePokerPlayer player3;
public GamePokerDealThread(GamePokerMainJPanel mainJPanel) {
this.mainJPanel = mainJPanel;
}
public GamePokerDealThread() {
// TODO Auto-generated constructor stub
}
@Override
public void run() {
//打乱牌组 <洗牌>
shufferCard();
//通过设置和修改每张扑克牌的位置 startX 和 startY来让发牌得以进行
for (int i = 0; i < mainJPanel.getCardList().size(); i++) {
if(i < mainJPanel.getCardList().size()-3) {
switch (i%3) {
case 0:
mainJPanel.trueTurn(mainJPanel.getCardList().get(i));
mainJPanel.getCardList().get(i).setStartX(200);
mainJPanel.getCardList().get(i).setStartY(100+i*5);
mainJPanel.getSendP1List().add(mainJPanel.getCardList().get(i));
break;
case 1:
mainJPanel.getCardList().get(i).setStartX(500+i*7);
mainJPanel.getCardList().get(i).setStartY(800);
mainJPanel.getSendP2List().add(mainJPanel.getCardList().get(i));
break;
case 2:
mainJPanel.trueTurn(mainJPanel.getCardList().get(i));
mainJPanel.getCardList().get(i).setStartX(900);
mainJPanel.getCardList().get(i).setStartY(100+i*5);
mainJPanel.getSendP3List().add(mainJPanel.getCardList().get(i));
break;
}
}else{
mainJPanel.trueTurn(mainJPanel.getCardList().get(i));
mainJPanel.getCardList().get(i).setStartX(500+(i%51)*30);
mainJPanel.getCardList().get(i).setStartY(100);
mainJPanel.getSendDipaiList().add(mainJPanel.getCardList().get(i));
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// //排序
sortCards(mainJPanel.getSendP1List());
rePosition2(mainJPanel.getSendP1List(), 0);
sortCards(mainJPanel.getSendP2List());
rePosition2(mainJPanel.getSendP2List(), 1);
sortCards(mainJPanel.getSendP3List());
rePosition2(mainJPanel.getSendP3List(), 2);
//重新绘制图片
mainJPanel.repaint();
}
// System.out.println(mainJPanel.getSendP1List());
// System.out.println(mainJPanel.getSendP2List());
}
//排序方法
private void sortCards(List<GamePokerCardType> list) {
Collections.sort(list, (o1,o2)->{
//花色值
int a1 = o1.getColorValue();
int a2 = o2.getColorValue();
//点数值
int b1 = o1.getPointValue();
int b2 = o2.getPointValue();
int flag = 0;
//降序排列
flag = b2 - b1;
if (flag == 0)
return a2 - a1;
else {
return flag;
}
});
}
//打乱牌组 <洗牌> 的方法
private void shufferCard() {
Collections.shuffle(mainJPanel.getCardList());
}
//重定位
public void rePosition2(List<GamePokerCardType> list,int flag) {
for (int i = 0; i < list.size(); i++) {
if (flag == 0) {
list.get(i).setStartX(200);
list.get(i).setStartY(100+i*15);
}
if (flag == 1) {
list.get(i).setStartX(507+i*21);
list.get(i).setStartY(800);
}
if (flag == 2) {
list.get(i).setStartX(900);
list.get(i).setStartY(110+i*15);
}
}
}
}
我想要达到的结果
图片不重合