Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void handleMouseEvent(MouseEvent e) {
- if (e.getComponent().getName().equals("colorPicker")){
- currColor.setBackground(e.getComponent().getBackground());
- }
- if (e.getComponent().getName() == "drawingSurface"){
- if (e.getID() == MouseEvent.MOUSE_MOVED) {
- coords.setText(e.getX() + ", " + e.getY());
- }
- if (currShape instanceof Freehand) {
- if (e.getID() == MouseEvent.MOUSE_DRAGGED) {
- currShape.addPoint(e.getPoint());
- localTempStream.onNext(currShape);
- }
- else if (e.getID() == MouseEvent.MOUSE_PRESSED) {
- currShape = new Freehand(currColor.getBackground(), thickness);
- }
- else if (e.getID() == MouseEvent.MOUSE_RELEASED) {
- localFinalStream.onNext(currShape);
- outgoingNetworkStream.onNext(currShape);
- }
- }
- else if (currShape instanceof Rectangle) {
- if (e.getID() == MouseEvent.MOUSE_DRAGGED) {
- currShape.setP2(e.getPoint());
- localTempStream.onNext(currShape);
- }
- else if (e.getID() == MouseEvent.MOUSE_PRESSED) {
- currShape = new Rectangle(currColor.getBackground(), thickness);
- currShape.setP1(e.getPoint());
- }
- else if (e.getID() == MouseEvent.MOUSE_RELEASED) {
- currShape.setP2(e.getPoint());
- localFinalStream.onNext(currShape);
- outgoingNetworkStream.onNext(currShape);
- }
- }
- else if (currShape instanceof Oval) {
- if (e.getID() == MouseEvent.MOUSE_DRAGGED) {
- currShape.setP2(e.getPoint());
- localTempStream.onNext(currShape);
- }
- else if (e.getID() == MouseEvent.MOUSE_PRESSED) {
- currShape = new Oval(currColor.getBackground(), thickness);
- currShape.setP1(e.getPoint());
- }
- else if (e.getID() == MouseEvent.MOUSE_RELEASED) {
- currShape.setP2(e.getPoint());
- localFinalStream.onNext(currShape);
- outgoingNetworkStream.onNext(currShape);
- }
- }
- else if (currShape instanceof Line) {
- if (e.getID() == MouseEvent.MOUSE_DRAGGED) {
- currShape.setP2(e.getPoint());
- localTempStream.onNext(currShape);
- }
- else if (e.getID() == MouseEvent.MOUSE_PRESSED) {
- currShape = new Line(currColor.getBackground(), thickness);
- currShape.setP1(e.getPoint());
- }
- else if (e.getID() == MouseEvent.MOUSE_RELEASED) {
- currShape.setP2(e.getPoint());
- localFinalStream.onNext(currShape);
- outgoingNetworkStream.onNext(currShape);
- }
- }
- }
- }
- private void handleActionEvent(ActionEvent a) {
- // menu code
- if (a.getActionCommand().equals("host")) {
- int port = 8700; // Integer.parseInt(JOptionPane.showInputDialog(new JFrame(), "Enter port to start server on:"));
- UPnP.openPortTCP(port);
- Observable.just(this.server = new RxServer(port, client.allEvents)).subscribeOn(Schedulers.io()); // start server on new thread
- Observable.just(client).subscribeOn(Schedulers.io()).subscribe(i -> i.connect("127.0.0.1", port)); // connect on new thread
- serverUp = true;
- }
- else if (a.getActionCommand().equals("join")) {
- JFrame popup = new JFrame();
- String ip = (String)JOptionPane.showInputDialog(popup, "IP:");
- int port = Integer.parseInt((String)JOptionPane.showInputDialog(popup, "Port:"));
- Observable.just(client).subscribeOn(Schedulers.io()).subscribe(i -> {
- i.connect(ip, port);
- i.allEvents.clear();
- localFinalStream.onNext(new Clear());
- });
- }
- else if (a.getActionCommand().equals("disconnect")) {
- if (serverUp) {
- Observable.just(server).subscribeOn(Schedulers.io()).subscribe(i -> i.shutdown());
- }
- Observable.just(client).subscribeOn(Schedulers.io()).subscribe(i -> i.disconnect());
- }
- else if (a.getActionCommand().equals("exit")) {
- System.exit(0);
- }
- // toolbar code
- else if (a.getActionCommand().equals("clearButton")){
- Clear clear = new Clear();
- localTempStream.onNext(clear);
- outgoingNetworkStream.onNext(clear);
- }
- else if(a.getActionCommand().equals("shapePicker")) {
- JComboBox cb = (JComboBox)a.getSource();
- String selection = (String)cb.getSelectedItem();
- if (selection.equals("Freehand")) {
- currShape = new Freehand(currColor.getBackground(), thickness);
- }
- else if (selection.equals("Rectangle")) {
- currShape = new Rectangle(currColor.getBackground(), thickness);
- }
- else if (selection.equals("Oval")) {
- currShape = new Oval(currColor.getBackground(), thickness);
- }
- else if (selection.equals("Line")) {
- currShape = new Line(currColor.getBackground(), thickness);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement