import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.io.*;
import java.awt.geom.*;
import java.util.*;
import java.net.*;
public class ShapeTalk {
//frame
JFrame frame;
//menu and other of the frame
JMenuBar menuBar;
JMenu file, edit, color, help;
JMenuItem newImage, open, save, saveas, quit, questions, about, colorChooser;
JMenuItem buttonReturn, buttonResume, buttonClear, buttonCopy;
DrawPanel centerCenter, unDoPanel;
JPanel centerPanel, mainPanel;
ButtonGroup tool, drawChange, selectChange;
JRadioButton scale, rotate, shear, translation;
JToggleButton curve, ellipse, eraser, fill, line, pencil, rectangle, roundrectangle, word, mouse, straw;
Icon curveI, ellipseI, eraserI, fillI, lineI, pencilI, rectangleI, roundrectangleI, wordI, mouseI, strawI;
JRadioButton drawButton, fillButton, changeButton;
JLabel sizeXText, sizeYText, posXText, posYText, degreeText;
JColorChooser colorSet;
JToolBar imageTool;
JSlider thickness;
Graphics2D graphic;
int state = 0; //store the graphic shape now
Color colorChosen; //store the graphic color now
Vector<Shape> store;
//to store the position of the shape
int x0, y0, x, y;
int connected = 0;
//to draw by a pencil
GeneralPath gPath; //GeneralPath对象实例
Point aPoint;
//store the number of selected object
int selectedObject = -1;
int selectedObjectCopy = 0;
//transform matrix of these shape
AffineTransform changeMatrix;
AffineTransform returnMatrix;
//class talk
class Talk extends JPanel {
//frame of talk
JPanel panel, bottomPanel, bottomRight, upperPanel, upper1, upper2, upper3;
JTextArea talkArea;
JTextField talkInput, serverIP, serverPort, myName;
JButton buttonSend, buttonSave, connectServer, disconnectServer;
JLabel serverIPLabel, serverPortLabel, myNameLabel;
//client
java.awt.List list1 = new java.awt.List(99); //Show the users online
Socket socket=null; //connect to the port
ObjectOutputStream ps=null; //output stream
ObjectInputStream iss = null;
Listen listen=null; //listen the thread
JScrollPane scollPane;
String name;
//function talk
Talk()
{
//store the basic information of the client
name = new String();
setLayout(new BorderLayout());
talkArea = new JTextArea(2, 2);
talkArea.setEditable(false);
scollPane = new JScrollPane(talkArea);
bottomPanel = new JPanel(new BorderLayout());
upperPanel = new JPanel(new GridLayout(2,1));
add(scollPane, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.SOUTH);
add(upperPanel, BorderLayout.NORTH);
//the frame of the talk part
upper1 = new JPanel();
upper2 = new JPanel();
upperPanel.add(upper1);
upperPanel.add(upper2);
serverIPLabel = new JLabel("ServerIP:");
serverPortLabel = new JLabel("ServerPort:");
myNameLabel = new JLabel("My Name:");
serverIP = new JTextField("127.0.0.1", 17);
serverPort = new JTextField("5652", 6);
myName = new JTextField("Client", 12);
connectServer = new JButton("Connect");
disconnectServer = new JButton("Disonnect");
disconnectServer.setEnabled(false);
upper1.add(serverIPLabel);
upper1.add(serverIP);
upper1.add(serverPortLabel);
upper1.add(serverPort);
upper1.add(myNameLabel);
upper1.add(myName);
upper2.add(connectServer);
upper2.add(disconnectServer);
talkInput = new JTextField();
bottomRight = new JPanel(new GridLayout(1, 2));
bottomPanel.add(talkInput, BorderLayout.CENTER);
bottomPanel.add(bottomRight, BorderLayout.EAST);
buttonSend = new JButton("Send");
buttonSave = new JButton("Save");
bottomRight.add(buttonSend);
bottomRight.add(buttonSave);
/**the actionlistener for textfield and send button*/
class AddTalk implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(socket!=null) //if there is a socket, send information
{
StringBuffer msg = new StringBuffer("MSG: ");
msg.append(name);
msg.append(":");
msg.append(talkInput.getText());
ImageInfo o = new ImageInfo(msg);
try{
ps.writeObject(o); //output the information
ps.flush();
}catch(IOException ex){
ex.printStackTrace(); //out put errors
}
talkInput.setText(""); //clear the textfield
}
}
}
//add the listerner to these buttons
talkInput.addActionListener(new AddTalk());
buttonSend.addActionListener(new AddTalk());
/**the actionlisterner of buttonsave*/
buttonSave.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
JFileChooser saveFile = new JFileChooser();
File currentFile = new File(".");
saveFile.setCurrentDirectory(currentFile);
//FileNameExtensionFilter saveFileFilter = new FileNameExtensionFilter("*.txt", "txt");
//saveFile.setFileFilter(saveFileFilter);
saveFile.setApproveButtonText("Save");
int selected = saveFile.showSaveDialog(null);
if (selected == JFileChooser.APPROVE_OPTION)
{
File file= saveFile.getSelectedFile();
if(file.exists() == true)
{
//JOptionPane confirm = new JOptionPane();
int change = JOptionPane.showConfirmDialog(null, "Do you confirm to change "+file.getName()+"?", "confirm", JOptionPane.YES_NO_OPTION);
if(change != JOptionPane.OK_OPTION)
{
return;
}
}
try{
FileOutputStream of = new FileOutputStream(file);
OutputStreamWriter os = new OutputStreamWriter(of);
os.write(talkArea.getText());
os.close();
of.close();
}catch(FileNotFoundException ex){
//JOptionPane writeError = new JOptionPane();
JOptionPane.showMessageDialog(null, "Cannot open the file!", "error", JOptionPane.INFORMATION_MESSAGE);
}catch(IOException ex){
//JOptionPane writeError = new JOptionPane();
JOptionPane.showMessageDialog(null, "Writing to file error!", "error", JOptionPane.INFORMATION_MESSAGE);
}
}
}
});//end of buttonSave.addActionListener
/**the actionlisterner of connectserver*/
connectServer.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
name = myName.getText();
try{
if (socket == null)
{
InetAddress ip;
int port;
try{
ip = InetAddress.getByName(serverIP.getText());
port = (int)Integer.parseInt(serverPort.getText());
socket = new Socket(ip, port); //实例化一个套接字
}catch(NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Format of IP or Port Error", "Error", JOptionPane.ERROR_MESSAGE);
}
ps=new ObjectOutputStream(socket.getOutputStream()); //获取输出流
iss=new ObjectInputStream(socket.getInputStream());
StringBuffer info = new StringBuffer("INFO: ");
String userinfo = name+":"+InetAddress.getLocalHost().toString();
info.append(userinfo);
ps.writeObject(new ImageInfo(info)); //输出信息
ps.flush();
listen = new Listen(name,socket); //实例化监听线程
listen.start(); //启动线程
}
talkArea.append("Connect to " + serverIP.getText() +":" + serverPort.getText() + " succeed!\n");
connectServer.setEnabled(false);
disconnectServer.setEnabled(true);
connected = 1;
}catch (Exception ex){
ex.printStackTrace(); //输出错误信息
talkArea.append("Connect to " + serverIP.getText() +":" + serverPort.getText() + " failed!\n"
罗哥分享
- 粉丝: 40
- 资源: 50
最新资源
- 从零学习自动驾驶Lattice规划算法(下) 轨迹采样 轨迹评估 碰撞检测 包含matlab代码实现和cpp代码实现,方便对照学习 cpp代码用vs2019编译 依赖qt5.15做可视化 更新:
- 风光储、风光储并网直流微电网simulink仿真模型 系统由光伏发电系统、风力发电系统、混合储能系统(可单独储能系统)、逆变器VSR+大电网构成 光伏系统采用扰动观察法实现mppt控
- (180014016)pycairo-1.18.2-cp35-cp35m-win32.whl.rar
- (180014046)pycairo-1.21.0-cp311-cp311-win32.whl.rar
- DS-7808-HS-HF / DS-7808-HW-E1
- (180014004)pycairo-1.20.0-cp36-cp36m-win32.whl.rar
- (178330212)基于Springboot+VUE的校园图书管理系统
- (402216)人脸识别
- enspOSPF多区域路由配置
- (175822648)java项目之电信计费系统完整代码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