位置未通过网络更新

时间:2018-04-23 01:19:31

标签: java sockets networking graphics

我写了一些简单的代码,允许用户使用键盘移动球。我已经使用套接字连接客户端和服务器,以查看客户端的球是否会与服务器对应。运动起作用,但在客户的面板中,球没有重新粉刷并且正在重新绘制,最后位置仍然很明显。以下是客户端及其服务器的代码。

客户端

import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class MovingBallClient extends JPanel implements ActionListener
{
    private static Socket conn;
    //Updates movement
    private int x, y;
    private static boolean left, right, up, down;
    private Timer tm;

    public static void main(String[]  args) throws UnknownHostException, IOException
    {
        JFrame f = new JFrame("Client");
        MovingBallClient p = new MovingBallClient();
        f.setSize(500, 500);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(p);
        conn = new Socket(InetAddress.getLocalHost(), 2000);
        while(true)
        {
            try{
                DataInputStream dis = new DataInputStream(conn.getInputStream());
                String dir = dis.readUTF();
                if(dir.equals("left"))
                {
                    left  = true;
                }
                else if(dir.equals("right"))
                {
                    right = true;
                }
                else if(dir.equals("up"))
                {
                    up = true;
                }
                else if(dir.equals("down"))
                {
                    down = true;
                }
            }catch(IOException ex){
                ex.printStackTrace();
            }
        }
    }

    public MovingBallClient() throws UnknownHostException, IOException
    {
        x = y = 50;
        tm = new Timer(5, this);
        tm.start();
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponents(g);
        g.setColor(Color.RED);
        g.fillOval(x, y, 50, 50);
    }

    public void actionPerformed(ActionEvent e)
    {
        if(left)
        {
            x -= 5;
            left = false;
        }
        else if(right)
        {
            x += 5;
            right = false;
        }
        else if(up)
        {
            y -= 5;
            up = false;
        }
        else if(down)
        {
            y += 5;
            down = false;
        }
        repaint();  
    }
}

服务器

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class MovingBallServer extends JPanel implements KeyListener, ActionListener{
    private int x, y;
    private boolean left, right, up, down;
    private Timer tm;
    private static ServerSocket server;
    private static Socket conn;
    private DataOutputStream dos;

    public MovingBallServer() throws UnknownHostException, IOException
    {
        x = y = 50;
        tm = new Timer(5, this);
        tm.start();
        left = right = up = down = false;
        setFocusable(true);
        addKeyListener(this);
    }
    public static void main(String[] args) throws UnknownHostException, IOException
    {
        JFrame f = new JFrame("Server");
        MovingBallServer p = new MovingBallServer();
        f.setSize(500, 500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.add(p);
        server = new ServerSocket(2000, 1, InetAddress.getLocalHost());
        conn = server.accept();
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.RED);
        g.fillOval(x, y, 50, 50);
    }

    public void keyPressed(KeyEvent e) 
    {
        int k = e.getKeyCode();
        if(k == KeyEvent.VK_UP)
            up = true;
        else if(k == KeyEvent.VK_DOWN)
            down = true;
        else if(k == KeyEvent.VK_LEFT)
            left = true;
        else if(k == KeyEvent.VK_RIGHT)
            right = true;
    }

    public void keyReleased(KeyEvent e) 
    {       
    }

    public void keyTyped(KeyEvent e) 
    {
    }

    public void actionPerformed(ActionEvent e) 
    {   
        if(right && x <= 430)
        {
            x += 5;
            right = false;
            try{
                DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
                dos.writeUTF("right");
            }catch(IOException ex)
            {
                ex.printStackTrace();
            }
        }

        if(left && x >= 5)
        {
            x -= 5;
            left = false;
            try{
                DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
                dos.writeUTF("left");
            }catch(IOException ex)
            {
                ex.printStackTrace();
            }
        }

        if(up && y >= 5)
        {
            y -= 5;
            up = false;
            try{
                DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
                dos.writeUTF("up");
            }catch(IOException ex)
            {
                ex.printStackTrace();
            }
        }

        if(down && y <= 405)
        {
            y += 5;
            down = false;
            try{
                DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
                dos.writeUTF("down");
            }catch(IOException ex)
            {
                ex.printStackTrace();
            }
        }
        repaint();
    }
}

0 个答案:

没有答案