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

📄 drawinginternalframe.java

📁 java2高级教程大学教程的配套光盘源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// DrawingInternalFrame.java
// DrawingInternalFrame is a JInternalFrame subclass for 
// DeitelDrawing drawings.
package com.deitel.advjhtp1.drawing;

// Java core packages
import java.awt.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.io.*;
import java.util.*;
import java.util.List;

// Java extension packages
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;

// Deitel packages
import com.deitel.advjhtp1.drawing.model.*;
import com.deitel.advjhtp1.drawing.model.shapes.*;
import com.deitel.advjhtp1.drawing.view.*;
import com.deitel.advjhtp1.drawing.controller.*;

public class DrawingInternalFrame extends JInternalFrame 
   implements Observer {
   
   // offsets to stagger new windows
   private static final int xOffset = 30;
   private static final int yOffset = 30;
   private static int openFrameCount = 0;

   // MVC components
   private DrawingModel drawingModel;
   private DrawingView drawingView;
   private MyShapeController myShapeController;
   private DragAndDropController dragAndDropController;
   private MyShapeControllerFactory shapeControllerFactory;
   
   // file management properties
   private JFileChooser fileChooser;
   private String fileName;
   private String absoluteFilePath;
   private boolean saved = true;
   
   private DrawingToolBar toolBar;
   private ZoomDialog zoomDialog;   
   
   // Actions for save, zoom, move, etc.
   private Action saveAction, saveAsAction, zoomAction, 
      moveAction, fillAction, gradientAction;

   // DrawingInternalFrame constructor
   public DrawingInternalFrame( String title ) 
   {
      super( title + " - " + ( ++openFrameCount ), true, true, 
         false, true );

      setDefaultCloseOperation( 
         WindowConstants.DO_NOTHING_ON_CLOSE );

      // create new DrawingModel
      drawingModel = new DrawingModel();
      
      // create new DrawingView for DrawingModel
      drawingView = new DrawingView( drawingModel );
      
      // register DrawingInternalFrame as a DrawingModel Observer
      drawingModel.addObserver( this );   
      
      // MyShapeControllerFactory for creating MyShapeControllers
      shapeControllerFactory = 
         MyShapeControllerFactory.getInstance();

      // create DragAndDropController for drag and drop operations
      dragAndDropController = 
         new DragAndDropController( drawingModel );
      
      // get default DragSource for current platform
      DragSource dragSource = DragSource.getDefaultDragSource();
      
      // create DragGestureRecognizer to register 
      // DragAndDropController as DragGestureListener
      dragSource.createDefaultDragGestureRecognizer( drawingView,
         DnDConstants.ACTION_COPY_OR_MOVE,
         dragAndDropController );
      
      // enable drawingView to accept drop operations, using
      // dragAndDropController as DropTargetListener
      drawingView.setDropTarget( new DropTarget( drawingView, 
         DnDConstants.ACTION_COPY_OR_MOVE, 
         dragAndDropController ) );

      // add drawingView to viewPanel, put viewPanel in
      // JScrollPane and add JScrollPane to DrawingInternalFrame
      JPanel viewPanel = new JPanel();
      viewPanel.add( drawingView );
      getContentPane().add( new JScrollPane( viewPanel ), 
         BorderLayout.CENTER );
          
      // create fileChooser and set its FileFilter
      fileChooser = new JFileChooser();
      fileChooser.setFileFilter( new DrawingFileFilter() );
      
      // show/hide ZoomDialog when frame activated/deactivated
      addInternalFrameListener( 
         new InternalFrameAdapter() {
         
            // when DrawingInternalFrame activated, make
            // associated zoomDialog visible
            public void internalFrameActivated( 
               InternalFrameEvent event ) 
            {
               if ( zoomDialog != null )  
                  zoomDialog.setVisible( true );
            }

            // when DrawingInternalFrame is deactivated, make
            // associated zoomDialog invisible
            public void internalFrameDeactivated( 
               InternalFrameEvent event ) 
            {
               if ( zoomDialog != null ) 
                  zoomDialog.setVisible( false );
            }
         }
         
      ); // end call to addInternalFrameListener
      
      // stagger each DrawingInternalFrame to prevent it from 
      // obscuring other InternalFrames
      setLocation( xOffset * openFrameCount,
         yOffset * openFrameCount ); 

      // add new DrawingToolBar to NORTH area
      toolBar = new DrawingToolBar();
      getContentPane().add( toolBar, BorderLayout.NORTH );
      
      // get name of first MyShape that shapeControllerFactory
      // supports and create MyShapeController
      String shapeName = 
         shapeControllerFactory.getSupportedShapes()[ 0 ];
      
      setMyShapeController( 
         shapeControllerFactory.newMyShapeController(
            drawingModel, shapeName ) );               
        
      // set DrawingInternalFrame size
      setSize( 500, 320 );   
      
   } // end DrawingInternalFrame constructor
   
   // get DrawingInternalFrame Save Action
   public Action getSaveAction() 
   { 
      return saveAction; 
   }
   
   // get DrawingInternalFrame Save As Action
   public Action getSaveAsAction() 
   { 
      return saveAsAction; 
   }
   
   // set Saved flag for current drawing and update frame
   // title to indicate saved state to user
   public void setSaved( boolean drawingSaved ) 
   {
      // set Saved property
      saved = drawingSaved;
      
      // get current DrawingInternalFrame title
      String title = getTitle();
      
      // if drawing is not saved and title does not end with
      // an asterisk, add asterisk to title
      if ( !title.endsWith( " *" ) && !isSaved() ) 
         setTitle( title + " *" );
      
      // if title ends with * and drawing has been saved, 
      // remove * from title
      else 
         
         if ( title.endsWith( " *" ) && isSaved() ) 
            setTitle( title.substring( 0, 
               title.length() - 2 ) );
      
      // enable save actions if drawing not saved
      getSaveAction().setEnabled( !isSaved() );
   }
   
   // return value of saved property
   public boolean isSaved() 
   { 
      return saved; 
   }
   
   // handle updates from DrawingModel
   public void update( Observable observable, Object object ) 
   { 
      // set saved property to false to indicate that
      // DrawingModel has changed
      setSaved( false ); 
   }
   
   // set fileName for current drawing
   public void setFileName( String file ) 
   {
      fileName = file;
      
      // update DrawingInternalFrame title
      setTitle( fileName );
   }
   
   // get fileName for current drawing
   public String getFileName() 
   { 
      return fileName; 
   }
   
   // get full path (absoluteFilePath) for current drawing
   public String getAbsoluteFilePath() 
   { 
      return absoluteFilePath; 
   }
   
   // set full path (absoluteFilePath) for current drawing
   public void setAbsoluteFilePath( String path ) 
   { 
      absoluteFilePath = path; 
   }
   
   // get DrawingModel for current drawing
   public DrawingModel getModel() 
   { 
      return drawingModel; 
   }
   
   // set JInternalFrame and ZoomDialog titles
   public void setTitle( String title )
   {
      super.setTitle( title );
      
      if ( zoomDialog != null )
         zoomDialog.setTitle( title );
   }
   
   // set MyShapeController for handling user input
   public void setMyShapeController( 
      MyShapeController controller )
   {
      // remove old MyShapeController
      if ( myShapeController != null ) {
         
         // remove mouse listeners
         drawingView.removeMouseListener( 
            myShapeController.getMouseListener() );
         
         drawingView.removeMouseMotionListener( 
            myShapeController.getMouseMotionListener() );
      }
      
      // set MyShapeController property
      myShapeController = controller;
      
      // register MyShapeController to handle mouse events
      drawingView.addMouseListener( 
         myShapeController.getMouseListener() );
      
      drawingView.addMouseMotionListener( 
         myShapeController.getMouseMotionListener() ); 
      
      // update new MyShapeController with currently selected
      // drawing properties (stroke size, color, fill, etc.)
      myShapeController.setStrokeSize( toolBar.getStrokeSize() );
      
      myShapeController.setPrimaryColor( 
         toolBar.getPrimaryColor() );
      
      myShapeController.setSecondaryColor( 
         toolBar.getSecondaryColor() );
      
      myShapeController.setDragMode( toolBar.getDragMode() );
      
      myShapeController.setShapeFilled( 
         toolBar.getShapeFilled() );
      
      myShapeController.setUseGradient( 
         toolBar.getUseGradient() );
      
   } // end method setMyShapeController

   // close DrawingInternalFrame; return false if drawing
   // was not saved and user canceled the close operation
   public boolean close() 
   {
      // if drawing not saved, prompt user to save
      if ( !isSaved() ) {
         
         // display JOptionPane confirmation dialog to allow
         // user to save drawing
         int response = JOptionPane.showInternalConfirmDialog( 
            this, "The drawing in this window has been " +
            "modified.  Would you like to save changes?",
            "Save Changes", JOptionPane.YES_NO_CANCEL_OPTION,
            JOptionPane.QUESTION_MESSAGE );
         
         // if user selects Yes, save drawing and close 
         if ( response == JOptionPane.YES_OPTION ) {        
            saveDrawing();
            dispose();
            
            // return true to indicate frame closed
            return true;
         }
         
         // if user selects No, close frame without saving
         else if ( response == JOptionPane.NO_OPTION ) {            
            dispose();
            return true;
         }
         
         // if user selects Cancel, do not save or close
         else            
            return false; // indicate frame was not closed
      }
      
      // if drawing has been saved, close frame
      else {         
         dispose();
         return true;
      }
      
   } // end method close 
  
   // open existing drawing from file
   public boolean openDrawing() 
   {
      // open JFileChooser Open dialog
      int response = fileChooser.showOpenDialog( this );
      
      // if user selected valid file, open an InputStream
      // and retrieve the saved shapes
      if ( response == fileChooser.APPROVE_OPTION ) {
         
         // get selecte file name
         String fileName = 
            fileChooser.getSelectedFile().getAbsolutePath();
         
         // get shapes List from file
         Collection shapes = 
            DrawingFileReaderWriter.readFile( fileName );
         
         // set shapes in DrawingModel
         drawingModel.setShapes( shapes );
         
         // set fileName property
         setFileName( fileChooser.getSelectedFile().getName() );
         
         // set absoluteFilePath property
         setAbsoluteFilePath( fileName );
         
         // set saved property
         setSaved( true );
         
         // return true to indicate successful file open
         return true;
      }
      
      // return false to indicate file open failed
      else 
         return false;
      

⌨️ 快捷键说明

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