Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import net.minecraft.client.Minecraft;
- import net.minecraft.client.main.Main;
- import javax.swing.*;
- import java.awt.*;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.security.MessageDigest;
- public class InfinityOriginLogin extends JFrame {
- public static boolean successful = false;
- private final JTextField usernameField;
- private final JPasswordField passwordField;
- private final JButton loginButton;
- private final String verifyAddress = "https://127.0.0.1:8000";
- public InfinityOriginLogin() {
- setTitle("InfinityOrigin Login");
- setSize(400, 300);
- setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
- setLocationRelativeTo(null);
- JPanel panel = new JPanel();
- panel.setLayout(new GridBagLayout());
- panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
- Font customFont = loadCustomFont(24f);
- if (customFont == null) {
- customFont = new Font("Arial", Font.BOLD, 24);
- }
- JLabel titleLabel = new JLabel("InfinityOrigin");
- titleLabel.setFont(customFont);
- titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
- JLabel usernameLabel = new JLabel("Username:");
- usernameField = new JTextField(15);
- JLabel passwordLabel = new JLabel("Password:");
- passwordField = new JPasswordField(15);
- loginButton = new JButton("Login");
- loginButton.setBackground(new Color(0, 153, 255));
- loginButton.setForeground(Color.WHITE);
- loginButton.setFocusPainted(false);
- loginButton.setFont(new Font("Arial", Font.BOLD, 14));
- loginButton.setPreferredSize(new Dimension(100, 30));
- loginButton.addActionListener(e -> {
- usernameField.setVisible(false);
- passwordField.setVisible(false);
- loginButton.setVisible(false);
- usernameLabel.setVisible(false);
- passwordLabel.setVisible(false);
- showLoadingAnimation();
- });
- GridBagConstraints gbc = new GridBagConstraints();
- gbc.insets = new Insets(10, 10, 10, 10);
- gbc.gridx = 0;
- gbc.gridy = 0;
- gbc.gridwidth = 2;
- gbc.anchor = GridBagConstraints.CENTER;
- panel.add(titleLabel, gbc);
- gbc.gridwidth = 1;
- gbc.gridy = 1;
- panel.add(usernameLabel, gbc);
- gbc.gridx = 1;
- panel.add(usernameField, gbc);
- gbc.gridx = 0;
- gbc.gridy = 2;
- panel.add(passwordLabel, gbc);
- gbc.gridx = 1;
- panel.add(passwordField, gbc);
- gbc.gridx = 0;
- gbc.gridy = 3;
- gbc.gridwidth = 2;
- panel.add(loginButton, gbc);
- add(panel);
- setVisible(true);
- }
- @SuppressWarnings("InfiniteLoopStatement")
- private void showLoadingAnimation() {
- InputStream gifStream = getClass().getResourceAsStream("/loading.gif");
- if (gifStream == null) {
- JOptionPane.showMessageDialog(this, "GIF 文件未找到", "错误", JOptionPane.ERROR_MESSAGE);
- return;
- }
- Image loadingImage = new ImageIcon("loading.gif").getImage();
- ImageIcon loadingGif = new ImageIcon(loadingImage.getScaledInstance(100, 100, Image.SCALE_DEFAULT));
- JLabel loadingGifLabel = new JLabel(loadingGif);
- loadingGifLabel.setHorizontalAlignment(SwingConstants.CENTER);
- Font customFont = loadCustomFont(18f);
- if (customFont == null) {
- customFont = new Font("Arial", Font.BOLD, 18);
- }
- JLabel loadingLabel = new JLabel("加载中,请稍等...");
- loadingLabel.setFont(customFont);
- loadingLabel.setHorizontalAlignment(SwingConstants.CENTER);
- JPanel loadingPanel = new JPanel(new BorderLayout());
- loadingPanel.add(loadingGifLabel, BorderLayout.CENTER);
- loadingPanel.add(loadingLabel, BorderLayout.SOUTH);
- getContentPane().removeAll();
- getContentPane().add(loadingPanel);
- revalidate();
- repaint();
- new SwingWorker<Void, Void>() {
- @Override
- protected Void doInBackground() throws Exception {
- Thread.sleep(300);
- return null;
- }
- @Override
- protected void done() {
- String username = usernameField.getText();
- String password = new String(passwordField.getPassword());
- if (!isStatus200()){
- JOptionPane.showMessageDialog(InfinityOriginLogin.this, "服务器异常,请联系腐竹", "错误", JOptionPane.ERROR_MESSAGE);
- }
- if (isSuccessfulLogin(username, password)) {
- JOptionPane.showMessageDialog(InfinityOriginLogin.this, "登录成功!");
- getContentPane().removeAll();
- getContentPane().add(new JPanel());
- revalidate();
- repaint();
- setVisible(false);
- dispose();
- successful = true;
- } else {
- JOptionPane.showMessageDialog(InfinityOriginLogin.this, "用户名或密码错误", "错误", JOptionPane.ERROR_MESSAGE);
- System.err.println("InfinityOrigin: Error login.");
- getContentPane().removeAll();
- getContentPane().add(new JPanel());
- revalidate();
- repaint();
- setVisible(false);
- dispose();
- }
- }
- }.execute();
- }
- private Font loadCustomFont(float size) {
- try (InputStream fontStream = getClass().getResourceAsStream("/" + "MCAE.ttf")) {
- if (fontStream == null) {
- System.err.println("字体文件未找到: " + "MCAE.ttf");
- return null;
- }
- Font font = Font.createFont(Font.TRUETYPE_FONT, fontStream);
- return font.deriveFont(size);
- } catch (Exception e) {
- System.err.println("加载字体失败: " + e.getMessage());
- return null;
- }
- }
- public static void start() {
- SwingUtilities.invokeLater(InfinityOriginLogin::new);
- }
-
- public boolean isSuccessfulLogin(String username, String password) {
- boolean value = false;
- try {
- String urlString = verifyAddress+"/login?username=" + username + "&password=" + password;
- URL url = new URL(urlString);
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("GET");
- BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- String inputLine;
- StringBuilder response = new StringBuilder();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- in.close();
- if (response.toString().equals("true")) {
- value = true;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return value;
- }
- public boolean isStatus200() {
- try {
- URL url = new URL(verifyAddress+"/status");
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("GET");
- connection.setConnectTimeout(5000);
- connection.setReadTimeout(5000);
- int responseCode = connection.getResponseCode();
- return responseCode >= 200 && responseCode < 300;
- } catch (IOException e) {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement