示例#1
0
    // Mouse Dragged
    @Override
    public void mouseDragged(MouseEvent e) {

      if (SwingUtilities.isLeftMouseButton(e)) {
        // Get coordinate (Relative x, y)
        cur_x = Coor_of_screen(e.getX()) + prefix_x();
        cur_y = Coor_of_screen(e.getY()) + prefix_y();
        cur_d = Get_Depth();

        // Out of bound check
        if ((pre_x != cur_x) || (pre_y != cur_y)) {
          //	  System.out.println("On: "+cur_x + ", " + cur_y);

          if (!selection_m) {

            if (left.sec_pan.change_s) {

              if (cells[cur_d][cur_x][cur_y] != null) {
                // XOR function
                cells[cur_d][cur_x][cur_y].state =
                    cells[cur_d][cur_x][cur_y].state ^ left.State.table;
              }

            } else {
              cells[cur_d][cur_x][cur_y] = left.Gen_Cell();
            }

            // Save it as reference if it status = true
            if (cells[cur_d][cur_x][cur_y].Any_state()) {
              System.out.println("Save for queue:" + cur_d + " " + cur_x + " " + cur_y);
              Q_of_cell.add(new Coor(cur_d, cur_x, cur_y));
            }
            buffering = false;
            re_paint();

          } else {

            if (selected) {

              if (select_drag) {
                // Calculate the different
                select_offset.Update(cur_x - pre_x, cur_y - pre_y);

              } else {
                // Normal situation
                // Draw rectangle on real time & highlight those cell which is alive
                shape = new Red_rect(startPoint, e);
                // Adjust select series data realtime to draw select area
                select_area.Update(
                    Coor_of_screen(shape.x),
                    Coor_of_screen(shape.y),
                    Get_Depth(),
                    prefix_x(),
                    prefix_y(),
                    Coor_of_screen(shape.width) + 2,
                    Coor_of_screen(shape.height) + 1);
              }

              repaint();

            } else {
              if (pasted) {
                // Calaulate different
                copyed_x += (cur_x - pre_x);
                copyed_y += (cur_y - pre_y);
                repaint();
              }
            }
          }
          // Change coordinate if it got changed
          pre_x = cur_x;
          pre_y = cur_y;
          pre_d = cur_d;
        }
      }
    }
示例#2
0
    // Mouse click
    @Override
    public void mousePressed(MouseEvent e) {

      // Get cooperate
      int cur_x = Coor_of_screen(e.getX()) + prefix_x();
      int cur_y = Coor_of_screen(e.getY()) + prefix_y();
      int cur_d = Get_Depth();
      //		  System.out.println("On "+cur_x + " " + cur_y);

      // Left Click
      if (e.getButton() == MouseEvent.BUTTON1) {
        System.out.println("Left click");

        if (selection_m) {

          if (selected) {
            // Is it dragging now or normal selected
            if (select_drag) {
              // Out bound while dragging
              if (!select_offset.Check_bound(cur_x, cur_y)) {
                System.out.println("Out of bound - While draging");
                // Make change perm
                select_area.Copy(
                    prefix_x() + select_offset.x,
                    prefix_y() + select_offset.y,
                    Get_Depth(),
                    cells,
                    Q_of_cell,
                    true);
                // Indicate finished moving
                select_offset = null;
                select_area = null;

                repaint();
                // Reset flag
                select_drag = false;
                selected = false;

                // Update change
                repaint();
              } else {
                // Update coor to prevend jerk happen
                pre_x = cur_x;
                pre_y = cur_y;
              }

            } else {

              // Selected something currently, check is it out of scope?

              if (!select_area.Check_bound(cur_x, cur_y)) {
                System.out.println("Out of bound - Selected Area");

                // Out of scope, so cancel selection.
                selected = false;

                // Update change
                repaint();

              } else {
                // In scope, ready to drag around. (Save the ini value)
                select_drag = true;
                // Take snapshot for cache
                select_area.Snapshot(cell_width, border_width, cells, left);
                // Update cur mouse coor
                pre_x = cur_x;
                pre_y = cur_y;
              }
            }

          } else {

            if (pasted) {

              // Cache
              int weight = copyed_area.weight;
              int height = copyed_area.height;
              int pos_x = copyed_area.Get_Offset_x(copyed_x);
              int pos_y = copyed_area.Get_Offset_y(copyed_y);
              if (cur_x < pos_x
                  || cur_x > pos_x + weight
                  || cur_y < pos_y
                  || cur_y > pos_y + height) {
                // Out bound, make change perm & reset flag
                System.out.println("Out bound - pasted");
                copyed_area.Paste(pos_x, pos_y, Get_Depth(), cells, Q_of_cell);

                repaint();
                // Reset flag
                pasted = false;
              } else {
                System.out.println("In Scope");
                // In scope, ready to drag around. (Save the ini value)
                // Update cur mouse coor
                pre_x = cur_x;
                pre_y = cur_y;
                System.out.println("Current: " + cur_x + " " + cur_y);
              }

            } else {
              // Brand new start
              startPoint = e.getPoint();
              select_area = new Selected_area();
              selected = true;
            }
          }

        } else {
          // Not selection mode, normal operation

          // State mode on?
          if (left.sec_pan.change_s && cells[cur_d][cur_x][cur_y] != null) {
            // XOR function
            cells[cur_d][cur_x][cur_y].state = cells[cur_d][cur_x][cur_y].state ^ left.State.table;

          } else if (!left.sec_pan.change_s) {
            // Gen cell to replace current one
            cells[cur_d][cur_x][cur_y] = left.Gen_Cell();
          }

          // Save it as reference if it status = true
          try {
            if (cells[cur_d][cur_x][cur_y].Any_state()) {
              Q_of_cell.add(new Coor(cur_d, cur_x, cur_y));
            }
          } catch (NullPointerException r) {
            System.out.println("Null");
          }
          buffering = false;
          re_paint();
        }
      }

      // Right click
      if (e.getButton() == MouseEvent.BUTTON3) {

        System.out.println("Right click \n x: " + cur_x + "  y: " + cur_y);

        if (cells[cur_d][cur_x][cur_y] != null) {

          if (left.sec_pan.change_s) {
            // update state only
            left.State.update_state(cells[cur_d][cur_x][cur_y].state);

          } else {
            left.Update_status(cells[cur_d][cur_x][cur_y]);
          }

        } else {
          left.Erase_State();
        }
      }
    }