📄 drawcanvas.java
字号:
* @return last mouse position
*/
public JFPoint getLastMousePos(){
return m_lastMousePos;
}
/** set last mouse position on this canvas.
* @param x,y The mouse position.
*/
public void setLastMousePos(double x, double y){
m_lastMousePos.setValue(x,y);
}
/** get current drawing page*/
public JFPage getDrawPage(){
return m_page;
}
/** set current drawing page as a new page*/
public void setDrawPage(JFPage page){
m_page.setValue(page);
m_parentDrawPane.setPageFormat(page.getPageFormat().getPageFormat());
}
/** get current operation manager*/
public JFOperationManager getOperationManager(){
return m_operationManager;
}
/** get selection object list.
*/
public Selection getSelection(){
return m_selection;
}
/** repaint all graphs*/
public void paint(Graphics g){
super.paint(g);
//redraw all graphs
drawGraph(g,false);
//draw selection and currently drawing shapes
finishPainting(g);
}
/** call super paint method of JPanel
* @param g Graphics context
*/
protected void superPaint(Graphics g){
super.paint(g);
}
/**
* A finish painting method is used to draw the selection and currently drawing shapes
* after a normal graph drawing operation.
*
* @param g A graphics context
*/
protected void finishPainting(Graphics g){
//redraw all picked graphs
m_selection.drawPicked(g,m_drawState.isRotating());
//try to repaint current drawing shape.
if (m_drawState.getDrawState()!=DrawConst.DRAWSTATE_NONE){
AbstractDrawEvent drawEvent=m_drawState.getCurrentDrawEvent();
if (drawEvent!=null){
drawEvent.drawCurrentShape(g,true);
/** Reset/delete last moving state below..
* This method should be called if in canvas repainting or scrolling,
* and be called manually by paint method. e.g. DrawCanvas.paint.
* The main purpose for this method is to avoid drawing a new unnecessary
* xor line between last point and last temp point.
*/
m_drawState.setLastTempPoint(m_drawState.getLastPoint());
}
}
}
public void update(Graphics g) {
paint(g); // Don't fill with background color; just call paint.
}
protected void clearCanvas(Graphics g){
if (g==null)
g =getGraphics();
Graphics2D g2 =(Graphics2D)g;
Dimension d =getSize();
g2.setPaintMode();
g2.setBackground(m_backgroundColor);
g2.clearRect(0, 0, d.width, d.height);
}
/** init canvas and clear all graph on it*/
protected void initCanvas(Graphics g){
clearCanvas(g);
Graphics2D g2 =(Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
//draw grids
if (!m_hideGrids){
m_gridFormat.setSize(getPreferredSize());
m_gridFormat.draw(g,m_zoomScale);
}
}
/** Draw all graphs
* @param g A graphics context
* @param isXorMode If is in xor mode now.
*/
public void drawGraph(Graphics g, boolean isXorMode){
java.awt.Component c =GUIUtils.getParent(this,".DrawFrame");
if (c!=null){
DrawFrame drawFrame =(DrawFrame)c;
if (drawFrame!=null){
String title =drawFrame.getCurrentTitle();
if (m_page.isModified())
title +=" *";
drawFrame.setDisplayTitle(title);
}
}
initCanvas(g);
//repaint all shapes in current page.
m_page.draw(g,isXorMode);
}
public void fireMouseEvent(MouseEvent e){
if (!hasFocus()){
requestFocus();
}
if (e!=null){
m_eventDispatcher.fireEvent(e);
}
}
/**
* Mouse press method required by the MouseListener interface.
* @param e A mouse press event.
*/
public void mousePressed(MouseEvent e) {
fireMouseEvent(e);
}
/**
* Mouse release method required by the MouseListener interface.
* @param e A mouse release event.
*/
public void mouseReleased(MouseEvent e) {
fireMouseEvent(e);
}
/**
* Mouse enter method required by the MouseListener interface.
* @param e A mouse enter event.
*/
public void mouseEntered(MouseEvent e) {
fireMouseEvent(e);
}
/**
* Mouse exit method required by the MouseListener interface.
* @param e A mouse exit event.
*/
public void mouseExited(MouseEvent e) {
fireMouseEvent(e);
}
/**
* Mouse click method required by the MouseListener interface.
* @param e A mouse click event.
*/
public void mouseClicked(MouseEvent e) {
fireMouseEvent(e);
}
/**
* Mouse move method required by the MouseMotionListener interface.
* @param e A mouse move event.
*/
public void mouseMoved(MouseEvent e) {
fireMouseEvent(e);
}
/**
* Mouse dragging method required by the MouseMotionListener interface.
* @param e A mouse drag event.
*/
public void mouseDragged(MouseEvent e) {
//The user is dragging us, so scroll!
//Rectangle r = new Rectangle(e.getX(), e.getY(), 1, 1);
//scrollRectToVisible(r);
fireMouseEvent(e);
}
/**
* Invoked when a key has been typed. This event occurs when a key press is followed by a key release.
* @param e A key event object.
*/
public void keyTyped(KeyEvent e){
//do nothing
}
/**
* Invoked when a key has been pressed
* @param e A key event object.
*/
public void keyPressed(KeyEvent e){
int keyCode =e.getKeyCode();
switch (keyCode){
case KeyEvent.VK_LEFT:
case KeyEvent.VK_RIGHT:
case KeyEvent.VK_UP:
case KeyEvent.VK_DOWN:
m_eventDispatcher.moveSelectionByKeyboard(keyCode);
e.consume();
break;
case KeyEvent.VK_DELETE:
getParentDrawPane().actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, com.jfimagine.jfdraw.action.ActionConst.CMD_EDIT_DELETE));
e.consume();
break;
default:
//other key stroke.
break;
}
}
/**
* Invoked when a key has been released
* @param e A key event object.
*/
public void keyReleased(KeyEvent e){
}
/** 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;
}
/**
* Get page format.
*
* @return The page format.
*
*/
public JFPageFormat getPageFormat(){
return m_page.getPageFormat();
}
/**
* Set page format.
*
* @param pageFormat A new page format.
*
*/
public void setPageFormat(PageFormat pageFormat){
m_page.getPageFormat().setPageFormat(pageFormat);
}
/**
* Get canvas format.
*
* @return The canvas format.
*
*/
public CanvasFormat getCanvasFormat(){
return m_page.getCanvasFormat();
}
/**
* Set canvas format.
*
* @param pageFormat A new page format.
*
*/
public void setCanvasFormat(CanvasFormat canvasFormat){
m_page.getCanvasFormat().setValue(canvasFormat);
adjustQuadrantsSize();
}
/**
* Adjust all quadrants' size of this page
*/
public void adjustQuadrantsSize(){
CanvasFormat canvasFormat =m_page.getCanvasFormat();
//quadrant size
double width =Math.max(canvasFormat.getScreenWidth(),JFQuadrant.DEFAULT_SIZE);
double height =Math.max(canvasFormat.getScreenHeight(),JFQuadrant.DEFAULT_SIZE);
//find all quadrants in all layers of this page,then change their size.
ObjectList layerList =m_page.getLayerList();
Iterator it =layerList.getList().iterator();
while (it!=null && it.hasNext()){
JFLayer layer =(JFLayer)it.next();
ObjectList shapeList =layer.getShapeList();
Iterator it1 =shapeList.getList().iterator();
while (it1!=null && it1.hasNext()){
AbstractShape shape =(AbstractShape)it1.next();
if (shape instanceof JFQuadrant){
JFQuadrant quadrant =(JFQuadrant)shape;
quadrant.setSize(width,height);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -