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

📄 libcanvas.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
字号:
/**
 *    $Id:LibCanvas.java $
 *
 *    Copyright 2004 ~ 2005  JingFei International Cooperation LTD. All rights reserved. *
 */
package com.jfimagine.jfdraw.gui.dialog;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Container;
import java.awt.RenderingHints;


import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JPanel;
import javax.swing.Scrollable;
import javax.swing.SwingConstants;
import javax.swing.JPopupMenu;

import com.jfimagine.jfgraph.geom.JFPoint;   

import com.jfimagine.jfgraph.shape.union.JFPage;
import com.jfimagine.jfgraph.shape.union.AbstractLibrary;
import com.jfimagine.jfgraph.shape.union.JFLibElem;

import com.jfimagine.jfgraph.shape.base.ShapeConst;
import com.jfimagine.jfgraph.shape.base.ObjectList;

import com.jfimagine.jfdraw.gui.resource.CADResource;
import com.jfimagine.jfdraw.gui.ToolFactory; 

import com.jfimagine.jfdraw.draw.DrawState;
import com.jfimagine.jfdraw.draw.AbstractDrawEvent;
import com.jfimagine.jfdraw.draw.EventDispatcher;
import com.jfimagine.jfdraw.draw.DrawConst;
import com.jfimagine.jfdraw.draw.Selection;

import com.jfimagine.jfgraph.geom.GeomConst;



 /**
 * Drawing canvas class. A main drawing canvas used to draw or show graphs.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.10 $
 */ 
public class LibCanvas extends JPanel  
		       implements Scrollable,MouseListener{

    /** for scrolling increment. */
    private double m_maxUnitIncrement = 1;
    
    /** A library for this canvas.*/
    private AbstractLibrary m_lib; 
   
    /** parent lib panel */
    private LibPanel m_parentPanel;
    
    /** insert into menu */
    private JPopupMenu m_libMenu;   
    
    /** last mouse click position */
    private JFPoint m_lastMousePos	=new JFPoint(-GeomConst.LARGE_VALUE,-GeomConst.LARGE_VALUE);
    
    /** 
     *  Constructor
     *  @param type Library or template type.
     *  @param parent parent LibPanel.
     */ 
    public LibCanvas(int type,LibPanel parent) {
        setOpaque(true);
        setBackground(Color.white);
        m_parentPanel	=parent;

        setAutoscrolls(true); //enable synthetic drag events
        addMouseListener(this); //handle mouse events(e.g. click, press,release)

    	m_libMenu	=new JPopupMenu();
    	
    	if (type==LibPanel.TYPE_LIBRARY){
    		LibPanel.addCurrLibraryMenus(m_libMenu,parent);
    		m_libMenu.addSeparator();
    		LibPanel.addLibraryMenus(m_libMenu,parent);
    	}else{
    		LibPanel.addCurrTemplateMenus(m_libMenu,parent);
    		m_libMenu.addSeparator();
    		LibPanel.addTemplateMenus(m_libMenu,parent);
	}
    }

    /** set parent lib panel
     *  @param panel a new parent LibPane
     */	
    public void setParentPanel(LibPanel panel){
    	m_parentPanel	=panel;
    }

    /** get parent lib pane
     *  @return the parent lib panel of this canvas.
     */	
    public LibPanel getParentPanel(){
    	return m_parentPanel;
    }

    /** get current library*/	
    public AbstractLibrary getLibrary(){
    	return m_lib;
    }

    /** set current library*/	
    public void setLibrary(AbstractLibrary lib){
    	m_lib	=lib;
    }
        
    /** repaint all graphs*/
    public void	paint(Graphics g){

	Graphics2D g2	=(Graphics2D)g;  
   	Dimension d	=getSize();
       
        g2.setPaintMode();
        //g2.setBackground(getBackground());
        g2.setBackground(Color.white);
        g2.clearRect(0, 0, d.width, d.height);  

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);

	if (m_lib!=null){
        	m_lib.draw(g,getPreferredSize().getWidth(),getPreferredSize().getHeight());
        }
    }


    public void update(Graphics g) {
           paint(g); // Don't fill with background color; just call paint.
    }

    /**
     * Mouse press method required by the MouseListener interface.
     * @param e A mouse press event.
     */
    public void mousePressed(MouseEvent e) {
    }

    /**
     * Mouse release method required by the MouseListener interface.
     * @param e A mouse release event.
     */
    public void mouseReleased(MouseEvent e) {
    }

    /**
     * Mouse enter method required by the MouseListener interface.
     * @param e A mouse enter event.
     */
    public void mouseEntered(MouseEvent e) {
    }

    /**
     * Mouse exit method required by the MouseListener interface.
     * @param e A mouse exit event.
     */
    public void mouseExited(MouseEvent e) {
    }

    /**
     * Mouse click method required by the MouseListener interface.
     * @param e A mouse click event.
     */
    public void mouseClicked(MouseEvent e) {
    	JFPoint pnt	=new JFPoint(e.getX(),e.getY());
    	m_lastMousePos.setValue(pnt);
    	if (m_lib!=null){
 	   	JFLibElem  lastElem	=m_lib.getLastPicked();
 	   	JFLibElem  elem		=m_lib.pickBy(pnt);
 	   	if (lastElem!=null || elem!=null){
 	   		repaint();
 	   	}
	}

    	if (e.isMetaDown()){
		m_libMenu.show(this,e.getX(),e.getY());
	}else if (e.getClickCount()>=2){
		m_parentPanel.insertCurrLibInto();
	}
    }
    
    /**
     *  get last picked element.
     */
    public JFLibElem getLastPickedElement(){
    	if (m_lib==null)
    		return null;
    	return m_lib.pickBy(m_lastMousePos);
    }

	
    /** implements parent method in Interface Scrollable*/	
    public Dimension getPreferredScrollableViewportSize() {
        return getPreferredSize();
    }
    	
    /** implements parent method in Interface Scrollable*/	
    public int getScrollableUnitIncrement(Rectangle visibleRect,
                                          int orientation,
                                          int direction) {
        //Get the current position.
        int currentPosition = 0;
        if (orientation == SwingConstants.HORIZONTAL) {
            currentPosition = visibleRect.x;
        } else {
            currentPosition = visibleRect.y;
        }

        //Return the number of pixels between currentPosition
        //and the nearest tick mark in the indicated direction.
        if (direction < 0) {
            int newPosition =(int)(currentPosition -
                             (currentPosition / m_maxUnitIncrement)
                             * m_maxUnitIncrement);
            return (newPosition == 0) ? (int)m_maxUnitIncrement : newPosition;
        } else {
            return (int)(((currentPosition / m_maxUnitIncrement) + 1)
                   * m_maxUnitIncrement
                   - currentPosition);
        }
    }

    /** implements parent method in Interface Scrollable*/	
    public int getScrollableBlockIncrement(Rectangle visibleRect,
                                           int orientation,
                                           int direction) {
        if (orientation == SwingConstants.HORIZONTAL) {
            return (int)(visibleRect.width - m_maxUnitIncrement);
        } else {
            return (int)(visibleRect.height - m_maxUnitIncrement);
        }
    }

    /** implements parent method in Interface Scrollable*/	
    public boolean getScrollableTracksViewportWidth() {
        return false;
    }

    /** implements parent method in Interface Scrollable*/	
    public boolean getScrollableTracksViewportHeight() {
        return false;
    }

    /** set current max unit increment while scrolling
     *  @param pixels Pixels as unit for scrolling.
     */	
    public void setMaxUnitIncrement(int pixels) {
        m_maxUnitIncrement = pixels;
    }



    
}

⌨️ 快捷键说明

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