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

📄 ruler.java

📁 完全基于java开发的svg矢量绘图工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package fr.itris.glips.svgeditor.canvas;

import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
import fr.itris.glips.svgeditor.*;
import java.util.*;

/**
 * the class of the rulers for a canvas
 * @author Jordi SUC
 */
public class Ruler extends JPanel{

	/**
	 * the font
	 */
	private final static Font font=new Font("theFont", Font.ROMAN_BASELINE, 9);
	
	/**
	 * the scrollpane
	 */
	private SVGScrollPane scrollpane=null;
	
	/**
	 * the ruler refresh manager
	 */
	private RulerRefreshManager rulerRefreshManager;
	
	/**
	 * whether the ruler is horizontal or vertical
	 */
	private boolean isHorizontal=true;
	
	/**
	 * the range object
	 */
	private Range range=null;
	
	/**
	 * the current position of the cursor
	 */
	private double currentPosition=0;
	
	/**
	 * the shape of the cursor 
	 */
	private GeneralPath cursorShape=new GeneralPath();
	
	/**
	 * the colors for drawing the cursors
	 */
	private static final Color fillColorCursor=new Color(255, 0, 0);
	
	/**
	 * the constructor of the class
	 * @param scrollpane a scroll pane
	 * @param isHorizontal whether the ruler is horizontal or vertical
	 */
	public Ruler(SVGScrollPane scrollpane, boolean isHorizontal){
		
		this.scrollpane=scrollpane;
		this.isHorizontal=isHorizontal;
		setDoubleBuffered(false);
		setOpaque(false);
		rulerRefreshManager=new RulerRefreshManager();
		rulerRefreshManager.start();
		
		//creating the shape of the cursor
		if(isHorizontal){
			
			cursorShape.moveTo(0, 0);
			cursorShape.lineTo(6, 0);
			cursorShape.lineTo(0, 6);
			cursorShape.lineTo(-6, 0);
			cursorShape.closePath();
			
		}else{
			
			cursorShape.moveTo(0, 0);
			cursorShape.lineTo(0, -6);
			cursorShape.lineTo(6, 0);
			cursorShape.lineTo(0, 6);
			cursorShape.closePath();
		}
	}
	
	@Override
	public void paint(Graphics g) {

		drawRuler((Graphics2D)g);
		drawCursor(g);
	}
	
	/**
	 * @return the number for aligning
	 */
	public double getRangeForAlignment(){
		
		double number=5;
		
		if(range!=null){
			
			try{
				number=range.getRanges().getLast();
			}catch (Exception ex){}
		}
		
		return number;
	}
	
	/**
	 * refreshes the range of this ruler
	 */
	public void refreshRange(){
		
		//reinitializing the refresh manager
		rulerRefreshManager.reinitialize();
		
		double viewPortLength=0;
		double canvasLength=0;
		double scale=scrollpane.getSVGCanvas().getScale();

		if(isHorizontal){
			
			viewPortLength=scrollpane.getViewPort().width;
			canvasLength=scrollpane.getSVGCanvas().getScaledCanvasSize().width;
			
		}else{
			
			viewPortLength=scrollpane.getViewPort().height;
			canvasLength=scrollpane.getSVGCanvas().getScaledCanvasSize().height;
		}
		
		if(canvasLength>viewPortLength){
			
			range=new Range(viewPortLength, scale);
			
		}else{
			
			range=new Range(canvasLength, scale);
		}
	}
	
	/**
	 * draws the cursor
	 * @param g a graphics object
	 */
	protected void drawCursor(Graphics g){
		
		//computing the position of the cursor
		Point mousePoint=scrollpane.getSVGCanvas().getMousePosition();
		
		if(mousePoint!=null){
			
			Rectangle canvasBounds=Ruler.this.scrollpane.getCanvasBounds();
			
			if(Ruler.this.isHorizontal){

				if(canvasBounds.x>0){
					
					currentPosition=mousePoint.x+canvasBounds.x;
					
					if(currentPosition<canvasBounds.x){
						
						currentPosition=canvasBounds.x;
						
					}else if(currentPosition>canvasBounds.x+canvasBounds.width){
						
						currentPosition=canvasBounds.x+canvasBounds.width;
					}
					
				}else{
					
					currentPosition=mousePoint.x+canvasBounds.x;
				}
				
			}else{
				
				if(canvasBounds.y>0){
					
					currentPosition=mousePoint.y+canvasBounds.y;
					
					if(currentPosition<canvasBounds.y){
						
						currentPosition=canvasBounds.y;
						
					}else if(currentPosition>canvasBounds.y+canvasBounds.height){
						
						currentPosition=canvasBounds.y+canvasBounds.height;
					}
					
				}else{
					
					currentPosition=mousePoint.y+canvasBounds.y;
				}
			}
			
			if(currentPosition<0){
				
				currentPosition=0;
			}
		}

		//drawing the cursor
		Shape cursor=null;

		if(isHorizontal){
			
			cursor=cursorShape.createTransformedShape(AffineTransform.getTranslateInstance(currentPosition, getHeight()-cursorShape.getBounds().height));
			
		}else{
			
			cursor=cursorShape.createTransformedShape(AffineTransform.getTranslateInstance(getWidth()-cursorShape.getBounds().width, currentPosition));
		}
		
		//paint the cursor
		Graphics2D g2=(Graphics2D)g.create();
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
		
		g2.setColor(fillColorCursor);
		g2.fill(cursor);
		
		g.dispose();
	}
	
	/**
	 * draws a ruler
	 * @param g a graphics object
	 */
	protected void drawRuler(Graphics2D g) {
		
		if(range==null){
			
			refreshRange();
		}

		g.setColor(Color.black);

		double smallestRange=range.getRanges().getLast();
		double scale=scrollpane.getSVGCanvas().getScale();
		double viewPortOrigin=0, viewPortLength=0, canvasLength=0;
		Rectangle canvasBounds=scrollpane.getCanvasBounds();

		//getting values used to draw the ruler
		double startPoint=0, startRangePoint=0, endPoint=0;
		
		if(canvasLength>viewPortLength){
			
			if(isHorizontal){
				
				viewPortOrigin=-canvasBounds.x;
				viewPortLength=scrollpane.getViewPort().width;
				canvasLength=canvasBounds.width;
				
			}else{
				
				viewPortOrigin=-canvasBounds.y;
				viewPortLength=scrollpane.getViewPort().height;
				canvasLength=canvasBounds.height;
			}

			//the scrolling is enabled
			startPoint=viewPortOrigin/scale;
			endPoint=(viewPortOrigin+viewPortLength)/scale;
			startRangePoint=Math.floor(startPoint/smallestRange)*smallestRange;

			double cur=0;
			
			while(cur<=endPoint){
				
				paintItem(g, cur, (startRangePoint+cur)*scale-viewPortOrigin);
				
				cur+=smallestRange;
			}

		}else{
			
			if(isHorizontal){
				
				viewPortOrigin=canvasBounds.x;
				viewPortLength=canvasBounds.width;
				canvasLength=canvasBounds.width;
				
			}else{
				
				viewPortOrigin=canvasBounds.y;
				viewPortLength=canvasBounds.height;
				canvasLength=canvasBounds.height;
			}
			
			//the scrolling is disabled
			startPoint=0;
			endPoint=canvasLength/scale;
			startRangePoint=0;
			
			double cur=0;
			
			while(cur<=endPoint){
				
				paintItem(g, cur, (startRangePoint+cur)*scale+viewPortOrigin);
				
				cur+=smallestRange;
			}
		}
	}
	
	/**
	 * draws a ruler item
	 * @param g the graphics
	 * @param canvasPos the position of the item in the unscaled canvas coordinates
	 * @param scrollpanePos the position of the item in the scrollpane coordinates
	 */
	protected void paintItem(Graphics g, double canvasPos, double scrollpanePos){
		
		//determining which range should be used for this item and then paints the item
		if(range.getRanges().size()==3){

			if(canvasPos%range.getRanges().getFirst()==0){
				
				paintRawItem(g, (int)canvasPos, (int)Math.round(scrollpanePos), 0, range.getRanges().getFirst()*scrollpane.getSVGCanvas().getScale());
				
			}else if(canvasPos%range.getRanges().get(1)==0){
				
				paintRawItem(g, (int)canvasPos, (int)Math.round(scrollpanePos), 1, range.getRanges().get(1)*scrollpane.getSVGCanvas().getScale());
				
			}else{
				
				paintRawItem(g, (int)canvasPos, (int)Math.round(scrollpanePos), 2, range.getRanges().getLast()*scrollpane.getSVGCanvas().getScale());
			}

		}else if(range.getRanges().size()==2){
			
			if(canvasPos%range.getRanges().getFirst()==0){
				
				paintRawItem(g, (int)canvasPos, (int)Math.round(scrollpanePos), 0, range.getRanges().getFirst()*scrollpane.getSVGCanvas().getScale());
				
			}else{
				
				paintRawItem(g, (int)canvasPos, (int)Math.round(scrollpanePos), 1, range.getRanges().get(1)*scrollpane.getSVGCanvas().getScale());
			}

		}else if(range.getRanges().size()==1){
			
			paintRawItem(g, (int)canvasPos, (int)Math.round(scrollpanePos), 0, range.getRanges().getFirst()*scrollpane.getSVGCanvas().getScale());
		}
	}
	
	/**
	 * paints an item

⌨️ 快捷键说明

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