优化资源文件获取方式,为打包应用程序包做准备

This commit is contained in:
yang 2022-10-09 21:42:23 +08:00
parent 56b69cb95c
commit 6904b50e2e
8 changed files with 61 additions and 18 deletions

View File

@ -6,7 +6,7 @@ plugins {
}
group 'org.rococy'
version '1.0-SNAPSHOT'
version '1.0'
repositories {
mavenCentral()
@ -46,11 +46,19 @@ test {
useJUnitPlatform()
}
jar {
manifest {
attributes "Implementation-Title": project.name
attributes "Implementation-Version": '1.0'
attributes 'Main-Class': 'org.rococy.roomit.MainWindow'
}
}
jlink {
imageZip = project.file("${buildDir}/distributions/app-${javafx.platform.classifier}.zip") as RegularFile
options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
launcher {
name = 'app'
name = 'Roomit'
}
}

View File

@ -14,13 +14,12 @@ import org.rococy.roomit.domain.KeyBoardConfiguration;
import org.rococy.roomit.listener.TrayIconMouseListener;
import org.rococy.roomit.listener.WindowGlobalKeyBoardListener;
import org.rococy.roomit.util.KeyBoardUtils;
import org.rococy.roomit.util.ResourceUtils;
import org.rococy.roomit.util.ScreenUtils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.IOException;
import java.net.URL;
import java.util.Objects;
/**
* @author Rococy
@ -59,7 +58,7 @@ public class MainWindow extends Application {
try {
MainWindow.stage = stage;
// 加载FXML文件
FXMLLoader fxmlLoader = new FXMLLoader(MainWindow.class.getResource("fxml/mainWindow.fxml"));
FXMLLoader fxmlLoader = new FXMLLoader(ResourceUtils.getResource("fxml/mainWindow.fxml"));
Scene scene = new Scene(fxmlLoader.load(), ScreenUtils.getScreenWidth(), ScreenUtils.getScreenHeight());
// 场景背景透明
scene.setFill(Paint.valueOf("#00000000"));
@ -98,8 +97,7 @@ public class MainWindow extends Application {
// 获取系统托盘
SystemTray systemTray = SystemTray.getSystemTray();
// 加载托盘图片 16*16的png图片
URL imageUrl = Objects.requireNonNull(MainWindow.class.getResource(GlobalConsts.LOGO_PATH));
TrayIcon trayIcon = new TrayIcon(ImageIO.read(imageUrl), "RoomIt");
TrayIcon trayIcon = new TrayIcon(ImageIO.read(ResourceUtils.getResource(GlobalConsts.LOGO_PATH)), "RoomIt");
// 托盘图片自适应大小
trayIcon.setImageAutoSize(true);
trayIcon.addMouseListener(new TrayIconMouseListener());

View File

@ -8,6 +8,7 @@ import javafx.scene.paint.Paint;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.rococy.roomit.constant.GlobalConsts;
import org.rococy.roomit.util.ResourceUtils;
import java.io.IOException;
@ -21,7 +22,7 @@ public class SettingWindow extends Application {
{
// 设置标题
stage.setTitle("Roomit-设置窗口");
stage.setTitle("RoomIt-设置窗口");
// 居中
stage.centerOnScreen();
// 只有一个x的任务栏
@ -34,14 +35,14 @@ public class SettingWindow extends Application {
@Override
public void start(Stage stage) throws IOException {
// 加载FXML文件
FXMLLoader fxmlLoader = new FXMLLoader(SettingWindow.class.getResource("fxml/settingWindow.fxml"));
FXMLLoader fxmlLoader = new FXMLLoader(ResourceUtils.getResource("fxml/settingWindow.fxml"));
Scene scene = new Scene(fxmlLoader.load());
// 场景填充透明背景
scene.setFill(Paint.valueOf("#00000000"));
// 设置场景
stage.setScene(scene);
// 设置图标
stage.getIcons().add(new Image(GlobalConsts.LOGO_PATH));
stage.getIcons().add(new Image(ResourceUtils.getResourceAsStream(GlobalConsts.LOGO_PATH)));
// 将窗口显示出来
stage.show();
}

View File

@ -10,6 +10,7 @@ import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import org.rococy.roomit.constant.GlobalConsts;
import org.rococy.roomit.util.ResourceUtils;
import java.io.IOException;
@ -35,21 +36,21 @@ public class ToolsWindow extends Application {
{
try {
// 加载FXML文件
FXMLLoader fxmlLoader = new FXMLLoader(SettingWindow.class.getResource("fxml/toolsWindow.fxml"));
FXMLLoader fxmlLoader = new FXMLLoader(ResourceUtils.getResource("fxml/toolsWindow.fxml"));
Scene scene = new Scene(fxmlLoader.load());
// 透明背景
scene.setFill(Paint.valueOf("#00000000"));
// 设置场景
stage.setScene(scene);
// 设置标题
stage.setTitle("Roomit-工具箱");
stage.setTitle("RoomIt-工具箱");
// 居中
stage.centerOnScreen();
// 只有一个x的任务栏
stage.setResizable(false);
stage.initModality(Modality.APPLICATION_MODAL);
// 设置图标
stage.getIcons().add(new Image(GlobalConsts.LOGO_PATH));
stage.getIcons().add(new Image(ResourceUtils.getResourceAsStream(GlobalConsts.LOGO_PATH)));
// 置顶
stage.setAlwaysOnTop(true);
} catch (IOException e) {

View File

@ -7,6 +7,7 @@ import javafx.scene.paint.Paint;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.rococy.roomit.util.ResourceUtils;
import java.io.IOException;
@ -23,7 +24,7 @@ public class TrayWindow extends Application {
{
try {
// 加载FXML文件
FXMLLoader fxmlLoader = new FXMLLoader(TrayWindow.class.getResource("fxml/trayWindow.fxml"));
FXMLLoader fxmlLoader = new FXMLLoader(ResourceUtils.getResource("fxml/trayWindow.fxml"));
Scene scene = new Scene(fxmlLoader.load());
// 设置场景

View File

@ -4,6 +4,7 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.rococy.roomit.constant.GlobalConsts;
import org.rococy.roomit.domain.*;
import java.io.IOException;
@ -23,7 +24,6 @@ public class ConfigurationManager {
private static ShapeConfiguration shapeConfiguration;
private static KeyBoardConfiguration keyBoardConfiguration;
private static final String CONFIGURATION_FILE_URL = System.getProperty("user.dir") + "\\src\\main\\resources\\org\\rococy\\roomit\\config.json";
static {
initConfiguration(readConfigFileContent());
@ -35,7 +35,7 @@ public class ConfigurationManager {
configMap.put("keyboard", keyBoardConfiguration.getKeyBoardMap());
try {
Files.writeString(Path.of(CONFIGURATION_FILE_URL), gson.toJson(configMap));
Files.writeString(Path.of(GlobalConsts.CONFIGURATION_FILE_URL), gson.toJson(configMap));
} catch (IOException e) {
e.printStackTrace();
}
@ -79,7 +79,7 @@ public class ConfigurationManager {
private static String readConfigFileContent() {
try {
return Files.readString(Path.of(CONFIGURATION_FILE_URL), StandardCharsets.UTF_8);
return Files.readString(Path.of(GlobalConsts.CONFIGURATION_FILE_URL), StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}

View File

@ -14,6 +14,12 @@ public class GlobalConsts {
/**
* logo图标路径
*/
public static final String LOGO_PATH = "/org/rococy/roomit/img/logo24x24.png";
public static final String LOGO_PATH = "img/logo24x24.png";
/**
* 配置文件路径
* TODO 完成配置文件路径 打包进image文件夹与bin目录同级
*/
public static final String CONFIGURATION_FILE_URL = "D:\\background\\IdeaProject\\RoomIt\\src\\main\\resources\\org\\rococy\\roomit\\config.json";
}

View File

@ -0,0 +1,28 @@
package org.rococy.roomit.util;
import org.rococy.roomit.MainWindow;
import java.io.InputStream;
import java.net.URL;
/**
* @author Rococy
* @date 2022/10/9
*/
public class ResourceUtils {
private static final Class<MainWindow> MAIN_WINDOW_CLASS = MainWindow.class;
private ResourceUtils() {
}
public static URL getResource(String name) {
return MAIN_WINDOW_CLASS.getResource(name);
}
public static InputStream getResourceAsStream(String name) {
return MAIN_WINDOW_CLASS.getResourceAsStream(name);
}
}