2010-04-08 3 views
0

로컬 사용자와 원격 사용자가 모두 able to draw simultaneously이어야하는 화이트 보드 응용 프로그램을 개발해야합니까? 가능하다면 어떤 논리입니까?내 화이트 보드 응용 프로그램에 문제가 있습니다

나는 코드를 이미 개발했는데 원격 사용자가 드로잉 할 도형이 그의 모양과 좌표로 대체 될 때이 작업을 수행 할 수 없습니다. 이 문제는 양쪽 모두를 동시에 그리는 경우에만 발생합니다.

어떤 사람입니까?

여기에 내 코드

class Paper extends JPanel implements MouseListener,MouseMotionListener,ActionListener 
{ 
    static BufferedImage image; 
    int bpressed; 
    Color color; 
    Point start; 
    Point end; 
    Point mp; 
    Button elipse=new Button("elipse"); 
    Button rectangle=new Button("rect"); 
    Button line=new Button("line"); 
    Button empty=new Button(""); 
    JButton save=new JButton("Save"); 
    JButton erase=new JButton("Erase"); 
    String selected; 
    int ex,ey;//eraser 
    DatagramSocket dataSocket; 
    JButton button = new JButton("test"); 
    Client client; 
    Point p=new Point(); 
    int w,h; 
    public Paper(DatagramSocket dataSocket) 
    {   
     this.dataSocket=dataSocket; 
     client=new Client(dataSocket); 
     System.out.println("paper"); 
     setBackground(Color.white); 
     addMouseListener(this); 
     addMouseMotionListener(this); 
     color = Color.black; 
     setBorder(BorderFactory.createLineBorder(Color.black));   
     //save.setPreferredSize(new Dimension(100,20)); 
     save.setMaximumSize(new Dimension(75,27)); 
     erase.setMaximumSize(new Dimension(75,27)); 
    } 

    public void paintComponent(Graphics g) 
    { 
     try 
     { 
     g.drawImage(image, 0, 0, this); 
     Graphics2D g2 = (Graphics2D)g; 
     g2.setPaint(Color.black); 
     if(selected==("elipse")) 
      g2.drawOval(start.x, start.y,(end.x-start.x),(end.y-start.y)); 
     else if(selected==("rect")) 
      g2.drawRect(start.x, start.y, (end.x-start.x),(end.y-start.y)); 
     else if(selected==("line")) 
      g2.drawLine(start.x,start.y,end.x,end.y); 
     } 
     catch(Exception e) 
     {} 
     } 
    //Function to draw the shape on image 
    public void draw() 
    { 
     Graphics2D g2 = image.createGraphics(); 
     g2.setPaint(color); 
     if(selected=="line") 
      g2.drawLine(start.x, start.y, end.x, end.y); 
     if(selected=="elipse") 
      g2.drawOval(start.x, start.y, (end.x-start.x),(end.y-start.y)); 
     if(selected=="rect") 
      g2.drawRect(start.x, start.y, (end.x-start.x),(end.y-start.y)); 
     repaint(); 
     g2.dispose(); 
     start=null; 
    } 

    //To add the point to the board which is broadcasted by the server 
    public synchronized void addPoint(Point ps,String varname,String shape,String event) 
    { 
     try 
     { 
      if(end==null) 
       end = new Point(); 
      if(start==null) 
       start = new Point(); 
      if(shape.equals("elipse")) 
       selected="elipse"; 
      else if(shape.equals("line")) 
       selected="line"; 
      else if(shape.equals("rect")) 
       selected="rect"; 
      else if(shape.equals("erase")) 
      { 
       selected="erase"; 
       erase(); 
      } 
      if(end!=null && start!=null) 
      { 
       if(varname.equals("end")) 
         end=ps; 
       if(varname.equals("mp")) 
         mp=ps;   
       if(varname.equals("start")) 
         start=ps; 
       if(event.equals("drag")) 
         repaint(); 
       else if(event.equals("release")) 
         draw(); 
     } 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 
    //To set the size of the image 
    public void setWidth(int x,int y) 
    { 
     System.out.println("("+x+","+y+")"); 
     w=x; 
     h=y; 
     image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 
     Graphics2D g2 = image.createGraphics(); 
     g2.setPaint(Color.white); 
     g2.fillRect(0,0,w,h); 
     g2.dispose(); 
    } 

    //Function which provides the erase functionality 
    public void erase() 
    { 
     Graphics2D pic=(Graphics2D) image.getGraphics(); 
     pic.setPaint(Color.white); 
     pic.fillRect(start.x, start.y, 10, 10); 
    } 

    //Function to add buttons into the panel, calling this function returns a panel 
    public JPanel addButtons() 
    { 
     JPanel buttonpanel=new JPanel(); 
     JPanel row1=new JPanel(); 
     JPanel row2=new JPanel(); 
     JPanel row3=new JPanel(); 
     JPanel row4=new JPanel(); 
     buttonpanel.setPreferredSize(new Dimension(80,80)); 

     //buttonpanel.setMinimumSize(new Dimension(150,150)); 
     row1.setLayout(new BoxLayout(row1,BoxLayout.X_AXIS)); 
     row1.setPreferredSize(new Dimension(150,150)); 
     row2.setLayout(new BoxLayout(row2,BoxLayout.X_AXIS)); 
     row3.setLayout(new BoxLayout(row3,BoxLayout.X_AXIS)); 
     row4.setLayout(new BoxLayout(row4,BoxLayout.X_AXIS)); 
     buttonpanel.setLayout(new BoxLayout(buttonpanel,BoxLayout.Y_AXIS)); 

     elipse.addActionListener(this); 
     rectangle.addActionListener(this); 
     line.addActionListener(this); 
     save.addActionListener(this); 
     erase.addActionListener(this); 

     buttonpanel.add(Box.createRigidArea(new Dimension(10,10))); 
     row1.add(elipse); 
     row1.add(Box.createRigidArea(new Dimension(5,0))); 
     row1.add(rectangle); 
     buttonpanel.add(row1); 
     buttonpanel.add(Box.createRigidArea(new Dimension(10,10))); 

     row2.add(line); 
     row2.add(Box.createRigidArea(new Dimension(5,0))); 
     row2.add(empty); 
     buttonpanel.add(row2); 
     buttonpanel.add(Box.createRigidArea(new Dimension(10,10))); 

     row3.add(save); 
     buttonpanel.add(row3); 
     buttonpanel.add(Box.createRigidArea(new Dimension(10,10))); 

     row4.add(erase); 
     buttonpanel.add(row4); 
     return buttonpanel; 
    } 
    //To save the image drawn 
    public void save() 
    { 
     try 
     { 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos); 
      JFileChooser fc = new JFileChooser(); 
      fc.showSaveDialog(this);   
      encoder.encode(image); 
      byte[] jpgData = bos.toByteArray(); 
      FileOutputStream fos = new FileOutputStream(fc.getSelectedFile()+".jpeg"); 
      fos.write(jpgData); 
      fos.close(); 
     //add replce confirmation here 

     } 
     catch (IOException e) 
     {   
      System.out.println(e); 
     } 
    } 
    public void mouseClicked(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void mouseEntered(MouseEvent arg0) { 

    } 
    public void mouseExited(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 
    public void mousePressed(MouseEvent e) 
    { 
      if(selected=="line"||selected=="erase") 
      { 
       start=e.getPoint(); 
       client.broadcast(start,"start", selected,"press"); 
      } 
      else if(selected=="elipse"||selected=="rect") 
      { 
       mp = e.getPoint(); 
       client.broadcast(mp,"mp", selected,"press"); 
      } 
    } 
    public void mouseReleased(MouseEvent e) 
    { 
     if(start!=null) 
     { 
      if(selected=="line") 
      { 
       end=e.getPoint();   
       client.broadcast(end,"end", selected,"release"); 
      } 
      else if(selected=="elipse"||selected=="rect") 
      { 
       end.x = Math.max(mp.x,e.getX()); 
       end.y = Math.max(mp.y,e.getY()); 
       client.broadcast(end,"end", selected,"release"); 
      } 
      draw(); 
     } 
    //start=null; 
    } 
    public void mouseDragged(MouseEvent e) 
    { 
     if(end==null) 
      end = new Point(); 

     if(start==null) 
       start = new Point(); 

     if(selected=="line") 
     { 
      end=e.getPoint(); 
      client.broadcast(end,"end", selected,"drag"); 
     } 
     else if(selected=="erase") 
     { 
      start=e.getPoint(); 
      erase(); 
      client.broadcast(start,"start", selected,"drag"); 
     } 
     else if(selected=="elipse"||selected=="rect") 
     { 
      start.x = Math.min(mp.x,e.getX()); 
      start.y = Math.min(mp.y,e.getY()); 
      end.x = Math.max(mp.x,e.getX()); 
      end.y = Math.max(mp.y,e.getY()); 
      client.broadcast(start,"start", selected,"drag"); 
      client.broadcast(end,"end", selected,"drag"); 
     } 
     repaint(); 
    } 
    @Override 
    public void mouseMoved(MouseEvent arg0) { 
     // TODO Auto-generated method stub 

    } 
    public void actionPerformed(ActionEvent e) 
    { 
     if(e.getSource()==elipse) 
      selected="elipse"; 
     if(e.getSource()==line) 
      selected="line";  
     if(e.getSource()==rectangle) 
      selected="rect"; 
     if(e.getSource()==save) 
      save(); 
     if(e.getSource()==erase) 
     { 
      selected="erase"; 
      erase(); 
     } 
    } 
} 

class Button extends JButton 
{ 
    String name; 
    public Button(String name) 
    { 
     this.name=name; 
     Dimension buttonSize = new Dimension(35,35); 
     setMaximumSize(buttonSize);  
    } 
    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 
     Graphics2D g2 = (Graphics2D)g; 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     //g2.setStroke(new BasicStroke(1.2f)); 
     if (name == "line")  g.drawLine(5,5,30,30); 
     if (name == "elipse") g.drawOval(5,7,25,20); 
     if (name== "rect") g.drawRect(5,5,25,23); 

    } 
} 
+0

여기에 작동 코드를 게시하여 – Deepak

답변

4

각 사용자에 대한 패널을 유지하려고하고, 서로의 상단에 그 레이어입니다. 배경이 투명하다면 모두 잘보아야합니다.

편집 : 레이어를 얻으려면 JLayeredPane을 시도 할 수 있습니다.

+0

과 함께 실행되는 샘플 코드를 얻을 수 있습니까? plz – swift

+0

나는이 시도, 계층화 된 패널에서 아무도 패널을 한 번에 두 사람이 동시에 그리는 경우 – swift

+0

고맙습니다 러셀, 그 작업 :) – swift

1

클라이언트에서 도착하는 요청을 처리하는 코드가 없습니다. 이 문제가 백그라운드 스레드에서 발생한다고 가정하면 SwingUtilities.invokeLater을 사용하여 EDT에서 변경해야합니다. EDT 이외의 곳에서 UI 구성 요소를 업데이트하면 예기치 않은 동작이 발생합니다. 또한 또한 Concurrency in Swing

, 이런 종류의 물건이 작동하지 않을 참조 :

자바에서
if (name == "line")  g.drawLine(5,5,30,30); 

== 운전자는 신원 비교를 수행합니다. name"line"이 같은 객체 인 경우 true를 평가합니다. 둘 다 동일한 값을 갖고 있어도 다른 객체를 참조하면 false가 평가됩니다. 평등을 비교하려면 .equals() 메서드를 사용해야합니다.

관련 문제