📄 drawinginternalframe.java
字号:
} // end method openDrawing
// save current drawing to file
public void saveDrawing()
{
// get absolute path to which file should be saved
String fileName = getAbsoluteFilePath();
// if fileName is null or empty, call saveDrawingAs
if ( fileName == null || fileName.equals( "" ) )
saveDrawingAs();
// write drawing to given fileName
else {
DrawingFileReaderWriter.writeFile( drawingModel,
fileName );
// update saved property
setSaved( true );
}
} // end method saveDrawing
// prompt user for file name and save drawing
public void saveDrawingAs()
{
// display JFileChooser Save dialog
int response = fileChooser.showSaveDialog( this );
// if user selected a file, save drawing
if ( response == fileChooser.APPROVE_OPTION )
{
// set absoluteFilePath property
setAbsoluteFilePath(
fileChooser.getSelectedFile().getAbsolutePath() );
// set fileName property
setFileName( fileChooser.getSelectedFile().getName() );
// write drawing to file
DrawingFileReaderWriter.writeFile( drawingModel,
getAbsoluteFilePath() );
// update saved property
setSaved( true );
}
} // end method saveDrawingAs
// display zoomDialog
public void showZoomDialog()
{
// if zoomDialog is null, create one
if (zoomDialog == null)
zoomDialog = new ZoomDialog( getModel(), getTitle() );
// make extant zoomDialog visible
else
zoomDialog.setVisible( true );
}
// dispose DrawingInternalFrame
public void dispose()
{
// dispose associated zoomDialog
if ( zoomDialog != null )
zoomDialog.dispose();
super.dispose();
}
// JToolBar subclass for DrawingInternalFrame
private class DrawingToolBar extends JToolBar {
// user interface components
private GradientIcon gradientIcon;
private JPanel primaryColorPanel, secondaryColorPanel;
private JButton primaryColorButton;
private JButton secondaryColorButton;
private JComboBox shapeChoice, strokeSizeChoice;
private JToggleButton gradientButton, fillButton;
private JToggleButton moveButton;
// DrawingToolBar constructor
public DrawingToolBar()
{
// create JComboBox for choosing current shape type
shapeChoice = new JComboBox(
shapeControllerFactory.getSupportedShapes() );
shapeChoice.setToolTipText( "Choose Shape" );
// when shapeChoice changes, get new MyShapeController
// from MyShapeControllerFactory
shapeChoice.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
// get selected shape type
String className =
shapeChoice.getSelectedItem().toString();
setMyShapeController(
shapeControllerFactory.newMyShapeController(
drawingModel, className ) );
}
}
); // end call to addActionListener
// create JComboBox for selecting stroke size
strokeSizeChoice = new JComboBox(
new String[] { "1.0", "2.0", "3.0", "4.0", "5.0",
"6.0", "7.0", "8.0", "9.0", "10.0" } );
strokeSizeChoice.setToolTipText( "Choose Line Width" );
// set stroke size property to selected value
strokeSizeChoice.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
myShapeController.setStrokeSize(
getStrokeSize() );
}
}
);
// create JToggleButton for filling shapes
fillButton = new JToggleButton( "Fill" );
fillAction = new AbstractDrawingAction( "Fill", null,
"Fill Shape", new Integer( 'L' ) ) {
public void actionPerformed( ActionEvent event )
{
myShapeController.setShapeFilled(
getShapeFilled() );
}
};
fillButton.setAction( fillAction );
// create GradientIcon to display gradient settings
gradientIcon = new GradientIcon( Color.black,
Color.white );
// create JToggleButton to enable/disable gradients
gradientButton = new JToggleButton( gradientIcon );
gradientAction = new AbstractDrawingAction( "",
gradientIcon, "Use Gradient", new Integer( 'G' ) ) {
public void actionPerformed( ActionEvent event )
{
myShapeController.setUseGradient(
getUseGradient() );
}
};
gradientButton.setAction( gradientAction );
// create JPanel to display primary drawing color
primaryColorPanel = new JPanel();
primaryColorPanel.setPreferredSize(
new Dimension( 16, 16 ) );
primaryColorPanel.setOpaque( true );
primaryColorPanel.setBackground( Color.black );
// create JButton for changing color1
primaryColorButton = new JButton();
primaryColorButton.add( primaryColorPanel );
// display JColorChooser for selecting startColor value
primaryColorButton.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
Color color = JColorChooser.showDialog(
DrawingInternalFrame.this, "Select Color",
primaryColorPanel.getBackground() );
if ( color != null ) {
primaryColorPanel.setBackground( color );
gradientIcon.setStartColor( color );
myShapeController.setPrimaryColor( color );
}
}
} // end ActionListener inner class
); // end call to addActionListener
// create JPanel to display secondary drawing color
secondaryColorPanel = new JPanel();
secondaryColorPanel.setPreferredSize(
new Dimension( 16, 16 ) );
secondaryColorPanel.setOpaque( true );
secondaryColorPanel.setBackground( Color.white );
// create JButton for changing secondary color
secondaryColorButton = new JButton();
secondaryColorButton.add( secondaryColorPanel );
// display JColorChooser for selecting endColor value
secondaryColorButton.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
Color color = JColorChooser.showDialog(
DrawingInternalFrame.this, "Select Color",
secondaryColorPanel.getBackground() );
if ( color != null ) {
secondaryColorPanel.setBackground( color );
gradientIcon.setEndColor( color );
myShapeController.setSecondaryColor(
color );
}
}
} // end ActionListener inner class
); // end call to addActionListener
// create Action for saving drawings
Icon saveIcon = new ImageIcon(
DrawingInternalFrame.class.getResource(
"images/save.gif" ) );
saveAction = new AbstractDrawingAction( "Save", saveIcon,
"Save Drawing", new Integer( 'S' ) ) {
public void actionPerformed( ActionEvent event )
{
saveDrawing();
}
};
// create action for saving drawings as given file name
Icon saveAsIcon = new ImageIcon(
DrawingInternalFrame.class.getResource(
"images/saveAs.gif" ) );
saveAsAction = new AbstractDrawingAction( "Save As",
saveAsIcon, "Save Drawing As", new Integer( 'A' ) ) {
public void actionPerformed( ActionEvent event )
{
saveDrawingAs();
}
};
// create action for displaying zoomDialog
Icon zoomIcon = new ImageIcon(
DrawingInternalFrame.class.getResource(
"images/zoom.gif" ) );
zoomAction = new AbstractDrawingAction( "Zoom", zoomIcon,
"Show Zoom Window", new Integer( 'Z' ) ) {
public void actionPerformed( ActionEvent event )
{
showZoomDialog();
}
};
// create JToggleButton for setting drag and drop mode
moveButton = new JToggleButton();
Icon moveIcon = new ImageIcon(
DrawingInternalFrame.class.getResource(
"images/move.gif" ) );
moveAction = new AbstractDrawingAction( "Move", null,
"Move Shape", new Integer( 'M' ) ) {
public void actionPerformed( ActionEvent event )
{
myShapeController.setDragMode(
getDragMode() );
dragAndDropController.setDragMode(
getDragMode() );
}
};
moveButton.setAction( moveAction );
// add Actions, buttons, etc. to JToolBar
add( saveAction );
add( saveAsAction );
addSeparator();
add( zoomAction );
addSeparator();
add( shapeChoice );
add( strokeSizeChoice );
addSeparator();
add( primaryColorButton );
add( secondaryColorButton );
addSeparator();
add( gradientButton );
add( fillButton );
addSeparator();
add( moveButton );
// disable floating
setFloatable( false );
} // end DrawingToolBar constructor
// get currently selected stroke size
public float getStrokeSize()
{
Object selectedItem = strokeSizeChoice.getSelectedItem();
return Float.parseFloat( selectedItem.toString() );
}
// get current shape filled value
public boolean getShapeFilled()
{
return fillButton.isSelected();
}
// get current use gradient property
public boolean getUseGradient()
{
return gradientButton.isSelected();
}
// get primary drawing Color
public Color getPrimaryColor()
{
return primaryColorPanel.getBackground();
}
// get secondary drawing Color
public Color getSecondaryColor()
{
return secondaryColorPanel.getBackground();
}
// get current drag mode
public boolean getDragMode()
{
return moveButton.isSelected();
}
} // end DrawingToolBar inner class
}
/***************************************************************
* (C) Copyright 2002 by Deitel & Associates, Inc. and *
* Prentice Hall. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have *
* used their best efforts in preparing the book. These *
* efforts include the development, research, and testing of *
* the theories and programs to determine their effectiveness. *
* The authors and publisher make no warranty of any kind, *
* expressed or implied, with regard to these programs or to *
* the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for *
* incidental or consequential damages in connection with, or *
* arising out of, the furnishing, performance, or use of *
* these programs. *
***************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -