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

📄 abstractrenderer2d.java

📁 化学图形处理软件
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
	{		if (width != 0 && height != 0)		{			Color saveColor = graphics.getColor();			graphics.setColor(backColor);			int[] coords = {x - border, y + border};			int[] bounds = {(int) getScreenSize(width + 2 * border),					(int) getScreenSize(height + 2 * border)};			int[] screenCoords = getScreenCoordinates(coords);			graphics.fillRect(screenCoords[0], screenCoords[1],					bounds[0], bounds[1]);			graphics.setColor(saveColor);		}		// else nothing to make empty	}	/**	 *  Paints the label of the given PseudoAtom, instead of it's symbol.	 *	 *@param  atom       The atom to be drawn	 */	public void paintPseudoAtomLabel(IPseudoAtom atom, Color backColor,			Graphics2D graphics, int alignment, boolean isRadical)	{		if (r2dm.getRenderingCoordinate(atom) == null)		{			logger.warn("Cannot draw atom without 2D coordinate");			return;		}		String atomSymbol = atom.getLabel();		if (atomSymbol == null)		{			logger.warn("Cannot draw null symbol: taking symbol as default.");			atomSymbol = atom.getSymbol();		}		if (isRadical)		{			logger.debug(" atom is radical, adding \u00B7");			atomSymbol += "\u00B7";		}		// The calculation fonts		Font normalFont = r2dm.getFont();		if (normalFont == null)		{			normalFont = graphics.getFont();		}		int normalFontSize = normalFont.getSize();		// get drawing fonts		float normalScreenFontSize = getScreenSize(normalFontSize);		Font normalScreenFont = normalFont.deriveFont(normalScreenFontSize);		// calculate SYMBOL width, height		graphics.setFont(normalFont);		FontMetrics fontMetrics = graphics.getFontMetrics();		int atomSymbolW = (new Integer(fontMetrics.stringWidth(atomSymbol))).intValue();		int atomSymbolFirstCharW = (new Integer(fontMetrics.stringWidth(atomSymbol.substring(0, 1)))).intValue();		int atomSymbolLastCharW = (new Integer(fontMetrics.stringWidth(atomSymbol.substring(atomSymbol.length() - 1)))).intValue();		int atomSymbolH = (new Integer(fontMetrics.getAscent())).intValue();		int labelX = 0;		int labelY = 0;		int labelW = atomSymbolW;		int labelH = atomSymbolH;		if (alignment == 1)		{			// left alignment			labelX = (int) (r2dm.getRenderingCoordinate(atom).x - (atomSymbolFirstCharW / 2));		} else		{			// right alignment			labelX = (int) (r2dm.getRenderingCoordinate(atom).x - (atomSymbolW + atomSymbolLastCharW / 2));		}		// labelY and labelH are the same for both left/right aligned		labelY = (int) (r2dm.getRenderingCoordinate(atom).y - (atomSymbolH / 2));		{			// make empty space			int border = 2;			// number of pixels			graphics.setColor(backColor);			int[] coords = {labelX - border, labelY + labelH + border};			int[] bounds = {(int) getScreenSize(labelW + 2 * border),					(int) getScreenSize(labelH + 2 * border)};			int[] screenCoords = getScreenCoordinates(coords);			graphics.fillRect(screenCoords[0], screenCoords[1],					bounds[0], bounds[1]);		}		{			// draw label			int[] coords = {labelX, labelY};			int[] screenCoords = getScreenCoordinates(coords);			graphics.setColor(Color.black);			graphics.setFont(normalScreenFont);			graphics.drawString(atomSymbol, screenCoords[0], screenCoords[1]);		}		// reset old font & color		graphics.setFont(normalFont);		graphics.setColor(r2dm.getForeColor());	}//	public void paintFragmentAtom(FragmentAtom atom, Color backColor,//			Graphics2D graphics, int alignment, boolean isRadical) {//		if (r2dm.getRenderingCoordinate(atom) == null)//		{//			logger.warn("Cannot draw atom without 2D coordinate");//			return;//		}//		//		if (atom.isExpanded()) {//			paintMolecule(atom.getFragment(), graphics);//		} else {//			paintPseudoAtomLabel(atom, backColor, graphics, alignment, isRadical);//		}//	}	public abstract void paintMolecule(IAtomContainer fragment, Graphics2D graphics);	/**	 *  Triggers the suitable method to paint each of the given bonds and selects	 *  the right color.	 *	 *@param  ringSet   The set of rings the molecule contains	 */	public void paintBonds(IAtomContainer atomCon, IRingSet ringSet, Graphics2D graphics)	{		Color bondColor;		IRing ring;		Iterator bonds = atomCon.bonds();		ArrayList painted_rings = new ArrayList();		logger.debug("Painting bonds...");		while (bonds.hasNext())		{			IBond currentBond = (IBond)bonds.next();			bondColor = (Color) r2dm.getColorHash().get(currentBond);			if (bondColor == null)			{				bondColor = r2dm.getForeColor();			}			if (currentBond == r2dm.getHighlightedBond() && (r2dm.getSelectedPart()==null || !r2dm.getSelectedPart().contains(currentBond)))			{				bondColor = r2dm.getHoverOverColor();				for (int j = 0; j < currentBond.getAtomCount(); j++)				{					paintColouredAtomBackground(currentBond.getAtom(j),							bondColor, graphics);				}			}			ring = RingSetManipulator.getHeaviestRing(ringSet, currentBond);			if (ring != null)			{				logger.debug("Found ring to draw");				if (ringIsAromatic(ring) && r2dm.getShowAromaticity())				{					logger.debug("Ring is aromatic");					if (r2dm.getShowAromaticityInCDKStyle())					{						paintAromaticRingBondCDKStyle(currentBond, ring, bondColor, graphics);					} else					{						if (!painted_rings.contains(ring))						{							paintRingRing(ring, bondColor, graphics);							painted_rings.add(ring);						}						paintSingleBond(currentBond, bondColor, graphics);					}				} else				{					logger.debug("Ring is *not* aromatic");					paintRingBond(currentBond, ring, bondColor, graphics);				}			} else			{				logger.debug("Drawing a non-ring bond");				paintBond(currentBond, bondColor, graphics);			}		}	}	/**	 *  A ring is defined aromatic if all atoms are aromatic, -or- all bonds are	 *  aromatic.	 */	public boolean ringIsAromatic(IRing ring)	{		boolean isAromatic = true;		for (int i = 0; i < ring.getAtomCount(); i++)		{			if (!ring.getAtom(i).getFlag(CDKConstants.ISAROMATIC))			{				isAromatic = false;			}		}		if (!isAromatic)		{			isAromatic = true;			Iterator bonds = ring.bonds();			while (bonds.hasNext())				if (!((IBond)bonds.next()).getFlag(CDKConstants.ISAROMATIC))					return false;		}		return isAromatic;	}	/**	 *  Triggers the paint method suitable to the bondorder of the given bond.	 *	 *@param  bond       The Bond to be drawn.	 */	public void paintBond(org.openscience.cdk.interfaces.IBond bond, Color bondColor, Graphics2D graphics)	{		if (r2dm.getRenderingCoordinate(bond.getAtom(0)) == null ||				r2dm.getRenderingCoordinate(bond.getAtom(1)) == null)		{			return;		}				if (!r2dm.getShowExplicitHydrogens()) {			if (bond.getAtom(0).getSymbol().equals("H")) return;			if (bond.getAtom(1).getSymbol().equals("H")) return;		}		if (bond.getStereo() != CDKConstants.STEREO_BOND_NONE && bond.getStereo() != CDKConstants.STEREO_BOND_UNDEFINED)		{			// Draw stero information if available			if (bond.getStereo() >= CDKConstants.STEREO_BOND_UP)			{				paintWedgeBond(bond, bondColor, graphics);			} else			{				paintDashedWedgeBond(bond, bondColor, graphics);			}		} else		{			// Draw bond order when no stereo info is available			if (bond.getOrder() == CDKConstants.BONDORDER_SINGLE)			{				paintSingleBond(bond, bondColor, graphics);			} else if (bond.getOrder() == CDKConstants.BONDORDER_DOUBLE)			{				paintDoubleBond(bond, bondColor, graphics);			} else if (bond.getOrder() == CDKConstants.BONDORDER_TRIPLE)			{				paintTripleBond(bond, bondColor, graphics);			} else if (bond.getOrder() == 8.)			{				paintAnyBond(bond, bondColor, graphics);			} else			{				// paint all other bonds as single bonds				paintSingleBond(bond, bondColor, graphics);			}		}	}	/**	 *  Triggers the paint method suitable to the bondorder of the given bond that	 *  is part of a ring with CDK's grey inner bonds.	 *	 *@param  bond       The Bond to be drawn.	 */	public void paintRingBond(org.openscience.cdk.interfaces.IBond bond, IRing ring, Color bondColor, Graphics2D graphics)	{		if (bond.getOrder() == 1.0)		{			// Added by rstefani (in fact, code copied from paintBond)			if (bond.getStereo() != CDKConstants.STEREO_BOND_NONE && bond.getStereo() != CDKConstants.STEREO_BOND_UNDEFINED)			{				// Draw stero information if available				if (bond.getStereo() >= CDKConstants.STEREO_BOND_UP)				{					paintWedgeBond(bond, bondColor, graphics);				} else				{					paintDashedWedgeBond(bond, bondColor, graphics);				}			} else			{				// end code by rstefani				paintSingleBond(bond, bondColor, graphics);			}		} else if (bond.getOrder() == 2.0)		{						paintSingleBond(bond, bondColor, graphics);			paintInnerBond(bond, ring, bondColor, graphics);		} else if (bond.getOrder() == 3.0)		{			paintTripleBond(bond, bondColor, graphics);		} else		{			logger.warn("Drawing bond as single even though it has order: ", bond.getOrder());			paintSingleBond(bond, bondColor, graphics);		}	}	/**	 *  Draws the ring in an aromatic ring.	 */	public void paintRingRing(IRing ring, Color bondColor, Graphics2D graphics)	{		Point2d center = GeometryTools.get2DCenter(ring, r2dm.getRenderingCoordinates());		double[] minmax = GeometryTools.getMinMax(ring, r2dm.getRenderingCoordinates());		double width = (minmax[2] - minmax[0]) * 0.7;		double height = (minmax[3] - minmax[1]) * 0.7;		int[] coords = {				(int) (center.x - (width / 2.0)),				(int) (center.y + (height / 2.0))				};		int[] screenCoords = getScreenCoordinates(coords);		int ring_width = (int) (width * r2dm.getZoomFactor());		int ring_height = (int) (height * r2dm.getZoomFactor());		// Calculate inner oval offset - must be a whole number of pixels > 1.		int offset = (int) Math.ceil(0.05 * Math.max(ring_width, ring_height));		int offsetX2 = 2 * offset;		// Fill outer oval.		graphics.setColor(bondColor);		graphics.fillOval(				screenCoords[0], screenCoords[1],				ring_width, ring_height);		// Erase inner oval.		graphics.setColor(r2dm.getBackColor());		graphics.fillOval(				screenCoords[0] + offset,				screenCoords[1] + offset,				ring_width - offsetX2,				ring_height - offsetX2);		// Reset drawing colour.		graphics.setColor(bondColor);	}	/**	 *  Paint a Bond in an aromatic ring, using CDK style, meaning grey inner	 *  bonds.	 */	public void paintAromaticRingBondCDKStyle(org.openscience.cdk.interfaces.IBond bond, IRing ring, Color bondColor, Graphics2D graphics)	{		paintSingleBond(bond, bondColor, graphics);		paintInnerBond(bond, ring, Color.lightGray, graphics);	}	/**	 *  Paints the given single bond.	 *	 *@param  bond       The single bond to be drawn	 */	public void paintSingleBond(org.openscience.cdk.interfaces.IBond bond, Color bondColor, Graphics2D graphics)	{		if (GeometryToolsInternalCoordinates.has2DCoordinates(bond))		{			paintOneBond(GeometryTools.getBondCoordinates(bond, r2dm.getRenderingCoordinates()), bondColor, graphics);		}	}	/**	 *  Paints the given single bond.	 *	 *@param  bond       The single bond to be drawn	 */	public void paintAnyBond(org.openscience.cdk.interfaces.IBond bond, Color bondColor, Graphics2D graphics)	{		if (GeometryToolsInternalCoordinates.has2DCoordinates(bond))		{			int[] screencoords=getScreenCoordinates(GeometryTools.getBondCoordinates(bond, r2dm.getRenderingCoordinates()));			int dashlength=4;			int spacelength=4;            if ((screencoords[0] == screencoords[2]) && (screencoords[1] == screencoords[3]))            {                    graphics.drawLine(screencoords[0], screencoords[1], screencoords[2], screencoords[3]);                    return;            }            double linelength = Math.sqrt((screencoords[2] - screencoords[0]) * (screencoords[2] - screencoords[0]) + (screencoords[3] - screencoords[1]) * (screencoords[3] - screencoords[1]));            double xincdashspace = (screencoords[2] - screencoords[0]) / (linelength / (dashlength + spacelength));            double yincdashspace = (screencoords[3] - screencoords[1]) / (linelength / (dashlength + spacelength));            double xincdash = (screencoords[2] - screencoords[0]) / (linelength / (dashlength));            double yincdash = (screencoords[3] - screencoords[1]) / (linelength / (dashlength));            int counter = 0;            for (double i = 0; i < linelength - dashlength; i += dashlength + spacelength)

⌨️ 快捷键说明

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