📄 abstractrenderer2d.java
字号:
atomSymbol += "-" + atomNumber; } } else if (r2dm.showAtomTypeNames() && atom.getAtomTypeName() != null && atom.getAtomTypeName().length() > 0) { atomSymbol = atom.getAtomTypeName(); } if (isRadical) { logger.debug(" atom is radical, adding \u00B7"); atomSymbol += "\u00B7"; } 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 atomSymbolH = (new Integer(fontMetrics.getAscent())).intValue(); int atomSymbolXOffset = atomSymbolFirstCharW / 2; int atomSymbolYOffset = atomSymbolH / 2; // calculate IMPLICIT H width, height int implicitHydrogenCount = atom.getHydrogenCount(); int hSymbolW = 0; // unless next condition, this is the default int hSymbolH = 0; // unless next condition, this is the default String hSymbol = "H"; String hMultiplierString = Integer.toString(implicitHydrogenCount); if (implicitHydrogenCount > 0) { // fontMetrics is identical, don't change hSymbolW = (new Integer(fontMetrics.stringWidth(hSymbol))).intValue(); hSymbolH = atomSymbolH; } graphics.setFont(subscriptFont); fontMetrics = graphics.getFontMetrics(); int hMultiplierW = 0; int hMultiplierH = 0; if (implicitHydrogenCount > 1) { // fontMetrics is identical, don't change hMultiplierW = (new Integer(fontMetrics.stringWidth(hMultiplierString))).intValue(); hMultiplierH = (new Integer(fontMetrics.getAscent())).intValue(); } // calculate CHARGE width, height // font is still subscript, that's fine int formalCharge = atom.getFormalCharge(); int formalChargeW = 0; // unless next condition, this is the default int formalChargeH = 0; String formalChargeString = ""; // if charge == 0, then don't print anything if (formalCharge != 0) { if (formalCharge > 1) { formalChargeString = Integer.toString(formalCharge) + "+"; } else if (formalCharge > 0) { formalChargeString = "+"; } else if (formalCharge < -1) { formalChargeString = Integer.toString(formalCharge * -1) + "-"; } else if (formalCharge < 0) { formalChargeString = "-"; } graphics.setFont(subscriptFont); fontMetrics = graphics.getFontMetrics(); formalChargeW = (new Integer(fontMetrics.stringWidth(formalChargeString))).intValue(); formalChargeH = (new Integer(fontMetrics.getAscent())).intValue(); } // calculate ISOTOPE width, height // font is still subscript, that's fine int atomicMassNumber = atom.getMassNumber(); int isotopeW = 0; // unless next condition, this is the default int isotopeH = 0; String isotopeString = ""; if (atomicMassNumber != 0 && isotopeFactory != null) { IIsotope majorIsotope = isotopeFactory.getMajorIsotope(atom.getSymbol()); if (majorIsotope != null && atomicMassNumber != majorIsotope.getMassNumber()) { graphics.setFont(subscriptFont); fontMetrics = graphics.getFontMetrics(); isotopeString = Integer.toString(atomicMassNumber); isotopeW = (new Integer(fontMetrics.stringWidth(isotopeString))).intValue(); isotopeH = (new Integer(fontMetrics.getAscent())).intValue(); } } // STEP 2: calculate x's and y's for all parts in the label int labelX = 0; int labelY = 0; if (alignment == 1) { // left alignment labelX = (int) (r2dm.getRenderingCoordinate(atom).x - (atomSymbolXOffset + isotopeW)); } else { // right alignment labelX = (int) (r2dm.getRenderingCoordinate(atom).x - (atomSymbolXOffset + Math.max(isotopeW, hMultiplierW) + hSymbolW)); } // labelY and labelH are the same for both left/right aligned labelY = (int) (r2dm.getRenderingCoordinate(atom).y + (atomSymbolYOffset + isotopeH)); // xy for atom symbol int[] atomSymbolCoords = new int[2]; if (alignment == 1) { // left alignment atomSymbolCoords[0] = labelX + isotopeW; } else { // right alignment atomSymbolCoords[0] = labelX + hSymbolW + Math.max(isotopeW, hMultiplierW); } atomSymbolCoords[1] = labelY - isotopeH - atomSymbolH; //Check if this is inside the tooltiptextarea int[] tipcoords = getScreenCoordinates(atomSymbolCoords); if (tooltiparea != null && tipcoords[0] > tooltiparea[0] && tipcoords[0] < tooltiparea[2] && tipcoords[1] > tooltiparea[1] && tipcoords[1] < tooltiparea[3]) { return; } // xy for implicit hydrogens int[] hSymbolCoords = new int[2]; if (alignment == 1) { // left alignment hSymbolCoords[0] = labelX + isotopeW + atomSymbolW; } else { // right alignment hSymbolCoords[0] = labelX; } hSymbolCoords[1] = labelY - isotopeH - atomSymbolH; // xy for implicit hydrogens multiplier int[] hMultiplierCoords = new int[2]; if (alignment == 1) { // left alignment hMultiplierCoords[0] = labelX + isotopeW + atomSymbolW + hSymbolW; } else { // right alignment hMultiplierCoords[0] = labelX + hSymbolW; } hMultiplierCoords[1] = labelY - isotopeH - atomSymbolH - hMultiplierH / 2; // xy for charge int[] chargeCoords = new int[2]; if (alignment == 1) { // left alignment chargeCoords[0] = labelX + isotopeW + atomSymbolW + hSymbolW; } else { // right alignment chargeCoords[0] = labelX + hSymbolW + Math.max(isotopeW, hMultiplierW) + atomSymbolW; } chargeCoords[1] = labelY - isotopeH; //xy for isotope int[] isotopeCoords = new int[2]; if (alignment == 1) { // left alignment isotopeCoords[0] = labelX; } else { // right alignment isotopeCoords[0] = labelX + hSymbolW; } isotopeCoords[1] = labelY - isotopeH; // STEP 3: draw empty backgrounds for all parts in the label int border = 2; // border for clearing background in pixels paintEmptySpace(atomSymbolCoords[0], atomSymbolCoords[1] + atomSymbolH, atomSymbolW, atomSymbolH, border, backColor, graphics); paintEmptySpace(hSymbolCoords[0], hSymbolCoords[1] + hSymbolH, hSymbolW, hSymbolH, border, backColor, graphics); paintEmptySpace(hMultiplierCoords[0], hMultiplierCoords[1] + hMultiplierH, hMultiplierW, hMultiplierH, border, backColor, graphics); paintEmptySpace(chargeCoords[0], chargeCoords[1] + formalChargeH, formalChargeW, formalChargeH, border, backColor, graphics); paintEmptySpace(isotopeCoords[0], isotopeCoords[1] + isotopeH, isotopeW, isotopeH, border, backColor, graphics); // STEP 4: draw all parts in the label Color atomColor = r2dm.getAtomColor(atom, r2dm.getForeColor()); { // draw SYMBOL int[] screenCoords = getScreenCoordinates(atomSymbolCoords); graphics.setColor(atomColor); graphics.setFont(normalScreenFont); graphics.drawString(atomSymbol, screenCoords[0], screenCoords[1]); // possibly underline SYMBOL if (atom.getProperty(ProblemMarker.ERROR_MARKER) != null || atom.getProperty(ProblemMarker.WARNING_MARKER) != null) { // RED for error, ORANGE for warnings if (atom.getProperty(ProblemMarker.ERROR_MARKER) != null) { graphics.setColor(Color.red); } else if (atom.getProperty(ProblemMarker.WARNING_MARKER) != null) { graphics.setColor(Color.orange); } // make zig zag bond int symbolLength = atom.getSymbol().length(); int zigzags = 1 + (2 * symbolLength); int spacing = atomSymbolW / zigzags; int width = atomSymbolH / 3; for (int i = -symbolLength; i <= symbolLength; i++) { int[] lineCoords = new int[6]; int halfspacing = spacing / 2; lineCoords[0] = atomSymbolCoords[0] + (atomSymbolW / 2) + (i * spacing) - halfspacing; lineCoords[1] = atomSymbolCoords[1] - 1 * width; lineCoords[2] = lineCoords[0] + halfspacing; lineCoords[3] = atomSymbolCoords[1] - 2 * width; lineCoords[4] = lineCoords[2] + halfspacing; lineCoords[5] = lineCoords[1]; int[] lineScreenCoords = getScreenCoordinates(lineCoords); graphics.drawLine(lineScreenCoords[0], lineScreenCoords[1], lineScreenCoords[2], lineScreenCoords[3]); graphics.drawLine(lineScreenCoords[2], lineScreenCoords[3], lineScreenCoords[4], lineScreenCoords[5]); } } } // draw IMPLICIT H's if (implicitHydrogenCount > 0 && r2dm.getShowImplicitHydrogens()) { int[] screenCoords = getScreenCoordinates(hSymbolCoords); graphics.setColor(atomColor); graphics.setFont(normalScreenFont); graphics.drawString(hSymbol, screenCoords[0], screenCoords[1]); if (implicitHydrogenCount > 1) { // draw number of hydrogens screenCoords = getScreenCoordinates(hMultiplierCoords); graphics.setColor(atomColor); graphics.setFont(subscriptScreenFont); graphics.drawString(hMultiplierString, screenCoords[0], screenCoords[1]); } } // draw CHARGE if (formalCharge != 0) { int[] screenCoords = getScreenCoordinates(chargeCoords); graphics.setColor(atomColor); graphics.setFont(normalScreenFont); graphics.drawString(formalChargeString, screenCoords[0], screenCoords[1]); } // draw ISOTOPE if (isotopeString.length() > 0) { int[] screenCoords = getScreenCoordinates(isotopeCoords); graphics.setColor(atomColor); graphics.setFont(subscriptScreenFont); graphics.drawString(isotopeString, screenCoords[0], screenCoords[1]); } // reset old font & color graphics.setFont(normalFont); graphics.setColor(r2dm.getForeColor()); } public void paintNumberOnly(IAtom atom, Color backColor, Graphics2D graphics, int atomNumber) { if (r2dm.getRenderingCoordinate(atom) == null) { logger.warn("Cannot draw atom without 2D coordinate"); return; } // The fonts for calculating geometries 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); // STEP 1: calculate widths and heights for all parts in the label // calculate SYMBOL width, height String atomSymbol = atomNumber+""; 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 atomSymbolH = (new Integer(fontMetrics.getAscent())).intValue(); int atomSymbolXOffset = atomSymbolFirstCharW / 2; int atomSymbolYOffset = atomSymbolH / 2; // STEP 2: calculate x's and y's for all parts in the label int labelX = (int) (r2dm.getRenderingCoordinate(atom).x - (atomSymbolXOffset)); // labelY and labelH are the same for both left/right aligned int labelY = (int) (r2dm.getRenderingCoordinate(atom).y + (atomSymbolYOffset)); // xy for atom symbol int[] atomSymbolCoords = new int[2]; atomSymbolCoords[0] = labelX ; atomSymbolCoords[1] = labelY - atomSymbolH; // STEP 3: draw empty backgrounds for all parts in the label int border = 2; // border for clearing background in pixels paintEmptySpace(atomSymbolCoords[0], atomSymbolCoords[1] + atomSymbolH, atomSymbolW, atomSymbolH, border, backColor, graphics); // STEP 4: draw all parts in the label Color atomColor = r2dm.getAtomColor(atom, r2dm.getForeColor()); { // draw SYMBOL int[] screenCoords = getScreenCoordinates(atomSymbolCoords); graphics.setColor(atomColor); graphics.setFont(normalScreenFont); graphics.drawString(atomSymbol, screenCoords[0], screenCoords[1]); // possibly underline SYMBOL if (atom.getProperty(ProblemMarker.ERROR_MARKER) != null || atom.getProperty(ProblemMarker.WARNING_MARKER) != null) { // RED for error, ORANGE for warnings if (atom.getProperty(ProblemMarker.ERROR_MARKER) != null) { graphics.setColor(Color.red); } else if (atom.getProperty(ProblemMarker.WARNING_MARKER) != null) { graphics.setColor(Color.orange); } // make zig zag bond int symbolLength = atom.getSymbol().length(); int zigzags = 1 + (2 * symbolLength); int spacing = atomSymbolW / zigzags; int width = atomSymbolH / 3; for (int i = -symbolLength; i <= symbolLength; i++) { int[] lineCoords = new int[6]; int halfspacing = spacing / 2; lineCoords[0] = atomSymbolCoords[0] + (atomSymbolW / 2) + (i * spacing) - halfspacing; lineCoords[1] = atomSymbolCoords[1] - 1 * width; lineCoords[2] = lineCoords[0] + halfspacing; lineCoords[3] = atomSymbolCoords[1] - 2 * width; lineCoords[4] = lineCoords[2] + halfspacing; lineCoords[5] = lineCoords[1]; int[] lineScreenCoords = getScreenCoordinates(lineCoords); graphics.drawLine(lineScreenCoords[0], lineScreenCoords[1], lineScreenCoords[2], lineScreenCoords[3]); graphics.drawLine(lineScreenCoords[2], lineScreenCoords[3], lineScreenCoords[4], lineScreenCoords[5]); } } } // reset old font & color graphics.setFont(normalFont); graphics.setColor(r2dm.getForeColor()); } /** * Makes a clear empty space using the background color. */ public void paintEmptySpace(int x, int y, int width, int height, int border, Color backColor, Graphics2D graphics)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -