Java Draw String drawStringOnScreen(String s, Color color, long milseconds)

Here you can find the source of drawStringOnScreen(String s, Color color, long milseconds)

Description

draw String On Screen

License

Open Source License

Parameter

Parameter Description
s a parameter
color a parameter
milseconds a parameter

Declaration

public static void drawStringOnScreen(String s, Color color, long milseconds) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;

import java.awt.Window;

public class Main {
    /**//from   w  w  w.jav  a2  s  .c o m
     *
     * @param s
     * @param color
     * @param milseconds
     */
    public static void drawStringOnScreen(String s, Color color, long milseconds) {
        Thread t = new Thread(() -> {
            Window w = new Window(null) {
                @Override
                public void paint(Graphics g) {
                    final Font font = getFont().deriveFont(48f);
                    g.setFont(font);
                    g.setColor(color);
                    final String message = s;
                    FontMetrics metrics = g.getFontMetrics();
                    g.drawString(message, (getWidth() - metrics.stringWidth(message)) / 2,
                            (getHeight() - metrics.getHeight()) / 2);
                }

                @Override
                public void update(Graphics g) {
                    paint(g);
                }
            };
            w.setAlwaysOnTop(true);
            w.setBounds(w.getGraphicsConfiguration().getBounds());
            w.setBackground(new Color(0, true));
            w.setVisible(true);
            try {
                Thread.sleep(milseconds);
            } catch (InterruptedException ex) {

            }
            w.dispose();
        });
        t.start();
    }
}

Related

  1. drawStringAtPoint(Graphics g, String s, Point p)
  2. drawStringEx(Graphics g1, String s, Font font, Rectangle rect, int align, int valign)
  3. drawStringInBox(Graphics g, String string, int x, int y)
  4. drawStringLeft(Graphics2D g, String text, Rectangle rect, int fontHeight, Color c)
  5. drawStringOnImage(Image image, String string, int x, int y)
  6. drawStringPair(Graphics2D g, String str1, String str2, int left, int right, int y, int size, Color color, boolean bold)
  7. drawStringRight(final Graphics g, final FontMetrics m, final String str, final int x, final int y)
  8. drawStringRightAlignedVTop(Graphics graphics, String string, int x, int yTop)
  9. drawStringUnderlineCharAt(Graphics g, String text, int underlinedIndex, int x, int y)