Example usage for java.awt Graphics drawString

List of usage examples for java.awt Graphics drawString

Introduction

In this page you can find the example usage for java.awt Graphics drawString.

Prototype

public abstract void drawString(AttributedCharacterIterator iterator, int x, int y);

Source Link

Document

Renders the text of the specified iterator applying its attributes in accordance with the specification of the java.awt.font.TextAttribute TextAttribute class.

Usage

From source file:OptimalPrimitives.java

protected void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    long startTime, endTime, totalTime;

    g.setColor(Color.WHITE);//from  w ww.ja  v a2  s.c  om
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(Color.BLACK);

    g.drawString("Bad vs. Good Primitive Rendering", 50, 20);
    g.drawString("(" + ITERATIONS + " iterations)", 100, 35);
    g.drawString("Bad: ", 10, BAD_Y + 30);
    g.drawString("Good: ", 10, GOOD_Y + 30);

    // Bad line
    Shape line = new Line2D.Double(LINE_X, BAD_Y, LINE_X + 50, BAD_Y + 50);
    startTime = System.nanoTime();
    for (int i = 0; i < ITERATIONS; ++i) {
        g2d.draw(line);
    }
    endTime = System.nanoTime();
    totalTime = (endTime - startTime) / 1000000;
    System.out.println("bad line = " + totalTime);
    g.drawString(totalTime + " ms", LINE_X, BAD_Y + 70);

    // Good line
    startTime = System.nanoTime();
    for (int i = 0; i < ITERATIONS; ++i) {
        g.drawLine(LINE_X, GOOD_Y, LINE_X + 50, GOOD_Y + 50);
    }
    endTime = System.nanoTime();
    totalTime = (endTime - startTime) / 1000000;
    System.out.println("good line = " + totalTime);
    g.drawString(totalTime + " ms", LINE_X, GOOD_Y + 70);

    // Bad rect
    Shape rect = new Rectangle(RECT_X, BAD_Y, 50, 50);
    startTime = System.nanoTime();
    for (int i = 0; i < ITERATIONS; ++i) {
        g2d.fill(rect);
    }
    endTime = System.nanoTime();
    totalTime = (endTime - startTime) / 1000000;
    System.out.println("bad rect = " + totalTime);
    g.drawString(totalTime + " ms", RECT_X, BAD_Y + 70);

    // Good rect
    startTime = System.nanoTime();
    for (int i = 0; i < ITERATIONS; ++i) {
        g.fillRect(RECT_X, GOOD_Y, 50, 50);
    }
    endTime = System.nanoTime();
    totalTime = (endTime - startTime) / 1000000;
    System.out.println("good rect = " + totalTime);
    g.drawString(totalTime + " ms", RECT_X, GOOD_Y + 70);
}

From source file:contactangle.ImageControl.java

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    if (img != null)
        g.drawImage(img, 0, 0, this);
    else//from   w  ww.  j  av  a  2s .  c  o m
        g.drawString("NO IMAGE", 0, 15);

    g.setColor(Color.RED);

    if (x1 < x2 && y1 < y2)
        g.drawRect(x1, y1, x2 - x1, y2 - y1);
    else if (x1 >= x2 && y1 < y2)
        g.drawRect(x2, y1, x1 - x2, y2 - y1);
    else if (x1 < x2 && y1 >= y2)
        g.drawRect(x1, y2, x2 - x1, y1 - y2);
    else
        g.drawRect(x2, y2, x1 - x2, y1 - y2);

    if (valid && img != null) {

        if (x1 >= x2) {
            int temp = x1;
            x1 = x2;
            x2 = temp;
        }
        if (y1 >= y2) {
            int temp = y1;
            y1 = y2;
            y2 = temp;
        }

        choosenPoints = new ArrayList<Point>();
        for (int y = y1; y < y2; y++) {
            for (int x = x1; x < x2; x++) {
                int pixelData = img.getRGB(x, y);
                if (pixelData == -1)
                    choosenPoints.add(new Point(x, y));
            }
        }

        SimpleRegression reg = new SimpleRegression();
        for (Point p : choosenPoints) {
            reg.addData(p.x, p.y);
        }

        int firstX = choosenPoints.get(0).x;
        int firstY = choosenPoints.get(0).y;
        double slope = reg.getSlope();
        g.setColor(Color.GREEN);
        g.drawLine(firstX, firstY, firstX + (70), firstY + (int) (slope * (70)));
        g.drawLine(firstX, firstY, firstX - (70), firstY - (int) (slope * (70)));

        double contactDegrees = (Math.atan(reg.getSlope()) / (2 * Math.PI)) * 360.0;

        DecimalFormat d = new DecimalFormat("##.###");

        g.drawString("Contact Angle = ", 25, 25);
        g.drawString(d.format(contactDegrees) + " degrees", 25, 38);
    }

}

From source file:FontList.java

public void paint(Graphics g) {
    for (int f = 0; f < families.length; f++) { // for each family
        for (int s = 0; s < styles.length; s++) { // for each style
            Font font = new Font(families[f], styles[s], 18); // create font
            g.setFont(font); // set font
            String name = families[f] + " " + stylenames[s]; // create name
            g.drawString(name, 20, (f * 4 + s + 1) * 20); // display name
        }/*  w w w .j av a 2  s.  c o  m*/
    }
}

From source file:pt.lsts.neptus.plugins.wg.WgCtdLayer.java

private void paintCaption(Graphics2D g) {
    cbar.setCmap(cmap);/* w  w  w  .  ja v  a 2  s  . c  o m*/
    cbar.setSize(new Dimension(20, 120));
    Graphics copy = g.create();
    copy.translate(10, 10);
    cbar.paint(copy);
    copy.setColor(Color.white);
    copy.drawString(String.format("%3.2f C", minTemp), 23, 120);
    copy.drawString(String.format("%3.2f C", maxTemp), 23, 10);
}

From source file:StringRectPaintPanel.java

public void paint(Graphics g) {
    g.setFont(new Font("", 0, 100));
    FontMetrics fm = getFontMetrics(new Font("", 0, 100));
    String s = "java2s";
    int x = 5;/*from   www  .j a  v a  2  s  .  c  om*/
    int y = 5;

    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);

        int h = fm.getHeight();
        int w = fm.charWidth(c);

        g.drawRect(x, y, w, h);
        g.drawString(String.valueOf(c), x, y + h);

        x = x + w;
    }
}

From source file:Main.java

public void paint(Graphics g) {
    Font f = g.getFont();// www.j  a va  2s .  c om
    int fontSize = f.getSize();
    int fontStyle = f.getStyle();

    String msg = ", Size: " + fontSize + ", Style: ";
    if ((fontStyle & Font.BOLD) == Font.BOLD)
        msg += "Bold ";
    if ((fontStyle & Font.ITALIC) == Font.ITALIC)
        msg += "Italic ";
    if ((fontStyle & Font.PLAIN) == Font.PLAIN)
        msg += "Plain ";

    g.drawString(msg, 4, 16);
}

From source file:org.accada.reader.hal.impl.sim.graphic.Antenna.java

/**
 * paints the rfid antenna/*from  ww w.  ja v  a  2 s .  c  o  m*/
 * 
 * @param the graphic representation of the component
 */
protected void paintComponent(Graphics g) {
    icon.paintIcon(this, g, 0, 0);
    g.setColor(Color.BLACK);
    g.setFont(new Font(simulator.getProperties().getProperty("AntennaLabelFont"), 0,
            simulator.getProperty("AntennaLabelSize")));
    g.drawString(id, (15 - id.length()) * 7 / 2, simulator.getProperty("AntennaHeight"));
}

From source file:org.accada.hal.impl.sim.graphic.Antenna.java

/**
 * paints the rfid antenna/*from  ww w  . jav a2s. c  o  m*/
 * 
 * @param g the graphic representation of the component
 */
protected void paintComponent(Graphics g) {
    icon.paintIcon(this, g, 0, 0);
    g.setColor(Color.BLACK);
    g.setFont(new Font(simulator.getProperties().getString("AntennaLabelFont"), 0,
            simulator.getProperty("AntennaLabelSize")));
    g.drawString(id, (15 - id.length()) * 7 / 2, simulator.getProperty("AntennaHeight"));
}

From source file:org.accada.hal.impl.sim.graphic.Reader.java

/**
 * paints the reader/*w  w  w .  j  a  va2  s . co m*/
 * 
 * @param g the graphic representation of the component
 */
protected void paintComponent(Graphics g) {
    icon.paintIcon(this, g, 0, 0);
    g.setColor(Color.BLACK);
    g.setFont(new Font(simulator.getProperties().getString("ReaderLabelFont"), 0,
            simulator.getProperty("ReaderLabelSize")));
    g.drawString(id, (18 - id.length()) * 9 / 2, simulator.getProperty("ReaderHeight"));
}

From source file:TestAppletNetscape.java

public void paint(Graphics g) {
    System.out.println("paint(): querying the database");
    try {/*from   ww w  . j  a  va 2 s.c  o  m*/
        PrivilegeManager.enablePrivilege("UniversalConnect");
        Statement stmt = conn.createStatement();
        ResultSet rset = stmt.executeQuery("select 'Hello '||initcap(USER) result from dual");
        while (rset.next())
            g.drawString(rset.getString(1), 10, 10);
        rset.close();
        stmt.close();
    } catch (SQLException e) {
        System.err.println("paint(): SQLException: " + e.getMessage());
    }
}