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

📄 cpcanvas.java

📁 this is best wamp jkbkgnkldjkb jkfbjdksgkjl bjkgsbkjfdb gjdsblkj gbfkjsd
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

	// FIXME: dragLeft no longer necessary, should not specify the drag button

	class CPFreehandMode extends CPMode {

		boolean dragLeft = false;
		Point2D.Float smoothMouse = new Point2D.Float(0, 0);

		public void mousePressed(MouseEvent e) {
			if (!dragLeft && e.getButton() == MouseEvent.BUTTON1) {
				Point p = e.getPoint();
				Point2D.Float pf = coordToDocument(p);

				dragLeft = true;
				artwork.beginStroke(pf.x, pf.y, CPTablet.getRef().getPressure());

				smoothMouse = (Point2D.Float) pf.clone();
			}
		}

		public void mouseDragged(MouseEvent e) {
			Point p = e.getPoint();
			Point2D.Float pf = coordToDocument(p);

			float smoothing = Math.min(.999f, (float) Math.pow(controller.getBrushInfo().smoothing, .3));

			smoothMouse.x = (1f - smoothing) * pf.x + smoothing * smoothMouse.x;
			smoothMouse.y = (1f - smoothing) * pf.y + smoothing * smoothMouse.y;

			if (dragLeft) {
				artwork.continueStroke(smoothMouse.x, smoothMouse.y, CPTablet.getRef().getPressure());
			}
		}

		public void mouseReleased(MouseEvent e) {
			if (dragLeft && e.getButton() == MouseEvent.BUTTON1) {
				dragLeft = false;
				artwork.endStroke();

				activeMode = defaultMode; // yield control to the default mode
			}
		}
	}

	//
	// Line drawing mode
	//

	class CPLineMode extends CPMode {

		boolean dragLine = false;
		Point dragLineFrom, dragLineTo;

		public void mousePressed(MouseEvent e) {
			if (!dragLine && e.getButton() == MouseEvent.BUTTON1) {
				Point p = e.getPoint();

				dragLine = true;
				dragLineFrom = dragLineTo = (Point) p.clone();
			}
		}

		public void mouseDragged(MouseEvent e) {
			Point p = e.getPoint();

			Rectangle r = new Rectangle(Math.min(dragLineFrom.x, dragLineTo.x), Math.min(dragLineFrom.y, dragLineTo.y),
					Math.abs(dragLineFrom.x - dragLineTo.x) + 1, Math.abs(dragLineFrom.y - dragLineTo.y) + 1);
			r = r.union(new Rectangle(Math.min(dragLineFrom.x, p.x), Math.min(dragLineFrom.y, p.y), Math
					.abs(dragLineFrom.x - p.x) + 1, Math.abs(dragLineFrom.y - p.y) + 1));
			dragLineTo = (Point) p.clone();
			repaint(r.x, r.y, r.width, r.height);
		}

		public void mouseReleased(MouseEvent e) {
			if (dragLine && e.getButton() == MouseEvent.BUTTON1) {
				Point p = e.getPoint();
				Point2D.Float pf = coordToDocument(p);

				dragLine = false;

				Point2D.Float from = coordToDocument(dragLineFrom);
				artwork.beginStroke(from.x, from.y, 1);
				artwork.continueStroke(pf.x, pf.y, 1);
				artwork.endStroke();

				Rectangle r = new Rectangle(Math.min(dragLineFrom.x, dragLineTo.x), Math.min(dragLineFrom.y,
						dragLineTo.y), Math.abs(dragLineFrom.x - dragLineTo.x) + 1, Math.abs(dragLineFrom.y
						- dragLineTo.y) + 1);
				repaint(r.x, r.y, r.width, r.height);

				activeMode = defaultMode; // yield control to the default mode
			}
		}

		public void paint(Graphics2D g2d) {
			if (dragLine) {
				g2d.drawLine(dragLineFrom.x, dragLineFrom.y, dragLineTo.x, dragLineTo.y);
			}
		}
	}

	//
	// Bezier drawing mode
	//

	class CPBezierMode extends CPMode {

		// bezier drawing
		static final int BEZIER_POINTS = 500;
		static final int BEZIER_POINTS_PREVIEW = 100;

		boolean dragBezier = false;
		int dragBezierMode; // 0 Initial drag, 1 first control point, 2 second point
		Point2D.Float dragBezierP0, dragBezierP1, dragBezierP2, dragBezierP3;

		public void mousePressed(MouseEvent e) {
			Point2D.Float p = coordToDocument(e.getPoint());

			if (!dragBezier && !spacePressed && e.getButton() == MouseEvent.BUTTON1) {
				dragBezier = true;
				dragBezierMode = 0;
				dragBezierP0 = dragBezierP1 = dragBezierP2 = dragBezierP3 = (Point2D.Float) p.clone();
			}
		}

		public void mouseDragged(MouseEvent e) {
			Point2D.Float p = coordToDocument(e.getPoint());

			if (dragBezier && dragBezierMode == 0) {
				dragBezierP2 = dragBezierP3 = (Point2D.Float) p.clone();
				repaint();
			}
		}

		public void mouseReleased(MouseEvent e) {
			if (dragBezier && e.getButton() == MouseEvent.BUTTON1) {
				if (dragBezierMode == 0) {
					dragBezierMode = 1;
				} else if (dragBezierMode == 1) {
					dragBezierMode = 2;
				} else if (dragBezierMode == 2) {
					dragBezier = false;

					Point2D.Float p0 = dragBezierP0;
					Point2D.Float p1 = dragBezierP1;
					Point2D.Float p2 = dragBezierP2;
					Point2D.Float p3 = dragBezierP3;

					CPBezier bezier = new CPBezier();
					bezier.x0 = p0.x;
					bezier.y0 = p0.y;
					bezier.x1 = p1.x;
					bezier.y1 = p1.y;
					bezier.x2 = p2.x;
					bezier.y2 = p2.y;
					bezier.x3 = p3.x;
					bezier.y3 = p3.y;

					float x[] = new float[BEZIER_POINTS];
					float y[] = new float[BEZIER_POINTS];

					bezier.compute(x, y, BEZIER_POINTS);

					artwork.beginStroke(x[0], y[0], 1);
					for (int i = 1; i < BEZIER_POINTS; i++) {
						artwork.continueStroke(x[i], y[i], 1);
					}
					artwork.endStroke();
					repaint();

					activeMode = defaultMode; // yield control to the default mode
				}
			}
		}

		public void mouseMoved(MouseEvent e) {
			Point2D.Float p = coordToDocument(e.getPoint());

			if (dragBezier && dragBezierMode == 1) {
				dragBezierP1 = (Point2D.Float) p.clone();
				repaint(); // FIXME: repaint only the bezier region
			}

			if (dragBezier && dragBezierMode == 2) {
				dragBezierP2 = (Point2D.Float) p.clone();
				repaint(); // FIXME: repaint only the bezier region
			}
		}

		public void paint(Graphics2D g2d) {
			if (dragBezier) {
				CPBezier bezier = new CPBezier();

				Point2D.Float p0 = coordToDisplay(dragBezierP0);
				Point2D.Float p1 = coordToDisplay(dragBezierP1);
				Point2D.Float p2 = coordToDisplay(dragBezierP2);
				Point2D.Float p3 = coordToDisplay(dragBezierP3);

				bezier.x0 = p0.x;
				bezier.y0 = p0.y;
				bezier.x1 = p1.x;
				bezier.y1 = p1.y;
				bezier.x2 = p2.x;
				bezier.y2 = p2.y;
				bezier.x3 = p3.x;
				bezier.y3 = p3.y;

				int x[] = new int[BEZIER_POINTS_PREVIEW];
				int y[] = new int[BEZIER_POINTS_PREVIEW];
				bezier.compute(x, y, BEZIER_POINTS_PREVIEW);

				g2d.drawPolyline(x, y, BEZIER_POINTS_PREVIEW);
				g2d.drawLine((int) p0.x, (int) p0.y, (int) p1.x, (int) p1.y);
				g2d.drawLine((int) p2.x, (int) p2.y, (int) p3.x, (int) p3.y);
			}
		}
	}

	//
	// Color picker mode
	//

	class CPColorPickerMode extends CPMode {

		int mouseButton;

		public void mousePressed(MouseEvent e) {
			Point p = e.getPoint();
			Point2D.Float pf = coordToDocument(p);

			mouseButton = e.getButton();

			if (artwork.isPointWithin(pf.x, pf.y)) {
				controller.setCurColorRgb(artwork.colorPicker(pf.x, pf.y));
			}

			setCursor(crossCursor);
		}

		public void mouseDragged(MouseEvent e) {
			Point p = e.getPoint();
			Point2D.Float pf = coordToDocument(p);

			if (artwork.isPointWithin(pf.x, pf.y)) {
				controller.setCurColorRgb(artwork.colorPicker(pf.x, pf.y));
			}
		}

		public void mouseReleased(MouseEvent e) {
			if (e.getButton() == mouseButton) {
				setCursor(defaultCursor);
				activeMode = defaultMode; // yield control to the default mode
			}
		}
	}

	//
	// Canvas move mode
	//

	class CPMoveCanvasMode extends CPMode {

		boolean dragMiddle = false;
		int dragMoveX, dragMoveY;
		Point dragMoveOffset;
		int dragMoveButton;

		public void mousePressed(MouseEvent e) {
			Point p = e.getPoint();

			if (!dragMiddle && (e.getButton() == MouseEvent.BUTTON2 || spacePressed)) {
				repaintBrushPreview();

				dragMiddle = true;
				dragMoveButton = e.getButton();
				dragMoveX = p.x;
				dragMoveY = p.y;
				dragMoveOffset = getOffset();
				setCursor(moveCursor);
			}
		}

		public void mouseDragged(MouseEvent e) {
			if (dragMiddle) {
				Point p = e.getPoint();

				setOffset(dragMoveOffset.x + p.x - dragMoveX, offsetY = dragMoveOffset.y + p.y - dragMoveY);
				repaint();
			}
		}

		public void mouseReleased(MouseEvent e) {
			if (dragMiddle && e.getButton() == dragMoveButton) {
				dragMiddle = false;
				setCursor(defaultCursor);

				activeMode = defaultMode; // yield control to the default mode
			}
		}
	}

	//
	// Flood fill mode
	//

	class CPFloodFillMode extends CPMode {

		public void mousePressed(MouseEvent e) {
			Point p = e.getPoint();
			Point2D.Float pf = coordToDocument(p);

			if (artwork.isPointWithin(pf.x, pf.y)) {
				artwork.floodFill(pf.x, pf.y);
				repaint();
			}

			activeMode = defaultMode; // yield control to the default mode
		}

		public void mouseDragged(MouseEvent e) {
		}

		public void mouseReleased(MouseEvent e) {
		}
	}

	//
	// CPRectSelection mode
	//

	class CPRectSelectionMode extends CPMode {

		Point firstClick;
		CPRect curRect = new CPRect();

		public void mousePressed(MouseEvent e) {
			Point p = coordToDocumentInt(e.getPoint());

			curRect.makeEmpty();
			firstClick = p;

			repaint();
		}

		public void mouseDragged(MouseEvent e) {
			Point p = coordToDocumentInt(e.getPoint());
			boolean square = e.isShiftDown();
			int squareDist = Math.max(Math.abs(p.x - firstClick.x), Math.abs(p.y - firstClick.y));

			if (p.x >= firstClick.x) {
				curRect.left = firstClick.x;
				curRect.right = square ? firstClick.x + squareDist : p.x;
			} else {
				curRect.left = square ? firstClick.x - squareDist : p.x;
				curRect.right = firstClick.x;
			}

			if (p.y >= firstClick.y) {
				curRect.top = firstClick.y;
				curRect.bottom = square ? firstClick.y + squareDist : p.y;
			} else {
				curRect.top = square ? firstClick.y - squareDist : p.y;
				curRect.bottom = firstClick.y;
			}

			repaint();
		}

		public void mouseReleased(MouseEvent e) {
			artwork.rectangleSelection(curRect);

			activeMode = defaultMode; // yield control to the default mode
			repaint();
		}

		public void paint(Graphics2D g2d) {
			if (!curRect.isEmpty()) {
				g2d.draw(coordToDisplay(curRect));
			}
		}
	}

	//
	// CPMoveTool mode
	//

	class CPMoveToolMode extends CPMode {

		Point firstClick;

		public void mousePressed(MouseEvent e) {
			Point p = coordToDocumentInt(e.getPoint());
			firstClick = p;

			artwork.beginPreviewMode(e.isAltDown());

			// FIXME: The following hack avoids a slight display glitch
			// if the whole move tool mess is fixed it probably won't be necessary anymore
			artwork.move(0, 0);
		}

		public void mouseDragged(MouseEvent e) {
			Point p = coordToDocumentInt(e.getPoint());
			artwork.move(p.x - firstClick.x, p.y - firstClick.y);
			repaint();
		}

		public void mouseReleased(MouseEvent e) {
			artwork.endPreviewMode();
			activeMode = defaultMode; // yield control to the default mode
			repaint();
		}
	}

	//
	// Canvas rotate mode
	//

	class CPRotateCanvasMode extends CPMode {

		Point firstClick;
		float initAngle;
		AffineTransform initTransform;
		boolean dragged;

		public void mousePressed(MouseEvent e) {
			Point p = e.getPoint();
			firstClick = (Point) p.clone();

			initAngle = getRotation();
			initTransform = new AffineTransform(transform);

			dragged = false;

			repaintBrushPreview();
		}

		public void mouseDragged(MouseEvent e) {
			dragged = true;

			Point p = e.getPoint();
			Dimension d = getSize();
			Point2D.Float center = new Point2D.Float(d.width / 2.f, d.height / 2.f);

			float deltaAngle = (float) Math.atan2(p.y - center.y, p.x - center.x)
					- (float) Math.atan2(firstClick.y - center.y, firstClick.x - center.x);

			AffineTransform rotTrans = new AffineTransform();
			rotTrans.rotate(deltaAngle, center.x, center.y);

			rotTrans.concatenate(initTransform);

			setRotation(initAngle + deltaAngle);
			setOffset((int) rotTrans.getTranslateX(), (int) rotTrans.getTranslateY());
			repaint();
		}

		public void mouseReleased(MouseEvent e) {
			if (!dragged) {
				resetRotation();
			}

			activeMode = defaultMode; // yield control to the default mode
		}
	}

	/*
	 * // // mode //
	 * 
	 * class CPMode extends CPMode { public void mousePressed(MouseEvent e) {} public void mouseDragged(MouseEvent e) {}
	 * public void mouseReleased(MouseEvent e) {} }
	 */
}

⌨️ 快捷键说明

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