0% found this document useful (0 votes)
2 views15 pages

Source Code

The document outlines a semester final project for an Object-Oriented Programming course, detailing the implementation of a chat application named LANCHAT. It includes source code for server and client classes, which facilitate communication between two clients using sockets, as well as the graphical user interface components for the chat interface. The project demonstrates the use of Java for network programming and GUI development.

Uploaded by

a.moiz3gr8
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views15 pages

Source Code

The document outlines a semester final project for an Object-Oriented Programming course, detailing the implementation of a chat application named LANCHAT. It includes source code for server and client classes, which facilitate communication between two clients using sockets, as well as the graphical user interface components for the chat interface. The project demonstrates the use of Java for network programming and GUI development.

Uploaded by

a.moiz3gr8
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OBJECT ORIENTED PROGRAMMING SYSTEM

SEMESTER FINAL PROJECT

LANCHAT

Submitted By:

TALHA EJAZ
MUHAMMAD ABDUL MOIZ
SOURCE CODE:
MyServer Class:
package LANCHATINTERFACE;

import [Link].*;
import [Link].*;

public class MyServer {


private static Socket client1 = null;
private static Socket client2 = null;

public static void main(String[] args) {


final int PORT = 12345;

try (ServerSocket serverSocket = new ServerSocket(PORT)) {


[Link]("Server started. Waiting for clients...");

client1 = [Link]();
[Link]("Client 1 connected.");
client2 = [Link]();
[Link]("Client 2 connected.");

// Start threads for relaying messages between clients


new RelayHandler(client1, client2).start();
new RelayHandler(client2, client1).start();

} catch (IOException e) {
[Link]();
}
}
}

class RelayHandler extends Thread {


private Socket fromClient;
private Socket toClient;

public RelayHandler(Socket from, Socket to) {


[Link] = from;
[Link] = to;
}

public void run() {


try (
BufferedReader in = new BufferedReader(new
InputStreamReader([Link]()));
PrintWriter out = new PrintWriter([Link](), true);
) {
String message;
while ((message = [Link]()) != null) {
[Link](message);
}
} catch (IOException e) {
[Link]("Connection lost between clients.");
}
}
}

MyClient Class:
package LANCHATINTERFACE;

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link].*;
import [Link].*;

public class MyClient {


public static String text;
public static String msg;
public static boolean dataRecieve = false;

public static void main(String[] args){


final String HOST = "localhost";
final int PORT = 12345;
Thread t1 = new Thread(new Runnable(){
public void run(){
new Myframe();
}
});

Thread t2 = new Thread(new Runnable(){


public void run(){
try (
Socket socket = new Socket(HOST, PORT);
BufferedReader serverIn = new BufferedReader(new
InputStreamReader([Link]()));
PrintWriter serverOut = new
PrintWriter([Link](), true);
) {
[Link]("Connected to server. Start chatting!");

// Thread to read messages from server


new Thread(() -> {
//String msg;
try {
while (([Link] = [Link]()) != null)
{
[Link] = true;
//[Link] = new String(msg);
[Link]([Link]);
}
} catch (IOException e) {
[Link]("Disconnected from server.");
}
}).start();

// Main thread writes user input to server


String userInput;
while ((userInput = [Link]()) != null)
{
if(TextPanel.click1){
[Link](userInput);
TextPanel.click1 = false;
}
}

} catch (IOException e) {
[Link]();
}
}
});

JLabel label = new JLabel();


[Link](null);
ImageIcon image = new ImageIcon("[Link]");
Image newImage = [Link]().getScaledInstance(800, 500,
Image.SCALE_SMOOTH);
ImageIcon icon = new ImageIcon("[Link]");
[Link](new ImageIcon(newImage));

JFrame openingFrame = new JFrame();


[Link](800, 500);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](null);
[Link]([Link]());
[Link]("LANCHAT");
[Link](new Color(25, 22, 69));
[Link](label,[Link]);
[Link](true);

Timer timer = new Timer(1000,new ActionListener() {


public void actionPerformed(ActionEvent e){
[Link]();
[Link]();
[Link]();
}
});
[Link](false);
[Link]();
}
}

MyFrame Class:
package LANCHATINTERFACE;

import [Link].*;
import [Link].*;

public class Myframe extends JFrame {


JButton [] frameButton = new JButton[3];
ImageIcon[] originalIcon = new ImageIcon[3];
Image[] setImages = new Image[3];
Image [] resizingImage = new Image[3];
ImageIcon[] resizedImage = new ImageIcon[3];

final Point[] initialPosition = {null};

public static Boolean flag = false;


JPanel titleBar1 = new JPanel();
TitleBar titleBar2 = new TitleBar();
JPanel mainTitlePanel = new JPanel();
TitleLabel titleLabel = new TitleLabel();

MainPanel mainPanel = new MainPanel();

ImageIcon icon = new ImageIcon("[Link]");

Myframe(){
[Link](JFrame.EXIT_ON_CLOSE);
[Link]("LANCHAT");
[Link](new Color(25, 22, 69));
[Link]([Link]());
[Link](new BorderLayout());
[Link](800,500);
[Link](mainPanel,[Link]);
[Link](null);
[Link](true);
[Link](true);
}
}

MainPanel Class:
package LANCHATINTERFACE;

import [Link];
import [Link];
import [Link];
import [Link];

public class MainPanel extends JPanel {


TextPanel textPanel = new TextPanel();
SidePanel sidePanel = new SidePanel();
MainPanel(){

[Link]([Link]);
[Link](new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();

[Link] = 0;
[Link] = 0;
[Link] = 1;
[Link] = 1;
[Link] = [Link];

[Link](sidePanel,gbc);

[Link] = 1;
[Link] = 3;

[Link](textPanel,gbc);
}
}

SidePanel Class:
package LANCHATINTERFACE;

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class SidePanel extends JPanel{


JButton userButton1 = new JButton("User 1");
JButton userButton2 = new JButton("User 2");
JButton userButton3 = new JButton("User 3");

JPanel lanChat = new JPanel();


JPanel onlineUsers = new JPanel();
JPanel users = new JPanel();

JLabel lanChatLabel = new JLabel();


JLabel onlineUsersLabel = new JLabel();

SidePanel(){
[Link](new Color(72,60,50));
[Link](new BoxLayout(this, BoxLayout.Y_AXIS));

[Link](new Dimension(300,50));
[Link](false);
[Link](new Font("Sans Serif",[Link],20));
[Link]([Link]);
[Link](new FlowLayout([Link]));
[Link](new Color(67,198,172));
[Link](false);

[Link](new Dimension(300,50));
[Link](false);
[Link](new Font("Sans Serif",[Link],20));
[Link]([Link]);
[Link](new FlowLayout([Link]));
[Link](new Color(67,198,172));
[Link](false);

[Link](new Dimension(300,50));
[Link](false);
[Link](new Font("Sans Serif",[Link],20));
[Link]([Link]);
[Link](new FlowLayout([Link]));
[Link](new Color(67,198,172));
[Link](false);

[Link](new Color(25, 22, 69));


[Link](new Dimension(150,80));

[Link](new Color(25, 22, 69));


[Link](new FlowLayout([Link]));
[Link](new Dimension(100,30));

[Link](new Color(25, 22, 69));


[Link](userButton1);
[Link](userButton2);
[Link](userButton3);
[Link](new Dimension(200,300));

[Link]("LAN CHAT");
[Link](new Color(25, 22, 69));
[Link](true);
[Link](new Font("Sans Serif",[Link],25));
[Link]([Link]);

[Link](" ONLINE USERS");


[Link](new Color(25, 22, 69));
[Link](true);
[Link](new Font("Sans Serif",[Link],20));
[Link]([Link]);

[Link](lanChatLabel);
[Link](onlineUsersLabel);

[Link](lanChat);
[Link](onlineUsers);
[Link](users);
}
}

TextPanel Class:
package LANCHATINTERFACE;

import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;

public class TextPanel extends JPanel implements


FocusListener,MouseListener,ActionListener,KeyListener{

public static boolean click1 = false;


Box vertical = [Link]();

JPanel panelUp = new JPanel();


JPanel panelMid = new JPanel();
JPanel panelDown = new JPanel();
TextShow textShow = new TextShow();

public static JTextArea messageBar = new JTextArea(3,20);


JLabel sideLabel = new JLabel();
JButton sendButton = new JButton();
ImageIcon originalIcon = new ImageIcon();
Image setImage;
Image resizingImage;
ImageIcon resizedImage = new ImageIcon();

TextPanel(){
[Link](new BorderLayout());

Thread t1 = new Thread (new Runnable() {


public void run(){
Border border1 = [Link](new Color(
192,192,192),2);
Border border2 = [Link]([Link],2);
[Link](new Dimension(5,40));
[Link]([Link]);
[Link](true);
[Link](true);
[Link](false);
[Link](false);
[Link](true);
[Link]("Type a message");
JScrollPane scrollPane = new JScrollPane(messageBar);
[Link](new Dimension(300,35));
[Link](border1);

JScrollBar vScroll = [Link]();

[Link](new [Link]() {
@Override
protected JButton createDecreaseButton(int orientation) {
return createZeroButton(); // Top arrow
}

@Override
protected JButton createIncreaseButton(int orientation) {
return createZeroButton(); // Bottom arrow
}

private JButton createZeroButton() {


JButton button = new JButton();
[Link](new Dimension(0, 0));
[Link](new Dimension(0, 0));
[Link](new Dimension(0, 0));
return button;
}

@Override
protected void configureScrollBarColors() {
[Link] = Color.DARK_GRAY;
[Link] = Color.LIGHT_GRAY;
}
});

[Link](new Dimension(5, Integer.MAX_VALUE));


originalIcon = new ImageIcon("[Link]");
setImage = [Link]();
resizingImage = [Link](38, 38,
Image.SCALE_SMOOTH);
resizedImage = new ImageIcon(resizingImage);

[Link](new Dimension(80,35));
[Link](border2);
[Link]("Send");
[Link](new Font("Sans Serif",[Link],15));
[Link]([Link]);
[Link](new Color(67,198,172));
[Link](false);
[Link](false);

JLabel panelUpLabel = new JLabel();


[Link]("CHAT ROOM");
[Link](new Font("Sans Serif",[Link],30));
[Link](new Color(67,198,172));

[Link](0,0,1000,50);
[Link]([Link]);
[Link](panelUpLabel);

[Link](new Dimension(100,400));
[Link](null);
[Link](new GridLayout(0,1));
[Link](new BoxLayout(panelMid,BoxLayout.Y_AXIS));
[Link](new Color(235,235,235,255));

[Link]([Link]);
[Link](new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
[Link] = [Link];

[Link] = 0;
[Link] = 0;
[Link] = 0;
[Link] = 0;

[Link](sideLabel,gbc);

[Link] = 1;
[Link] = 0;
[Link] = 1;
[Link] = 0;
[Link](scrollPane,gbc);

[Link](new Font("Sans Serif",[Link],22));

[Link] = 2;
[Link] = 0;
[Link](sendButton,gbc);

[Link](panelUp,BorderLayout.PAGE_START);
[Link](panelMid);
[Link](panelDown,BorderLayout.PAGE_END);

[Link](new Runnable() {
public void run(){
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
}
});
}
});
[Link]();
Thread t2 = new Thread(new Runnable() {
public void run(){
while(true){
if([Link]){
[Link](new Runnable() {
public void run() {
recievedMessage();
[Link] = false;
}
});
}

try {
[Link](50); // prevent busy waiting
} catch (InterruptedException e) {
[Link]();
}
}
}
});
[Link]();
}
public void focusGained(FocusEvent e){
if([Link]().equals("Type a message")){
[Link]("");
}
}
public void focusLost(FocusEvent e){
if([Link]().isEmpty()){
[Link]("Type a message");
[Link](false);
}
}
public void actionPerformed(ActionEvent e){
if([Link]() == sendButton){
click1 = true;
String text = [Link]();
JPanel out = formateLabel(text);

[Link](new BorderLayout());

JPanel right = new JPanel(new BorderLayout());


[Link](out,BorderLayout.LINE_END);
[Link](right);
[Link]([Link](15));

[Link](vertical,BorderLayout.PAGE_START);
[Link]("");

[Link]();
[Link]();
}
}
public void mousePressed(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){
if([Link]() == messageBar){
[Link](true);
[Link](true);
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e){
switch ([Link]()){
case 10:
click1 = true;
String text = [Link]();
JPanel out = formateLabel(text);

[Link](new BorderLayout());

JPanel right = new JPanel(new BorderLayout());


[Link](out,BorderLayout.LINE_END);
[Link](right);
[Link]([Link](15));

[Link](vertical,BorderLayout.PAGE_START);
[Link]("");

[Link]();
[Link]();
break;
}
}
public static JPanel formateLabel(String out){
JPanel panel = new JPanel();
[Link](new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel output = new JLabel(out);
[Link](new Color(67,198,172));
[Link](true);
[Link](new EmptyBorder(15,15,15,50));
[Link](output);
return panel;
}
public static JPanel formateLabel2(String out){
JPanel panel = new JPanel();
[Link](new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel output = new JLabel(out);
[Link]([Link]);
[Link](true);
[Link](new EmptyBorder(15,15,15,50));
[Link](output);
return panel;
}

public void recievedMessage(){


JPanel out = formateLabel2([Link]);
[Link](new BorderLayout());

JPanel left = new JPanel(new BorderLayout());


[Link](out,BorderLayout.LINE_START);
[Link](left);
[Link]([Link](15));

[Link](vertical,BorderLayout.PAGE_START);

[Link]();
[Link]();
}
}

You might also like