- package craky;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.EventQueue;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.LinearGradientPaint;
- import java.awt.MouseInfo;
- import java.awt.Toolkit;
- import java.awt.Window;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.util.Random;
- import javax.swing.JComponent;
- import javax.swing.JDialog;
- import javax.swing.Timer;
- import javax.swing.UIManager;
- import sun.swing.SwingUtilities2;
- public class Go extends JDialog
- {
- private static final long serialVersionUID = -1891510658078755857L;
- private static boolean noStop = false;
- private static int frameCount = 200;
- private static String welcome = "Hello";
- public static void main(String[] args)
- {
- if(args != null)
- {
- noStop = args.length > 0 && args[0].equals("true");
- if(args.length > 1)
- {
- try
- {
- frameCount = Integer.parseInt(args[1]);
- frameCount = frameCount < 0? 200: frameCount;
- }
- catch(Exception e)
- {}
- }
- if(args.length > 2 && args[2] != null && !args[2].isEmpty())
- {
- welcome = args[2];
- }
- }
- EventQueue.invokeLater(new Runnable()
- {
- public void run()
- {
- try
- {
- Go win = new Go();
- win.setVisible(true);
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- });
- }
- public Go()
- {
- setSize(1, 1);
- setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
- add(new TextPane(this), BorderLayout.CENTER);
- this.setUndecorated(true);
- this.setAlwaysOnTop(true);
- this.setBackground(new Color(0, 0, 0, 0));
- }
- private static class TextPane extends JComponent implements ActionListener
- {
- private static final long serialVersionUID = 2973523866863391259L;
- private static final Font FONT = new Font("Microsoft YaHei", Font.BOLD, 50);
- private static final String USER = System.getProperty("user.name");
- private static final Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit().getScreenSize();
- private Window win;
- private Dimension textSize;
- private Random rand;
- private Timer timer;
- private int finalX, finalY;
- private double lastX, lastY, xStep, yStep;
- private boolean xIncrease, yIncrease;
- private TextPane(Window win)
- {
- this.win = win;
- this.textSize = new Dimension();
- this.rand = new Random();
- this.xIncrease = this.yIncrease = true;
- this.timer = new Timer(1, this);
- this.putClientProperty(SwingUtilities2.AA_TEXT_PROPERTY_KEY, UIManager.getLookAndFeel().getDefaults().get(SwingUtilities2.AA_TEXT_PROPERTY_KEY));
- this.addMouseListener(new MouseAdapter()
- {
- public void mouseEntered(MouseEvent e)
- {
- if(e.isControlDown())
- {
- timer.stop();
- }
- else
- {
- go();
- }
- }
- });
- win.addKeyListener(new KeyAdapter()
- {
- public void keyReleased(KeyEvent e)
- {
- if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
- {
- System.exit(0);
- }
- }
- });
- }
- protected void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- Graphics2D g2d = (Graphics2D)g;
- g.setFont(FONT);
- FontMetrics fm = g.getFontMetrics();
- int welcomeWidth = fm.stringWidth(welcome);
- int userWidth = fm.stringWidth(USER);
- int lineHeight = fm.getHeight();
- int leading = fm.getLeading();
- if(win.getWidth() <= 1 || win.getHeight() <= 1)
- {
- textSize.width = Math.max(welcomeWidth, userWidth);
- textSize.height = lineHeight * 2 - leading;
- win.setSize(textSize.width, textSize.height);
- win.setLocationRelativeTo(null);
- }
- float[] fractions = {0.1f, 0.5f, 0.9f};
- Color[] colors = {Color.ORANGE, Color.MAGENTA, Color.PINK};
- g2d.setPaint(new LinearGradientPaint(0, 0, welcomeWidth, lineHeight - leading, fractions, colors));
- SwingUtilities2.drawString(this, g, welcome, 0, fm.getAscent());
- fractions = new float[]{0.2f, 0.5f, 0.8f};
- colors = new Color[]{Color.BLUE, Color.GREEN, Color.DARK_GRAY};
- g2d.setPaint(new LinearGradientPaint(0, lineHeight, userWidth, lineHeight * 2 - leading, fractions, colors));
- SwingUtilities2.drawString(this, g, USER, 0, fm.getAscent() + lineHeight);
- }
- private void go()
- {
- timer.stop();
- finalX = rand.nextInt(SCREEN_SIZE.width - textSize.width);
- finalY = rand.nextInt(SCREEN_SIZE.height - textSize.height);
- lastX = win.getX();
- lastY = win.getY();
- xStep = (finalX - lastX) / frameCount;
- yStep = (finalY - lastY) / frameCount;
- xIncrease = finalX > lastX;
- yIncrease = finalY > lastY;
- timer.start();
- }
- public void actionPerformed(ActionEvent e)
- {
- lastX += xStep;
- lastY += yStep;
- int newX = (int)lastX;
- int newY = (int)lastY;
- if((xIncrease && newX > finalX) || (!xIncrease && newX < finalX))
- {
- newX = finalX;
- }
- if((yIncrease && newY > finalY) || (!yIncrease && newY < finalY))
- {
- newY = finalY;
- }
- win.setLocation(newX, newY);
- if(newX == finalX && newY == finalY)
- {
- timer.stop();
- if(noStop || win.getBounds().contains(MouseInfo.getPointerInfo().getLocation()))
- {
- go();
- }
- }
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1510201410638.html
来源: http://www.codesnippet.cn/detail/1510201410638.html