📄 gridformat.java
字号:
/**
* $Id:GridFormat.java $
*
* Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved. *
*/
package com.jfimagine.jfgraph.shape.decorate;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Font;
import java.awt.Color;
import java.awt.BasicStroke;
import java.awt.geom.GeneralPath;
import com.jfimagine.jfgraph.shape.decorate.JFPageFormat;
import com.jfimagine.jfgraph.geom.JFPoint;
/**
* GridFormat class. Grids for graph drawing.
*
* @author CookieMaker
*
* @version $Revision: 1.00 $
*/
public class GridFormat {
/**Grid types*/
public static final int GRIDTYPE_NONE =0;
public static final int GRIDTYPE_LINE =1;
public static final int GRIDTYPE_CROSS =2;
public static final int GRIDTYPE_DOT =3;
/**size types*/
public static final int SIZETYPE_INCH =0; //inches
public static final int SIZETYPE_CM =1; //centimeters
public static final int SIZETYPE_MM =2; //millimeters
private static GridFormat m_defaultGridFormat =new GridFormat();
/**
* set global default grid format
* @param gridFormat A new grid format
*/
public static void setDefaultGridFormat(GridFormat gridFormat){
m_defaultGridFormat.setValue(gridFormat);
}
/**
* get global default grid format
* @return The global default grid format
*/
public static GridFormat getDefaultGridFormat(){
return m_defaultGridFormat;
}
/** grid color*/
private Color m_gridColor =new Color(220,220,220);
/** Default grid size is 1/2 inch*/
private double m_gridSize =0.5;
/** size type*/
private int m_gridSizeType =SIZETYPE_INCH;
/** grid type*/
private int m_gridType =GRIDTYPE_LINE;
/** If snap to grid*/
private boolean m_snapToGrid=false;
/** snap size*/
private double m_snapSize =1.0/16.0;
/** snap size type*/
private int m_snapSizeType =SIZETYPE_INCH;
/** width of the entire canvas*/
private double m_width =600;
/** height of the entire canvas*/
private double m_height =800;
/**
* Constructor for GridFormat
*/
public GridFormat(){
if (m_defaultGridFormat!=null){
setValue(m_defaultGridFormat);
}
}
/**
* Get current grid color.
*
* @return The grid color.
*
*/
public Color getGridColor(){
return m_gridColor;
}
/**
* Set current grid color.
*
* @param gridColor A new grid color.
*
*/
public void setGridColor(Color gridColor){
if (gridColor!=null)
m_gridColor =new Color(gridColor.getRGB());
}
/**
* Set a metric or english grid formats. This operation will reset all the definition of current grid format.
* @param isMetric True if is metric, false english.
*/
public void setIsMetric(boolean isMetric) {
if (isMetric){
m_gridSize =1;
m_gridSizeType =SIZETYPE_CM;
m_snapSize =1;
m_snapSizeType =SIZETYPE_MM;
}else{
m_gridSize =0.5;
m_gridSizeType =SIZETYPE_INCH;
m_snapSize =1.0/16.0;
m_snapSizeType =SIZETYPE_INCH;
}
}
/**
* Get the grid size.
* @return the grid size.
*/
public double getGridSize(){
return m_gridSize;
}
/**
* Set the grid size.
* @param gridSize A new grid size.
*/
public void setGridSize(double gridSize){
m_gridSize =gridSize;
}
/**
* Get the grid size type.
* @return the grid size type.
*/
public int getGridSizeType(){
return m_gridSizeType;
}
/**
* Set the grid size type.
* @param gridSizeType A new grid size type.
*/
public void setGridSizeType(int gridSizeType){
m_gridSizeType =gridSizeType;
}
/**
* get the grid size in pixels.
* @param zoom Zoom scale,default to 1.
* @return grid size in pixels
*/
public double getGridSizeInPixels(double zoom){
return getSizeInPixels(m_gridSize,m_gridSizeType) * zoom;
}
/**
* get the size in pixels.
* @param size A size to be calculated
* @param sizeType A size unit type.
* @return size in pixels
*/
private double getSizeInPixels(double size, int sizeType){
if (size<=0){
return 0;
}
switch (sizeType){
case SIZETYPE_INCH:
return size * JFPageFormat.INCH_SCREEN;
case SIZETYPE_CM:
return size * (JFPageFormat.INCH_SCREEN/2.54);
case SIZETYPE_MM:
return size * (JFPageFormat.INCH_SCREEN/2.54/10);
}
return 0;
}
/**
* Get the gridType between grid.
* @return the gridType.
*/
public int getGridType(){
return m_gridType;
}
/**
* Set the gridType between grid.
* @param gridType A new gridType.
*/
public void setGridType(int gridType){
m_gridType =gridType;
}
/**
* Get the width of entire canvas.
* @return the width.
*/
public double getWidth(){
return m_width;
}
/**
* Get the height of entire canvas.
* @return the height.
*/
public double getHeight(){
return m_height;
}
/**
* Set the size of entire canvas
* @param d size of canvas.
*/
public void setSize(Dimension d){
setSize(d.getWidth(),d.getHeight());
}
/**
* Set the size of entire canvas
* @param width Width of canvas
* @param height Height of canvas
*/
public void setSize(double width, double height){
m_width =width;
m_height =height;
}
/**
* Get the snapToGrid between grid.
* @return the snapToGrid.
*/
public boolean getSnapToGrid(){
return m_snapToGrid;
}
/**
* Set the snapToGrid between grid.
* @param snapToGrid A new snapToGrid.
*/
public void setSnapToGrid(boolean snapToGrid){
m_snapToGrid =snapToGrid;
}
/**
* Get the snapSize between grid.
* @return the snapSize.
*/
public double getSnapSize(){
return m_snapSize;
}
/**
* Set the snapSize between grid.
* @param snapSize A new snapSize.
*/
public void setSnapSize(double snapSize){
m_snapSize =snapSize;
}
/**
* Get the snap size type.
* @return the snap size type.
*/
public int getSnapSizeType(){
return m_snapSizeType;
}
/**
* Set the snap size type.
* @param snapSizeType A new snap size type.
*/
public void setSnapSizeType(int snapSizeType){
m_snapSizeType =snapSizeType;
}
/**
* get the snap size in pixels.
* @param zoom Zoom scale,default to 1.
* @return snap size in pixels
*/
public double getSnapSizeInPixels(double zoom){
return getSizeInPixels(m_snapSize,m_snapSizeType) * zoom;
}
/**
* Set new value for this gridFormat.
*
* @param gridFormat A new grid format.
*
*/
public void setValue(GridFormat gridFormat){
if (gridFormat==null)
return;
m_gridColor =new Color(gridFormat.getGridColor().getRGB());
m_gridType =gridFormat.m_gridType;
m_gridSize =gridFormat.m_gridSize;
m_gridSizeType =gridFormat.m_gridSizeType;
m_snapToGrid =gridFormat.m_snapToGrid;
m_snapSize =gridFormat.m_snapSize;
m_snapSizeType =gridFormat.m_snapSizeType;
m_width =gridFormat.m_width;
m_height =gridFormat.m_height;
}
/**
* Draw grids on current canvas.
* @param g A graphics context.
* @param zoom Zoom scale,default to 1.
*/
public void draw(Graphics g, double zoom){
if (g==null)
return;
if (m_width<=1 || m_height<=1 || m_gridSize<=0 || m_gridType==GRIDTYPE_NONE)
return;
Graphics2D g2 =(Graphics2D)g;
g2.setStroke(new BasicStroke(1));
g2.setColor(m_gridColor);
switch (m_gridType){
case GRIDTYPE_LINE:
drawLineGrids(g2,zoom);
break;
case GRIDTYPE_CROSS:
drawCrossGrids(g2,zoom);
break;
case GRIDTYPE_DOT:
drawDotGrids(g2,zoom);
break;
}
}
/**
* Draw line grids on current canvas.
* @param g A graphics context.
* @param zoom Zoom scale,default to 1.
*/
private void drawLineGrids(Graphics2D g2, double zoom){
GeneralPath path =new GeneralPath();
float step =(float)getGridSizeInPixels(zoom);
if (step<=1)
return;
float x =step;
float width =(float)m_width;
float height =(float)m_height;
//vertical lines
while (x<=width){
path.moveTo(x-1,0);
path.lineTo(x-1,height);
x +=step;
}
//horizontall lines
float y =step;
while (y<=height){
path.moveTo(0,y-1);
path.lineTo(width,y-1);
y +=step;
}
g2.draw(path);
}
/**
* Draw cross grids on current canvas.
* @param g A graphics context.
* @param zoom Zoom scale,default to 1.
*/
private void drawCrossGrids(Graphics2D g2,double zoom){
GeneralPath path =new GeneralPath();
float halfCross =3;//two pixels of a half of the cross line.
float step =(float)getGridSizeInPixels(zoom);
if (step<=1)
return;
float x =step-1;
float width =(float)m_width;
float height =(float)m_height;
while (x<=width){
float y =step-1;
while (y<=height){
//horizontal line segment of cross
path.moveTo(x-halfCross,y);
path.lineTo(x+halfCross,y);
//vertical line segment of cross
path.moveTo(x,y-halfCross);
path.lineTo(x,y+halfCross);
y +=step;
}
x +=step;
}
g2.draw(path);
}
/**
* Draw dot grids on current canvas.
* @param g A graphics context.
* @param zoom Zoom scale,default to 1.
*/
private void drawDotGrids(Graphics2D g2,double zoom){
GeneralPath path =new GeneralPath();
float step =(float)getGridSizeInPixels(zoom);
if (step<=1)
return;
float x =step-1;
float width =(float)m_width;
float height =(float)m_height;
while (x<=width){
float y =step-1;
while (y<=height){
//first line of the dot(actually a line in two pixels)
path.moveTo(x,y);
path.lineTo(x+1,y);
//second line of the dot(actually a line in two pixels)
path.moveTo(x,y+1);
path.lineTo(x+1,y+1);
y +=step;
}
x +=step;
}
g2.draw(path);
}
/** A temporary position for a new snapped mouse position.*/
private JFPoint m_snapPos =new JFPoint();
/** get snap position.
* @param newX,newY A new position that need to be snapped.
* @return A snapped position
*/
public JFPoint getSnapPos(double newX, double newY){
boolean snapToGrid =getSnapToGrid();
double snapSize =getSnapSizeInPixels(1);
if (!snapToGrid || snapSize<=1){
m_snapPos.setValue(newX,newY);
return m_snapPos;
}
//snap to increment
//newX =refX + ((int)((newX-refX)/snapSize))* snapSize;
//newY =refY + ((int)((newY-refY)/snapSize))* snapSize;
//snap to grid
newX =Math.round(newX/snapSize) * snapSize;
newY =Math.round(newY/snapSize) * snapSize;
m_snapPos.setValue(newX,newY);
return m_snapPos;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -