美术资源源于网络,仅供学习,侵删
作者只学了3星期的java,大佬们可能会觉得我写的很差,望指正
Demo.java
import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.util.concurrent.atomic.AtomicBoolean;
public class Demo {
public static int Screen_Width;
public static int Screen_Height;
private static void getScreen() {
//工作区域分辨率
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle rectangle = graphicsEnvironment.getMaximumWindowBounds();
//System.out.println(rectangle.height);
//System.out.println(rectangle.width);
Screen_Width = rectangle.width;
Screen_Height = rectangle.height;
}
private static void setTray(JFrame jFrame,String name) {
//设置系统托盘
if (SystemTray.isSupported()) {// 判断系统是否支持系统托盘
AtomicBoolean picture = new AtomicBoolean(true);
SystemTray tray = SystemTray.getSystemTray(); // 获取当前系统的托盘
// 为托盘添加一个右键弹出菜单
PopupMenu popMenu = new PopupMenu();
MenuItem itemOpen = new MenuItem("open");
itemOpen.addActionListener(e -> jFrame.setVisible(true));
MenuItem itemHide = new MenuItem("Hide");
itemHide.addActionListener(e -> jFrame.setVisible(false));
/*
MenuItem itemClose = new MenuItem("close");
itemClose.addActionListener(e -> jFrame.removeAll());
*/
MenuItem itemExit = new MenuItem("exit");
itemExit.addActionListener(e -> System.exit(0));
popMenu.add(itemOpen);
popMenu.add(itemHide);
//popMenu.add(itemClose);
popMenu.add(itemExit);
// 设置托盘图标
URL img_icon = Demo.class.getResource("res/icon/icon.png");
ImageIcon icon = new ImageIcon(img_icon);
Image image = icon.getImage().getScaledInstance(icon.getIconWidth(), icon.getIconHeight(), Image.SCALE_DEFAULT);
//TrayIcon trayIcon = new TrayIcon(image, "Desktop Pet Creeper", popMenu);
TrayIcon trayIcon = new TrayIcon(image,name,popMenu);
trayIcon.setImageAutoSize(true); // 自适应尺寸,这个属性至关重要
try {
tray.add(trayIcon);
} catch (AWTException e1) {
e1.printStackTrace();
}
}
}
public static void Birth(String name) {
JFrame jFrame = new JFrame();
JPanel jPanel = new JPanel();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*
设置窗体透明和关闭标题
*/
jFrame.setUndecorated(true);
jFrame.setBackground(new Color(0,0,0,0));
jPanel.setBackground(new Color(0,0,0,0));
jFrame.setType(JFrame.Type.UTILITY);
jFrame.setBounds(0,0,0,0);
jFrame.setAlwaysOnTop(true);
setTray(jFrame,name);
jFrame.setVisible(true);
jFrame.add(jPanel);
getScreen();//更新屏幕分辨率数值
/*
Creeper creeper = new Creeper();
creeper.setForm(jFrame,jPanel);
System.out.println(Screen_Width);
creeper.setScreen(Screen_Width,Screen_Height);
creeper.free_run();
*/
DeskCreeper deskCreeper = new DeskCreeper();
deskCreeper.setForm(jFrame,jPanel);
deskCreeper.setScreen(Screen_Width,Screen_Height);
deskCreeper.free_run();
}
public static void main(String[] args) {
Birth("creeper");
}
}
上面这部分应该很好理解吧,就设置了关于窗体的属性,以及实例化一个桌宠对象(DeskCreeper),桌宠的属性我会在下面放出来
Image_Draw.java
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class Image_Draw {
public static JLabel loadpicture(String img_place) {
/*
传入图片位置,返回配置好的JLabel控件
*/
JLabel jLabel = new JLabel();
URL img_url = Demo.class.getResource(img_place);
ImageIcon icon = new ImageIcon(img_url);
icon.setImage(icon.getImage().getScaledInstance(icon.getIconWidth(),icon.getIconHeight(), Image.SCALE_DEFAULT));
jLabel.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight());
jLabel.setIcon(icon);
return jLabel;
}
public static void draw_replace(JFrame jFrame,JPanel jPanel,String img_place,int x,int y) {
/*
初始化并开始播放下一张图片
*/
try {
jPanel.removeAll(); //删除子窗体中的所有控件
JLabel jLabel = loadpicture(img_place);
jPanel.add(jLabel); //添加控件
jFrame.setLocation(x,y);
jFrame.repaint();
jFrame.pack();
}catch (Exception e) {
e.printStackTrace();
}
}
}
用这个组件来通过更换窗体中控件达到更换图片的效果(有点绕啊)
剩下的DeskCreeper.java有点大,我复制过来就会特别卡,所以我放在下部中写出来
我先放效果图
dump
walk
turn
climb
down
第一种:
第二种
好吧,可能看不出来什么,其实就是从什么面滑下来的动画
上部到这就结束了,距离开学还剩2.5天,开心
java JFrame 下部
时间:2020/5/8