Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.98 KB | None | 0 0
  1.     private void handleMouseEvent(MouseEvent e) {
  2.         if (e.getComponent().getName().equals("colorPicker")){
  3.             currColor.setBackground(e.getComponent().getBackground());
  4.         }
  5.        
  6.         if (e.getComponent().getName() == "drawingSurface"){
  7.             if (e.getID() == MouseEvent.MOUSE_MOVED) {
  8.                 coords.setText(e.getX() + ", " + e.getY());
  9.             }
  10.             if (currShape instanceof Freehand) {
  11.                 if (e.getID() == MouseEvent.MOUSE_DRAGGED) {   
  12.                     currShape.addPoint(e.getPoint());
  13.                     localTempStream.onNext(currShape); 
  14.                 }
  15.                 else if (e.getID() == MouseEvent.MOUSE_PRESSED) {
  16.                     currShape = new Freehand(currColor.getBackground(), thickness);
  17.                 }
  18.                
  19.                 else if (e.getID() == MouseEvent.MOUSE_RELEASED) {
  20.                     localFinalStream.onNext(currShape);
  21.                     outgoingNetworkStream.onNext(currShape);
  22.                 }
  23.             }
  24.             else if (currShape instanceof Rectangle) {
  25.                 if (e.getID() == MouseEvent.MOUSE_DRAGGED) {   
  26.                     currShape.setP2(e.getPoint());
  27.                     localTempStream.onNext(currShape); 
  28.                 }
  29.                 else if (e.getID() == MouseEvent.MOUSE_PRESSED) {
  30.                     currShape = new Rectangle(currColor.getBackground(), thickness);
  31.                     currShape.setP1(e.getPoint());
  32.                 }
  33.                
  34.                 else if (e.getID() == MouseEvent.MOUSE_RELEASED) {
  35.                     currShape.setP2(e.getPoint());
  36.                     localFinalStream.onNext(currShape);
  37.                     outgoingNetworkStream.onNext(currShape);
  38.                 }
  39.             }
  40.             else if (currShape instanceof Oval) {
  41.                 if (e.getID() == MouseEvent.MOUSE_DRAGGED) {   
  42.                     currShape.setP2(e.getPoint());
  43.                     localTempStream.onNext(currShape); 
  44.                 }
  45.                 else if (e.getID() == MouseEvent.MOUSE_PRESSED) {
  46.                     currShape = new Oval(currColor.getBackground(), thickness);
  47.                     currShape.setP1(e.getPoint());
  48.                 }
  49.                
  50.                 else if (e.getID() == MouseEvent.MOUSE_RELEASED) {
  51.                     currShape.setP2(e.getPoint());
  52.                     localFinalStream.onNext(currShape);
  53.                     outgoingNetworkStream.onNext(currShape);
  54.                 }
  55.             }
  56.             else if (currShape instanceof Line) {
  57.                 if (e.getID() == MouseEvent.MOUSE_DRAGGED) {   
  58.                     currShape.setP2(e.getPoint());
  59.                     localTempStream.onNext(currShape);
  60.                 }
  61.                 else if (e.getID() == MouseEvent.MOUSE_PRESSED) {
  62.                     currShape = new Line(currColor.getBackground(), thickness);
  63.                     currShape.setP1(e.getPoint());
  64.                 }
  65.                
  66.                 else if (e.getID() == MouseEvent.MOUSE_RELEASED) {
  67.                     currShape.setP2(e.getPoint());
  68.                     localFinalStream.onNext(currShape);
  69.                     outgoingNetworkStream.onNext(currShape);
  70.                 }
  71.             }
  72.         }
  73.        
  74.     }
  75.    
  76.     private void handleActionEvent(ActionEvent a) {
  77.         // menu code
  78.         if (a.getActionCommand().equals("host")) {
  79.             int port = 8700; // Integer.parseInt(JOptionPane.showInputDialog(new JFrame(), "Enter port to start server on:"));
  80.             UPnP.openPortTCP(port);
  81.             Observable.just(this.server = new RxServer(port, client.allEvents)).subscribeOn(Schedulers.io()); // start server on new thread
  82.             Observable.just(client).subscribeOn(Schedulers.io()).subscribe(i -> i.connect("127.0.0.1", port)); // connect on new thread
  83.             serverUp = true;
  84.         }
  85.         else if (a.getActionCommand().equals("join")) {
  86.             JFrame popup = new JFrame();
  87.             String ip = (String)JOptionPane.showInputDialog(popup, "IP:");
  88.             int port = Integer.parseInt((String)JOptionPane.showInputDialog(popup, "Port:"));
  89.             Observable.just(client).subscribeOn(Schedulers.io()).subscribe(i -> {
  90.                 i.connect(ip, port);
  91.                 i.allEvents.clear();
  92.                 localFinalStream.onNext(new Clear());
  93.                 });
  94.         }
  95.         else if (a.getActionCommand().equals("disconnect")) {
  96.             if (serverUp) {
  97.                 Observable.just(server).subscribeOn(Schedulers.io()).subscribe(i -> i.shutdown());
  98.             }
  99.             Observable.just(client).subscribeOn(Schedulers.io()).subscribe(i -> i.disconnect());
  100.         }
  101.         else if (a.getActionCommand().equals("exit")) {
  102.             System.exit(0);
  103.         }
  104.         // toolbar code
  105.         else if (a.getActionCommand().equals("clearButton")){
  106.             Clear clear = new Clear();
  107.             localTempStream.onNext(clear);
  108.             outgoingNetworkStream.onNext(clear);
  109.         }
  110.         else if(a.getActionCommand().equals("shapePicker")) {
  111.             JComboBox cb = (JComboBox)a.getSource();
  112.             String selection = (String)cb.getSelectedItem();
  113.             if (selection.equals("Freehand")) {
  114.                 currShape = new Freehand(currColor.getBackground(), thickness);
  115.             }
  116.             else if (selection.equals("Rectangle")) {
  117.                 currShape = new Rectangle(currColor.getBackground(), thickness);
  118.             }
  119.             else if (selection.equals("Oval")) {
  120.                 currShape = new Oval(currColor.getBackground(), thickness);
  121.             }
  122.             else if (selection.equals("Line")) {
  123.                 currShape = new Line(currColor.getBackground(), thickness);
  124.             }
  125.         }
  126.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement