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

📄 dsdesktoppane.java

📁 用JAVA编写的绘图程序 功能简介: 支持存储
💻 JAVA
字号:
package drawsmart.itsv.basic;

import java.awt.*;
import javax.swing.*;
import javax.swing.event.MouseInputListener;
import drawsmart.itsv.framework.JDSComponentface;
import drawsmart.itsv.framework.JDSLineface;
import drawsmart.itsv.framework.JDSDesktopface;
import java.util.ArrayList;
import java.awt.Rectangle;
import drawsmart.itsv.tool.DSArrayListUnRe;
import drawsmart.itsv.tool.GetResource;
import drawsmart.itsv.framework.JDSXMLReadWriteface;
import java.io.*;
import java.util.*;
import drawsmart.itsv.model.SetupDataModel;
import drawsmart.itsv.framework.JDSSetupDataModelface;

/**
 * <p>Title: 绘图桌面基类</p>
 * <p>Description: 实现JDSDesktopface接口 但是没有实现 fireComponentChanged 的两个方法,setMouseSyle 鼠标样式设置方法,在本抽象类的继承类中实现</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author 崔江
 * @version 2.0
 */

public abstract class DSDesktopPane extends JPanel implements JDSDesktopface
{
  //存放已经添加的组件
  private ArrayList arrayComponent=new ArrayList();

  //存放已经添加的连接器
  private ArrayList arrayLine=new ArrayList();

  //选中的组件
  private JDSComponentface[] jDSComponentfacesSelect;

  //记录可以恢复的组件
  private DSArrayListUnRe arrayListUnRe=new DSArrayListUnRe();

  private HashMap hashMap;

  private JDSSetupDataModelface jDSSetupDataModelface=null;

  private int unre=-1;

  //
  private Rectangle rectangle=null;

  //换行符
  String newline=System.getProperty("line.separator");

  //生成XML文档
  String encoding=GetResource.getXMLResource("encoding");

  //XML接口
  JDSXMLReadWriteface jDSXMLReadWriteface;

  Random iRandom=null;

  public DSDesktopPane(JDSXMLReadWriteface jDSXMLReadWriteface,HashMap hashMap,JDSSetupDataModelface jDSSetupDataModelface)
  {
    super();
    this.hashMap=hashMap;
    //将布局器至空,并设置颜色为桌面色
    setLayout(null);
    //setBackground(UIManager.getColor("Desktop.background"));
    setBackground(Color.white);

    this.jDSXMLReadWriteface=jDSXMLReadWriteface;

    long long1=System.currentTimeMillis();
    iRandom=new Random(long1);

    this.jDSSetupDataModelface =jDSSetupDataModelface;
  }

  /**
   * 绘制组件及其选取状态\拉框选取时的框线,实现接口方法
   * 此方法需要重写
   * @param g
   */

  public void paintChildren(Graphics g)
  {

  }

  /** 刷新一个组件的所有连接的线 */
  public void fireAssoicatorChanged()
  {

  }

  /** 添加鼠标监听器
   *
   * @param mouseInputListener */
  public void addMouseInputListener(MouseInputListener mouseInputListener) {
    this.addMouseListener(mouseInputListener);
    this.addMouseMotionListener(mouseInputListener);
  }

  /**
   *移除鼠标监听器
   * @param l
   */
  public void removeMouseInputListener(MouseInputListener l) {
    this.removeMouseListener(l);
    this.removeMouseMotionListener(l);
  }

  /**
   * 取得某点上的组件
   * @param point 鼠标单击时的点
   * @return
   */
  public JDSComponentface getJDSComponent(Point point) {
    JDSComponentface[] jDSComponentfaces=getJDSAllComponent();
    for (int i = 0; i < jDSComponentfaces.length; i++) {
      Rectangle rectangle = jDSComponentfaces[i].getRectSize();
      if ( rectangle.contains(point.getX(),point.getY())) {
        return jDSComponentfaces[i];
      }
    }
    return null;
  }

  /**
   * 取得某点上的直线
   * @param point 鼠标单击时的点
   * @return
   */
  public JDSLineface getJDSLineface(Point point)
  {
    JDSLineface[] jDSLineface=getJDSAllLineface();
    for (int i = 0; i < jDSLineface.length; i++) {
      if ( jDSLineface[i].containPoint(point)) {
        return jDSLineface[i];
      }
    }
    return null;
  }

  /** 删除选取的组件
   *
   * @param jDSComponent 选取的组件*/
  public void removeSelectionJDSComponent(JDSComponentface jDSComponent) {
    arrayComponent.remove(jDSComponent);
  }

  /** 删除选取的直线
     *
     * @param jDSComponent */
  public void removeSelectionJDSLine(JDSLineface jDSLineface)
  {
    arrayLine.remove(jDSLineface);
  }

  /** 获得所有组件
   *  主要用于刷新和存储
   * */
  public JDSComponentface[] getJDSAllComponent() {
    Object[] objects=arrayComponent.toArray();
    JDSComponentface[] jDSComponents=new JDSComponentface[objects.length];
    for(int i=0;i<jDSComponents.length;i++)
    {
      jDSComponents[i]=(JDSComponentface)objects[i];
    }

    return jDSComponents;
  }

  /** 获得所有连接器
   * 用于存储和加载 */
  public JDSLineface[] getJDSAllLineface() {
    Object[] objects=arrayLine.toArray();
    JDSLineface[] jDSLinefaces=new JDSLineface[objects.length];
    for(int i=0;i<jDSLinefaces.length;i++)
    {
      jDSLinefaces[i]=(JDSLineface)objects[i];
    }

    return jDSLinefaces;
  }

  /**
   * 添加一个组件
   * @param jDSComponent 添加的组件
   */
  public void addJDSComponent(JDSComponentface jDSComponent) {
    arrayComponent.add(jDSComponent);
  }

  /**
   * 添加一个连接器
   * @param jDSLineface 添加的连接器
   */
  public void addJDSLineface(JDSLineface jDSLineface) {
    //判断是否重复
    boolean bool=true;
    for(int i=0;i<arrayLine.size();i++)
    {
      JDSLineface jDSLinefaces=(JDSLineface)arrayLine.get(i);
      if(jDSLinefaces.getJDSComponentEnd()==jDSLineface.getJDSComponentEnd() && jDSLinefaces.getJDSComponentStart()==jDSLineface.getJDSComponentStart())
      {
        bool=false;
        break;
      }
    }

    //如果不重复
    if (bool==true)
    arrayLine.add(jDSLineface);
  }

  /**
   * 设置已经选中的组件
   */
  public void setSelectComponentface(JDSComponentface[] jDSComponent) {
    jDSComponentfacesSelect=jDSComponent;
  }

  /**
   * 设置已经选中的组件
   */
  public JDSComponentface[] getSelectComponentface() {
    return jDSComponentfacesSelect;
  }
  /** 设置所有组件
   *  主要用于刷新和存储
   * */
  public void setJDSAllComponent(JDSComponentface[] jDSComponentfaces) {
    arrayComponent.clear();
    for(int i=0;i<jDSComponentfaces.length;i++)
    {
      if(jDSComponentfaces[i]!=null)
        arrayComponent.add(jDSComponentfaces[i]);
    }
  }

  /** 设置所有连接器
   * 用于存储和加载 */
  public void setJDSAllLineface(JDSLineface[] JDSLinefaces) {
    arrayLine.clear();
    for(int i=0;i<JDSLinefaces.length;i++)
    {
      if(JDSLinefaces[i]!=null)
        arrayLine.add(JDSLinefaces[i]);
    }

  }
  /**
   * 删除所有的组件
   */
  public void delAllJDSComponent()
  {
    arrayComponent.clear();
  }

  /**
   * 添加一个连接器
   * @param jDSLineface 添加的连接器
   */
  public void delAllJDSLineface()
  {
    arrayLine.clear();
  }

  /**
   * 设置选择框
   * @param rectangle Rectangle
   */
  public void setSelectRect(Rectangle rectangle)
  {
    this.rectangle=rectangle;
  }

