⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpanel.java

📁 一个小型网络仿真器的实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

    if(!connecting) return;

    if(lastSelected!=null) {
      currentHot=e.getPoint();
      if(lastHover!=null) {
        lastHover.setSelected(false);
        lastHover=null;
      }
      SimComponent comp=isHit(e.getPoint());
      if (comp!=null && comp!=lastSelected) {
        comp.setSelected(true);
        lastHover=comp;
      }
      repaint();
    }
    else {
      SimComponent comp=isHit(e.getPoint());
      if(comp!=null) {
        if(lastHover!=null) lastHover.setSelected(false);
        comp.setSelected(true);
        lastHover=comp;
        repaint();
      }
      else if(lastHover!=null) {
        lastHover.setSelected(false);
        lastHover=null;
        repaint();
      }
    }
  }

  public void mouseEntered(MouseEvent e) {
    mouseMoved(e);
  }

  public void mouseExited(MouseEvent e) {
  }

  public void mouseClicked(MouseEvent e) {
  }

///////////////////////////// helpers ////////////////////////////////////

//deselect all components
  private void deselectAll() {
    java.util.List components = theSim.getSimComponents();
    for(int i=0;i<components.size();i++) {
      SimComponent thisComp=(SimComponent)components.get(i);
      thisComp.setSelected(false);
    }
  }

//test if the point hits any component, return that component, or null
  private SimComponent isHit(Point testPoint) {
    java.util.List components = theSim.getSimComponents();
    for(int i=0;i<components.size();i++) {
      SimComponent thisComp=(SimComponent)components.get(i);
      Image img=thisComp.getImage(this);
      int w=img.getWidth(this);
      int h=img.getHeight(this);
      Rectangle compRect=new Rectangle(thisComp.getX()-w/2,thisComp.getY()-h/2,w,h);
      if(compRect.contains(testPoint)) return thisComp;
    }
    return null;
  }

//test hit and select base on a window
  private void windowSelect(Rectangle window) {
    java.util.List components = theSim.getSimComponents();
    for(int i=0;i<components.size();i++) {
      SimComponent thisComp=(SimComponent)components.get(i);
      Image img=thisComp.getImage(this);
      int w=img.getWidth(this);
      int h=img.getHeight(this);
      Rectangle compRect=new Rectangle(thisComp.getX()-w/2,thisComp.getY()-h/2,w,h);
      if(window.intersects(compRect))
        thisComp.setSelected(true);
      else
        thisComp.setSelected(false);
    }
  }

//test hit and toggle selection base on a window
  private void windowToggle(Rectangle window) {
    java.util.List components = theSim.getSimComponents();
    for(int i=0;i<components.size();i++) {
      SimComponent thisComp=(SimComponent)components.get(i);
      Image img=thisComp.getImage(this);
      int w=img.getWidth(this);
      int h=img.getHeight(this);
      Rectangle compRect=new Rectangle(thisComp.getX()-w/2,thisComp.getY()-h/2,w,h);
      if(window.intersects(compRect)) {
        if(!toggledOn.contains(thisComp) && !toggledOff.contains(thisComp)) {
          if(thisComp.isSelected()) {
            thisComp.setSelected(false);
            toggledOff.add(thisComp);
          }
          else {
            thisComp.setSelected(true);
            toggledOn.add(thisComp);
          }
        }
      }
      else {
        if(toggledOn.contains(thisComp)) {
          thisComp.setSelected(false);
          toggledOn.remove(thisComp);
        }
        else if(toggledOff.contains(thisComp)) {
          thisComp.setSelected(true);
          toggledOff.remove(thisComp);
        }
      }
    }
  }

//move all selected components
  private void moveSelectedComponents(int dx,int dy) {
    java.util.List components = theSim.getSimComponents();
    for(int i=0;i<components.size();i++) {
      SimComponent thisComp=(SimComponent)components.get(i);
      if(!thisComp.isSelected()) continue;
      thisComp.setLocation(thisComp.getX()+dx,thisComp.getY()+dy);

    //Link tagging support
      if(theGUI.isLinkTagging() && !thisComp.getCompClass().equals("Link")) {
        //search for link neighbors
        for(int j=0;j<thisComp.neighborCount();j++) {
          SimComponent n=(SimComponent)thisComp.neighbor(j);
          if(n.getCompClass().equals("Link") && !n.isSelected()) {
            for(int k=0;k<n.neighborCount();k++) {
              SimComponent anchorComp=(SimComponent)n.neighbor(k);
              if(anchorComp!=thisComp) { //this is the one!
                n.setLocation((thisComp.getX()+anchorComp.getX())/2,(thisComp.getY()+anchorComp.getY())/2);
                break;
              }
            }
          }
        }
      }

    //Apps tagging support
      if(theGUI.isAppTagging() && !thisComp.getCompClass().equals("Application")) {
        //search for application neighbors
        for(int j=0;j<thisComp.neighborCount();j++) {
          SimComponent n=(SimComponent)thisComp.neighbor(j);
          if(n.getCompClass().equals("Application") && !n.isSelected()) {
            n.setLocation(n.getX()+dx,n.getY()+dy);
          }
        }
      }
    }
  }

//connect two components
  private boolean connectComponents(SimComponent comp1,SimComponent comp2) {
    if(comp1.isConnectable(comp2) && comp2.isConnectable(comp1)) {
      comp1.addNeighbor(comp2);
      comp2.addNeighbor(comp1);
      theGUI.connectionsChanged();
      return true;
    }
    else {
      JOptionPane.showMessageDialog(theGUI.getMainFrame(),"Please check connection rules...",
                                    "Components Not Connectable",JOptionPane.INFORMATION_MESSAGE);
      return false;
    }
  }

//reset all state variables except the mode (the 'connecting' boolean)
  private void clearAllState() {
    origin=null;
    window=null;
    moving=false;
    anchored=false;
    toggling=false;
    toggledOn=null;
    toggledOff=null;
    lastHover=null;
    lastSelected=null;
    currentHot=null;
  }

/////////////////////////// end helpers //////////////////////////////////

//////////////////////// call from SimGUI ////////////////////////////////

//change UI mode
  void changeMode(int mode) {
    switch(mode) {
      case NORMAL:
        deselectAll();
        setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        clearAllState();
        connecting=false;
        repaint();
        break;
      case CONNECT:
        deselectAll();
        setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
        clearAllState();
        connecting=true;
        repaint();
        break;
    }
  }

  void setZooming(double factor) {
    isZooming=true;
    zoomFactor=factor;
    repaint();
  }

  void resetZooming() {
    isZooming=false;
    zoomFactor=1.0;
    repaint();
  }

  void registerAnimation(SimComponent acomp,int compinfo_id) {
    anim.addMember(acomp,compinfo_id);
  }

  void unregisterAnimation(SimComponent acomp,int compinfo_id) {
    anim.removeMember(acomp,compinfo_id);
  }

  void stopAnim() {
    anim.stopAnim();
  }

  void startAnim() {
    anim.startAnim();
  }

/*
  Image getFullTopoImage(int width, int height) {
    Image fulltopo=createImage(width,height);
    paintComponent(fulltopo.getGraphics());
    return fulltopo;
  }
*/

//////////////////////// end call from SimGUI /////////////////////////////

///////////////////// main painting //////////////////////////////

  protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    java.util.List components = theSim.getSimComponents();
    int i,j;

    //draw all connections
    g.setColor(Color.black);
    for(i=0;i<components.size();i++) {
      SimComponent thisComp=(SimComponent)components.get(i);
      SimComponent []neighbors=thisComp.getNeighbors();
      for(j=0;j<neighbors.length;j++) {
        if(components.indexOf(neighbors[j])>i) {//only connect to `downstream'
          if(isZooming) {
            g.drawLine((int)(thisComp.getX()*zoomFactor),(int)(thisComp.getY()*zoomFactor),
                        (int)(neighbors[j].getX()*zoomFactor),
                        (int)(neighbors[j].getY()*zoomFactor));
          }
          else {
            g.drawLine(thisComp.getX(),thisComp.getY(),
                        neighbors[j].getX(),neighbors[j].getY());
          }
        }
      }
    }

    //draw all components
    for(i=0;i<components.size();i++) {
      SimComponent thisComp=(SimComponent)components.get(i);
      int x,y;
      if(isZooming) {
        x=(int)(thisComp.getX()*zoomFactor);
        y=(int)(thisComp.getY()*zoomFactor);
      }
      else {
        x=thisComp.getX();
        y=thisComp.getY();
      }
      Image img=thisComp.getImage(this);
      int w,h;
      if(isZooming) {
        w=(int)(img.getWidth(this)*zoomFactor);
        h=(int)(img.getHeight(this)*zoomFactor);
      }
      else {
        w=img.getWidth(this);
        h=img.getHeight(this);
      }
      g.drawImage(img,x-w/2,y-h/2,w,h,this);

      String name=thisComp.getName();
      Font f;
      if(isZooming)
        f=new Font("Helvetica",Font.PLAIN,(int)(theSim.DEFAULT_COMP_POINTSIZE*zoomFactor));
      else
        f=new Font("Helvetica",Font.PLAIN,theSim.DEFAULT_COMP_POINTSIZE);
      FontMetrics fm=g.getFontMetrics(f);
      g.setFont(f);
      g.setColor(Color.white);
      g.drawString(name,x-w/2+(w-fm.stringWidth(name))/2,y-h/2+(h-fm.getHeight())/2+fm.getAscent());

      if(thisComp.isSelected()) {
        g.drawImage(shadowImg,x-w/2-5,y-h/2-5,w+10,h+10,this);
      }
/*
      else if(thisComp.isTagged(1)) {
        g.drawImage(shadowImgT1,x-w/2-5,y-h/2-5,w+10,h+10,this);
      }
*/
    }

    //draw selection rectangle
    if(anchored || toggling) {
      g.setColor(Color.white);
      if(isZooming)
        g.drawRect((int)(window.x*zoomFactor),(int)(window.y*zoomFactor),
                    (int)(window.width*zoomFactor),(int)(window.height*zoomFactor));
      else
        g.drawRect(window.x,window.y,window.width,window.height);
    }

    //draw connect line
    if(connecting & lastSelected!=null) {
      g.setColor(Color.white);
      if(isZooming)
        g.drawLine((int)(lastSelected.getX()*zoomFactor),(int)(lastSelected.getY()*zoomFactor),
                    (int)(currentHot.x*zoomFactor),(int)(currentHot.y*zoomFactor));
      else
        g.drawLine(lastSelected.getX(),lastSelected.getY(),currentHot.x,currentHot.y);
    }

    //animation support
    anim.doAnim(g);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -