java 图像在屏幕_java 在屏幕上绘制图像(单例运行)

package com.czp;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Toolkit;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.io.IOException;

import java.net.Inet4Address;

import java.net.ServerSocket;

import javax.swing.JOptionPane;

import javax.swing.JWindow;

/***

* draw IP on screen

*

*

* @author CaoZhongping

*

*/

public class MyWindow extends JWindow {

private static final int FONT_SIZE = 26;

private final String FONT_NAME = "楷体";

private static final long serialVersionUID = 1L;

private ServerSocket sock;

private static Font font;

private int width;

public MyWindow(int width, int height) {

super();

font = new Font(FONT_NAME, Font.PLAIN, FONT_SIZE);

this.width = width;

this.setSize(width, height);

this.setPostion();

this.setAlwaysOnTop(true);

this.setVisible(true);

this.bindPort();

this.addMouseListener(new MyWinLister(this));

}

/**

* 单例运行

*/

private void bindPort() {

new Thread(new Runnable() {

@Override

public void run() {

try {

sock = new ServerSocket(6666);

sock.accept();

} catch (IOException e) {

JOptionPane.showMessageDialog(null, "GetIP Aready Running");

System.exit(0);

}

}

}).start();

}

public ServerSocket getSock() {

return sock;

}

@Override

public void paint(Graphics g) {

g.setColor(Color.BLUE);

g.setFont(font);

g.drawString("Your IP is :" + getLocalIP() + " ", 0, this.getY()

+ FONT_SIZE);

}

private String getLocalIP() {

try {

return Inet4Address.getLocalHost().getHostAddress();

} catch (Exception e) {

return e.toString();

}

}

/**

* center to screen

*/

private void setPostion() {

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

this.setLocation((int) (d.getWidth() - this.width), 2);

}

/**

*

* double click quit

*

*/

class MyWinLister extends MouseAdapter {

private MyWindow window;

public MyWinLister(MyWindow window) {

super();

this.window = window;

}

@Override

public void mouseClicked(MouseEvent e) {

super.mouseClicked(e);

if (e.getClickCount() == 2) {

try {

this.window.getSock().close();

this.window.dispose();

} catch (IOException e1) {

e1.printStackTrace();

}

System.exit(0);

}

}

}

public static void main(String[] args) {

new MyWindow(320, 30);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值