Example usage for java.awt Graphics2D drawChars

List of usage examples for java.awt Graphics2D drawChars

Introduction

In this page you can find the example usage for java.awt Graphics2D drawChars.

Prototype

public void drawChars(char[] data, int offset, int length, int x, int y) 

Source Link

Document

Draws the text given by the specified character array, using this graphics context's current font and color.

Usage

From source file:com.liud.dailynote.ThumbnailatorTest.java

public void testYS7() throws IOException {
    String result = "src/main/resources/images/";
    BufferedImage bi = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bi.createGraphics();
    g.setColor(Color.LIGHT_GRAY);
    g.drawRect(0, 0, 10, 10);/*from   www .j  a  v  a  2 s.  c  o  m*/
    char[] data = "liudTest".toCharArray();
    g.drawChars(data, 0, data.length, 5, 32);

    // watermark ? 1.? 2.? 3.?
    Thumbnails.of(result + "sijili.jpg").scale(1.0f).watermark(Positions.CENTER, bi, 1.0f)
            .toFile(result + "image_warter_liud.jpg");
}

From source file:business.model.CaptchaModel.java

/**
 *
 * @param CaptchaText/*from w  w w . j a  v a2 s.  com*/
 * @return
 */
public static BufferedImage CreateCaptchaImageOld(String CaptchaText) {
    BufferedImage localBufferedImage = new BufferedImage(width, height, 1);
    try {
        Graphics2D localGraphics2D = localBufferedImage.createGraphics();
        //        List<Font> fonts = FontFactory.getListFont();
        List<Font> fonts = new ArrayList<Font>();
        if (fonts.isEmpty()) {
            fonts.add(new Font("Nimbus Roman No9 L", 1, 30));
            fonts.add(new Font("VN-NTime", 1, 30));
            fonts.add(new Font("DT-Times", 1, 30));
            fonts.add(new Font("Times New Roman", 1, 30));
            fonts.add(new Font("Vni-Book123", 1, 30));
            fonts.add(new Font("VNI-Centur", 1, 30));
            fonts.add(new Font("DT-Brookly", 1, 30));
        }
        //        fonts.add(loadFont("BINHLBI.TTF"));
        RenderingHints localRenderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        localRenderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        localGraphics2D.setRenderingHints(localRenderingHints);
        localGraphics2D.fillRect(0, 0, width, height);
        localGraphics2D.setColor(new Color(50, 50, 50));
        Random localRandom = new Random();
        int i = 0;
        int j = 0;
        for (int k = 0; k < CaptchaText.length(); k++) {
            i += 25 + Math.abs(localRandom.nextInt()) % 5;
            j = 25 + Math.abs(localRandom.nextInt()) % 20;
            int tmp = localRandom.nextInt(fonts.size() - 1);
            localGraphics2D.setFont(fonts.get(tmp));

            localGraphics2D.drawChars(CaptchaText.toCharArray(), k, 1, i, j);
        }
        localBufferedImage = getDistortedImage(localBufferedImage);
        Graphics2D localGraphics2D2 = localBufferedImage.createGraphics();
        localGraphics2D2.setRenderingHints(localRenderingHints);
        //        localGraphics2D2.fillRect(0, 0, width, height);
        localGraphics2D2.setColor(new Color(50, 50, 50));
        CubicCurve2D c = new CubicCurve2D.Double();// draw QuadCurve2D.Float with set coordinates
        for (int l = 0; l < 7; l++) {
            int x1 = Util.rand(0, width / 2);
            ;
            int x2 = Util.rand(width / 2, width);
            int y1 = Util.rand(0, height);
            int y2 = Util.rand(0, height);
            int ctrlx1 = (x1 + x2) / 4 * Util.rand(1, 3);
            int ctrly1 = y1;
            int ctrlx2 = (x1 + x2) / 4 * Util.rand(1, 3);
            int ctrly2 = y2;

            c.setCurve(x1, y1, ctrlx2, ctrly2, ctrlx1, ctrly1, x2, y2);
            localGraphics2D2.draw(c);
            //            localGraphics2D2.drawLine(randomX1(), randomY1(), randomX2(), randomY2());
        }

        localGraphics2D.dispose();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return null;
    }
    return localBufferedImage;
}

From source file:com.rapidminer.gui.plotter.charts.DistributionPlotter.java

