Java desktop pet

osc_ bjmmswh6 2021-01-21 12:06:48
java desktop pet


My desktop pet 【 static state 】

The recent winter vacation has just begun , And then there's a lot of time , Just want to be a desktop pet , But my ability is limited , It can only be static , In fact, I think static is pretty good, right QAQ, Want to do dynamic words can refer to the link 2, It can be implemented with threads
Thank you for these two articles : Connect 1. Connect 2

First step

We need a picture with a transparent background , I use PS Of , For the first time , If the boss has a better way to help me ...
First we need to prepare a picture
For example, the giant attacking ( Three dai li ) Baidu's picture
 The picture is found on the Internet
Then open our PS application , Open this picture
 Insert picture description here
Choose this wand , Click on the blank space on the keyboard delete
The effect is as follows
 Insert picture description here
Then we can get a picture with transparent background
You can adjust the size , Adjust as you want, just OK









The second step

Let's start writing code now , It's a mess , Forgive me , There's a lot of code for moving links directly , If you want to learn, you can go to the original link
The main function

// Create a JFrame
JFrame frame=new JFrame(" Desktop pets ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// The frame is transparent 
frame.setUndecorated(true);// Cancel the title bar 
frame.setBackground(new Color(0,0,0,0));// The background is transparent 
// Add tray icon 
setTray(frame);
// Set the position and display it on the front end 
frame.setAlwaysOnTop(true);
// Set the cancel form taskbar icon 
frame.setType(JFrame.Type.UTILITY);
frame.setVisible(true);
frame.setSize(200,200);
frame.setLocation(1670,20);
// The elapsed time 
jishiqi();
// Global position variables , For mouse position on the window 
Point origin = new Point();
// Add icon information 
JLabel label=new JLabel();
ImageIcon icon=new ImageIcon("tupian\\ Three dai li .png");
int chongwuwidth=icon.getIconWidth();
int chongwuheight=icon.getIconHeight();
icon.setImage(icon.getImage().getScaledInstance(chongwuwidth, chongwuheight, Image.SCALE_AREA_AVERAGING));
label.setBounds(frame.getX(), frame.getY(), chongwuwidth, chongwuheight);
label.setIcon(icon);
frame.add(label);
// Add a form listener 
frame.addMouseListener(new MouseAdapter() {

// Press down (mousePressed It's not a click , Instead, the mouse is pressed and not raised )
public void mousePressed(MouseEvent e)
{

origin.x=e.getX();
origin.y=e.getY();
}
public void mouseClicked(MouseEvent e)
{

if(e.getButton()==MouseEvent.BUTTON3)
setable(frame,e.getComponent(),e.getX(),e.getY(),label);
}
});
frame.addMouseMotionListener(new MouseMotionListener() {

@Override
public void mouseMoved(MouseEvent e) {

// TODO Automatically generated method stubs 
}
@Override
public void mouseDragged(MouseEvent e) {

Point p=frame.getLocation();
// The current position of the window + The current position of the mouse in the window - It's in the window when the mouse is down 
frame.setLocation(p.x+e.getX()-origin.x,p.y+e.getY()-origin.y);
}
});
frame.validate();
}

Tray , You can put your desktop icon in the tray icon
 Insert picture description here
The code is as follows

private static void setTray(JFrame frame)
{

if(SystemTray.isSupported())// Determine whether the system supports the system tray 
{

SystemTray tray=SystemTray.getSystemTray();// Get the tray of the current system 
// Add a right-click pop-up menu to the tray 
PopupMenu menu=new PopupMenu();
MenuItem openItem=new MenuItem(" open ");
openItem.addActionListener(e->frame.setVisible(true));
MenuItem closeItem=new MenuItem(" To minimize the ");
closeItem.addActionListener(e->frame.setVisible(false));
MenuItem exItem=new MenuItem(" sign out ");
exItem.addActionListener(e->System.exit(0));
menu.add(openItem);
menu.addSeparator();
menu.add(closeItem);
menu.addSeparator();
menu.add(exItem);
ImageIcon icon=new ImageIcon("tupian\\ lovely .png");
Image image=icon.getImage().getScaledInstance(icon.getIconWidth(), icon.getIconHeight(),Image.SCALE_DEFAULT);
TrayIcon trayIcon=new TrayIcon(image," Desktop pets ", menu);
trayIcon.setImageAutoSize(true);// Adaptive size * Very important 
try {

tray.add(trayIcon);
} catch (AWTException e1) {

// TODO Automatically generated catch block 
e1.printStackTrace();
}
}
}

timer , You can count your total online time
 Insert picture description here
The code is as follows

public static void jishiqi()
{

int time=60000;
ActionListener jishi=new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

try {

RandomAccessFile raf=new RandomAccessFile("time.txt","rw");
int times=Integer.parseInt(raf.readLine())+1;
raf.seek(0);
raf.write((times+"").getBytes());
raf.close();
} catch (NumberFormatException | IOException e1) {

// TODO Automatically generated catch block 
e1.printStackTrace();
}
}
};
new javax.swing.Timer(time,jishi).start();
}

Pop up menu , When you right-click on a pet , It's going to be about pet attributes , Pet settings , Pet selection, etc
 Insert picture description here
The code is as follows

public static void setable(JFrame frame,Component b,int x,int y,JLabel label)
{

JPopupMenu menu=new JPopupMenu();
JMenuItem shuxingItem=new JMenuItem(" Pet settings ");
shuxingItem.addActionListener(e->set());
JMenuItem selectItem=new JMenuItem(" Pet attributes ");
selectItem.addActionListener(e->{

try {

able();
} catch (IOException e1) {

// TODO Automatically generated catch block 
e1.printStackTrace();
}
});
JMenuItem setItem=new JMenuItem(" Pet choice ");
setItem.addActionListener(e->select(frame,label));
JMenuItem qqItem=new JMenuItem(" Contact the author ");
qqItem.addActionListener(e->JOptionPane.showMessageDialog(null," Why have we met before "," Contact the author ",1));
menu.add(shuxingItem);
menu.addSeparator();
menu.add(selectItem);
menu.addSeparator();
menu.add(setItem);
menu.addSeparator();
menu.add(qqItem);
menu.show(b, x, y);
}

Pet property panel
 Insert picture description here
The code is as follows

public static JFrame able() throws IOException
{

// Pet property variables 
RandomAccessFile raf=new RandomAccessFile("time.txt","rw");
int time=Integer.parseInt(raf.readLine());
raf.close();
int level;
if(time<60)
level=1;
else if(time>=60&&time<=200)
level=2;
else if(time>200&&time<=500)
level=3;
else if(time>500&&time<=100)
level=4;
else
level=5;
BufferedReader bufferedReader=new BufferedReader(new FileReader("name.txt"));
String chongwunameString="";
String zhurennameString="";
String string="";
while((string=bufferedReader.readLine())!=null)
{

chongwunameString=string;
zhurennameString=bufferedReader.readLine();
}
bufferedReader.close();
// Pet property panel 
JFrame frame=new JFrame(" Pet attributes ");
frame.setSize(400,300);
frame.setLocationRelativeTo(null);
frame.setLayout(null);
frame.setResizable(false);
frame.setVisible(true);
JPanel panel=new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT,30,20));
JLabel label=new JLabel(" Pet name : "+chongwunameString);
JLabel label2=new JLabel(" The owner's name : "+zhurennameString);
JLabel label3=new JLabel(" Grade : lv "+level+" ");
JLabel label4=new JLabel(" Cumulative online time : "+time+" minute ");
panel.add(label3);
panel.add(label);
panel.add(label2);
panel.add(label4);
panel.setBounds(0, 0,200, 300);
frame.add(panel);
frame.validate();
return frame;
}

Pet settings panel
 Insert picture description here
The code is as follows

public static JFrame set()
{

// Pet settings panel 
JFrame frame=new JFrame(" Pet settings ");
frame.setSize(250,250);
frame.setLocationRelativeTo(null);
frame.setLayout(null);
frame.setResizable(false);
frame.setVisible(true);
JPanel panel=new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT,5,10));
panel.setBounds(0, 0, 250,150);
JLabel label=new JLabel(" Pet name :");
JLabel label2=new JLabel(" The owner's name :");
JTextField jTextField=new JTextField(20);
JTextField jTextField2=new JTextField(20);
panel.add(label);
panel.add(jTextField);
panel.add(label2);
panel.add(jTextField2);
frame.add(panel);
JButton button=new JButton(" Confirm settings ");
button.setBounds(75, 165, 100,30);
frame.add(button);
button.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

try {

FileWriter fileWriter=new FileWriter("name.txt");
fileWriter.write(jTextField.getText()+"\n");
fileWriter.write(jTextField2.getText());
fileWriter.close();
JOptionPane.showMessageDialog(null," Setup completed "," Set up ",1);
frame.setVisible(false);
} catch (IOException e1) {

// TODO Automatically generated catch block 
e1.printStackTrace();
}
}
});
return frame;
}

Pet selection panel
 Insert picture description here
The code is as follows

public static void select(JFrame frame,JLabel label)
{

// Pet selection panel 
JFrame frame2=new JFrame(" Pet choice ");
frame2.setSize(450,300);
frame2.setLocationRelativeTo(null);
frame2.setLayout(null);
frame2.setResizable(false);
frame2.setVisible(true);
JPanel panel=new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel label2=new JLabel(" Ancient Chinese costume ");
ImageIcon icon=new ImageIcon("tupian\\ Ancient Chinese costume .png");
JLabel label3=new JLabel();
icon.setImage(icon.getImage().getScaledInstance(icon.getIconWidth(),icon.getIconHeight(),Image.SCALE_DEFAULT));
label3.setIcon(icon);
panel.add(label2);
panel.add(label3);
panel.setBounds(0, 0, 200,190);
frame2.add(panel);
JPanel panel2=new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel nameLabel=new JLabel(" Willett ");
ImageIcon icon2=new ImageIcon("tupian\\ Human spirits .png");
JLabel iconLabel=new JLabel();
icon.setImage(icon.getImage().getScaledInstance(icon2.getIconWidth(), icon2.getIconHeight(), Image.SCALE_DEFAULT));
iconLabel.setIcon(icon2);
panel2.add(nameLabel);
panel2.add(iconLabel);
panel2.setBounds(225, 0, 200, 190);
frame2.add(panel2);
ButtonGroup buttonGroup=new ButtonGroup();
JRadioButton gufengbut=new JRadioButton();
JRadioButton jinglingbut=new JRadioButton();
gufengbut.setBounds(85, 200, 40, 40);
jinglingbut.setBounds(325, 200, 40, 40);
buttonGroup.add(jinglingbut);
buttonGroup.add(gufengbut);
frame2.add(gufengbut);
frame2.add(jinglingbut);
Button button=new Button(" choice ");
button.setBounds(160, 230, 100, 40);
frame2.add(button);
button.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

if(gufengbut.isSelected())
{

selectchongwu(frame, label, "tupian\\ smile .png");
frame2.setVisible(false);
}
if(jinglingbut.isSelected())
{

selectchongwu(frame, label, "tupian\\ Willett .png");
frame2.setVisible(false);
}
}
});
frame2.validate();
}

This is the replacement function for pets

public static void selectchongwu(JFrame frame,JLabel label,String nameString)
{

ImageIcon icon=new ImageIcon(nameString);
int chongwuwidth=icon.getIconWidth();
int chongwuheight=icon.getIconHeight();
icon.setImage(icon.getImage().getScaledInstance(chongwuwidth, chongwuheight, Image.SCALE_AREA_AVERAGING));
label.setIcon(icon);
}

The third step

That's to appreciate the beauty of my Sanli
 Insert picture description here
All of these can be changed , I didn't learn very well , I hope I can be taught the problem by the boss
End .


版权声明
本文为[osc_ bjmmswh6]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210121115259704L.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云