新鲜感~ 2021-12-26 23:41 采纳率: 0%
浏览 132

调用repaint()函数 界面却没有立即重绘。

调用repaint()函数 界面却没有立即重绘。 在做五子棋 把自己的棋子位置传到对方的棋盘。把对方数组置数了,调用重绘函数没有立即重绘

问题涉及的主要代码:

if (str[0].equals("qiziAddress")) {

                        qiziPos[Integer.parseInt(str[1])][Integer.parseInt(str[2])] = 1;
                        System.out.println("调用了重绘函数");
                        testQiPan.repaint();

                    }

程序主要代码:

package play;
import javax.imageio.ImageIO;
import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import static java.lang.Integer.parseInt;

public class play extends JFrame implements ActionListener {
JFrame jfPlay = new JFrame("客户端游戏主界面");
JSplitPane rightJsp = new JSplitPane(); //右
JSplitPane leftJsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);//左

JTabbedPane njTbable = new JTabbedPane();
JTabbedPane useJpan = new JTabbedPane();
JTabbedPane serveJpan = new JTabbedPane();

JPanel useImg = new JPanel();
JPanel serveImg = new JPanel();
JPanel rightImg = new JPanel();
JPanel playImg = new JPanel();
JPanel use=new JPanel();
JPanel useSiting = new JPanel();
JTextArea jta = new JTextArea();//服务器信息 ,显示谁进入大厅


JButton autoPlay = new JButton("自动落座");
JButton exit = new JButton("退出");
JLabel jtext = new JLabel(" 《五子棋游戏》");

JButton LuseSit[]=new JButton[31];        //放头像的按钮
JLabel useTable[]=new JLabel[50];         //放桌子的按钮
JLabel userBottonName[] = new JLabel[31];//底部的 user 名字

//数组记录座位状态    【 0 】 表示座位没人  落座后置【1】
int isSiting [] = new int[31];

int last =1;
int isSit = 0; //最终的座位号
//第一个界面传过来的参数
String m_url ; //头像地址
String m_name ; //名字
String name =null; //

//第三个面板的分割条
JSplitPane rightThreeJsp = new JSplitPane(); //右

//第三个界面的JTabbedPane
JSplitPane jspUp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
JSplitPane jspDown = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
//JSplitPane jspRightBotton = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
JSplitPane jspRightBotton = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
JTabbedPane mySelfJtb = new JTabbedPane();
JTabbedPane opponentJtb = new JTabbedPane();
JTabbedPane chatJtb = new JTabbedPane();

JLabel opponentImg = new JLabel();
JLabel opponentName = new JLabel();
JLabel myImg = new JLabel();
JLabel myName = new JLabel();

JPanel mySelf = new JPanel();
JPanel opponent = new JPanel();
JPanel chat = new JPanel();
JPanel rightJpanel = new JPanel();
JPanel rightBottomJpanel = new JPanel();
JPanel BottomJpanel = new JPanel();

//网络编程部分
 Socket client=null;
DataOutputStream dos =null;

Image qiPan=null;
Image blackQizi=null;
Image whiteQizi=null;
int cursorX=-1;
int cursorY=-1;
int arrayX=-1;
int arrayY=-1;
int chessType = 1; //0 无棋 1 黑棋 2 白棋
static int[][] qiziPos=new int[15][15];
TestQiPan testQiPan =new TestQiPan();

JTextArea chatArea =new JTextArea();

public play(String url,String name){

m_url = url;
m_name = name;

jta.setEnabled(false);

    leftJsp.setBottomComponent(serveJpan);
    leftJsp.setTopComponent(useJpan);
    rightJsp.setLeftComponent(leftJsp);
    rightJsp.setRightComponent(rightImg);

    useJpan.addTab("用户信息",useImg);
    serveJpan.addTab("服务器信息",serveImg);

    njTbable.addTab("游戏大厅",rightJsp);
    njTbable.addTab("五子棋游戏",rightThreeJsp);

    jfPlay.add(njTbable);

    rightImg.setLayout(new BorderLayout());
    playImg.setLayout(new BorderLayout());

    JPanel l = new JPanel();
    l.add(autoPlay);
    l.add(exit);
    playImg.add(jtext,BorderLayout.WEST);
    playImg.add(l,BorderLayout.EAST);

    rightImg.add(playImg,BorderLayout.NORTH);

    useImg.setLayout(new BorderLayout());

JLabel useImgeBoy = new JLabel(new ImageIcon("res/img/boy1.gif"));
JLabel useName = new JLabel(m_name);
JLabel userImge = new JLabel(new ImageIcon(m_url));

serveImg.add(jta);//显示新用户进来大厅

    use.setLayout(new BorderLayout());
    use.add(useName, BorderLayout.SOUTH);
    use.add(userImge,BorderLayout.CENTER);

    useImg.add(useImgeBoy,"Center");
    useImg.add(use,"South");

//座位
useSiting.setLayout(null);

JLabel useNum[]=new JLabel[50];//底部的名字

int tableNum = 1;
int leftNum=1;

for(int i = 0;i<5;i++)
{
int one = 0;
int two = 0;
int three =0;
for(int j = 1;j<10;j++)
{
useTable[j+i9] = new JLabel(new ImageIcon("res/img/xqnoone.gif"));
useNum[j+i
9] = new JLabel(""+(tableNum));

    if(j==2 || j==5 || j==8 )
    {

        useTable[j+i*9].setBounds(  110+one*220,          50+i*110,         55,55);
        useNum[j+i*9].setBounds(  130+one*220,          50+i*110+60,         55,55);
        //useNum[j+i*9].setColor()
        useSiting.add(useNum[j+i*9]);
        useSiting.add(useTable[j+i*9]);
        one++;
        tableNum++;

    }
    else if(j==1|| j==4 || j==7)
    {
        LuseSit[leftNum] = new JButton(new ImageIcon("res/img/noone.gif"));
        userBottonName[leftNum] =new JLabel("");
        LuseSit[leftNum].setBounds(   55+two*220,          50+i*110,         55,55);

        userBottonName[leftNum].setBounds(55+two*220+10,          50+i*110+45,         55,55);

        useSiting.add(LuseSit[leftNum]);
        useSiting.add(userBottonName[leftNum]);

        two++;

    }
    else if (j==3||j==6||j==9)
    {
        LuseSit[leftNum] = new JButton(new ImageIcon("res/img/noone.gif"));
        userBottonName[leftNum] =new JLabel("");
        userBottonName[leftNum].setBounds( 165+three*220+10,          50+i*110+45,         55,55);
        LuseSit[leftNum].setBounds(   165+three*220,          50+i*110,         55,55);

        useSiting.add(LuseSit[leftNum]);
        useSiting.add( userBottonName[leftNum]);
        three++;

    }

    if(j==1|| j==4 || j==7||j==3||j==6||j==9)
    {
        LuseSit[leftNum].setActionCommand(""+leftNum);
        LuseSit[leftNum].addActionListener(this);
        leftNum++;
    }
    rightImg.add(useSiting);
}

}

   // Color backgroundColor = new Color(30 ,144, 255);
Color backgroundColor = new Color(81,113,158);
    useSiting.setBackground(backgroundColor);

//自动落座
autoPlay.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {

    for(int i = 1;i<31;i++)
    {
        if(isSiting[i]==0)
        {
            LuseSit[last].setIcon(new ImageIcon("res/img/noone.gif"));
            userBottonName[last].setText("");

            LuseSit[i].setIcon(new ImageIcon(m_url));
            userBottonName[i].setText(m_name);

            isSiting[last]=0;

            isSiting[i]=1;

            String info=null;
            info = "MSG:"+m_name+":"+m_url+":"+i+":"+last;
            DataOutputStream dos;
            try {
                dos=new DataOutputStream(client.getOutputStream());
                dos.writeUTF(info);
                System.out.println(info);
            }
            catch (IOException ioException) {
                ioException.printStackTrace();
            }


            last =i;
            break;
        }
    }
    }
});

//第三个页面 开始

//TestQiPan qiPanMsg = new TestQiPan();

rightThreeJsp.setLeftComponent(jspUp);
jspDown.setTopComponent(mySelfJtb);

rightThreeJsp.setBottomComponent(jspRightBotton);
jspRightBotton.setTopComponent(new TestQiPan());//右上容器
jspRightBotton.setBottomComponent(rightBottomJpanel);//右下底部 容器



//jspRightBotton.setBottomComponent(rightBottomJpanel);//右下底部 容器

jspUp.setTopComponent(opponentJtb);
jspUp.setBottomComponent(jspDown);
jspDown.setBottomComponent(chatJtb);

rightThreeJsp.setDividerLocation(250);
jspUp.setDividerLocation(200);
jspDown.setDividerLocation(200);
jspRightBotton.setDividerLocation(560);


opponentJtb.addTab("对手",opponent);
mySelfJtb.addTab("自己",mySelf);
chatJtb.addTab("聊天",chat);

/*
//chatLabel=new JLabel("544554445");
chatLabel.setText("231132");
chatArea.add(chatLabel);
chat.add(chatArea);
*/

rightJpanel.setSize(550,550);
rightJpanel.setVisible(true);

//五子棋游戏 界面 开始

JLabel opponentTime = new JLabel("本步剩余时间");


opponent.setLayout(new BorderLayout());
opponentImg.setIcon(new ImageIcon(m_url));
opponentName.setText(m_name);

opponentImg.setBounds(85,10,55,55);
opponentName.setBounds(120,12,55,55);
opponent.add(opponentImg);
opponent.add(opponentName);
opponent.add(opponentTime,"Center");



JLabel myTime = new JLabel("本步剩余时间");

mySelf.setLayout(new BorderLayout());
myImg.setIcon(new ImageIcon(m_url));
myName.setText(m_name);

myImg.setBounds(85,10,55,55);
myName.setBounds(120,12,55,55);
mySelf.add(myImg);
mySelf.add(myName);
mySelf.add(myTime,"Center");

//聊天
chat.setLayout(new BorderLayout());
JPanel chatBotton = new JPanel();
JButton buttonChat =new JButton("发送");
JTextField textChat = new JTextField();

chatBotton.setLayout(new GridLayout(1,1));

chatBotton.add(textChat);
chatBotton.add(buttonChat);

chat.add(chatBotton,"South");
//聊天窗口
JLabel chatLabel = new JLabel("844");
chatLabel.setBounds(50,50,60,60);

chat.add(chatLabel);
BottomJpanel.setLayout(new GridLayout(1,4));
JButton exitButton = new JButton("退出");
JButton starButton = new JButton("开始");
JButton qiuheButton = new JButton("求和");
JButton renshuButton = new JButton("认输");

BottomJpanel.add(exitButton);
BottomJpanel.add(starButton);
BottomJpanel.add(qiuheButton);
BottomJpanel.add(renshuButton);

BottomJpanel.setBounds(680,550,200,80);
rightBottomJpanel.add(BottomJpanel);

//设置窗体属性
    //窗体标题

    jfPlay.setTitle("客户端游戏主界面");
    //设置窗体大小
    jfPlay.setSize(900, 700);
    //设置窗体位置
    jfPlay.setLocation(800, 200);
    //设置关闭窗体同时退出JVM
    jfPlay.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //显示窗体
    //jfPlay.setVisible(false);

}


//切换窗口
public boolean exchange()
{
    jfPlay.setVisible(true);
    return true;
}
//监听座位的点击事件,点击后更新头像 名字
@Override
public void actionPerformed (ActionEvent e) {
    String s = e.getActionCommand();
    int num = parseInt(s);
    String info=null;

    if(isSiting[last] == 0 && last ==1 )
    {
        LuseSit[num].setIcon(new ImageIcon(m_url));
        userBottonName[num].setText(m_name);
        isSiting[num] =1;

        //last = num;
        isSit=num;
        info = "MSG:"+m_name+":"+m_url+":"+isSit+":"+last;
        DataOutputStream dos;
        try {
            dos=new DataOutputStream(client.getOutputStream());
            dos.writeUTF(info);

        }
        catch (IOException ioException) {
            ioException.printStackTrace();
        }

        last = num;

    }

    else if(isSiting[last] == 1 && isSiting[num] == 0 )
    {

        LuseSit[last].setIcon(new ImageIcon("res/img/noone.gif"));
        userBottonName[last].setText("");

        LuseSit[num].setIcon(new ImageIcon(m_url));
        userBottonName[num].setText(m_name);

        isSiting[num] =1;
        isSiting[last] =0;

        isSit=num;

        //向服务器发送最新的座位信息
        info = "MSG:"+m_name+":"+m_url+":"+isSit+":"+last;
        DataOutputStream dos;
        try {
            dos=new DataOutputStream(client.getOutputStream());
            dos.writeUTF(info);
            // System.out.println(info);
        }
        catch (IOException ioException) {
            ioException.printStackTrace();
        }
        last = num;
        //System.out.println(info);

    }

}

//连接服务器
public void connectServer()
{
try {
name=m_name;
client = new Socket("127.0.0.1",1234);
dos = new DataOutputStream(client.getOutputStream());
dos.writeUTF(m_name);
}
catch(Exception e)
{
e.printStackTrace();
}
}

public void createReaderThread()
{
    try {
        clientReader reader = new clientReader(new DataInputStream(client.getInputStream()),
                LuseSit, userBottonName,  isSiting,opponentImg,opponentName,myImg,opponentName,
                last, serveImg,jta, chatArea);
        reader.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

//继承JPanel重写方法
public class TestQiPan extends JPanel implements MouseListener {
    public TestQiPan(){
        try {

/* qiPan= ImageIO.read(new File("I:\fives\src\res\wuzi\board.gif"));
blackQizi= ImageIO.read(new File("I:\fives\src\res\wuzi\heiqi.gif"));
whiteQizi= ImageIO.read(new File("I:\fives\src\res\wuzi\baiqi.gif"));*/
qiPan= ImageIO.read(new File("res/wuzi/board.gif"));
blackQizi= ImageIO.read(new File("res/wuzi/heiqi.gif"));
whiteQizi= ImageIO.read(new File("res/wuzi/baiqi.gif"));
} catch (IOException e) {
e.printStackTrace();
}
this.addMouseListener(this);
}

    @Override
    public void paint(Graphics g) {
        //调用的super.paint(g),让父类做一些事前的工作,如刷新屏幕
        super.paint(g);
        g.drawImage(qiPan,0,0,this);

        for(int i=0;i<15;i++){
            for(int j=0;j<15;j++){
                int X=i*35+10;
                int Y=j*35+10;

                if(qiziPos[i][j]==1)
                {
                    g.drawImage(blackQizi,X,Y,this);
                    // System.out.println("X坐标:"+(arrayX*35+10)+"  Y坐标:"+(arrayY*35+10));
                }
                else if(qiziPos[i][j]==2)
                {
                    g.drawImage(whiteQizi,X,Y,this);
                    // System.out.println("X坐标:"+(arrayX*35+10)+"  Y坐标:"+(arrayY*35+10));
                }
                else if(qiziPos[i][j]!=0)
                {
                    return ;
                }


            }
        }
       // System.out.println("调用了重绘函数");
    }
    //-----------------------------------------------------------------------------------------------------------
    @Override
    public void mouseClicked(MouseEvent e) {
        cursorX=e.getX();
        cursorY=e.getY();
        arrayX=(cursorX-10)/35;
        arrayY=(cursorY-10)/35;
        //变换棋子的颜色
        qiziPos[arrayX][arrayY]=chessType;
        this.repaint();
        chessType=chessType%2==0?1:2;


        String info=null;
        //info = "qiziAddress:"+arrayX+":"+arrayY+":"+opponentName.getText()+":"+opponentImg.getIcon();
        DataOutputStream dos;
        try {
            dos=new DataOutputStream(client.getOutputStream());
            info = "qiziAddress:"+arrayX+":"+arrayY;
            dos.writeUTF(info);
        }
        catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}

class clientReader extends Thread {
    DataInputStream dis = null;
    JButton LuseSit[] = null;
    JLabel userBottonName[]=null;
    int isSiting []=null;
    JLabel opponentImg=null;
    JLabel myImg=null;
    JLabel opponentName=null;
    JLabel myName=null;
    JPanel serveImg=null;
    int last=0;
    JTextArea jta=null;
    JTextArea chatArea=null;

    public clientReader(DataInputStream dis, JButton LuseSit[],JLabel userBottonName[],
                        int isSiting [],JLabel opponentImg,JLabel opponentName,JLabel myImg,JLabel myName,
                        int last,JPanel serveImg,JTextArea jta,JTextArea chatArea) {
        this.dis = dis;
        this.LuseSit = LuseSit;
        this.userBottonName=userBottonName;
        this.isSiting = isSiting;
        this.opponentImg=opponentImg;
        this.opponentName=opponentName;
        this.myImg=myImg;
        this.myName=myName;
        this.last=last;
        this.serveImg=serveImg;
        this.jta=jta;
         this. chatArea= chatArea;
    }

    public void run() {
        //进入大厅时发送 名字给服务器
        try{
            dos.writeUTF("login"+":"+m_name);

        } catch (IOException e) {
            e.printStackTrace();
        }
        while (true) {

//info = "MSG:"+m_name+":"+m_url+":"+isSit+":"+last;
String info = null;
try {
info = dis.readUTF();
String[] str = info.split(":");
//【服务器信息】显示 新用户进入大厅
/if(str[0].equals("login"))
{
jta.append(str[1]+"进入大厅!"+"\n");
}
/
//换座位时,将原来座位的头像名字初始化,点击的座位改成 自己的头像 名字
if (str[0].equals("MSG")) {
LuseSit[parseInt(str[4])].setIcon(new ImageIcon("res/img/noone.gif"));
userBottonName[parseInt(str[4])].setText("");
isSiting[parseInt(str[4])] = 0;

                    LuseSit[parseInt(str[3])].setIcon(new ImageIcon(str[2]));
                    userBottonName[parseInt(str[3])].setText(str[1]);
                    isSiting[parseInt(str[3])] = 1;
                }
                //匹配到了对手 将名字头像 传到游戏界面
                if (isSit != 0 && isSit % 2 == 0 && isSiting[isSit - 1] == 1)  //偶数 右边
                {
                    String r_oppoIcon = String.valueOf(LuseSit[isSit - 1].getIcon());
                    String r_oppoName = userBottonName[isSit - 1].getText();

                    opponentImg.setIcon(new ImageIcon(r_oppoIcon));
                    opponentName.setText(r_oppoName);
                    //发送自己下子的信息给对手,同步信息给对手棋盘
                    if (str[0].equals("qiziAddress"))
                    {
                        qiziPos[Integer.parseInt(str[1])][Integer.parseInt(str[2])] = 1;
                        System.out.println("调用了重绘函数");
                        testQiPan.repaint();
                    }
                } else if (isSit != 0 && isSit % 2 == 1 && isSiting[isSit + 1] == 1) //奇数 左边
                {
                    String l_oppoIcon = String.valueOf(LuseSit[isSit + 1].getIcon());
                    String l_oppoName = userBottonName[isSit + 1].getText();

                    opponentImg.setIcon(new ImageIcon(l_oppoIcon));
                    opponentName.setText(l_oppoName);

                    if (str[0].equals("qiziAddress")) {

                        qiziPos[Integer.parseInt(str[1])][Integer.parseInt(str[2])] = 1;
                        System.out.println("调用了重绘函数");
                        testQiPan.repaint();

                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }
    }
}
/*//监听座位的点击事件,点击后更新头像 名字
@Override
public void actionPerformed (ActionEvent e) {
    String s = e.getActionCommand();
     int num = parseInt(s);
    String info=null;

if(isSiting[last] == 0 && last ==1 )
{
    LuseSit[num].setIcon(new ImageIcon(m_url));
    userBottonName[num].setText(m_name);
    isSiting[num] =1;

    //last = num;
    isSit=num;
    info = "MSG:"+m_name+":"+m_url+":"+isSit+":"+last;
    DataOutputStream dos;
    try {
        dos=new DataOutputStream(client.getOutputStream());
        dos.writeUTF(info);

    }
    catch (IOException ioException) {
        ioException.printStackTrace();
    }

    last = num;

}

else if(isSiting[last] == 1 && isSiting[num] == 0 )
{

    LuseSit[last].setIcon(new ImageIcon("res/img/noone.gif"));
    userBottonName[last].setText("");

    LuseSit[num].setIcon(new ImageIcon(m_url));
    userBottonName[num].setText(m_name);

    isSiting[num] =1;
    isSiting[last] =0;

    isSit=num;

            //向服务器发送最新的座位信息
            info = "MSG:"+m_name+":"+m_url+":"+isSit+":"+last;
                DataOutputStream dos;
                try {
                    dos=new DataOutputStream(client.getOutputStream());
                    dos.writeUTF(info);
                   // System.out.println(info);
                }
                catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                last = num;
                //System.out.println(info);

}

}*/
    }
没有报错,实验结果达不到预期
我的解答思路和尝试过的方法
调用重绘函数后,立即重绘
  • 写回答

1条回答 默认 最新

  • 关注

    删除super.paint(g)语句:

    img

    评论

报告相同问题?

问题事件

  • 创建了问题 12月26日

悬赏问题

  • ¥15 通过程序读取主板上报税口的数据
  • ¥15 matlab修改为并行
  • ¥15 大数据应用基础专业问题
  • ¥20 数据分析出错了,希望有能人看看,解决一下
  • ¥15 尝试访问%1服务的windows注册表时遇到问题。必须先解决此问题,然后才能运行安装过程。(请确认您正在使用管理员权限运行)373
  • ¥15 (关键词-运算放大器)
  • ¥15 关于#游戏策划#的问题:当浏览器输入兑换码,疯狂点击领取按钮,邮箱马上到账几十个兑换码礼包
  • ¥15 虚拟机打不开,怎么解决
  • ¥15 为什么游戏兑换码能被重复领取
  • ¥30 (急!)java实现二叉链表构建二叉树,实现相关功能