  /**
   * 获得选择框
   * @return Rectangle
   */
  public Rectangle getSelectRect()
  {
    return rectangle;
  }
  /**
   * 添加可恢复的组件
   * @param jDSComponentfacesUnRe JDSComponentface[]
   * @param jDSLinefacesUnRe JDSLineface
   */
  public void addUnReList()
  {
    //System.out.println("UNRE unre:"+unre+" size"+arrayListUnRe.size());
    //同步
    if(unre!=(arrayListUnRe.size()-1))
    {
      //System.out.println("执行了删除操作");
      arrayListUnRe.removeRange(unre+1,arrayListUnRe.size());
      /*
      for(;unre!=(arrayListUnRe.size()-1);)
      {
        arrayListUnRe.remove(arrayListUnRe.size()-1);
      }*/
    }
    Object[] obj=new Object[2];
    //System.out.println("UNRE unre:"+unre+" size:"+arrayListUnRe.size());
    //JDSComponentface jDSComponentface=getJDSAllComponent()[0];
    //System.out.println("X:"+jDSComponentface.getComponentX()+" Y:"+jDSComponentface.getComponentY());
    String spath=GetResource.getXMLResource("temppath");
    File objfile=new File(spath+"tempunre.xml");
    if(!objfile.exists())
    {
      try
      {
        objfile.createNewFile();
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
    }
    try
    {
      BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new
          FileOutputStream(objfile, false));
      String str1 = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>" +
          newline;
      String str2 = "<dramflow>" + newline;
      String str3 = "</dramflow>" + newline;
      bufferedOutputStream.write(str1.getBytes());
      bufferedOutputStream.write(str2.getBytes());
      bufferedOutputStream.write(str3.getBytes());
      bufferedOutputStream.close();
      //写入XML文档
      jDSXMLReadWriteface.insertElement(objfile,this);
      obj=jDSXMLReadWriteface.openElementUnRe(objfile,this);
      objfile.delete();
    }
    catch(Exception ioe)
    {
      ioe.printStackTrace();
    }

    /*
    JDSComponentface[] jDSComponentfacesUnRe=new JDSComponentface[getJDSAllComponent().length];
    for(int i=0;i<jDSComponentfacesUnRe.length;i++)
    {
      jDSComponentfacesUnRe[i]=(JDSComponentface)getJDSAllComponent()[i].cloneComponentFull();
    }

    JDSLineface[] jDSLinefacesUnRe=new JDSLineface[getJDSAllLineface().length];
    for(int i=0;i<jDSLinefacesUnRe.length;i++)
    {
      jDSLinefacesUnRe[i]=(JDSLineface)getJDSAllLineface()[i].cloneComponent();
    }
    obj[0]=jDSComponentfacesUnRe;
    obj[1]=jDSLinefacesUnRe;*/


    ArrayList arrayListComponentUnRe=(ArrayList)obj[0];
    ArrayList arrayListLineUnRe=(ArrayList)obj[1];

    JDSComponentface[] jDSComponentfacesUnRe=(JDSComponentface[])arrayListComponentUnRe.toArray(new JDSComponentface[0]);
    JDSLineface[] jDSJDSLinefaceUnRe=(JDSLineface[])arrayListLineUnRe.toArray(new JDSLineface[0]);

    obj[0]=jDSComponentfacesUnRe;
    obj[1]=jDSJDSLinefaceUnRe;

    arrayListUnRe.add(obj);
    unre=arrayListUnRe.size()-1;
    //System.out.println(unre);
  }

  /**
   * 获得上一个组件
   */
  public void getUnJDS()
  {
    //System.out.println("UN unre:"+unre+" size"+arrayListUnRe.size());
    if(unre<=-1) return;
    unre--;
    if(arrayListUnRe.size()==0)
    {
      unre=-1;
      return;
    }
    Object[] obj;
    if(unre==-1)
    {
      obj=(Object[])arrayListUnRe.get(0);
    }
    else
    {
        obj=(Object[])arrayListUnRe.get(unre);
    }

    JDSComponentface[] jDSComponentfacesUnRe=(JDSComponentface[])obj[0];

    //JDSComponentface jDSComponentface=jDSComponentfacesUnRe[0];
    //System.out.println("X:"+jDSComponentface.getComponentX()+" Y:"+jDSComponentface.getComponentY());

    JDSLineface[] jDSLinefacesUnRe=(JDSLineface[])obj[1];

    setJDSAllComponent(jDSComponentfacesUnRe);
    setJDSAllLineface(jDSLinefacesUnRe);
    //System.out.println(unre);
    repaint();
  }

  /**
   * 获得下一个组件
   */
  public void getReJDS()
  {

  }

  /**
   * 获得中子值
   * @return long
   */
  public int getRandom()
  {
    return iRandom.nextInt();
  }

  /**
   *
   * @param setupDataModel SetupDataModel
   * @param sID String
   */
  public void addSetupDataModel(SetupDataModel setupDataModel,String sID)
  {
    hashMap.put(sID,setupDataModel);
  }

  /**
   * 删除环节
   * @param sID String
   */
  public void delSetupDataModel(String sID)
  {
    hashMap.remove(sID);
  }
  //获得环节
  public JDSSetupDataModelface getJDSSetupDataModelface()
  {
    return jDSSetupDataModelface;
  }
  /**
   * 获得配置集合
   * @return HashMap
   */
  public HashMap getHashMap()
  {
    return hashMap;
  }
  /**
   * 重置配置集合
   * @param hashMap HashMap
   */
  public void setHashMap(HashMap hashMapset)
  {
    hashMap.clear();
    //this.hashMap=hashMapset;
    //测试
    Set set = hashMapset.keySet();
    Iterator iterator = set.iterator();
    while (iterator.hasNext()) {
      String str1 = iterator.next().toString();
      SetupDataModel setupDataModel=(SetupDataModel)hashMapset.get(str1);
      hashMap.put(str1,setupDataModel);
    }

  }
}

⌨️ 快捷键说明

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