// 以下是菜单、工具栏及快捷键的命令响应方法
  /** 撤消操作 */
  protected void commandUndo()
  {
    // 如果正在编辑标签文字,则结束编辑,刷新显示
    view.checkLabelEditingStatus();
    // 恢复鼠标状态
    view.isPuttingImg = false;
    setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    if (!doc.drawObjects.isEmpty())
    {
      doc.drawObjects.remove(doc.drawObjects.size() - 1);
      doc.setDirty(true);
      doc.clearBuffer(view.bgcolor);
      view.repaint();
    }
  }
  /** 字体 */
  protected void commandFont()
  {
    // 如果正在编辑标签文字,则结束编辑,刷新显示
    view.checkLabelEditingStatus();
    // 恢复鼠标状态
    view.isPuttingImg = false;
    setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
// 显示对话框,选择字体
    DrawFontChooser chooser = new DrawFontChooser(this, "请选择字体", true, view.font);
    if (chooser.showDialog())
      view.font = chooser.getSelectedFont();
  }
  /** 新建 */
  protected void commandNew()
  {
    // 如果正在编辑标签文字,则结束编辑,刷新显示
    view.checkLabelEditingStatus();
    // 恢复鼠标状态
    view.isPuttingImg = false;
    setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    doc.newDocument();
    view.repaint();
  }
 /**显示示意图 */
  protected void commandShow()
  {
    // 如果正在编辑标签文字,则结束编辑,刷新显示
    view.checkLabelEditingStatus();
    // 恢复鼠标状态
    view.isPuttingImg = false;
    setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    doc.showDocument();
    view.repaint();
  }
  /** 保存 */
  protected void commandSave()
  {
    // 如果正在编辑标签文字,则结束编辑,刷新显示
    view.checkLabelEditingStatus();
    // 恢复鼠标状态
    view.isPuttingImg = false;
    setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    doc.saveDocument();    
  }
  /**打印 */
  protected void commandPrint()
  {
    // 如果正在编辑标签文字,则结束编辑,刷新显示
    view.checkLabelEditingStatus();
     // 恢复鼠标状态
    view.isPuttingImg = false;
    setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    doc.printDLPic();
  }
  /** 黑色画笔 */
  protected void commandBlackPen()
  {
    // 如果正在编辑标签文字,则结束编辑,刷新显示
    view.checkLabelEditingStatus();
    // 恢复鼠标状态
    view.isPuttingImg = false;
    setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    view.color = Color.black;
  }
  /** 电缆 */
  protected void commandDianLan()  
  {
    // 如果正在编辑标签文字,则结束编辑,刷新显示
    view.checkLabelEditingStatus();
    view.isPuttingImg = true;
    view.ImgIcon = DianLan;
    setCursor(new Cursor(Cursor.HAND_CURSOR));
  }
  /** 传感器*/
  protected void commandCGQLeft()
  {
    // 如果正在编辑标签文字,则结束编辑,刷新显示
    view.checkLabelEditingStatus();
    view.isPuttingImg = true;
    view.ImgIcon = CGQLeft;
    setCursor(new Cursor(Cursor.HAND_CURSOR));
  }
     /** 退出系统 */
  protected void exit()  
  {  
    if (doc.closeDocument())
    {
      dispose();
      String showRes=doc.disconnectDB(); 
      System.exit(0);      
    }
  }
}

