📄 canvasformat.java
字号:
/**
* $Id:CanvasFormat.java $
*
* Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved. *
*/
package com.jfimagine.jfgraph.shape.decorate;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.awt.Color;
import java.awt.Font;
import java.awt.print.Paper;
import java.awt.Dimension;
import java.awt.Toolkit;
import com.jfimagine.jfdom.Document;
import com.jfimagine.jfdom.Element;
import com.jfimagine.jfgraph.shape.base.AbstractObject;
import com.jfimagine.jfgraph.shape.base.ShapeConst;
import com.jfimagine.jfgraph.shape.base.JFVersion;
/**
* Canvas format used to assign a canvas' drawing area.
*
* @author CookieMaker
*
* @version $Revision: 1.00 $
*/
public class CanvasFormat extends AbstractObject{
//*****************************************************************
// XML Tags
//*****************************************************************
/**
* A XML string tag represents a canvas format.
*/
public static final String XML_CANVASFORMAT ="CanvasFormat";
/**
* A XML string tag represents the canvas width.
*/
public static final String XML_WIDTH ="canvasWidth";
/**
* A XML string tag represents the unit of canvas width.
*/
public static final String XML_WIDTHUNIT ="canvasWidthUnit";
/**
* A XML string tag represents the canvas height
*/
public static final String XML_HEIGHT ="canvasHeight";
/**
* A XML string tag represents the unit of canvas height
*/
public static final String XML_HEIGHTUNIT ="canvasHeightUnit";
/**default canvas width in inches */
public static final int CANVASSIZE_DEFAULT_INCH =20;
/**default canvas width unit : inch*/
public static final int CANVASSIZE_DEFAULT_UNIT =ShapeConst.MEASURE_TYPE_IN;
/**default canvas width in centimeters */
public static final int CANVASSIZE_DEFAULT_CM =50;
/**minimum canvas width in inch or centimeter */
public static final int CANVASSIZE_MINIMUM =1;
/**maximum canvas width in inch or centimeter */
public static final int CANVASSIZE_MAXIMUM =1000;
private static CanvasFormat m_defaultCanvasFormat =new CanvasFormat();
/**
* set global default canvas format
* @param canvasFormat A new canvas format
*/
public static void setDefaultCanvasFormat(CanvasFormat canvasFormat){
m_defaultCanvasFormat.setValue(canvasFormat);
}
/**
* get global default canvas format
* @return The global default canvas format
*/
public static CanvasFormat getDefaultCanvasFormat(){
return m_defaultCanvasFormat;
}
/** canvas width in inches or centimeters. */
private double m_width=CANVASSIZE_DEFAULT_INCH;
public double getWidth(){
return m_width;
}
public void setWidth(double width){
if (width<CANVASSIZE_MINIMUM)
width =CANVASSIZE_MINIMUM;
if (width>CANVASSIZE_MAXIMUM)
width =CANVASSIZE_MAXIMUM;
m_width =width;
}
/** canvas width unit, inch or centemiter */
private int m_widthUnit=CANVASSIZE_DEFAULT_UNIT;
public int getWidthUnit(){
return m_widthUnit;
}
public void setWidthUnit(int widthUnit){
m_widthUnit =widthUnit;
}
/** canvas width in pixels. */
public double getScreenWidth(){
return m_width * ShapeConst.getUnitTimesOfINCH(m_widthUnit) * JFPageFormat.INCH_SCREEN;
}
/** canvas height in inches or centimeters. */
private double m_height=CANVASSIZE_DEFAULT_INCH;
public double getHeight(){
return m_height;
}
public void setHeight(double height){
if (height<CANVASSIZE_MINIMUM)
height =CANVASSIZE_MINIMUM;
if (height>CANVASSIZE_MAXIMUM)
height =CANVASSIZE_MAXIMUM;
m_height =height;
}
/** canvas height unit, inch or centemiter */
private int m_heightUnit=CANVASSIZE_DEFAULT_UNIT;
public int getHeightUnit(){
return m_heightUnit;
}
public void setHeightUnit(int heightUnit){
m_heightUnit =heightUnit;
}
/** canvas height in pixels. */
public double getScreenHeight(){
return m_height * ShapeConst.getUnitTimesOfINCH(m_heightUnit) * JFPageFormat.INCH_SCREEN;
}
public void setIsMetric(boolean isMetric) {
boolean defaultHeightUsed =
m_height==CANVASSIZE_DEFAULT_INCH && m_heightUnit==ShapeConst.MEASURE_TYPE_IN ||
m_height==CANVASSIZE_DEFAULT_CM && m_heightUnit==ShapeConst.MEASURE_TYPE_CM;
boolean defaultWidthUsed =
m_width==CANVASSIZE_DEFAULT_INCH && m_widthUnit==ShapeConst.MEASURE_TYPE_IN ||
m_width==CANVASSIZE_DEFAULT_CM && m_widthUnit==ShapeConst.MEASURE_TYPE_CM;
if (isMetric){
m_heightUnit =ShapeConst.MEASURE_TYPE_CM;
m_widthUnit =ShapeConst.MEASURE_TYPE_CM;
if (defaultHeightUsed)
m_height =CANVASSIZE_DEFAULT_CM;
if (defaultWidthUsed)
m_width =CANVASSIZE_DEFAULT_CM;
}else{
m_heightUnit =ShapeConst.MEASURE_TYPE_IN;
m_widthUnit =ShapeConst.MEASURE_TYPE_IN;
if (defaultHeightUsed)
m_height =CANVASSIZE_DEFAULT_INCH;
if (defaultWidthUsed)
m_width =CANVASSIZE_DEFAULT_INCH;
}
}
/**
* Set new value for this canvasFormat.
*
* @param canvasFormat A new canvas format.
*
*/
public void setValue(CanvasFormat canvasFormat){
if (canvasFormat==null)
return;
m_width =canvasFormat.getWidth();
m_widthUnit =canvasFormat.getWidthUnit();
m_height =canvasFormat.getHeight();
m_heightUnit =canvasFormat.getHeightUnit();
}
/**
* Constructor for CanvasFormat
*/
public CanvasFormat(){
setObjectType(ShapeConst.DECORATETYPE_CANVASFORMAT);
setXMLTag(XML_CANVASFORMAT);
if (m_defaultCanvasFormat!=null){
setValue(m_defaultCanvasFormat);
}
}
/**
* Convert this object to String
*
* @return An string represents the content of the object
*
*/
public String toString(){
StringBuffer buf =new StringBuffer();
buf.append(super.toString());
buf.append(";canasWidth="); buf.append(m_width);
buf.append(";WidthUnit="); buf.append(m_widthUnit);
buf.append(";canasHeight="); buf.append(m_height);
buf.append(";HeightUnit="); buf.append(m_heightUnit);
return buf.toString();
}
/**
* Creates a new AbstractObject of the same class and with the same contents as this object.
* This method implements the method defined in AbstractObject.
*
* @return A clone of this class.
*
*/
protected AbstractObject cloneMe() throws CloneNotSupportedException{
return new CanvasFormat();
}
/**
* Creates a new object of the same class and with the same contents as this object.
*
* @return A clone of this instance.
*
*/
public Object clone() throws CloneNotSupportedException{
try{
CanvasFormat canvasFormat =(CanvasFormat) super.clone();
canvasFormat.m_width =this.m_width;
canvasFormat.m_widthUnit =this.m_widthUnit;
canvasFormat.m_height =this.m_height;
canvasFormat.m_heightUnit =this.m_heightUnit;
return canvasFormat;
}catch(Exception e){
throw new CloneNotSupportedException(e.getMessage());
}
}
/**
* Returns the hashcode for this Object.
*
* @return hash code for this Point2D.
*
*/
public int hashCode(){
return super.hashCode() ^
(int)m_width ^
(int)m_widthUnit ^
(int)m_height ^
(int)m_heightUnit
;
}
/**
* Determines whether or not two objects are equal.
*
* @param obj an object to be compared with this object
*
* @return true if the object to be compared is an instance of Port and has the same values; false otherwise.
*
*/
public boolean equals(Object obj){
if (!super.equals(obj))
return false;
if (obj == this)
return true;
if (!(obj instanceof CanvasFormat))
return false;
CanvasFormat canvasFormat= (CanvasFormat)obj;
return ((int)m_width==(int)canvasFormat.m_width) &&
((int)m_widthUnit==(int)canvasFormat.m_widthUnit) &&
((int)m_height==(int)canvasFormat.m_height) &&
((int)m_heightUnit==(int)canvasFormat.m_heightUnit)
;
}
/**
* Append necessary xml child for current element,
* this method will be called internally by toDOM.
*
* @param element A XML element to append child xml nodes
* @param version A file version notification so this object can obey the rules to save data.
*
*/
protected void appendChildToDOM(Element element,JFVersion version){
if (element==null)
return;
super.appendChildToDOM(element,version);
element.addChild(new Element(XML_WIDTH, m_width));
element.addChild(new Element(XML_WIDTHUNIT, m_widthUnit));
element.addChild(new Element(XML_HEIGHT, m_height));
element.addChild(new Element(XML_HEIGHTUNIT, m_heightUnit));
}
/**
* Extract needed xml child from current element,
* this method will be called internally by fromDOM.
*
* @param element An element used to extract needed xml child
* @param version A file version notification so this object can obey the rules to fetch data.
*
*/
protected void extractChildFromDOM(Element element,JFVersion version){
if (element==null)
return;
super.extractChildFromDOM(element,version);
m_width =Element.getDoubleValue(element.getChild(XML_WIDTH));
m_widthUnit =Element.getIntValue(element.getChild(XML_WIDTHUNIT));
m_height =Element.getDoubleValue(element.getChild(XML_HEIGHT));
m_heightUnit =Element.getIntValue(element.getChild(XML_HEIGHTUNIT));
}
/**
* Save this object to a binary stream <br>
*
* @param stream An binary output stream
* @param version A file version notification so this object can obey the rules to save data.
*
* @exception java.io.IOException
*
*/
public void saveToStream(com.jfimagine.utils.io.JFWriter stream,JFVersion version) throws IOException{
super.saveToStream(stream,version);
stream.writeDouble(m_width);
stream.writeInt(m_widthUnit);
stream.writeDouble(m_height);
stream.writeInt(m_heightUnit);
}
/**
* Load object data from a binary stream <br>
*
* @param stream An binary input stream
*
* @param skipHead Skip head 'TYPE' check, an shape object should always
* has its own shape-type stored, if this shape-type has already been readed,
* this loadFromStream should/could not read the type anymore.
*
* @param version A file version notification so this object can obey the rules to fetch data.
* @exception java.io.IOException
*
*/
public void loadFromStream(com.jfimagine.utils.io.JFReader stream,boolean skipHead,JFVersion version) throws IOException{
super.loadFromStream(stream,skipHead,version);
m_width =stream.readDouble();
m_widthUnit =stream.readInt();
m_height =stream.readDouble();
m_heightUnit =stream.readInt();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -