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:OutputApplet.java

/**
 * The "paint" method is called by AWT when it wants us to display our
 * current state on the screen.//www .ja  va  2s.c o m
 */

public synchronized void paint(Graphics g) {
    // If there are no results available, display the current message.
    if (queryResults == null) {
        g.drawString(message, 5, 50);
        return;
    }

    // Display the results.
    g.drawString("Prices of coffee per pound:  ", 5, 10);
    int y = 30;
    java.util.Enumeration e = queryResults.elements();
    while (e.hasMoreElements()) {
        String text = (String) e.nextElement();
        g.drawString(text, 5, y);
        y = y + 15;
    }
}

From source file:PaintAllFontsFromGraphicEvironment.java

public void paint(Graphics g) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

    Font[] allFonts = ge.getAllFonts();

    for (int i = 0; i < allFonts.length; i++) {
        Font f = allFonts[i].deriveFont(10.0f);
        g.setFont(f);//from   w  w  w .ja  v a 2 s. c o  m

        g.setColor(Color.black);
        g.drawString("Hello!", 10, 20 * i);

    }
}

From source file:MyCheckBoxUI.java

public void paint(Graphics g, JComponent c) {
    AbstractButton b = (AbstractButton) c;
    ButtonModel model = b.getModel();
    Dimension d = b.getSize();//www .  j av a  2s  .  c  om

    g.setFont(c.getFont());
    FontMetrics fm = g.getFontMetrics();
    g.setColor(Color.white);
    g.drawString("Am I a check box", 10, 10);

}

From source file:Main.java

private void myPaint(Component comp, Graphics g) {
    int x = comp.getX();
    int y = comp.getY();
    g.translate(x, y);/*w  w  w .  j av a 2  s.com*/
    cursor.translate(-x, -y);
    if (comp.contains(cursor)) {
        String cls_name = comp.getClass().getName();
        g.setColor(Color.black);
        g.drawString(cls_name, 0, 10);
    }
    if (comp instanceof Container) {
        Container cont = (Container) comp;
        for (int i = 0; i < cont.getComponentCount(); i++) {
            Component child = cont.getComponent(i);
            myPaint(child, g);
        }
    }

    cursor.translate(x, y);
    g.translate(-x, -y);
}

From source file:WriteFile.java

public void paint(Graphics g) {
    try {/*  ww w . j a va  2 s .c om*/
        dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(myFile), 128));
        dos.writeChars("Cats can hypnotize you when you least expect it\n");
        dos.flush();
        g.drawString("Successfully wrote to the file named " + myFile + " -- go take a look at it!", 10, 10);
    } catch (SecurityException e) {
        g.drawString("writeFile: caught security exception: " + e, 10, 10);
    } catch (IOException ioe) {
        g.drawString("writeFile: caught i/o exception", 10, 10);
    }
}

From source file:SlidePuzzle.java

public void paint(Graphics g) {
    if (m_fAllLoaded) {
        Rectangle r = g.getClipRect();
        g.clearRect(r.x, r.y, r.width, r.height);
        g.drawImage(logo, 0, 0, null);//w  w  w  . ja v  a2s .  c om

    } else
        g.drawString("Loading images...", 10, 20);

}

From source file:Main.java

public Main() {
    super("JScrollPane Demo");
    JScrollPane scrollPane = new JScrollPane(label);

    JLabel[] corners = new JLabel[4];
    for (int i = 0; i < 4; i++) {
        corners[i] = new JLabel();
        corners[i].setBackground(Color.white);
        corners[i].setOpaque(true);/*from  ww w.jav a  2s  .com*/
    }

    JLabel rowheader = new JLabel() {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Rectangle rect = g.getClipBounds();
            for (int i = 50 - (rect.y % 50); i < rect.height; i += 50) {
                g.drawLine(0, rect.y + i, 3, rect.y + i);
                g.drawString("" + (rect.y + i), 6, rect.y + i + 3);
            }
        }

        public Dimension getPreferredSize() {
            return new Dimension(25, (int) label.getPreferredSize().getHeight());
        }
    };
    rowheader.setBackground(Color.white);
    rowheader.setOpaque(true);

    JLabel columnheader = new JLabel() {

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Rectangle r = g.getClipBounds();
            for (int i = 50 - (r.x % 50); i < r.width; i += 50) {
                g.drawLine(r.x + i, 0, r.x + i, 3);
                g.drawString("" + (r.x + i), r.x + i - 10, 16);
            }
        }

        public Dimension getPreferredSize() {
            return new Dimension((int) label.getPreferredSize().getWidth(), 25);
        }
    };
    columnheader.setBackground(Color.white);
    columnheader.setOpaque(true);

    scrollPane.setRowHeaderView(rowheader);
    scrollPane.setColumnHeaderView(columnheader);
    scrollPane.setCorner(JScrollPane.LOWER_LEFT_CORNER, corners[0]);
    scrollPane.setCorner(JScrollPane.LOWER_RIGHT_CORNER, corners[1]);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, corners[2]);
    scrollPane.setCorner(JScrollPane.UPPER_RIGHT_CORNER, corners[3]);

    getContentPane().add(scrollPane);
    setSize(400, 300);
    setVisible(true);
}

From source file:HeaderDemo.java

public HeaderDemo() {
    super("JScrollPane Demo");
    JScrollPane scrollPane = new JScrollPane(label);

    JLabel[] corners = new JLabel[4];
    for (int i = 0; i < 4; i++) {
        corners[i] = new JLabel();
        corners[i].setBackground(Color.white);
        corners[i].setOpaque(true);/*  w  ww . j  ava  2s .  co m*/
    }

    JLabel rowheader = new JLabel() {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Rectangle rect = g.getClipBounds();
            for (int i = 50 - (rect.y % 50); i < rect.height; i += 50) {
                g.drawLine(0, rect.y + i, 3, rect.y + i);
                g.drawString("" + (rect.y + i), 6, rect.y + i + 3);
            }
        }

        public Dimension getPreferredSize() {
            return new Dimension(25, (int) label.getPreferredSize().getHeight());
        }
    };
    rowheader.setBackground(Color.white);
    rowheader.setOpaque(true);

    JLabel columnheader = new JLabel() {

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Rectangle r = g.getClipBounds();
            for (int i = 50 - (r.x % 50); i < r.width; i += 50) {
                g.drawLine(r.x + i, 0, r.x + i, 3);
                g.drawString("" + (r.x + i), r.x + i - 10, 16);
            }
        }

        public Dimension getPreferredSize() {
            return new Dimension((int) label.getPreferredSize().getWidth(), 25);
        }
    };
    columnheader.setBackground(Color.white);
    columnheader.setOpaque(true);

    scrollPane.setRowHeaderView(rowheader);
    scrollPane.setColumnHeaderView(columnheader);
    scrollPane.setCorner(JScrollPane.LOWER_LEFT_CORNER, corners[0]);
    scrollPane.setCorner(JScrollPane.LOWER_RIGHT_CORNER, corners[1]);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, corners[2]);
    scrollPane.setCorner(JScrollPane.UPPER_RIGHT_CORNER, corners[3]);

    getContentPane().add(scrollPane);
    setSize(400, 300);
    setVisible(true);
}

From source file:Main.java

protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.drawLine(10, 100, 380, 100);//from  w ww  .j  av a2s  . c o  m
    g.drawLine(200, 30, 200, 190);

    g.drawLine(380, 100, 370, 90);
    g.drawLine(380, 100, 370, 110);
    g.drawLine(200, 30, 190, 40);
    g.drawLine(200, 30, 210, 40);

    g.drawString("X", 360, 80);
    g.drawString("Y", 220, 40);

    Polygon p = new Polygon();
    Polygon p2 = new Polygon();

    for (int x = -170; x <= 170; x++) {
        p.addPoint(x + 200, 100 - (int) (50 * f((x / 100.0) * 2 * Math.PI)));

    }

    for (int x = -170; x <= 170; x++) {
        p2.addPoint(x + 200, 100 - (int) (50 * gCos((x / 100.0) * 2 * Math.PI)));

    }

    g.setColor(Color.red);
    g.drawPolyline(p.xpoints, p.ypoints, p.npoints);
    g.drawString("-2\u03c0", 95, 115);
    g.drawString("-\u03c0", 147, 115);
    g.drawString("\u03c0", 253, 115);
    g.drawString("2\u03c0", 305, 115);
    g.drawString("0", 200, 115);

    g.setColor(Color.blue);
    g.drawPolyline(p2.xpoints, p2.ypoints, p2.npoints);

}

From source file:FontDemo.java

/**
 * Draws the font names in its font. Called by AWT when painting is needed
 * Does lazy evaluation of Font creation, caching the results (without this,
 * scrolling performance suffers even on a P3-750).
 *///from   ww  w  . j a  va2  s. co  m
public void paint(Graphics g) {
    for (int i = 0; i < fontNames.length; i += 1) {
        if (fonts[i] == null) {
            fonts[i] = new Font(fontNames[i], Font.BOLD, 14);
        }
        g.setFont(fonts[i]);
        int x = 20;
        int y = 20 + (YINCR * i);
        g.drawString(fontNames[i], x, y);
    }
}