码农小Q仔 2022-08-23 00:41 采纳率: 100%
浏览 15
已结题

swing图片显示问题

问题遇到的现象和发生背景

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);
        }
    }
}

}

我想要达到的结果

图片不重合

  • 写回答

2条回答 默认 最新

  • 码农小Q仔 2022-08-24 12:04
    关注

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月24日
  • 已采纳回答 8月24日
  • 创建了问题 8月23日

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?