List of usage examples for java.awt Graphics2D setFont
public abstract void setFont(Font font);
From source file:org.apache.ofbiz.common.CommonEvents.java
public static String getCaptcha(HttpServletRequest request, HttpServletResponse response) { try {//w ww .jav a 2s . c om Delegator delegator = (Delegator) request.getAttribute("delegator"); final String captchaSizeConfigName = StringUtils.defaultIfEmpty(request.getParameter("captchaSize"), "default"); final String captchaSizeConfig = EntityUtilProperties.getPropertyValue("captcha", "captcha." + captchaSizeConfigName, delegator); final String[] captchaSizeConfigs = captchaSizeConfig.split("\\|"); final String captchaCodeId = StringUtils.defaultIfEmpty(request.getParameter("captchaCodeId"), ""); // this is used to uniquely identify in the user session the attribute where the captcha code for the last captcha for the form is stored final int fontSize = Integer.parseInt(captchaSizeConfigs[0]); final int height = Integer.parseInt(captchaSizeConfigs[1]); final int width = Integer.parseInt(captchaSizeConfigs[2]); final int charsToPrint = UtilProperties.getPropertyAsInteger("captcha", "captcha.code_length", 6); final char[] availableChars = EntityUtilProperties .getPropertyValue("captcha", "captcha.characters", delegator).toCharArray(); //It is possible to pass the font size, image width and height with the request as well Color backgroundColor = Color.gray; Color borderColor = Color.DARK_GRAY; Color textColor = Color.ORANGE; Color circleColor = new Color(160, 160, 160); Font textFont = new Font("Arial", Font.PLAIN, fontSize); int circlesToDraw = 6; float horizMargin = 20.0f; double rotationRange = 0.7; // in radians BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = (Graphics2D) bufferedImage.getGraphics(); g.setColor(backgroundColor); g.fillRect(0, 0, width, height); //Generating some circles for background noise g.setColor(circleColor); for (int i = 0; i < circlesToDraw; i++) { int circleRadius = (int) (Math.random() * height / 2.0); int circleX = (int) (Math.random() * width - circleRadius); int circleY = (int) (Math.random() * height - circleRadius); g.drawOval(circleX, circleY, circleRadius * 2, circleRadius * 2); } g.setColor(textColor); g.setFont(textFont); FontMetrics fontMetrics = g.getFontMetrics(); int maxAdvance = fontMetrics.getMaxAdvance(); int fontHeight = fontMetrics.getHeight(); String captchaCode = RandomStringUtils.random(6, availableChars); float spaceForLetters = -horizMargin * 2 + width; float spacePerChar = spaceForLetters / (charsToPrint - 1.0f); for (int i = 0; i < captchaCode.length(); i++) { // this is a separate canvas used for the character so that // we can rotate it independently int charWidth = fontMetrics.charWidth(captchaCode.charAt(i)); int charDim = Math.max(maxAdvance, fontHeight); int halfCharDim = (charDim / 2); BufferedImage charImage = new BufferedImage(charDim, charDim, BufferedImage.TYPE_INT_ARGB); Graphics2D charGraphics = charImage.createGraphics(); charGraphics.translate(halfCharDim, halfCharDim); double angle = (Math.random() - 0.5) * rotationRange; charGraphics.transform(AffineTransform.getRotateInstance(angle)); charGraphics.translate(-halfCharDim, -halfCharDim); charGraphics.setColor(textColor); charGraphics.setFont(textFont); int charX = (int) (0.5 * charDim - 0.5 * charWidth); charGraphics.drawString("" + captchaCode.charAt(i), charX, ((charDim - fontMetrics.getAscent()) / 2 + fontMetrics.getAscent())); float x = horizMargin + spacePerChar * (i) - charDim / 2.0f; int y = ((height - charDim) / 2); g.drawImage(charImage, (int) x, y, charDim, charDim, null, null); charGraphics.dispose(); } // Drawing the image border g.setColor(borderColor); g.drawRect(0, 0, width - 1, height - 1); g.dispose(); response.setContentType("image/jpeg"); ImageIO.write(bufferedImage, "jpg", response.getOutputStream()); HttpSession session = request.getSession(); Map<String, String> captchaCodeMap = UtilGenerics.checkMap(session.getAttribute("_CAPTCHA_CODE_")); if (captchaCodeMap == null) { captchaCodeMap = new HashMap<String, String>(); session.setAttribute("_CAPTCHA_CODE_", captchaCodeMap); } captchaCodeMap.put(captchaCodeId, captchaCode); } catch (Exception ioe) { Debug.logError(ioe.getMessage(), module); } return "success"; }
From source file:domainhealth.frontend.graphics.JFreeChartGraphImpl.java
/** * Check to see if current graph has empty chart lines (ie. no data-series * in any lines), and if so, write the text 'NO DATA' in large letters * across the front of the graph/*from www.j av a 2 s.c om*/ * * @param graphImage The current image representation of the generated graph */ @SuppressWarnings("unchecked") private void addNoDataLogoIfEmpty(BufferedImage graphImage) { int maxStatCount = 0; List<XYSeries> seriesList = xySeriesCollection.getSeries(); for (XYSeries series : seriesList) { maxStatCount = Math.max(maxStatCount, series.getItemCount()); } if (maxStatCount <= 0) { Graphics2D graphics2D = get2DGraphics(graphImage); graphics2D.setFont(new Font(GRAPH_TEXT_FONT, Font.PLAIN, 36)); graphics2D.drawString(NO_DATA_TEXT, 200, 210); graphics2D.dispose(); } else if (maxStatCount <= 1) { Graphics2D graphics2D = get2DGraphics(graphImage); graphics2D.setFont(new Font(GRAPH_TEXT_FONT, Font.PLAIN, 22)); graphics2D.drawString(WAITING_FOR_DATA_TEXT_LN1, 152, 205); graphics2D.dispose(); graphics2D = get2DGraphics(graphImage); graphics2D.setFont(new Font(GRAPH_TEXT_FONT, Font.PLAIN, 15)); graphics2D.drawString(WAITING_FOR_DATA_TEXT_LN2, 81, 225); graphics2D.dispose(); } }
From source file:org.ofbiz.common.CommonEvents.java
public static String getCaptcha(HttpServletRequest request, HttpServletResponse response) { try {/*w ww . j a va2s .c o m*/ Delegator delegator = (Delegator) request.getAttribute("delegator"); final String captchaSizeConfigName = StringUtils.defaultIfEmpty(request.getParameter("captchaSize"), "default"); final String captchaSizeConfig = EntityUtilProperties.getPropertyValue("captcha.properties", "captcha." + captchaSizeConfigName, delegator); final String[] captchaSizeConfigs = captchaSizeConfig.split("\\|"); final String captchaCodeId = StringUtils.defaultIfEmpty(request.getParameter("captchaCodeId"), ""); // this is used to uniquely identify in the user session the attribute where the captcha code for the last captcha for the form is stored final int fontSize = Integer.parseInt(captchaSizeConfigs[0]); final int height = Integer.parseInt(captchaSizeConfigs[1]); final int width = Integer.parseInt(captchaSizeConfigs[2]); final int charsToPrint = UtilProperties.getPropertyAsInteger("captcha.properties", "captcha.code_length", 6); final char[] availableChars = EntityUtilProperties .getPropertyValue("captcha.properties", "captcha.characters", delegator).toCharArray(); //It is possible to pass the font size, image width and height with the request as well Color backgroundColor = Color.gray; Color borderColor = Color.DARK_GRAY; Color textColor = Color.ORANGE; Color circleColor = new Color(160, 160, 160); Font textFont = new Font("Arial", Font.PLAIN, fontSize); int circlesToDraw = 6; float horizMargin = 20.0f; double rotationRange = 0.7; // in radians BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = (Graphics2D) bufferedImage.getGraphics(); g.setColor(backgroundColor); g.fillRect(0, 0, width, height); //Generating some circles for background noise g.setColor(circleColor); for (int i = 0; i < circlesToDraw; i++) { int circleRadius = (int) (Math.random() * height / 2.0); int circleX = (int) (Math.random() * width - circleRadius); int circleY = (int) (Math.random() * height - circleRadius); g.drawOval(circleX, circleY, circleRadius * 2, circleRadius * 2); } g.setColor(textColor); g.setFont(textFont); FontMetrics fontMetrics = g.getFontMetrics(); int maxAdvance = fontMetrics.getMaxAdvance(); int fontHeight = fontMetrics.getHeight(); String captchaCode = RandomStringUtils.random(6, availableChars); float spaceForLetters = -horizMargin * 2 + width; float spacePerChar = spaceForLetters / (charsToPrint - 1.0f); for (int i = 0; i < captchaCode.length(); i++) { // this is a separate canvas used for the character so that // we can rotate it independently int charWidth = fontMetrics.charWidth(captchaCode.charAt(i)); int charDim = Math.max(maxAdvance, fontHeight); int halfCharDim = (charDim / 2); BufferedImage charImage = new BufferedImage(charDim, charDim, BufferedImage.TYPE_INT_ARGB); Graphics2D charGraphics = charImage.createGraphics(); charGraphics.translate(halfCharDim, halfCharDim); double angle = (Math.random() - 0.5) * rotationRange; charGraphics.transform(AffineTransform.getRotateInstance(angle)); charGraphics.translate(-halfCharDim, -halfCharDim); charGraphics.setColor(textColor); charGraphics.setFont(textFont); int charX = (int) (0.5 * charDim - 0.5 * charWidth); charGraphics.drawString("" + captchaCode.charAt(i), charX, ((charDim - fontMetrics.getAscent()) / 2 + fontMetrics.getAscent())); float x = horizMargin + spacePerChar * (i) - charDim / 2.0f; int y = ((height - charDim) / 2); g.drawImage(charImage, (int) x, y, charDim, charDim, null, null); charGraphics.dispose(); } // Drawing the image border g.setColor(borderColor); g.drawRect(0, 0, width - 1, height - 1); g.dispose(); response.setContentType("image/jpeg"); ImageIO.write(bufferedImage, "jpg", response.getOutputStream()); HttpSession session = request.getSession(); Map<String, String> captchaCodeMap = UtilGenerics.checkMap(session.getAttribute("_CAPTCHA_CODE_")); if (captchaCodeMap == null) { captchaCodeMap = new HashMap<String, String>(); session.setAttribute("_CAPTCHA_CODE_", captchaCodeMap); } captchaCodeMap.put(captchaCodeId, captchaCode); } catch (Exception ioe) { Debug.logError(ioe.getMessage(), module); } return "success"; }
From source file:NormSample.java
public void init() { // preparing values for the normalization forms ComboBox formValues.put("NFC", Normalizer.Form.NFC); formValues.put("NFD", Normalizer.Form.NFD); formValues.put("NFKC", Normalizer.Form.NFKC); formValues.put("NFKD", Normalizer.Form.NFKD); formComboBox = new JComboBox(); for (Iterator it = formValues.keySet().iterator(); it.hasNext();) { formComboBox.addItem((String) it.next()); }//from w ww . j a v a 2s . c o m // preparing samples for normalization // text with the acute accent symbol templateValues.put("acute accent", "touch" + "\u00e9"); // text with ligature templateValues.put("ligature", "a" + "\ufb03" + "ance"); // text with the cedilla templateValues.put("cedilla", "fa" + "\u00e7" + "ade"); // text with half-width katakana templateValues.put("half-width katakana", "\uff81\uff6e\uff7a\uff9a\uff70\uff84"); normalizationTemplate = new JComboBox(); for (Iterator it = templateValues.keySet().iterator(); it.hasNext();) { normalizationTemplate.addItem((String) it.next()); } // defining a component to output normalization results paintingComponent = new JComponent() { static final long serialVersionUID = -3725620407788489160L; public Dimension getSize() { return new Dimension(550, 200); } public Dimension getPreferredSize() { return new Dimension(550, 200); } public Dimension getMinimumSize() { return new Dimension(550, 200); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setFont(new Font("Serif", Font.PLAIN, 20)); g2.setColor(Color.BLACK); g2.drawString("Original string:", 100, 80); g2.drawString("Normalized string:", 100, 120); g2.setFont(new Font("Serif", Font.BOLD, 24)); // output of the original sample selected from the ComboBox String original_string = templateValues.get(normalizationTemplate.getSelectedItem()); g2.drawString(original_string, 320, 80); // normalization and output of the normalized string String normalized_string; java.text.Normalizer.Form currentForm = formValues.get(formComboBox.getSelectedItem()); normalized_string = Normalizer.normalize(original_string, currentForm); g2.drawString(normalized_string, 320, 120); } }; setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); add(paintingComponent); JPanel controls = new JPanel(); controls.setLayout(new BoxLayout(controls, BoxLayout.X_AXIS)); controls.add(new Label("Normalization Form: ")); controls.add(formComboBox); controls.add(new Label("Normalization Template:")); controls.add(normalizationTemplate); add(controls); formComboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { paintingComponent.repaint(); } }); normalizationTemplate.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { paintingComponent.repaint(); } }); }
From source file:Draw2DRotate.java
public void paint(Graphics graphics) { Graphics2D g = (Graphics2D) graphics; AffineTransform transform = AffineTransform.getRotateInstance(Math.PI / 16.0d); g.setTransform(transform);// w w w. ja v a 2 s . c o m Line2D.Double shape = new Line2D.Double(0.0, 0.0, 300.0, 300.0); g.draw(shape); g.setFont(new Font("Helvetica", Font.BOLD, 24)); String text = ("Java2s"); g.drawString(text, 300, 50); Toolkit toolkit = Toolkit.getDefaultToolkit(); Image image = toolkit.getImage("image1.gif"); g.drawImage(image, 100, 150, this); }
From source file:org.optaplanner.examples.rocktour.swingui.RockTourWorldPanel.java
public void resetPanel(RockTourSolution solution) { translator = new LatitudeLongitudeTranslator(); RockBus bus = solution.getBus();/* w w w . ja v a 2 s . c o m*/ translator.addCoordinates(bus.getStartLocation().getLatitude(), bus.getStartLocation().getLongitude()); translator.addCoordinates(bus.getEndLocation().getLatitude(), bus.getEndLocation().getLongitude()); for (RockShow show : solution.getShowList()) { translator.addCoordinates(show.getLocation().getLatitude(), show.getLocation().getLongitude()); } Dimension size = getSize(); double width = size.getWidth(); double height = size.getHeight(); translator.prepareFor(width, height); Graphics2D g = createCanvas(width, height); g.setFont(g.getFont().deriveFont((float) LOCATION_NAME_TEXT_SIZE)); List<RockShow> showList = solution.getShowList(); int maxAvailableDateSetSize = showList.stream().mapToInt(show -> show.getAvailableDateSet().size()).max() .orElse(-1); for (RockShow show : showList) { RockLocation location = show.getLocation(); int x = translator.translateLongitudeToX(location.getLongitude()); int y = translator.translateLatitudeToY(location.getLatitude()); double percentage = (double) show.getAvailableDateSet().size() / maxAvailableDateSetSize; g.setColor(TangoColorFactory.buildPercentageColor(TangoColorFactory.PLUM_3, TangoColorFactory.PLUM_1, percentage)); g.fillRect(x - 1, y - 1, 3, 3); if (location.getCityName() != null && showList.size() <= 500) { g.drawString(StringUtils.abbreviate(location.getCityName(), 20), x + 3, y - 3); } if (show.getDate() != null) { g.drawString(DAY_FORMATTER.format(show.getDate()), x + 3, y - 3 + LOCATION_NAME_TEXT_SIZE * 3 / 2); } } g.setColor(TangoColorFactory.ALUMINIUM_4); RockLocation busStartLocation = bus.getStartLocation(); int domicileX = translator.translateLongitudeToX(busStartLocation.getLongitude()); int domicileY = translator.translateLatitudeToY(busStartLocation.getLatitude()); g.fillRect(domicileX - 2, domicileY - 2, 5, 5); if (busStartLocation.getCityName() != null && showList.size() <= 500) { g.drawString(busStartLocation.getCityName(), domicileX + 3, domicileY - 3); } Set<RockShow> needsBackToDomicileLineSet = new HashSet<>(showList); for (RockShow trailingShow : showList) { if (trailingShow.getPreviousStandstill() instanceof RockShow) { needsBackToDomicileLineSet.remove(trailingShow.getPreviousStandstill()); } } g.setColor(TangoColorFactory.CHOCOLATE_1); for (RockShow show : showList) { if (show.getPreviousStandstill() != null) { RockLocation previousLocation = show.getPreviousStandstill().getDepartureLocation(); RockLocation location = show.getLocation(); translator.drawRoute(g, previousLocation.getLongitude(), previousLocation.getLatitude(), location.getLongitude(), location.getLatitude(), true, false); // Back to bus line if (needsBackToDomicileLineSet.contains(show)) { translator.drawRoute(g, location.getLongitude(), location.getLatitude(), busStartLocation.getLongitude(), busStartLocation.getLatitude(), true, true); } } } g.setFont(g.getFont().deriveFont((float) TEXT_SIZE)); // Legend g.setColor(TangoColorFactory.ALUMINIUM_4); g.fillRect(5, (int) height - 17 - TEXT_SIZE, 5, 5); g.drawString("Bus start", 15, (int) height - 10 - TEXT_SIZE); g.setColor(TangoColorFactory.PLUM_2); g.fillRect(6, (int) height - 11, 3, 3); g.drawString("Show (darker means less available)", 15, (int) height - 5); repaint(); }
From source file:PrintLineByLine.java
public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2d.setRenderingHints(rh);// w w w .ja va 2 s . c om g2d.setFont(new Font("Purisa", Font.PLAIN, 13)); g2d.drawString("Line 1", 20, 30); g2d.drawString("Line 2", 20, 60); g2d.drawString("Line 3", 20, 90); g2d.drawString("Line 4", 20, 120); g2d.drawString("Line 5", 20, 150); g2d.drawString("Line 6", 20, 180); }
From source file:Main.java
@Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); g2d.setColor(Color.RED);/* ww w .jav a 2 s.c o m*/ g2d.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight()); g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2); Font font = new Font("Arial", Font.BOLD, 48); g2d.setFont(font); FontMetrics fm = g2d.getFontMetrics(); int x = ((getWidth() - fm.stringWidth(text)) / 2); int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent(); g2d.setColor(Color.BLACK); g2d.drawString(text, x, y); g2d.dispose(); }
From source file:org.slage.TextObject.java
/** * Draw text at the object's position//from w w w . j a v a2 s . c om * * @param G2D graphics context to render the text to */ public void draw(java.awt.Graphics2D G2D) { Font oldFont = G2D.getFont(); Color oldColor = G2D.getColor(); G2D.setFont(font); G2D.setColor(color); G2D.drawString(getName(), getPosition().x, getPosition().y); G2D.setFont(oldFont); G2D.setColor(oldColor); }
From source file:org.fhcrc.cpl.viewer.quant.gui.LogRatioHistMouseListener.java
/** * Draw the ratio in its box. Separated from drawBoxForRatio so the box can be drawn empty * @param g//from w w w. j a v a 2 s. co m */ protected void drawRatioInBox(Graphics2D g) { drawBoxForRatio(g); g.setFont(new Font("Verdana", Font.PLAIN, 10)); g.setColor(Color.BLACK); g.setPaint(Color.BLACK); g.drawString("Ratio: " + lastMousedRatio, 16, 24); }