Select Git revision
StartScreen.java 5.16 KiB
package GUI;
import client.Client;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
public class StartScreen {
private JTextPane information;
private JPanel panel1;
private JTextField usernameField;
private JButton joinButton;
private JTextField textField2;
private boolean kickedOut;
private boolean appTerminated;
private JTextField serverField;
private JTextField textField3;
private JPasswordField passwordField;
JFrame frame;
private Client client;
public StartScreen(Client client)
{
this.client = client;
}
ActionListener actionListener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == joinButton)
{
String serverAddress = serverField.getText();
String userName = usernameField.getText();
String password = new String(passwordField.getPassword());
if (userName.length() <= 15) {
int connectionStatus = client.connect(userName, serverAddress, password);
if( connectionStatus == 1 )
{
frame.setVisible(false);
frame.dispose();
if (client.getApplicationMain().getFrame() == null) {
client.startApplication();
}
else {
client.clearChat();
client.clearDrawingArea();
client.setVisibleApplication();
}
}
else if( connectionStatus == 2 || connectionStatus == 6 )
{
showErrorMessage("Duplicate usernameField: Please enter a new usernameField");
}
else if( connectionStatus == 3 )
{
showErrorMessage("Cannot connect to server: Please check the server address");
}
else if( connectionStatus == 4 )
{
showErrorMessage("Incorrect Password");
}
else if( connectionStatus == 5 )
{
frame.setVisible(false);
frame.dispose();
client.setVisibleApplication();
}
else
{
showErrorMessage("Unknown Connection Status");
}
}
else {
showErrorMessage("Username must be less or equal to 15 characters");
}
}
}
};
public static void showErrorMessage(String message)
{
JOptionPane.showMessageDialog(null,
message, "Error", JOptionPane.ERROR_MESSAGE);
}
public void go()
{
joinButton.addActionListener(actionListener);
frame = new JFrame("StartScreen");
frame.setContentPane(panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.getRootPane().setDefaultButton(joinButton);
joinButton.requestFocus();
//frame.getContentPane().add(new JPanelWithBackground("sample.jpeg"));
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
if (isAppTerminated()){
showKickAllMessage();
setAppTerminated(false);
}
}
});
}
public void setVisible(){
frame.setVisible(true);
}
public void setKickedOut(boolean kickedOut) {
this.kickedOut = kickedOut;
}
public void setAppTerminated(boolean appTerminated) {
this.appTerminated = appTerminated;
}
public boolean isKickedOut() {
return kickedOut;
}
public boolean isAppTerminated() {
return appTerminated;
}
public void showKickAllMessage() {
JOptionPane.showMessageDialog(null,
"The manager terminated the application", "Application terminated",
JOptionPane.ERROR_MESSAGE);
}
// public class JPanelWithBackground extends JPanel
// {
//
// private Image backgroundImage;
//
// // Some code to initialize the background image.
// // Here, we use the constructor to load the image. This
// // can vary depending on the use case of the panel.
// public JPanelWithBackground(String fileName) throws IOException
// {
// backgroundImage = ImageIO.read(new File(fileName));
// }
//
// public void paintComponent(Graphics g) {
// super.paintComponent(g);
//
// // Draw the background image.
// g.drawImage(backgroundImage, 0, 0, this);
// }
// }
}