📄 jfruler.java
字号:
/**
* $Id:JFRuler.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.RenderingHints;
import java.awt.geom.AffineTransform;
import com.jfimagine.jfgraph.geom.Angle;
import com.jfimagine.jfgraph.shape.decorate.JFPageFormat;
import com.jfimagine.jfgraph.shape.base.ShapeConst;
import com.jfimagine.utils.commonutil.CommonUtil;
/**
* JFRuler class. Horizontal or vertical rules for drawing measurement.
*
* @author CookieMaker
*
* @version $Revision: 1.1.1 $
*/
public class JFRuler {
public static final int HORIZONTAL = 0;
public static final int VERTICAL = 1;
/** A default thick size of a ruler*/
public static final int SIZE = 18;
private int m_orientation;
private int m_height =0;
private int m_width =0;
private boolean m_isMetric =true;
private double m_increment;
private double m_units;
/** zoom scale.*/
private double m_zoomScale =1.0;
/**
* Scale value, e.g. 1cm = [scaleValue] * km
*/
private double m_scaleValue =1;
/**
* Scale units, e.g. 1cm = 100 * [scaleUnit]
*/
private int m_scaleUnit =ShapeConst.MEASURE_TYPE_IN;
/**
* Screen unit, e.g. 1 * [screenUnit] = 1000 * meter
*/
private int m_screenUnit =ShapeConst.MEASURE_TYPE_IN;
/**
* Constructors
*/
public JFRuler() {
setIncrementAndUnits();
}
private void setIncrementAndUnits() {
if (m_isMetric) {
m_units = ((double)JFPageFormat.INCH_SCREEN / (double)2.54); // dots per centimeter
m_increment = m_units / 10;
} else {
m_units = JFPageFormat.INCH_SCREEN;
m_increment = m_units / 16;
}
}
public boolean isMetric() {
return m_isMetric;
}
public void setIsMetric(boolean isMetric) {
this.m_isMetric = isMetric;
setIncrementAndUnits();
}
public int getOrientation() {
return m_orientation;
}
public void setOrientation(int orientation) {
m_orientation =orientation;
}
public int getIncrement() {
return (int)getIncrementDouble();
}
public double getIncrementDouble() {
//acutal length per unit.
int lenPerUnit =0;
if (m_zoomScale<0.5)
lenPerUnit =4; //4 inches per unit in english or 4 cms per unit in metric
else if (m_zoomScale<1.0)
lenPerUnit =2; //2 inches per unit in english or 2 cms per unit in metric
else
lenPerUnit =1; //1 inch per unit in english or 1 cm per unit in metric
return (m_increment * m_zoomScale * lenPerUnit);
}
public int getHeight() {
return m_height;
}
public void setHeight(int ph) {
m_height =ph;
}
public int getWidth() {
return m_width;
}
public void setWidth(int pw) {
m_width =pw;
}
public void setScale(double scaleValue, int scaleUnit, int screenUnit){
m_scaleValue =scaleValue;
m_scaleUnit =scaleUnit;
m_screenUnit =screenUnit;
}
/** get zoom scale
* @return the zoom scale
*/
public double getZoomScale(){
return m_zoomScale;
}
/** set zoom scale
* @param zoomScale A new zoom scale.
*/
public void setZoomScale(double zoomScale){
m_zoomScale =zoomScale;
}
/** Get a printing unit on ruler by scale parameters.
* @return the printing unit.
*/
private String getPrintUnit(){
return ShapeConst.getMeasureAbbr(m_scaleUnit);
}
/** Get a printing scaled value on ruler by scale parameters.
* @param value An value to be scaled.
* @return the printing scaled value.
*/
private String getPrintingScaledValue(double value){
double ret=value;
int scaleUnit =m_scaleUnit;
int screenUnit =m_screenUnit;
ret =value * m_scaleValue;
if (isMetric()){
if (screenUnit==ShapeConst.MEASURE_TYPE_IN)
ret /=2.54;
}else{
if (screenUnit==ShapeConst.MEASURE_TYPE_CM)
ret *=2.54;
}
String retValue =CommonUtil.f2fs(ret);
if (retValue.endsWith(".00"))
retValue =retValue.substring(0,retValue.length()-3);
return retValue;
}
public void draw(Graphics g, Color backColor, Color foreColor, boolean fillBackground) {
Graphics2D g2 =(Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
int x =0;
int y =0;
int w =0;
int h =0;
if (m_orientation==HORIZONTAL){
w =(int)(m_width * m_zoomScale);
h =SIZE;
}else{
w =SIZE;
h =(int)(m_height * m_zoomScale);
}
// Fill clipping area with light gray.
if (fillBackground){
g.setColor(backColor);
g.fillRect(x,y,w,h);
}
// Do the ruler labels in a small font that's black.
g.setFont(new Font("Arial", Font.PLAIN, 9));
g.setColor(foreColor);
((Graphics2D)g).setStroke(new BasicStroke(1));
// Some vars we need.
double start = 0;
double end = 0;
int tickLength = 0;
String text = null;
//acutal length per unit.
int lenPerUnit =0;
if (m_zoomScale<0.35)
lenPerUnit =4; //4 inches per unit in english or 4 cms per unit in metric
else if (m_zoomScale<0.7)
lenPerUnit =2; //2 inches per unit in english or 2 cms per unit in metric
else
lenPerUnit =1; //1 inch per unit in english or 1 cm per unit in metric
double unit =m_units * m_zoomScale * lenPerUnit;
double increment =m_increment * m_zoomScale * lenPerUnit;
//a unit will contains how many increment segments.
int oneUnit =16;
if (isMetric())
oneUnit =10;
int halfUnit =oneUnit/2;
int quarterUnit =oneUnit/4; //for inches only
// Use clipping bounds to calculate first and last tick locations.
if (m_orientation == HORIZONTAL) {
start = (int)(x / increment) * increment;
end = ((int)((x + w) / increment) + 1)* increment;
} else {
start =(int)(y / increment) * increment;
end = ((int)((y + h) / increment) + 1) * increment;
}
// Make a special case of 0 to display the number
// within the rule and draw a units label.
if (start == 0) {
text ="0 "+ getPrintUnit();
tickLength = 10;
if (m_orientation == HORIZONTAL) {
g.drawLine(0, SIZE-1, 0, SIZE-tickLength-1);
g.drawString(text, 2, 10);
} else {
g.drawLine(SIZE-1, 0, SIZE-tickLength-1, 0);
//g.drawString("0", 3, 10);
}
text = null;
start = increment;
}
// ticks and labels
int incrementCnt=0;
for (double i = start; i < end; i += increment) {
incrementCnt++;
int currPos =(int)i;
if (incrementCnt % oneUnit == 0) {
tickLength = 10;
//text = Integer.toString(incrementCnt/oneUnit * lenPerUnit);
text =getPrintingScaledValue(incrementCnt/oneUnit * lenPerUnit);
} else if (incrementCnt % halfUnit==0){
tickLength = 7;
text =null;
} else if (!isMetric() && (incrementCnt % quarterUnit==0)){
tickLength = 5;
text =null;
}else{
tickLength = 3;
text = null;
}
if (tickLength != 0) {
if (m_orientation == HORIZONTAL) {
g.drawLine(currPos, SIZE-1, currPos, SIZE-tickLength-1);
if (text != null)
g.drawString(text, currPos+2, 10);
} else {
g.drawLine(SIZE-1, currPos, SIZE-tickLength-1, currPos);
if (text != null){
//g.drawString(text, 3, currPos + 10);
AffineTransform origXform = g2.getTransform();
AffineTransform newXform = (AffineTransform)(origXform.clone());
newXform.rotate(-Angle.PI/2, 10, currPos-3);
g2.setTransform(newXform);
g2.drawString(text,10,currPos-3);
g2.setTransform(origXform);
}
}
}
}
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -