congrats Icon
New! Announcing Tabnine Chat Beta
Learn More
Tabnine Logo
Graphics.drawString
Code IndexAdd Tabnine to your IDE (free)

How to use
drawString
method
in
java.awt.Graphics

Best Java code snippets using java.awt.Graphics.drawString (Showing top 20 results out of 3,060)

Refine searchRefine arrow

  • Graphics.setColor
  • Graphics.setFont
  • Graphics.fillRect
  • Window.setVisible
  • JFrame.setDefaultCloseOperation
  • JFrame.<init>
  • Container.add
  • Graphics.getFontMetrics
  • FontMetrics.stringWidth
origin: stackoverflow.com

 void drawString(Graphics g, String text, int x, int y) {
  for (String line : text.split("\n"))
    g.drawString(line, x, y += g.getFontMetrics().getHeight());
}
origin: stackoverflow.com

 public static void main(String[] args) throws Exception {
  final BufferedImage image = ImageIO.read(new URL(
    "http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));

  Graphics g = image.getGraphics();
  g.setFont(g.getFont().deriveFont(30f));
  g.drawString("Hello World!", 100, 100);
  g.dispose();

  ImageIO.write(image, "png", new File("test.png"));
}
origin: wildfly/wildfly

/**
 * Draws the empty board, no pieces on it yet, just grid lines
 */
void drawEmptyBoard(Graphics g) {
  int x=x_offset, y=y_offset;
  Color old_col=g.getColor();
  g.setFont(def_font2);
  old_col=g.getColor();
  g.setColor(checksum_col);
  g.drawString(("Checksum: " + checksum), x_offset + field_size, y_offset - 20);
  g.setFont(def_font);
  g.setColor(old_col);
  for(int i=0; i < num_fields; i++) {
    for(int j=0; j < num_fields; j++) {  // draws 1 row
      g.drawRect(x, y, field_size, field_size);
      x+=field_size;
    }
    g.drawString((String.valueOf((num_fields - i - 1))), x + 20, y + field_size / 2);
    y+=field_size;
    x=x_offset;
  }
  for(int i=0; i < num_fields; i++) {
    g.drawString((String.valueOf(i)), x_offset + i * field_size + field_size / 2, y + 30);
  }
}
origin: wildfly/wildfly

public void paintNode(Graphics g, Node n, FontMetrics fm) {
  String addr=n.addr != null? n.addr.toString() : null;
  int x=(int)n.x;
  int y=(int)n.y;
  g.setColor((n == pick)? selectColor : (n.fixed? fixedColor : nodeColor));
  int w=fm.stringWidth(n.lbl) + 10;
  if(addr != null)
    w=Math.max(w, fm.stringWidth(addr) + 10);
  if(addr == null)
    addr="<no address>";
  int h=(fm.getHeight() + 4) * 2;
  n.width=w;
  n.height=h;
  n.xloc=x - w / 2;
  n.yloc=y - h / 2;
  g.fillRect(x - w / 2, y - h / 2, w, h);
  g.setColor(Color.black);
  g.drawRect(x - w / 2, y - h / 2, w - 1, h - 1);
  g.drawString(n.lbl, x - (w - 10) / 2, (y - (h - 4) / 2) + fm.getAscent());
  g.drawString(addr, x - (w - 10) / 2, (y - (h - 4) / 2) + 2 * fm.getAscent() + 4);
}
origin: stackoverflow.com

g.setColor(Color.WHITE);
g.setFont(new Font("serif", Font.BOLD, 60));
g.drawString(s, getWidth() / 2 - g.getFontMetrics().stringWidth(s) / 2,
    getHeight() / 2 + g.getFontMetrics().getHeight() / 2);
JFrame f = new JFrame();
f.setSize(500, 500);
f.setTitle("Sometimes Red, Sometimes Blue");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new TestPanel());
f.setVisible(true);
origin: stackoverflow.com

public static void main(String args[]) {
  FrameTestBase t = new FrameTestBase();
  t.add(new JComponent() {
    public void paintComponent(Graphics g) {
      String str = "hello world!";
      int y = 50;
      FontMetrics fm = g.getFontMetrics();
      Rectangle2D rect = fm.getStringBounds(str, g);
      g.setColor(bgColor);
      g.fillRect(x,
            y - fm.getAscent(),
            (int) rect.getWidth(),
            (int) rect.getHeight());
      g.setColor(textColor);
      g.drawString(str, x, y);
  t.setDefaultCloseOperation(EXIT_ON_CLOSE);
  t.setSize(400, 200);
  t.setVisible(true);
origin: loklak/loklak_server

/**
 * test image generator
 */
private static RenderedImage generateTestImage(int width, int height, Random r, double angle) {
  BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  Graphics g = img.getGraphics();
  g.setColor(Color.white); g.fillRect(0, 0, width, height); g.setColor(Color.BLUE);
  int x = width / 2;
  int y = height / 2;
  int radius = Math.min(x, y);
  g.drawLine(x, y, x + (int) (radius * Math.cos(angle)), y + (int) (radius * Math.sin(angle)));
  g.drawString("giftest", r.nextInt(width), r.nextInt(height));
  return img;
}
origin: stackoverflow.com

public void paintComponent(Graphics g) {
  super.paintComponent(g);
  int w2 = g.getFontMetrics().stringWidth(TITLE) / 2;
  g.drawString(TITLE, textPt.x - w2, textPt.y);
      JFrame f = new JFrame(TITLE);
      f.add(new MouseDragTest());
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.pack();
      f.setLocationRelativeTo(null);
      f.setVisible(true);
origin: stackoverflow.com

 import java.awt.*;

public class TestComponent extends JPanel {

  private void drawString(Graphics g, String text, int x, int y) {
    for (String line : text.split("\n"))
      g.drawString(line, x, y += g.getFontMetrics().getHeight());
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    drawString(g, "hello\nworld", 20, 20);
    g.setFont(g.getFont().deriveFont(20f));
    drawString(g, "part1\npart2", 120, 120);
  }

  public static void main(String s[]) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new TestComponent());
    f.setSize(220, 220);
    f.setVisible(true);
  }
}
origin: stackoverflow.com

  JFrame frame = new JFrame("Test");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setLayout(new BorderLayout());
  frame.add(new TestPane());
String text = "This is a test xyx";
g.setColor(Color.RED);
g.drawLine(0, height / 2, getWidth(), height / 2);
FontMetrics fm = g.getFontMetrics();
int totalWidth = (fm.stringWidth(text) * 2) + 4;
g.setColor(Color.BLACK);
g.drawString(text, x, y + ((fm.getDescent() + fm.getAscent()) / 2));
x += fm.stringWidth(text) + 2;
y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
g.drawString(text, x, y);
origin: wildfly/wildfly

void drawNumbers(Graphics g) {
  Point p;
  String num;
  FontMetrics fm=g.getFontMetrics();
  int len=0;
  synchronized(array) {
    for(int i=0; i < num_fields; i++)
      for(int j=0; j < num_fields; j++) {
        num=String.valueOf(array[i][j]);
        len=fm.stringWidth(num);
        p=index2Coord(i, j);
        g.drawString(num, p.x - (len / 2), p.y);
      }
  }
}
origin: stackoverflow.com

 public static void main(String[] args)
  throws IOException
{
  JFrame frame = new JFrame();
  frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  frame.setLocationRelativeTo(null);
  frame.setSize(800, 600);

  BufferedImage modifiedImg = ImageIO.read(new File("c:\\test.png"));
  Graphics graphics = modifiedImg.getGraphics();
  graphics.setColor(Color.red);
  graphics.drawString("Label", 100, 100);// draw text
  graphics.drawLine(1, 100, 100, 100);// draw line

  JLabel label = new JLabel();
  label.setIcon(new ImageIcon(modifiedImg));
  frame.getContentPane().add(label, BorderLayout.CENTER);

  frame.setVisible(true);
}
origin: stackoverflow.com

 public void paintComponent(Graphics g)
{
  super.paintComponent(g);
  g.setColor(Color.YELLOW);
  g.fillOval(10, 10, grootte, grootte);

  String str = ""+grootte;
  FontMetrics fm = g.getFontMetrics();
  Rectangle2D strBounds = fm.getStringBounds(str, g);

  g.setColor(Color.BLACK);
  g.drawString(str, 10 + grootte/2 - (int)strBounds.getWidth()/2, 10 + grootte/2 + (int)strBounds.getHeight()/2);
}
origin: stackoverflow.com

 public class Foo extends JPanel {
 @Override
 protected void paintComponent(Graphics g) {
  super.paintComponent(g);
  g.drawString("hello", 0, 20);
 }

 public static void main(String[] args) {
  JFrame frame = new JFrame("Foo");
  frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  frame.add(new Foo());
  frame.pack();
  frame.setVisible(true);
 }
}
origin: SonarSource/sonarqube

try {
 if (isCurrentLine(rowStartOffset))
  g.setColor(getCurrentLineForeground());
 else
  g.setColor(getForeground());
 int stringWidth = fontMetrics.stringWidth(lineNumber);
 int x = getOffsetX(availableWidth, stringWidth) + insets.left;
 int y = getOffsetY(rowStartOffset, fontMetrics);
 g.drawString(lineNumber, x, y);
origin: runelite/runelite

@Override
protected void paintEnabledText(JLabel l, Graphics g, String s, int textX, int textY)
{
  if (l instanceof JShadowedLabel)
  {
    JShadowedLabel ll = (JShadowedLabel) l;
    g.setColor(ll.getShadow());
    g.drawString(s, textX + ll.getShadowSize().x, textY + ll.getShadowSize().y);
  }
  g.setColor(l.getForeground());
  g.drawString(s, textX, textY);
}
origin: stackoverflow.com

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
EventQueue.invokeLater(new Runnable() {
  public void run() {
    new JavaPaintUI().setVisible(true);
  super.paintComponent(g);
  g.drawString("BLAH", 20, 20);
  g.drawRect(200, 200, 200, 200);
origin: groovy/groovy-core

  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    // starting position in document
    int start = textEditor.viewToModel(getViewport().getViewPosition());
    // end position in document
    int end = textEditor.viewToModel(new Point(10,
        getViewport().getViewPosition().y +
            (int) textEditor.getVisibleRect().getHeight())
    );
    // translate offsets to lines
    Document doc = textEditor.getDocument();
    int startline = doc.getDefaultRootElement().getElementIndex(start) + 1;
    int endline = doc.getDefaultRootElement().getElementIndex(end) + 1;
    Font f = textEditor.getFont();
    int fontHeight = g.getFontMetrics(f).getHeight();
    int fontDesc = g.getFontMetrics(f).getDescent();
    int startingY = -1;
    try {
      startingY = textEditor.modelToView(start).y + fontHeight - fontDesc;
    } catch (BadLocationException e1) {
      System.err.println(e1.getMessage());
    }
    g.setFont(f);
    for (int line = startline, y = startingY; line <= endline; y += fontHeight, line++) {
      String lineNumber = StringGroovyMethods.padLeft(Integer.toString(line), 4, " ");
      g.drawString(lineNumber, 0, y);
    }
  }
}
origin: libgdx/libgdx

  private void draw (Graphics g, Array<TextureRegion> regions, Color color, boolean drawIndex) {
    int i=0;
    for(TextureRegion region : regions){
      int x = region.getRegionX(), y = region.getRegionY(),
        h = region.getRegionHeight();
      if(drawIndex){
        String indexString = ""+i;
        Rectangle bounds = g.getFontMetrics().getStringBounds(indexString, g).getBounds();
        g.setColor(indexBackgroundColor);
        g.fillRect(x, y+h-bounds.height, bounds.width, bounds.height);
        g.setColor(indexColor);
        g.drawString(indexString, x, y+h);
        ++i;
      }
      g.setColor(color);
      g.drawRect(x, y, region.getRegionWidth(), h);
    }
  }
}
origin: stackoverflow.com

g.drawImage(background, 0, 0, this);
double theta = 2 * Math.PI * index++ / 64;
g.setColor(Color.blue);
rect.setRect(
  (int) (Math.sin(theta) * w / 3 + w / 2 - RADIUS),
  2 * RADIUS, 2 * RADIUS);
g.fillOval(rect.x, rect.y, rect.width, rect.height);
g.setColor(Color.white);
if (frameCount == FRAMES) {
  averageTime = totalTime / FRAMES;
g.drawString(s, 5, 16);
  Dimension d = field.getPreferredSize();
  field.setBounds(e.getX(), e.getY(), d.width, d.height);
  add(field);
  Graphics2D g = background.createGraphics();
  g.clearRect(0, 0, w, h);
  g.setColor(Color.green.darker());
  for (int i = 0; i < 128; i++) {
    g.drawLine(w / 2, h / 2, r.nextInt(w), r.nextInt(h));
java.awtGraphicsdrawString

Javadoc

Draws the text given by the specified string, using this graphics context's current font and color. The baseline of the leftmost character is at position (x, y) in this graphics context's coordinate system.

Popular methods of Graphics

  • setColor
  • drawImage
    Draws as much of the specified image as is currently available. The image is drawn with its top-left
  • fillRect
    Fills the specified rectangle. The left and right edges of the rectangle are atx and x + width - 1.
  • drawLine
    Draws a line, using the current color, between the points(x1, y1) and (x2, y2) in this graphics con
  • dispose
    Disposes of this graphics context and releases any system resources that it is using. A Graphics obj
  • setFont
    Sets this graphics context's font to the specified font. All subsequent text operations using this g
  • drawRect
    Draws the outline of the specified rectangle. The left and right edges of the rectangle are atx and
  • getFontMetrics
  • create
    Creates a new Graphics object based on thisGraphics object, but with a new translation and clip area
  • getColor
    Gets this graphics context's current color.
  • translate
    Translates the origin of the graphics context to the point (x,y) in the current coordinate system. M
  • getClipBounds
    Returns the bounding rectangle of the current clipping area. The coordinates in the rectangle are re
  • translate,
  • getClipBounds,
  • setClip,
  • getFont,
  • fillPolygon,
  • fillOval,
  • drawOval,
  • getClip,
  • drawRoundRect

Popular in Java

  • Making http post requests using okhttp
  • setScale (BigDecimal)
  • findViewById (Activity)
  • requestLocationUpdates (LocationManager)
  • GridBagLayout (java.awt)
    The GridBagLayout class is a flexible layout manager that aligns components vertically and horizonta
  • GridLayout (java.awt)
    The GridLayout class is a layout manager that lays out a container's components in a rectangular gri
  • PriorityQueue (java.util)
    A PriorityQueue holds elements on a priority heap, which orders the elements according to their natu
  • ResourceBundle (java.util)
    ResourceBundle is an abstract class which is the superclass of classes which provide Locale-specifi
  • Reference (javax.naming)
  • SAXParseException (org.xml.sax)
    Encapsulate an XML parse error or warning.> This module, both source code and documentation, is in t
  • Top plugins for WebStorm
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now