解决方案 »

  1.   

    绘图API中应该有现成的功能,用不着自己做的,再找找.
      

  2.   

    printDoc类
    package draw;
    import java.util.*;
    import java.text.DateFormat;
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    import java.io.*;
    import com.sun.image.codec.jpeg.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import java.util.StringTokenizer; 
    import java.sql.*;  
    import java.awt.Graphics;
    import java.net.URL;
    import java.awt.print.*;
    import java.util.Calendar;
    import javax.swing.UIManager;
    /**darwprint 程序的主文档类*/
    public class PrintDoc
    {
      /** 使用该文档类的主窗口 */
      protected JFrame mainFrame;
    /** 与文档类相关的视图类 */
      protected PrintView view;
       /**使用主类 */  
     protected Draw draw;
    protected MainFrame mf;
    /** 文档的名称,不是文档在文件系统中的名称 */
      protected String name =  "未定标题";
    /** 文档在文件系统中对应的文件对象 */
      protected File file = null;
      /** 文档在文件系统所处的目录 */
      protected String folder = null;
    /** 文档的“脏”标记,true 表示文档已经被更新,false 表示文档内容没有发生变化 */
      protected boolean isDirty = false;
    /** 保存当前鼠标运动轨迹的对象 */
      public MouseTrack mouseTrack = new MouseTrack();
    /** 指示线轨迹 */
      public PointTrack pointTrack = new PointTrack();
     /** 电缆中所有对象的集合 */
      public Vector drawObjects = new Vector(10, 10);
       /** 保存从数据库中提取的电缆图所用对象的集合 */
      public Vector drawObjs = new Vector(10, 10);
     /**内存缓冲区,保存要在窗口中显示的所有对象的图象,以加快对象的绘制速度,还可以加快图象文件的输出速度。*/
      public BufferedImage bufferedImage =
        new BufferedImage(1280, 1024,   // 最大支持 1280 * 1024 的窗口大小
          BufferedImage.TYPE_INT_RGB);
      final static private String jdbcDriver ="oracle.jdbc.driver.OracleDriver";
    private boolean connected = false; 
        private Connection connection = null; 
        private ResultSet rs = null; 
        private String query = null; 
        private String rsLine = null; 
        private String driver = null; 
        private String url = null; 
        private String user = null; 
        private String password = null; 
        StringBuffer sql = new StringBuffer();  
       String  strSQL="";  
       int intDLID=001;
     /**构造函数*/
      public PrintDoc(JFrame mainFrame)
      {
        try
        {
             this.mainFrame = mainFrame;
     // 设置主窗口的标题
          setTitle();
        }
        catch(Exception ex)
        {
          ex.printStackTrace();
        }
      }
     /**设置主窗口的标题*/
      public void setTitle()
      {
        if (isDirty)
          mainFrame.setTitle("Drawprint - " + name + "*");
        else
          mainFrame.setTitle("Drawprint - " + name);
      } /**设置与文档类相关的视图类@param theView 与文档类相关的视图类*/
      public void setView(PrintView theView)
      {
        // 设置文档类与视图类的关联
        view = theView;
      }
     /** 设置文档的“脏”标记,即设置文档是否被更新,如被更新,则需要保存@param isDirty true 表示文档已经被更新,false 表示文档内容没有发生变化*/
      public void setDirty(boolean isDirty)
      {
        this.isDirty = isDirty;
        setTitle();
      }/** 返回文档的“脏”标记@return true 表示文档已经被更新,false 表示文档内容没有发生变化*/
      public boolean isDirty()
      {
        return isDirty;
      }/** 将所有可显示对象绘制在内存缓冲区中。只画那些还没有画过的对象。@param bgcolor 绘制时使用的背景色*/
      public void renderBuffer(Color bgcolor)
      { 
      
        Graphics g = bufferedImage.getGraphics();
        for(int i = 0; i < drawObjects.size(); i++)
        {
          DrawObject drawObject = (DrawObject)drawObjects.get(i);
          if (!drawObject.isRendered)
          { 
            // 调用对象的 render 方法绘制对象
            drawObject.render(g, bgcolor);
            // 设置该对象为已绘制
            drawObject.isRendered = true;
          }
       }
        
      } /**清除内存缓冲区以及所有对象的 isRendered 标记@param bgcolor 清除时使用的背景色 */
      public void clearBuffer(Color bgcolor)
      {
        Graphics g = bufferedImage.getGraphics();
        g.setColor(bgcolor);
        g.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
    // 设置电缆所有对象的标记为“没有绘制过”
        int j = drawObjects.size();
        for(int i = 0; i < j; i++)
        {
          DrawObject drawObject = (DrawObject)drawObjects.get(i);
          drawObject.isRendered = false;
        }
      }/**关闭当前文档,如果文档更新过但没有存盘则提示存盘@return 提问保存时,用户如选择“取消”或关闭窗口,则返回false,否则返回true*/ 
      protected boolean closeDocument()
      {
        if (isDirty())
        {
          int ret = JOptionPane.showConfirmDialog(mainFrame, "文档 " + name + " 尚未保存。是否保存?", 
            "提示", JOptionPane.YES_NO_CANCEL_OPTION);
          if (ret == JOptionPane.YES_OPTION)      
            return saveDocument();
          else if (ret == JOptionPane.NO_OPTION)
            return true;
          else
            return false;
        }
        else 
          return true;
      } /**新建文档*/
      public void newDocument()
      {
        if (!closeDocument())
          return;
        name = "未定标题";
        file = null;
        setDirty(false);
        clearBuffer(view.bgcolor);
        drawObjects.clear();
        mouseTrack.clear();
        pointTrack.clear();    
      }
      /**保存文档 @return 如果保存过程中用户取消操作,则返回false,否则返回true*/
      public boolean saveDocument()
      {
        if (!isDirty())
          return true;
        for(int i = 0; i < drawObjects.size(); i++)  
        {  
          DrawObject drawObject = (DrawObject)drawObjects.get(i);
          if (!drawObject.isSaved)
          {
            if(drawObject.typeID==30){  //标签对象
             DrawLabel drawlabel = (DrawLabel)drawObjects.get(i);
               int cx=drawlabel.x;                         
               int cy=drawlabel.y;                         
               String strColor=drawlabel.color.toString();    
               String strFont=drawlabel.font.toString();     
               String strCap=drawlabel.text.toString();      
             sql.append("Insert into BIAOQIANXinXi(BIAOQIANID,CX,CY,DIANLANID,");
               sql.append(" COLOR,FONT,CAPTION ) Values(" );
               sql.append(" BIAOQIANXinXiIndex.nextval ");
               sql.append(" ,"+cx+","+cy+",001,'"+strColor+"','"+strFont+"','"+strCap+"')");
               strSQL = sql.toString(); 
               sql.setLength(0);
               String showRes=UpdateDB(strSQL);
               //int ret = JOptionPane.showConfirmDialog(mainFrame,"showRes="+showRes, "提示", JOptionPane.CANCEL_OPTION);
            }else if(drawObject.typeID==3){ //直线对象
               DrawLine drawline = (DrawLine)drawObjects.get(i);
               int cx1=drawline.x1;                         
               int cy1=drawline.y1;  
               int cx2=drawline.x2;                         
               int cy2=drawline.y2;                         
               String strColor=drawline.color.toString();    
               sql.append("Insert into XIANDUANXinXi(XIANDUANID,CX1,CY1,CX2,CY2,DIANLANID,");
               sql.append(" COLOR ) Values(" );
               sql.append(" XIANDUANXinXiIndex.nextval ");
               sql.append(" ,"+cx1+","+cy1+","+cx2+","+cy2+",001,'"+strColor+"')");
            strSQL = sql.toString(); 
               sql.setLength(0);
               String showRes=UpdateDB(strSQL);
           }else if(drawObject.typeID==25){ //图标对象
               DrawIcon drawicon = (DrawIcon)drawObjects.get(i);
               int cx=drawicon.x;                         
               int cy=drawicon.y;                         
               String strIconType=drawicon.icon.toString();    
                sql.append("Insert into TUXINGXinXi(TUXINGID,TYPE,CX,CY,");
               sql.append(" DIANLANID ) Values(");
               sql.append(" TUXINGXinXiIndex.nextval ");
               sql.append(" ,'"+strIconType+"',"+cx+","+cy+",001)");
               strSQL = sql.toString(); 
               sql.setLength(0);
               String showRes=UpdateDB(strSQL);
               //int ret = JOptionPane.showConfirmDialog(mainFrame,"strIconType="+strIconType+"showRes="+showRes, "提示", JOptionPane.CANCEL_OPTION);
          }
          drawObject.isSaved = true;
         }
      } 
         return true;
      }
      

  3.   

    /**显示电缆示意图*/
      public void showDocument()
      {
      
        ResultSet rs=null;
        int intLeftPosi = 0;
         int intRightPosi = 0;     newDocument();
         
         if (!connected) { 
               
                int ret = JOptionPane.showConfirmDialog(mainFrame,"No database connected.", "提示", JOptionPane.CANCEL_OPTION);
                     
         } else { 
                if (connection == null) { 
                      
                      int ret = JOptionPane.showConfirmDialog(mainFrame,"Database connection error.", "提示", JOptionPane.CANCEL_OPTION);
                   
                } else { 
                      try { 
                             Statement stmt = connection.createStatement(); 
                             //获得当前电缆在数据库中的标签对象,并存入Vector对象
                             sql.append("Select CX,CY,COLOR,FONT,CAPTION "); 
                             sql.append(" from  BIAOQIANXinXi ");
                             sql.append(" where DIANLANID="+intDLID+" ");
        
                             strSQL = sql.toString(); 
                             sql.setLength(0);
                          
                             rs = stmt.executeQuery(strSQL);
                             
                             while(rs.next()){
             
                                int x=rs.getInt("CX");
                                int y=rs.getInt("CY");
                                String strColor=rs.getString("COLOR");
                                String strFont=rs.getString("FONT");
                                String strCap=rs.getString("CAPTION");
                                
                                intLeftPosi = strColor.lastIndexOf("[");
                                intRightPosi = strColor.lastIndexOf("]");
                                
                                strColor = strColor.substring(intLeftPosi+1,intRightPosi);
                                StringTokenizer item = new StringTokenizer(strColor, ",");
                                String sR = item.nextToken();
                                String sG = item.nextToken();
                                String sB = strColor.substring(strColor.lastIndexOf(",") + 1);
                                
                                sR = sR.substring(sR.lastIndexOf("=") + 1);
                                sG = sG.substring(sG.lastIndexOf("=") + 1);
                                sB = sB.substring(sB.lastIndexOf("=") + 1);
                                
                                int intR=Integer.parseInt(sR);
                                int intG=Integer.parseInt(sG);
                                int intB=Integer.parseInt(sB);
                                
                                Color color = new Color(intR,intG,intB);
                                
                                intLeftPosi = strFont.lastIndexOf("[");
                                intRightPosi = strFont.lastIndexOf("]");
                                
                                strFont = strFont.substring(intLeftPosi+1,intRightPosi);
                                item = new StringTokenizer(strFont, ",");
                                String sFamily = item.nextToken();
                                String sName = item.nextToken();
                                String sStyle = item.nextToken();
                                String sSize =strFont.substring(strFont.lastIndexOf(",") + 1);
                                
                                sFamily = sFamily.substring(sFamily.lastIndexOf("=") + 1);
                                sName = sName.substring(sName.lastIndexOf("=") + 1);
                                sStyle = sStyle.substring(sStyle.lastIndexOf("=") + 1);
                                sSize = sSize.substring(sSize.lastIndexOf("=") + 1);
                                 
                                int intSize=Integer.parseInt(sSize);
                                int fontStyle = 0;
                                if(sStyle.equals("plain"))
                                    fontStyle = Font.PLAIN;
                                else if (sStyle.equals("italic"))
                                    fontStyle = Font.ITALIC;
                                else if (sStyle.equals("bold"))
                                    fontStyle = Font.BOLD;
                                else if (sStyle.equals("italic|bold"))
                                    fontStyle = Font.ITALIC | Font.BOLD;
          
                                Font font = new Font(sFamily,fontStyle,intSize);
                                DrawObject drawObj  = new DrawLabel(strCap, x, y, color, font, false);
                                if (drawObj != null)
                                    drawObjs.add(drawObj);
                                    
                             }
                             rs.close();
                             //获得当前电缆在数据库中的线段对象,并存入Vector对象
                             sql.append("Select CX1,CY1,CX2,CY2,COLOR "); 
                             sql.append(" from  XIANDUANXinXi ");
                             sql.append(" where DIANLANID="+intDLID+" ");
                             strSQL = sql.toString(); 
                             sql.setLength(0);
                             rs = stmt.executeQuery(strSQL);
                             while(rs.next()){
                                int x1=rs.getInt("CX1");
                                int y1=rs.getInt("CY1");
                                int x2=rs.getInt("CX2");
                                int y2=rs.getInt("CY2");
                                String strColor=rs.getString("COLOR");
                                intLeftPosi = strColor.lastIndexOf("[");
                                intRightPosi = strColor.lastIndexOf("]");
                               strColor = strColor.substring(intLeftPosi+1,intRightPosi);
                                StringTokenizer item = new StringTokenizer(strColor, ",");
                                String sR = item.nextToken();
                                String sG = item.nextToken();
                                String sB = strColor.substring(strColor.lastIndexOf(",") + 1);
                                
                                sR = sR.substring(sR.lastIndexOf("=") + 1);
                                sG = sG.substring(sG.lastIndexOf("=") + 1);
                                sB = sB.substring(sB.lastIndexOf("=") + 1);
                                int intR=Integer.parseInt(sR);
                                int intG=Integer.parseInt(sG);
                                int intB=Integer.parseInt(sB);
                                Color color = new Color(intR,intG,intB);
                                DrawObject drawObj = new DrawLine(x1,y1, x2, y2, color);
                               
                                drawObjs.add(drawObj);
                             }
                             rs.close();
      

  4.   

    //获得当前电缆在数据库中的图形对象,并存入Vector对象
                             sql.append("Select TYPE,CX,CY "); 
                             sql.append(" from  TUXINGXinXi ");
                             sql.append(" where DIANLANID="+intDLID+" ");
                             strSQL = sql.toString(); 
                             sql.setLength(0);
                             rs = stmt.executeQuery(strSQL);
                             while(rs.next()){
                                String imgIcon=rs.getString("TYPE");
                                int x=rs.getInt("CX");
                                int y=rs.getInt("CY");
                              imgIcon=imgIcon.substring(imgIcon.lastIndexOf("/") + 1);
                      ImageIcon icon = new ImageIcon(getClass().getResource(imgIcon));
                                DrawObject drawObj = new DrawIcon(icon, x, y);
                                if (drawObj != null)
                                    drawObjs.add(drawObj);
                                }
                             rs.close();
                             stmt.close(); 
                         } catch (SQLException e) { 
                          e.printStackTrace(); 
                         } 
     //int ret = JOptionPane.showConfirmDialog(mainFrame,"显示电缆示图 ?", "提示", JOptionPane.CANCEL_OPTION);
               Graphics g = bufferedImage.getGraphics();
                     Color bgcolor = new Color(255, 255, 255);
                     for(int i = 0; i < drawObjs.size(); i++){
                      DrawObject drawObj = (DrawObject)drawObjs.get(i);
                         if (!drawObj.isRendered){ 
                              // 调用对象的 render 方法绘制对象
                              drawObj.render(g, bgcolor);
                              // 设置该对象为已绘制
                              drawObj.isRendered = true;
                         }
                       }
               } 
         } 
      }/**打印电缆示意图*/
      public void printDLPic()
      {
         PrinterJob job = PrinterJob.getPrinterJob();
          PageFormat pf = job.defaultPage();
          Paper paper = new Paper();
          paper.setImageableArea(14, 30, paper.getWidth()-36, paper.getHeight()-36);
          pf.setPaper(paper);
          pf.setOrientation(PageFormat.LANDSCAPE);
          Book bk = new Book();
          paintCover prn = new paintCover();
          bk.append(prn, pf, 1);
          job.setPageable(bk);
          job.setJobName("电缆打印");
          if(job.printDialog())
          {
             try{
                  job.print();
             }catch(Exception ee){
                      System.out.println(ee);
             }
          }
      }
      public void printDL(Graphics g)
      {
       ResultSet rs=null;
        int intLeftPosi = 0;
         int intRightPosi = 0;
    newDocument();
         if (!connected) { 
               int ret = JOptionPane.showConfirmDialog(mainFrame,"No database connected.", "提示", 
    JOptionPane.CANCEL_OPTION);
                     } else { 
               if (connection == null) { 
               int ret = JOptionPane.showConfirmDialog(mainFrame,"Database connection 
    error.", "提示", JOptionPane.CANCEL_OPTION);
                   } else { 
                      try { Statement stmt = connection.createStatement(); 
                             //获得当前电缆在数据库中的标签对象,并存入Vector对象
                             sql.append("Select CX,CY,COLOR,FONT,CAPTION "); 
                             sql.append(" from  BIAOQIANXinXi ");
                             sql.append(" where DIANLANID="+intDLID+" ");
                            strSQL = sql.toString(); 
                             sql.setLength(0);
                          rs = stmt.executeQuery(strSQL);
                             while(rs.next()){
                             int x=rs.getInt("CX");
                                int y=rs.getInt("CY");
                                String strColor=rs.getString("COLOR");
                                String strFont=rs.getString("FONT");
                                String strCap=rs.getString("CAPTION");
                                intLeftPosi = strColor.lastIndexOf("[");
                                intRightPosi = strColor.lastIndexOf("]");
                           strColor = strColor.substring(intLeftPosi+1,intRightPosi);
                            StringTokenizer item = new StringTokenizer(strColor, ",");
                                String sR = item.nextToken();
                                String sG = item.nextToken();
                        String sB = strColor.substring(strColor.lastIndexOf(",") + 1);
                                sR = sR.substring(sR.lastIndexOf("=") + 1);
                                sG = sG.substring(sG.lastIndexOf("=") + 1);
                                sB = sB.substring(sB.lastIndexOf("=") + 1);
                                int intR=Integer.parseInt(sR);
                                int intG=Integer.parseInt(sG);
                                int intB=Integer.parseInt(sB);
                                Color color = new Color(intR,intG,intB);
                                intLeftPosi = strFont.lastIndexOf("[");
                                intRightPosi = strFont.lastIndexOf("]");
                              strFont = strFont.substring(intLeftPosi+1,intRightPosi);
                                item = new StringTokenizer(strFont, ",");
                                String sFamily = item.nextToken();
                                String sName = item.nextToken();
                                String sStyle = item.nextToken();
                        String sSize =strFont.substring(strFont.lastIndexOf(",") + 1);
                            sFamily = sFamily.substring(sFamily.lastIndexOf("=") + 1);
                                sName = sName.substring(sName.lastIndexOf("=") + 1);
                               sStyle = sStyle.substring(sStyle.lastIndexOf("=") + 1);
                                sSize = sSize.substring(sSize.lastIndexOf("=") + 1);
                                 int intSize=Integer.parseInt(sSize);
                                int fontStyle = 0;
                                if(sStyle.equals("plain"))
                                    fontStyle = Font.PLAIN;
                                else if (sStyle.equals("italic"))
                                    fontStyle = Font.ITALIC;
                                else if (sStyle.equals("bold"))
                                    fontStyle = Font.BOLD;
                                else if (sStyle.equals("italic|bold"))
                                    fontStyle = Font.ITALIC | Font.BOLD;
                              Font font = new Font(sFamily,fontStyle,intSize);
               DrawObject drawObj  = new DrawLabel(strCap, x, y, color, font, false);
                                if (drawObj != null)
                                    drawObjs.add(drawObj);
                                    }
                             rs.close();
      

  5.   

    我写的一个画工作流是这样实现的,至于看你的代码(很累)不想看
    供你参阅一下我的思路元素:线,图标
    线(为起与结束坐标)鼠标点击时判断交点
    点击删除时,去元素集合找(Vector或HashTable之类的)有与之相交则删除至于画ICon有现成的new Image("icon.gif");that's all.