📄 abstractrenderer2d.java
字号:
{ graphics.drawLine((int) (screencoords[0] + xincdashspace * counter), (int) (screencoords[1] + yincdashspace * counter), (int) (screencoords[0] + xincdashspace * counter + xincdash), (int) (screencoords[1] + yincdashspace * counter + yincdash)); counter++; } if ((dashlength + spacelength) * counter <= linelength) { graphics.drawLine((int) (screencoords[0] + xincdashspace * counter), (int) (screencoords[1] + yincdashspace * counter), screencoords[2], screencoords[3]); } } } /** * Paints The given double bond. * *@param bond The double bond to be drawn */ public void paintDoubleBond(org.openscience.cdk.interfaces.IBond bond, Color bondColor, Graphics2D graphics) { int[] coords = GeometryTools.distanceCalculator(GeometryTools.getBondCoordinates(bond,r2dm.getRenderingCoordinates()), r2dm.getBondDistance() / 2); int[] newCoords1 = {coords[0], coords[1], coords[6], coords[7]}; paintOneBond(newCoords1, bondColor, graphics); int[] newCoords2 = {coords[2], coords[3], coords[4], coords[5]}; paintOneBond(newCoords2, bondColor, graphics); } /** * Paints the given triple bond. * *@param bond The triple bond to be drawn */ public void paintTripleBond(org.openscience.cdk.interfaces.IBond bond, Color bondColor, Graphics2D graphics) { paintSingleBond(bond, bondColor, graphics); int[] coords = GeometryTools.distanceCalculator(GeometryTools.getBondCoordinates(bond,r2dm.getRenderingCoordinates()), (r2dm.getBondWidth() / 2 + r2dm.getBondDistance())); int[] newCoords1 = {coords[0], coords[1], coords[6], coords[7]}; paintOneBond(newCoords1, bondColor, graphics); int[] newCoords2 = {coords[2], coords[3], coords[4], coords[5]}; paintOneBond(newCoords2, bondColor, graphics); } /** * Paints the inner bond of a double bond that is part of a ring. * *@param bond The bond to be drawn *@param ring The ring the bond is part of *@param bondColor Color of the bond */ public void paintInnerBond(org.openscience.cdk.interfaces.IBond bond, IRing ring, Color bondColor, Graphics2D graphics) { Point2d center = GeometryTools.get2DCenter(ring, r2dm.getRenderingCoordinates()); int[] coords = GeometryTools.distanceCalculator(GeometryTools.getBondCoordinates(bond,r2dm.getRenderingCoordinates()), (r2dm.getBondWidth() / 2 + r2dm.getBondDistance())); double dist1 = Math.sqrt(Math.pow((coords[0] - center.x), 2) + Math.pow((coords[1] - center.y), 2)); double dist2 = Math.sqrt(Math.pow((coords[2] - center.x), 2) + Math.pow((coords[3] - center.y), 2)); if (dist1 < dist2) { int[] newCoords1 = {coords[0], coords[1], coords[6], coords[7]}; paintOneBond(shortenBond(newCoords1, ring.getRingSize()), bondColor, graphics); } else { int[] newCoords2 = {coords[2], coords[3], coords[4], coords[5]}; paintOneBond(shortenBond(newCoords2, ring.getRingSize()), bondColor, graphics); } } /** * Calculates the coordinates for the inner bond of a doublebond that is part * of a ring. It is drawn shorter than a normal bond. * *@param coords The original coordinates of the bond *@param edges Number of edges of the ring it is part of *@return The calculated coordinates of the now shorter bond */ protected int[] shortenBond(int[] coords, int edges) { int xDiff = (coords[0] - coords[2]) / (edges * 2); int yDiff = (coords[1] - coords[3]) / (edges * 2); int[] newCoords = {coords[0] - xDiff, coords[1] - yDiff, coords[2] + xDiff, coords[3] + yDiff}; return newCoords; } /** * Really paints the bond. It is triggered by all the other paintbond methods * to draw a polygon as wide as bond width. * *@param coords *@param bondColor Color of the bond */ public void paintOneBond(int[] coords, Color bondColor, Graphics2D graphics) { graphics.setColor(bondColor); int[] newCoords = GeometryTools.distanceCalculator(coords, r2dm.getBondWidth() / 2); int[] screenCoords = getScreenCoordinates(newCoords); int[] xCoords = {screenCoords[0], screenCoords[2], screenCoords[4], screenCoords[6]}; int[] yCoords = {screenCoords[1], screenCoords[3], screenCoords[5], screenCoords[7]}; graphics.fillPolygon(xCoords, yCoords, 4); } /** * Paints the given bond as a wedge bond. * *@param bond The singlebond to be drawn *@param bondColor Color of the bond */ public void paintWedgeBond(org.openscience.cdk.interfaces.IBond bond, Color bondColor, Graphics2D graphics) { double wedgeWidth = r2dm.getBondWidth() * 2.0; // this value should be made customazible int[] coords = GeometryTools.getBondCoordinates(bond,r2dm.getRenderingCoordinates()); int[] screenCoords = getScreenCoordinates(coords); graphics.setColor(bondColor); int[] newCoords = GeometryTools.distanceCalculator(coords, wedgeWidth); int[] newScreenCoords = getScreenCoordinates(newCoords); if (bond.getStereo() == CDKConstants.STEREO_BOND_UP) { int[] xCoords = {screenCoords[0], newScreenCoords[6], newScreenCoords[4]}; int[] yCoords = {screenCoords[1], newScreenCoords[7], newScreenCoords[5]}; graphics.fillPolygon(xCoords, yCoords, 3); } else { int[] xCoords = {screenCoords[2], newScreenCoords[0], newScreenCoords[2]}; int[] yCoords = {screenCoords[3], newScreenCoords[1], newScreenCoords[3]}; graphics.fillPolygon(xCoords, yCoords, 3); } } /** * Paints the given bond as a dashed wedge bond. * *@param bond The single bond to be drawn *@param bondColor Color of the bond */ public void paintDashedWedgeBond(org.openscience.cdk.interfaces.IBond bond, Color bondColor, Graphics2D graphics) { graphics.setColor(bondColor); double bondLength = GeometryTools.getLength2D(bond, r2dm.getRenderingCoordinates()); int numberOfLines = (int) (bondLength / 4.0); // this value should be made customizable double wedgeWidth = r2dm.getBondWidth() * 2.0; // this value should be made customazible double widthStep = wedgeWidth / (double) numberOfLines; Point2d point1 = r2dm.getRenderingCoordinate(bond.getAtom(0)); Point2d point2 = r2dm.getRenderingCoordinate(bond.getAtom(1)); if (bond.getStereo() == CDKConstants.STEREO_BOND_DOWN_INV) { // draw the wedge bond the other way around point1 = r2dm.getRenderingCoordinate(bond.getAtom(1)); point2 = r2dm.getRenderingCoordinate(bond.getAtom(0)); } Vector2d lengthStep = new Vector2d(point2); lengthStep.sub(point1); lengthStep.scale(1.0 / numberOfLines); Vector2d vector2d = GeometryToolsInternalCoordinates.calculatePerpendicularUnitVector(point1, point2); Point2d currentPoint = new Point2d(point1); Point2d q1 = new Point2d(); Point2d q2 = new Point2d(); for (int i = 0; i <= numberOfLines; ++i) { Vector2d offset = new Vector2d(vector2d); offset.scale(i * widthStep); q1.add(currentPoint, offset); q2.sub(currentPoint, offset); int[] lineCoords = {(int) q1.x, (int) q1.y, (int) q2.x, (int) q2.y}; lineCoords = getScreenCoordinates(lineCoords); graphics.drawLine(lineCoords[0], lineCoords[1], lineCoords[2], lineCoords[3]); currentPoint.add(lengthStep); } } /** * Paints a line between the start point and end point of the pointer vector * that is stored in the Renderer2DModel. */ public void paintPointerVector(Graphics2D graphics) { if (r2dm.getPointerVectorStart() != null) { if (r2dm.getPointerVectorEnd() != null) { Point startPoint = r2dm.getPointerVectorStart(); Point endPoint = r2dm.getPointerVectorEnd(); int[] points = {startPoint.x, startPoint.y, endPoint.x, endPoint.y}; int[] newCoords = GeometryTools.distanceCalculator(points, r2dm.getBondWidth() / 2); int[] screenCoords = getScreenCoordinates(newCoords); int[] xCoords = {screenCoords[0], screenCoords[2], screenCoords[4], screenCoords[6]}; int[] yCoords = {screenCoords[1], screenCoords[3], screenCoords[5], screenCoords[7]}; graphics.setColor(r2dm.getForeColor()); // apply zoomFactor graphics.fillPolygon(xCoords, yCoords, 4); } else { logger.warn("Start point of vector was not null, but end was!"); } } else if (r2dm.getPointerVectorEnd() != null) { logger.warn("End point of vector was not null, but start was!"); } } /** * Returns the Renderer2DModel of this Renderer. * *@return the Renderer2DModel of this Renderer */ public Renderer2DModel getRenderer2DModel() { return this.r2dm; } /** * Sets the Renderer2DModel of this Renderer. * *@param r2dm the new Renderer2DModel for this Renderer */ public void setRenderer2DModel(Renderer2DModel r2dm) { this.r2dm = r2dm; } /** * Gets the screenCoordinates attribute of the Renderer2D object * *@return The screenCoordinates value */ protected Point getScreenCoordinates(Point point) { graphicsHeight = (int) r2dm.getBackgroundDimension().getHeight(); //logger.debug("HEIGHT: " + graphicsHeight); Point screenCoordinate = new Point(); double zoomFactor = r2dm.getZoomFactor(); screenCoordinate.x = (int) ((double) point.x * zoomFactor); screenCoordinate.y = graphicsHeight - (int) ((double) point.y * zoomFactor); if(useScreenSize){ screenCoordinate.y=graphicsHeight - screenCoordinate.y; }else{ screenCoordinate.y+=10; screenCoordinate.x+=10; } return screenCoordinate; } /** * Expects an array of even length with x's at the uneven indices and y's at * the even indices. * *@return The screenCoordinates value */ public int[] getScreenCoordinates(int[] coords) { graphicsHeight = (int) r2dm.getBackgroundDimension().getHeight(); logger.debug("HEIGHT: " + graphicsHeight); int[] screenCoordinates = new int[coords.length]; double zoomFactor = r2dm.getZoomFactor(); final int coordCount = coords.length / 2; for (int i = 0; i < coordCount; i++) { screenCoordinates[i * 2] = (int) ((double) coords[i * 2] * zoomFactor); screenCoordinates[i * 2 + 1] = (int) ((double) coords[i * 2 + 1] * zoomFactor); if(useScreenSize){ screenCoordinates[i * 2 + 1]=graphicsHeight - screenCoordinates[i * 2 + 1]; }else{ screenCoordinates[i * 2 + 1]+=10; screenCoordinates[i * 2]+=10; } } return screenCoordinates; } /** * Gets the screenSize attribute of the Renderer2D object * *@return The screenSize value */ protected float getScreenSize(int size) { return (float) size * (float) r2dm.getZoomFactor(); } /** * Paints the toolTipText for an atom * *@param atom The atom. *@param graphics The current graphics object. */ public void paintToolTip(org.openscience.cdk.interfaces.IAtom atom, Graphics2D graphics, int atomNumber) { tooltiparea = new int[4]; String text = r2dm.getToolTipText(atom); String[] result = text.split("\\n"); int widestline = 0; for (int i = 0; i < result.length; i++) { String text2 = result[i]; Font normalFont = r2dm.getFont(); if (normalFont == null) { normalFont = graphics.getFont(); } graphics.setFont(normalFont); FontMetrics fontMetrics = graphics.getFontMetrics(); int atomSymbolW = (new Integer(fontMetrics.stringWidth(text2))).intValue(); if (atomSymbolW > widestline) { widestline = atomSymbolW; } } Font normalFont = r2dm.getFont(); if (normalFont == null) { normalFont = graphics.getFont(); } graphics.setFont(normalFont); FontMetrics fontMetrics = graphics.getFontMetrics(); int[] provcoords = {(int) r2dm.getRenderingCoordinate(atom).x + 10, (int) r2dm.getRenderingCoordinate(atom).y}; int[] screenCoords = getScreenCoordinates(provcoords); for (int i = 0; i < result.length; i++) { if (i == 0) { tooltiparea[0] = screenCoords[0]; tooltiparea[1] = screenCoords[1]; } String text2 = result[i]; int atomSymbolH = (new Integer(fontMetrics.getAscent())).intValue(); graphics.setColor(Color.YELLOW); graphics.fillRect(screenCoords[0], screenCoords[1] + ((atomSymbolH + 4) * i), widestline + 4, atomSymbolH + 4); graphics.setColor(Color.BLACK); graphics.drawString(text2, screenCoords[0] + 2, screenCoords[1] + atomSymbolH + 2 + ((atomSymbolH + 4) * i)); if (i == result.length - 1) { tooltiparea[2] = screenCoords[0] + widestline + 4; tooltiparea[3] = screenCoords[1] + ((atomSymbolH + 4) * i) + atomSymbolH + 4; } } } /** * The mouseMoved event (used for atom toolTipTexts). * *@param mouseEvent The event. */ public void mouseMoved(MouseEvent mouseEvent) { if (r2dm.getHighlightedAtom() != null) { r2dm.setShowTooltip(true); } else { r2dm.setShowTooltip(false); } } /** * The mouseDragged event (not used currently). * *@param mouseEvent The event. */ public void mouseDragged(MouseEvent mouseEvent) { } public void updateRenderingCoordinates(IAtomContainer atomCon, Renderer2DModel r2dm){ for(int i=0;i<atomCon.getAtomCount();i++){ if(r2dm.getRenderingCoordinate(atomCon.getAtom(i))==null) r2dm.setRenderingCoordinate(atomCon.getAtom(i),atomCon.getAtom(i).getPoint2d()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -