2014-05-20 1 views
0

저는 Java에서 매우 새로운데, 학교를 위해 뭔가해야합니다. Server-Client Puns 게임을하려고합니다. 채팅 클라이언트 및 서버 클래스가 있고 마우스로 JPanel에 이미지를 그리는 클래스가 있지만 다른 클라이언트와 그림을 공유하는 방법을 알지 못합니다. 아이디어를 좀 주시겠습니까? 감사!클라이언트 개체간에 JPanel 공유

클라이언트 코드 :

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.rmi.registry.*; 
import java.util.*; 

public class PunsClient extends JFrame { 

    //GUI 
    private JButton polacz, rozlacz; 
    private JPanel topPanel; 
    private JPanel centerPanel; 
    private Painter painter; 
    private JTextField host, wiadomosc; 
    private JTextArea komunikaty; 
    private JList<String> zalogowani; 
    private DefaultListModel<String> listaZalogowanych; 
    //Klient 
    private String nazwaSerwera = "localhost"; 
    private Klient watekKlienta; 
    private PunsClient instancjaKlienta; 
    private Puns serwer; 
    private ClientImpl klient; 

    public PunsClient() { 
     super("Klient"); 

     instancjaKlienta = this; 

     setSize(800, 600); 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     setLayout(new BorderLayout()); 

     topPanel = new JPanel(new FlowLayout()); 
     centerPanel = new JPanel(new FlowLayout()); 
     painter = new Painter(); 
     komunikaty = new JTextArea(20,12); 
     komunikaty.setLineWrap(true); 
     komunikaty.setEditable(false); 

     wiadomosc = new JTextField(); 

     host = new JTextField(nazwaSerwera, 12); 
     polacz = new JButton("Połącz"); 
     rozlacz = new JButton("Rozłącz"); 
     rozlacz.setEnabled(false); 

     listaZalogowanych = new DefaultListModel<String>(); 
     zalogowani = new JList<String>(listaZalogowanych); 
     zalogowani.setFixedCellWidth(120); 

     ObslugaZdarzen obsluga = new ObslugaZdarzen(); 

     polacz.addActionListener(obsluga); 
     rozlacz.addActionListener(obsluga); 

     wiadomosc.addKeyListener(obsluga); 

     addWindowListener(new WindowAdapter() { 

      public void windowClosing(WindowEvent e) { 
       rozlacz.doClick(); 
       setVisible(false); 
       System.exit(0); 
      } 
     }); 

     topPanel.add(new JLabel("Serwer RMI: ")); 
     topPanel.add(host); 
     topPanel.add(polacz); 
     topPanel.add(rozlacz); 

     centerPanel.add(painter); 
     centerPanel.add(new JScrollPane(komunikaty), BorderLayout.EAST); 

     add(topPanel, BorderLayout.NORTH); 
     add(centerPanel, BorderLayout.CENTER); 
     //add(painter, BorderLayout.CENTER); 
     //add(new JScrollPane(komunikaty), BorderLayout.EAST); 
     //add(new JScrollPane(zalogowani), BorderLayout.EAST); 
     add(wiadomosc, BorderLayout.SOUTH); 

     setVisible(true); 

    } 

    private class ObslugaZdarzen extends KeyAdapter implements ActionListener { 

     public void actionPerformed(ActionEvent e) { 
      if (e.getActionCommand().equals("Połącz")) { 
       wyswietlKomunikat("Łączę z: " + nazwaSerwera + "..."); 
       polacz.setEnabled(false); 
       rozlacz.setEnabled(true); 
       host.setEnabled(false); 
       watekKlienta = new Klient(); 
       watekKlienta.start(); 
      } 
      if (e.getActionCommand().equals("Rozłącz")) { 
       listaZalogowanych.clear(); 
       try { 
        serwer.opusc(klient); 
       } catch (Exception ex) { 
        System.out.println("Błąd: " + ex); 
       } 
       rozlacz.setEnabled(false); 
       polacz.setEnabled(true); 
       host.setEnabled(true); 
      } 
     } 

     public void keyReleased(KeyEvent e) { 
      if (e.getKeyCode() == 10) { 
       try { 
        serwer.wiadomosc(klient, wiadomosc.getText()); 
        wiadomosc.setText(""); 
       } catch (Exception ex) { 
        System.out.println("Błąd: " + ex); 
       } 
      } 
     } 
    } 

    private class Klient extends Thread { 

     public void run() { 
      try { 
       Registry rejestr = LocateRegistry.getRegistry(host.getText()); 
       serwer = (Puns) rejestr.lookup("RMICzat"); 
       wyswietlKomunikat("Połączyłem się z serwerem."); 
       String nick = JOptionPane.showInputDialog(null, "Podaj nick: "); 
       klient = new ClientImpl(instancjaKlienta, nick); 
       serwer.dolacz(klient); 

      } catch (Exception e) { 
       System.out.println("Błąd połączenia: " + e); 
      } 
     } 
    } 

    public void wyswietlKomunikat(String tekst) { 
     komunikaty.append(tekst + "\n"); 
     komunikaty.setCaretPosition(komunikaty.getDocument().getLength()); 
    } 

    public void odswiezListe(Vector<Client> lista) { 

       listaZalogowanych.clear(); 

       for (Client n : lista) { 
        try { 
         listaZalogowanych.addElement(n.pobierzNicka()); 
         System.out.println(n.pobierzNicka()); 
        } catch (Exception e) { 
         System.out.println("Błąd: " + e); 
        } 
       } 
    } 

    public static void main(String[] args) { 
     new PunsClient(); 
    } 
} 

화가 코드 :

import java.awt.*; 

import java.awt.event.*; 

import javax.swing.*; 

public class Painter extends JPanel { 

    private static final long serialVersionUID = 1L; 
    int xvalue = -10, yvalue = -10; 

    public Painter() { 
     setPreferredSize(new Dimension(400, 400)); 
     addMouseMotionListener(new MouseMotionAdapter() { 

      public void mouseDragged(MouseEvent event) { 
       xvalue = event.getX(); 
       yvalue = event.getY(); 
       repaint(); 
       } 
      }); 
     } 

    public void paint (Graphics g) { 
     g.fillOval(xvalue, yvalue, 10, 10); 
     } 
    } 
+0

몇 가지 코드를 제공해 주시겠습니까? – james

+0

@JamesClark이 (가) 편집했습니다. :) – user3608388

답변

0

나의 제안은 당신은 내가 생각하는 방법 getGraphics()이있는에서 BufferedImage을 사용하는 것입니다. 이 방법을 그림 용 그래픽으로 사용하면 BufferedImage을 보낼 수 있습니다.

+0

그래, 이미 알아 냈어. 고마워! 나는 그것을 시도 할 것이다 :) – user3608388

관련 문제