Reputation: 37
I'm trying to send my java file where I have a JFrame drawing with button that changes the color of the background from Client to Server. The server recieves that Drawing and opens it but when I click buttons nothing change. What am I doing wrong? Also for some reason the app doesn't run sometimes.
Code with the drawing
package Drawings;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
public class DrawingTwo extends JFrame {
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 300;
public Color tvColor;
public Color smileColor;
public int h;
public int h2;
public int h3;
public int h4;
public int h5;
public int h6;
public String l;
public DrawComponent c;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new DrawingTwo();
}
});
}
public DrawingTwo() {
super();
setOneChanell();
Container container = getContentPane();
container.setBackground(new Color(242, 212, 252));
container.setLayout(new BorderLayout(20, 20));
container.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
ButtonListener listener = new ButtonListener();
JLabel lb = new JLabel(l);
lb.setText(l);
container.add(lb);
JButton j2 = new JButton("2");
j2.addActionListener(listener);
container.add(j2, BorderLayout.NORTH);
c = new DrawComponent();
container.add(c, BorderLayout.CENTER);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public void setOneChanell() {
this.tvColor = new Color(255, 153, 153);
this.smileColor = new Color(255, 247, 10);
this.h = 110;
this.h2 = 55;
this.h3 = 60;
this.h4 = 60;
this.h5 = 0;
this.h6 = -180;
this.l = "Канал для веселых";
}
public void setTwoChanell() {
this.tvColor = new Color(172, 194, 157);
this.smileColor = new Color(0, 161, 219);
this.h = 115;
this.h2 = 87;
this.h3 = 50;
this.h4 = 40;
this.h5 = 0;
this.h6 = +180;
this.l = "Канал для грустных";
}
public class DrawComponent extends JComponent {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(37, 26, 210, 130);
g.setColor(tvColor);
g.fillRect(42, 30, 200, 120);
g.setColor(Color.darkGray);
g.fillRect(135, 156, 15, 20);
g.setColor(Color.darkGray);
g.fillRect(83, 170, 120, 13);
g.setColor(smileColor);
g.fillOval(100, 45, 80, 80);
g.setColor(Color.BLACK);
g.drawArc(120, 70, 10, 10, 0, 360);
g.drawArc(150, 70, 10, 10, 0, 360);
g.drawString(l, 83, 200);
g.setColor(Color.BLACK);
g.drawArc(h, h2, h3, h4, h5, h6);
}
}
public class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
JButton button = (JButton) event.getSource();
if (button.getText().equals("2")) {
setTwoChanell();
button.setText("1");
} else {
setOneChanell();
button.setText("2");
}
c.repaint();
}
}
}
THis is Client file
package ClientToServer;
import java.io.*;
import java.net.Socket;
import Drawings.DrawingTwo;
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 12345);
OutputStream outputStream = socket.getOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(new DrawingTwo());
objectOutputStream.flush();
objectOutputStream.close();
}
}
This is Server file
package ClientToServer;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import Drawings.DrawingTwo;
import javax.swing.*;
public class Server {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ServerSocket serverSocket = new ServerSocket(12345);
Socket client = serverSocket.accept();
ObjectInputStream inputStream = new ObjectInputStream(client.getInputStream());
DrawingTwo object = (DrawingTwo) inputStream.readObject();
object.setVisible(true);
object.setTitle("Server");
object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.close();
inputStream.close();
serverSocket.close();
}
}
Upvotes: 0
Views: 546
Reputation: 939
I suggest you to read about socket communications a bit more. If you have a server socket, it should run all the time. In your code you close your socket connection right after you receive the data from client.
Another thing is; you have limited your communication with only one type: DrawingTwo() class. On your server side, you cannot receive any other data.
Let's take a look step by step on your code.
You must define a new object for communication purposes
import java.io.Serializable;
public class CommunicationObject implements Serializable{
private DrawingTwo mDrawingTwo;
private boolean mSmileyFace = true;
private boolean mIsInitialConnection = true;
public DrawingTwo getmDrawingTwo() {
return mDrawingTwo;
}
public void setmDrawingTwo(DrawingTwo mDrawingTwo) {
this.mDrawingTwo = mDrawingTwo;
}
public boolean ismSmileyFace() {
return mSmileyFace;
}
public void setmSmileyFace(boolean mSmileyFace) {
this.mSmileyFace = mSmileyFace;
}
public boolean ismIsInitialConnection() {
return mIsInitialConnection;
}
public void setmIsInitialConnection(boolean mIsInitialConnection) {
this.mIsInitialConnection = mIsInitialConnection;
}
}
You have 3 fields here.
Your server must listen all the time
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
public class Server {
static boolean isRunning = true;
public static void main(String[] args) throws IOException, ClassNotFoundException {
ServerSocket serverSocket = new ServerSocket(12345);
Socket client = null;
ObjectInputStream inputStream = null;
InputStream is = null;
DrawingTwo drawingTwo = null;
CommunicationObject communicationObject = null;
while (isRunning) {
client = serverSocket.accept();
inputStream = new ObjectInputStream(client.getInputStream());
is = client.getInputStream();
communicationObject = (CommunicationObject) inputStream.readObject();
if (communicationObject.ismIsInitialConnection()) {
drawingTwo = (DrawingTwo) communicationObject.getmDrawingTwo();
drawingTwo.setVisible(true);
drawingTwo.setTitle("Server");
drawingTwo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} else {
if (communicationObject.ismSmileyFace()) {
drawingTwo.setOneChanell();
} else {
drawingTwo.setTwoChanell();
}
drawingTwo.c.repaint();
}
}
client.close();
inputStream.close();
serverSocket.close();
}
}
Please note that isRunning is always true now. You must handle the exceptions and other situations to stop it or restart it.
Create a getter for your JButton in your DrawingTwo class
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.beans.PropertyChangeListener;
public class DrawingTwo extends JFrame{
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 300;
public Color tvColor;
public Color smileColor;
public int h;
public int h2;
public int h3;
public int h4;
public int h5;
public int h6;
public String l;
public DrawComponent c;
private JButton j2;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new DrawingTwo();
}
});
}
public DrawingTwo() {
super();
setOneChanell();
Container container = getContentPane();
container.setBackground(new Color(242, 212, 252));
container.setLayout(new BorderLayout(20, 20));
container.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
ButtonListener listener = new ButtonListener();
JLabel lb = new JLabel(l);
lb.setText(l);
container.add(lb);
j2 = new JButton("2");
j2.addActionListener(listener);
container.add(j2, BorderLayout.NORTH);
c = new DrawComponent();
container.add(c, BorderLayout.CENTER);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public void setOneChanell() {
this.tvColor = new Color(255, 153, 153);
this.smileColor = new Color(255, 247, 10);
this.h = 110;
this.h2 = 55;
this.h3 = 60;
this.h4 = 60;
this.h5 = 0;
this.h6 = -180;
this.l = "1";
}
public void setTwoChanell() {
this.tvColor = new Color(172, 194, 157);
this.smileColor = new Color(0, 161, 219);
this.h = 115;
this.h2 = 87;
this.h3 = 50;
this.h4 = 40;
this.h5 = 0;
this.h6 = +180;
this.l = "2";
}
public JButton getJButton() {
return j2;
}
public class DrawComponent extends JComponent {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.fillRect(37, 26, 210, 130);
g.setColor(tvColor);
g.fillRect(42, 30, 200, 120);
g.setColor(Color.darkGray);
g.fillRect(135, 156, 15, 20);
g.setColor(Color.darkGray);
g.fillRect(83, 170, 120, 13);
g.setColor(smileColor);
g.fillOval(100, 45, 80, 80);
g.setColor(Color.BLACK);
g.drawArc(120, 70, 10, 10, 0, 360);
g.drawArc(150, 70, 10, 10, 0, 360);
g.drawString(l, 83, 200);
g.setColor(Color.BLACK);
g.drawArc(h, h2, h3, h4, h5, h6);
}
}
public class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
JButton button = (JButton) event.getSource();
if (button.getText().equals("2")) {
setTwoChanell();
button.setText("1");
} else {
setOneChanell();
button.setText("2");
}
c.repaint();
}
}
}
Remember that you have 2 functionalities for your button
At last, the Client class.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JButton;
public class Client {
static OutputStream outputStream;
static ObjectOutputStream objectOutputStream;
static Socket socket;
public static void main(String[] args) throws UnknownHostException, IOException {
DrawingTwo drawingTwo = new DrawingTwo();
CommunicationObject communicationObject = new CommunicationObject();
communicationObject.setmDrawingTwo(drawingTwo);
communicationObject.setmIsInitialConnection(true);
write(communicationObject);
JButton button = drawingTwo.getJButton();
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
if (button.getText().equals("2")) {
communicationObject.setmIsInitialConnection(false);
communicationObject.setmSmileyFace(false);
write(communicationObject);
} else {
communicationObject.setmIsInitialConnection(false);
communicationObject.setmSmileyFace(true);
write(communicationObject);
}
} catch (Exception e) {
// TODO: handle exception
}
}
});
}
public static void write(CommunicationObject comObject) throws IOException
{
socket = new Socket("localhost", 12345);
outputStream = socket.getOutputStream();
objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(comObject);
objectOutputStream.flush();
objectOutputStream.close();
}
}
You create a CommunicationObject here and assign your DrawingTwo class to it.
Take a look where you extract the JButton from DrawingTwo into Client class, so you can bind ActionListener to it. From this action listener you can now communicate with your server.
NOTE Since both Client and Server has access to DrawingTwo, you don't need to send whole class via socket. Just send a message to Server, that it should create an instance by itself.
Upvotes: 2