多个用户在同一个JFrame中工作

时间:2016-04-15 08:52:54

标签: java swing

尝试处理网络项目,其中两个或更多用户需要能够在同一个JFrame中绘制。

如何最真实地处理这种情况,使用他们的IP连接两台PC,然后创建一个实时场景供他们使用,或者某种包装系统,用户发送他们所拥有的东西drawne最近,然后其他用户可以看到刚刚发送的内容?

编辑:添加了我的上下文绘图类

public class CanvasFrame extends JPanel {

    ArrayList<Point> location = new ArrayList<Point>(); 

    JTextArea consoleOutput = new JTextArea(1,20);

    public void addComponentToPane(Container pane) {
        consoleOutput.setEditable(false);
    }

    public CanvasFrame() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                location.clear();
                location.add(e.getPoint());
            }
        });

        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                location.add(e.getPoint());
                repaint();
            }
        });
        setPreferredSize(new Dimension(800, 500));
        setBackground(Color.WHITE);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if(location.isEmpty()){
            return;
        }

         Point p = location.get(0);
         for (int i = 1; i < location.size(); i++) {
             Point q = location.get(i);
             g.drawLine(p.x, p.y, q.x, q.y);
             p = q;
         }
    }

    public static void main(String[] args) throws Exception {

        InetAddress SERVERIP = InetAddress.getLocalHost();

        Runnable runnable = new Runnable() {
            @Override
            public void run() {

                JFrame frame = new JFrame("Drawing with friends");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new CanvasFrame(), BorderLayout.CENTER);

                JTextArea IPadress = new JTextArea(1,20);
                IPadress.setEditable(false);
                IPadress.append("DEVICE IP: " + SERVERIP.getHostAddress());
                frame.add(IPadress, BorderLayout.SOUTH);

                frame.setSize(new Dimension(800,600));
                frame.setLocationRelativeTo(null);
                frame.setResizable(false);
                frame.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(runnable);
    }
}

0 个答案:

没有答案