Advertisement
Guest User

LauncherPanel

a guest
Mar 29th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.54 KB | None | 0 0
  1. package fr.Sylou_.DBDO.launcher;
  2.  
  3. import static fr.theshark34.swinger.Swinger.drawFullsizedImage;
  4. import static fr.theshark34.swinger.Swinger.getResource;
  5. import static fr.theshark34.swinger.Swinger.getTransparentWhite;
  6.  
  7. import java.awt.Color;
  8. import java.awt.Graphics;
  9. import java.awt.Image;
  10. import java.io.File;
  11.  
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JOptionPane;
  15. import javax.swing.JPanel;
  16. import javax.swing.JPasswordField;
  17. import javax.swing.JTextField;
  18.  
  19. import fr.theshark34.openauth.AuthenticationException;
  20. import fr.theshark34.openlauncherlib.util.Saver;
  21. import fr.theshark34.openlauncherlib.util.ramselector.RamSelector;
  22. import fr.theshark34.swinger.colored.SColoredBar;
  23. import fr.theshark34.swinger.event.SwingerEvent;
  24. import fr.theshark34.swinger.event.SwingerEventListener;
  25. import fr.theshark34.swinger.textured.STexturedButton;
  26.  
  27. public class LauncherPanel extends JPanel implements SwingerEventListener
  28. {
  29.     private Image background = getResource("background.png");
  30.    
  31.     private Saver saver = new Saver(new File(Launcher.DBDO_DIR, "launcher.properties"));
  32.    
  33.     private JTextField usernameField = new JTextField(saver.get("username"));
  34.     private JPasswordField passwordField = new JPasswordField();
  35.    
  36.     private STexturedButton playButton = new STexturedButton(getResource("play.png"));
  37.     private STexturedButton quitButton = new STexturedButton(getResource("quit.png"));
  38.     private STexturedButton hideButton = new STexturedButton(getResource("hide.png"));
  39.    
  40.     private SColoredBar progressBar = new SColoredBar(getTransparentWhite(100), getTransparentWhite(175));
  41.     private JLabel infoLabel = new JLabel("Clique sur Jouer !", 0);
  42.    
  43.     private RamSelector ramSelector = new RamSelector(new File(Launcher.DBDO_DIR, "ram.txt"));
  44.    
  45.     public LauncherPanel()
  46.     {
  47.         setLayout(null);
  48.        
  49.         usernameField.setFont(usernameField.getFont().deriveFont(17F));
  50.         usernameField.setOpaque(false);
  51.         usernameField.setBorder(null);
  52.         usernameField.setBounds(360, 232, 254, 19);
  53.         this.add(usernameField);
  54.        
  55.         passwordField.setFont(usernameField.getFont());
  56.         passwordField.setOpaque(false);
  57.         passwordField.setBorder(null);
  58.         passwordField.setBounds(360, 286, 254, 20);
  59.         this.add(passwordField);
  60.        
  61.         playButton.setBounds(402, 337);
  62.         playButton.addEventListener(this);
  63.         this.add(playButton);
  64.        
  65.         quitButton.setBounds(932, 9);
  66.         quitButton.addEventListener(this);
  67.         this.add(quitButton);
  68.        
  69.         hideButton.setBounds(891, 9);
  70.         hideButton.addEventListener(this);
  71.         this.add(hideButton);
  72.        
  73.         progressBar.setBounds(239, 480, 497, 40);
  74.         this.add(progressBar);
  75.        
  76.         infoLabel.setForeground(Color.WHITE);
  77.         infoLabel.setFont(usernameField.getFont());
  78.         infoLabel.setBounds(239, 430, 497, 60);
  79.         this.add(infoLabel);
  80.     }
  81.    
  82.     @Override
  83.     public void onEvent(SwingerEvent e) {
  84.         if(e.getSource() == playButton) {
  85.             setFieldsEnabled(false);
  86.            
  87.             if(usernameField.getText().replaceAll(" ", "").length() == 0 || passwordField.getText().length() == 0) {
  88.                 JOptionPane.showMessageDialog(this, "Erreur, veuillez entrer un pseudo et un mot de passe valide.", "Erreur", JOptionPane.ERROR_MESSAGE);
  89.                 setFieldsEnabled(true);
  90.                 return;
  91.             }
  92.            
  93.             Thread t = new Thread() {
  94.                 @Override
  95.                 public void run() {
  96.                     try {
  97.                     Launcher.auth(usernameField.getText(), passwordField.getText());
  98.                     }catch(AuthenticationException e) {
  99.                         JOptionPane.showMessageDialog(LauncherPanel.this, "Erreur, impossible de se connecter : " + e.getErrorModel().getErrorMessage(), "Erreur", JOptionPane.ERROR_MESSAGE);
  100.                         setFieldsEnabled(true);
  101.                         return;
  102.                     }
  103.                    
  104.                     try {
  105.                         Launcher.update();
  106.                         }catch(Exception e) {
  107.                             Launcher.interruptThread();
  108.                             JOptionPane.showMessageDialog(LauncherPanel.this, "Erreur, impossible de mettre le jeu à jour : " + e, "Erreur", JOptionPane.ERROR_MESSAGE);
  109.                             setFieldsEnabled(true);
  110.                             return;
  111.                     }
  112.                 }
  113.             };
  114.             t.start();
  115.         } else if(e.getSource() == quitButton)
  116.             System.exit(0);
  117.         else if(e.getSource() == hideButton)
  118.             LauncherFrame.getInstance().setState(JFrame.ICONIFIED);
  119.     }
  120.    
  121.     @Override
  122.     public void paintComponent(Graphics graphics) {
  123.         super.paintComponent(graphics);
  124.         drawFullsizedImage(graphics, this, background);
  125.     }
  126.    
  127.     private void setFieldsEnabled(boolean enabled) {
  128.         usernameField.setEnabled(enabled);
  129.         passwordField.setEnabled(enabled);
  130.         playButton.setEnabled(enabled);
  131.     }
  132.    
  133.     public SColoredBar getProgressBar() {
  134.         return progressBar;
  135.     }
  136.    
  137.     public void setInfoText(String text) {
  138.         infoLabel.setText(text);
  139.     }
  140.    
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement