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

📄 sketchframe.java

📁 用JAVA开发的一个小型画图软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  public void openSketch(File inFile) {
    try {
      ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(
                                       new FileInputStream(inFile)));
      theApp.insertModel((SketchModel)in.readObject());
      in.close();
      modelFile = inFile;
      filename = modelFile.getName();            // Update the file name
      setTitle(frameTitle+modelFile.getPath());  // Change the window title
      sketchChanged = false;                     // Status is unchanged
    } catch(Exception e) {
      System.out.println(e);
      JOptionPane.showMessageDialog(SketchFrame.this,
                                    "Error reading a sketch file.",
                                    "File Input Error",
                                    JOptionPane.ERROR_MESSAGE);
    }
  }

  // Method called by SketchModel object when it changes
  public void update(Observable o, Object obj) {
    sketchChanged = true;
  }

  // Method making the current element color available
  public Color getElementColor() { 
    return elementColor; 
  }

  // Method making the current element type available
  public int getElementType() { 
    return elementType; 
  }

  // Method making the current font available
  public Font getCurrentFont() {  
    return font;  
  }

  // Method to set the current font
  public void setCurrentFont(Font font) {  
    this.font = font;  
  }

  // Retrieve the pop-up menu
  public JPopupMenu getPopup() {  
    return popup;  
  }

  // Method to return the name of the current sketch
  public String getSketchName() {
    return filename;
  }

  // Method to return a reference to the current PageFormat object
  public PageFormat getPageFormat() {
    return pageFormat;
  }

  // Handle menu action events
  public void actionPerformed(ActionEvent e)  {
    Object source = e.getSource();
    if(source == aboutItem) {
      // Create about dialog with the menu item as parent
      JOptionPane.showMessageDialog(this,                          // Parent
                            "Sketcher Copyright Ivor Horton 2004", // Message
                            "About Sketcher",                      // Title
                            JOptionPane.INFORMATION_MESSAGE);      // Message type
    } else if(e.getSource() == fontItem) {       // Set the dialog window position
      Rectangle bounds = getBounds();
      fontDlg.setLocation(bounds.x + bounds.width/3, bounds.y + bounds.height/3);
      fontDlg.setVisible(true);                  // Show the dialog
    } else if(source == customColorItem) {
      Color color = JColorChooser.showDialog(this, "Select Custom Color",
                                                           elementColor);
      if(color != null) {
        elementColor = color;
        statusBar.setColorPane(color);
      }
    } else if(source == printSetupItem) {
      PageFormat pf = printJob.pageDialog(printAttr);
      if(pf != null) {
        pageFormat = pf;    // update the page format
      }
    } else if(source == printWindowItem) {
      if(printer == null) {
        JOptionPane.showMessageDialog(this,
                                      "No default printer available.",
                                      "Printer Error",
                                      JOptionPane.ERROR_MESSAGE);
        return;
      }
      // The app window is the page source
      printJob.setPrintable(this, pageFormat);
      try {                                  
        printJob.print();                    // ...then print
      } catch(PrinterException pe) {
        System.out.println(pe);
        JOptionPane.showMessageDialog(SketchFrame.this,
                                      "Error printing the application window.",
                                      "Printer Error",
                                      JOptionPane.ERROR_MESSAGE);
      }
    }
  }

  // Inner class defining Action objects for File menu items
  class FileAction extends AbstractAction {    
    // Constructor to implement an accelerator and a tooltip
    FileAction(String name, KeyStroke keystroke, String tooltip) {
      this(name, keystroke);                     // Call the other constructor
      if(tooltip != null) {                      // If there is tooltip text
        putValue(SHORT_DESCRIPTION, tooltip);    // ...squirrel it away
      }
    }

    // Constructor to implement a tooltip
    FileAction(String name, String tooltip) {
      this(name);                                // Call the other constructor
      if(tooltip != null) {                      // If there is tooltip text
        putValue(SHORT_DESCRIPTION, tooltip);    // ...squirrel it away
      }
    }

    // Constructor
    FileAction(String name) {
      super(name);
      String iconFileName = "Images/" + name + ".gif";
      if(new File(iconFileName).exists()) {
        putValue(SMALL_ICON, new ImageIcon(iconFileName));
      }
    }

   // Constructor
    FileAction(String name, KeyStroke keystroke) {
      this(name);
      if(keystroke != null) {
        putValue(ACCELERATOR_KEY, keystroke);
      }
    }

    // Event handler
    public void actionPerformed(ActionEvent e) {
      String name = (String)getValue(NAME);
      if(name.equals(saveAction.getValue(NAME))) {
              saveOperation();
      } else if(name.equals(saveAsAction.getValue(NAME))) {
       File file = showDialog("Save Sketch As",
                              "Save",
                              "Save the sketch",
                              modelFile == null ? new File(
                                                files.getCurrentDirectory(),
                                                filename):modelFile);
       if(file != null) {
          if(file.exists() && !file.equals(modelFile))
           if(JOptionPane.NO_OPTION ==                  // Overwrite warning
              JOptionPane.showConfirmDialog(SketchFrame.this,
                                       file.getName()+" exists. Overwrite?",
                                       "Confirm Save As",
                                       JOptionPane.YES_NO_OPTION,
                                       JOptionPane.WARNING_MESSAGE))
            return;                                     // No file selected
         saveSketch(file);
       }
       return;
      } else if(name.equals(openAction.getValue(NAME))) {
        checkForSave();

        // Now open a sketch file
        File file = showDialog(
                           "Open Sketch File",        // Dialog window title
                           "Open",                    // Button lable
                           "Read a sketch from file", // Button tooltip text
                           null);                     // No file selected
        if(file != null) {                            // If a file was selected
          openSketch(file);                        // then read it
        }
      } else if(name.equals(newAction.getValue(NAME))) {
        checkForSave();
        theApp.insertModel(new SketchModel());    // Insert new empty sketch
        modelFile = null;                         // No file for it
        filename = DEFAULT_FILENAME;              // Default name
        setTitle(frameTitle + files.getCurrentDirectory() + "\\" + filename);
        sketchChanged = false;                    // Not changed yet
      } if(name.equals(printAction.getValue(NAME))) {
        // Verify there is a default printer 
        if(printer == null) {
            JOptionPane.showMessageDialog(SketchFrame.this, 
                                          "No default printer available.",
                                          "Printer Error",
                                          JOptionPane.ERROR_MESSAGE);
            return;
        }

        // The view is the page source
        printJob.setPageable(theApp.getView());
        boolean printIt = true;
        if(e.getSource() != printButton) {       // If it's not the toolbar button
          printIt = printJob.printDialog();      // ...display the print dialog
//          printIt = printJob.printDialog(printAttr); // ...display the print dialog
        }
        if(printIt) {                            // If printIt is true...             
          try {                                  
            printJob.print();                    // ...then print
//            printJob.print(printAttr);           // ...then print
          } catch(PrinterException pe) {
            System.out.println(pe);
            JOptionPane.showMessageDialog(SketchFrame.this,
                                          "Error printing a sketch.",
                                          "Printer Error",
                                          JOptionPane.ERROR_MESSAGE);
          }
        }
      } else if(name.equals(closeAction.getValue(NAME))) {
        checkForSave();
        System.exit(0);
      }
    }
  }

  // Inner class defining Action objects for Element type menu items
  class TypeAction extends AbstractAction {    
    // Constructor to implement a tooltip
    TypeAction(String name, int typeID, String tooltip) {
      this(name, typeID);
      if(tooltip != null) {                      // If there is a tooltip
        putValue(SHORT_DESCRIPTION, tooltip);    // ...squirrel it away
      }
    }

    TypeAction(String name, int typeID) {
      super(name);
      this.typeID = typeID;
      String iconFileName = "Images/" + name + ".gif";
      if(new File(iconFileName).exists()) {
        putValue(SMALL_ICON, new ImageIcon(iconFileName));
      }
    }
  
    public void actionPerformed(ActionEvent e) {
      elementType = typeID;  
      statusBar.setTypePane(typeID);
    }

    private int typeID;
  }

  // Handles color menu items
  class ColorAction  extends AbstractAction {
    // Constructor to implement a tooltip
    public ColorAction(String name, Color color, String tooltip) {
      this(name, color);
      if(tooltip != null) {                      // If there is a tooltip
        putValue(SHORT_DESCRIPTION, tooltip);    // ...squirrel it away
      }
    }

    public ColorAction(String name, Color color) {
      super(name);
      this.color = color;
      String iconFileName = "Images/" + name + ".gif";
      if(new File(iconFileName).exists()) {
        putValue(SMALL_ICON, new ImageIcon(iconFileName));
      }
    }

    public void actionPerformed(ActionEvent e) {
      elementColor = color;
      statusBar.setColorPane(color);
    }

    private Color color;
  }


  // File actions
  private FileAction newAction, openAction, closeAction,
                     saveAction, saveAsAction, printAction;

  // Element type actions
  private TypeAction lineAction, rectangleAction, circleAction, curveAction, textAction;
 
  // Element color actions
  private ColorAction redAction, yellowAction,
                      greenAction, blueAction;

  private JMenuBar menuBar = new JMenuBar();           // Window menu bar

  // Sundry menu items
  private JMenuItem aboutItem, fontItem;
  private JMenuItem customColorItem;
  private JMenuItem printSetupItem;
  private JMenuItem printWindowItem;
  
  private JToolBar toolBar = new JToolBar();           // Window toolbar
  private StatusBar statusBar = new StatusBar();       // Window status bar
  private Color elementColor = DEFAULT_ELEMENT_COLOR;  // Current element color
  private int elementType = DEFAULT_ELEMENT_TYPE;      // Current element type
  private Font font = DEFAULT_FONT;                    // Current font
  private Sketcher theApp;                             // The application object
  private FontDialog fontDlg;                          // The font dialog
  private JPopupMenu popup = new JPopupMenu("General");// Window pop-up

  private String frameTitle;                           // Frame title
  private String filename = DEFAULT_FILENAME;          // Current model file name
  private boolean sketchChanged = false;               // Model changed flag
  private File modelFile;                              // File for the current sketch
  private JFileChooser files;                          // File chooser dialog
  private JButton printButton;                         // Toolbar button for printing

  private PrinterJob printJob;                         // The current printer job
  private PageFormat pageFormat;                       // The printing page format
  private PrintService printer;                        // The printer to be used
  private HashPrintRequestAttributeSet printAttr = new  HashPrintRequestAttributeSet();
}

⌨️ 快捷键说明

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