@Override
public void updatePlotter() {

    JFreeChart chart = null;/*from w  w  w.j  a  v a  2  s . c om*/
    Attribute attr = null;
    if (plotColumn != -1 && createFromModel) {
        attr = model.getTrainingHeader().getAttributes().get(model.getAttributeNames()[plotColumn]);
    }
    if (attr != null && attr.isNominal()
            && attr.getMapping().getValues().size() > MAX_NUMBER_OF_DIFFERENT_NOMINAL_VALUES) {
        // showing no chart because of too many different values
        chart = new JFreeChart(new Plot() {

            private static final long serialVersionUID = 1L;

            @Override
            public String getPlotType() {
                return "empty";
            }

            @Override
            public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState,
                    PlotRenderingInfo info) {
                String msg = I18N.getGUILabel("plotter_panel.too_many_nominals", "Distribution Plotter",
                        DistributionPlotter.MAX_NUMBER_OF_DIFFERENT_NOMINAL_VALUES);
                g2.setColor(Color.BLACK);
                g2.setFont(g2.getFont().deriveFont(g2.getFont().getSize() * 1.35f));
                g2.drawChars(msg.toCharArray(), 0, msg.length(), 50, (int) (area.getHeight() / 2 + 0.5d));
            }
        });
        AbstractChartPanel panel = getPlotterPanel();
        // Chart Panel Settings
        if (panel == null) {
            panel = createPanel(chart);
        } else {
            panel.setChart(chart);
        }
        // ATTENTION: WITHOUT THIS WE GET SEVERE MEMORY LEAKS!!!
        panel.getChartRenderingInfo().setEntityCollection(null);
    } else {
        preparePlots();

        if (!createFromModel && (groupColumn < 0 || plotColumn < 0)) {
            CategoryDataset dataset = new DefaultCategoryDataset();
            chart = ChartFactory.createBarChart(null, // chart title
                    "Not defined", // x axis label
                    RANGE_AXIS_NAME, // y axis label
                    dataset, // data
                    PlotOrientation.VERTICAL, true, // include legend
                    true, // tooltips
                    false // urls
            );
        } else {
            try {
                if (model.isDiscrete(translateToModelColumn(plotColumn))) {
                    chart = createNominalChart();
                } else {
                    chart = createNumericalChart();
                }
            } catch (Exception e) {
                // do nothing - just do not draw the chart
            }
        }

        if (chart != null) {
            chart.setBackgroundPaint(Color.white);

            // get a reference to the plot for further customization...
            Plot commonPlot = chart.getPlot();
            commonPlot.setBackgroundPaint(Color.WHITE);
            if (commonPlot instanceof XYPlot) {
                XYPlot plot = (XYPlot) commonPlot;

                plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
                plot.setRangeGridlinePaint(Color.LIGHT_GRAY);

                // domain axis
                if (dataTable != null) {
                    if (dataTable.isDate(plotColumn) || dataTable.isDateTime(plotColumn)) {
                        DateAxis domainAxis = new DateAxis(dataTable.getColumnName(plotColumn));
                        domainAxis.setTimeZone(com.rapidminer.tools.Tools.getPreferredTimeZone());
                        plot.setDomainAxis(domainAxis);
                    } else {
                        NumberAxis numberAxis = new NumberAxis(dataTable.getColumnName(plotColumn));
                        plot.setDomainAxis(numberAxis);
                    }
                }

                plot.getDomainAxis().setLabelFont(LABEL_FONT_BOLD);
                plot.getDomainAxis().setTickLabelFont(LABEL_FONT);

                plot.getRangeAxis().setLabelFont(LABEL_FONT_BOLD);
                plot.getRangeAxis().setTickLabelFont(LABEL_FONT);

                // ranging
                if (dataTable != null) {
                    Range range = getRangeForDimension(plotColumn);
                    if (range != null) {
                        plot.getDomainAxis().setRange(range, true, false);
                    }

                    range = getRangeForName(RANGE_AXIS_NAME);
                    if (range != null) {
                        plot.getRangeAxis().setRange(range, true, false);
                    }
                }

                // rotate labels
                if (isLabelRotating()) {
                    plot.getDomainAxis().setTickLabelsVisible(true);
                    plot.getDomainAxis().setVerticalTickLabels(true);
                }

            } else if (commonPlot instanceof CategoryPlot) {
                CategoryPlot plot = (CategoryPlot) commonPlot;

                plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
                plot.setRangeGridlinePaint(Color.LIGHT_GRAY);

                plot.getRangeAxis().setLabelFont(LABEL_FONT_BOLD);
                plot.getRangeAxis().setTickLabelFont(LABEL_FONT);

                plot.getDomainAxis().setLabelFont(LABEL_FONT_BOLD);
                plot.getDomainAxis().setTickLabelFont(LABEL_FONT);
            }

            // legend settings
            LegendTitle legend = chart.getLegend();
            if (legend != null) {
                legend.setPosition(RectangleEdge.TOP);
                legend.setFrame(BlockBorder.NONE);
                legend.setHorizontalAlignment(HorizontalAlignment.LEFT);
                legend.setItemFont(LABEL_FONT);
            }

            AbstractChartPanel panel = getPlotterPanel();
            // Chart Panel Settings
            if (panel == null) {
                panel = createPanel(chart);
            } else {
                panel.setChart(chart);
            }

            // ATTENTION: WITHOUT THIS WE GET SEVERE MEMORY LEAKS!!!
            panel.getChartRenderingInfo().setEntityCollection(null);
        }
    }
}

From source file:com.flexoodb.common.FlexUtils.java

static public BufferedImage createCaptcha(String text, Color background, Color fontcolor, int fontsize,
        int width, int height, int x, int y) throws Exception {
    BufferedImage img = new BufferedImage(width + 10, height, BufferedImage.TYPE_INT_RGB);
    img.createGraphics();/*from   www.j a va 2s  . c  om*/
    Graphics2D g = (Graphics2D) img.getGraphics();
    g.setColor(background);
    g.fillRect(0, 0, img.getWidth(), img.getHeight());
    Font font = new Font("Monospaced", Font.BOLD + Font.ITALIC, fontsize);

    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
    g.setFont(font);
    g.setColor(fontcolor);

    char[] c = text.toCharArray();

    java.util.Random random = new java.util.Random();

    for (int i = 0; i < c.length; i++) {
        int j = random.nextInt(9);
        if (j > 5) {
            j = j - 5;
        } else {
            j = -1 * (j - 2);
        }

        char[] c1 = new char[1];
        c1[0] = c[i];
        g.drawChars(c1, 0, 1, x + (i * 16), y + j);
    }

    for (int i = -10; i < 20; i++) {
        int j = random.nextInt(8);
        g.drawOval((i + j) - 20, ((i * 8) - (j > 3 ? 4 : -4)), width + 20, height);
    }
    //ImageIO.write(img,"png",new File(outputfile));
    return img;

}