package 阿基米德螺线_;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
public class Arjmid_Server extends JFrame implements ActionListener,ChangeListener{
Socket[] sockets=new Socket[4];
JTextField text_cb;
JButton P_start,P_sava,P_read;
private JSpinner js;
private JRadioButton bcolor[];
private double x;
private JCheckBox start;
private int rgb,color;
private String file;
DataOutputStream[] douts=new DataOutputStream[4];
public Arjmid_Server(int port)throws IOException
{
super("阿基米德螺线绘制服务�?"+InetAddress.getLocalHost()+":"+port);
this.setBounds(0,0,1365,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel tb=new JPanel();
this.getContentPane().add(tb,"South");
tb.add(new JLabel("设置圈数"));
text_cb=new JTextField("8",10);
text_cb.addActionListener(this);
tb.add(text_cb);
text_cb.setEnabled(false);
P_start=new JButton("绘制");
tb.add(P_start);
P_start.addActionListener(this);
P_start.setEnabled(false);
P_sava=new JButton("保存数据");
tb.add(P_sava);
P_sava.addActionListener(this);
P_sava.setEnabled(false);
P_read=new JButton("读取数据");
tb.add(P_read);
P_read.addActionListener(this);
P_read.setEnabled(false);
start=new JCheckBox("动�?�绘�?");
tb.add(start);
start.setEnabled(false);
js=new JSpinner();
js.setValue(Double.parseDouble(text_cb.getText()));
js.addChangeListener(this);
js.setEnabled(false);
tb.add(js);
String colorstr[]={"�?","�?","�?"};
ButtonGroup colorgroup=new ButtonGroup();
bcolor=new JRadioButton[colorstr.length];
for(int i=0;i<bcolor.length;i++){
bcolor[i]=new JRadioButton(colorstr[i]);
colorgroup.add(bcolor[i]);
tb.add(bcolor[i]);
bcolor[i].setEnabled(false);
bcolor[i].addActionListener(this);
}
JTextArea text=new JTextArea();
text.setFont(new Font("Courier",Font.PLAIN,15));
this.getContentPane().add(text);
text.setLineWrap(true);
this.setVisible(true);
for(int i=0;i<4;i++)
{
text.append(port+"端口等待TCP连接");
sockets[i]=new ServerSocket(port).accept(); //等待接收客户端的连接申请
text.append("连接"+sockets[i].getInetAddress()+":"+sockets[i].getPort()+"\n");
douts[i]=new DataOutputStream(sockets[i].getOutputStream()); //获得socket输入�?
port++; //在下�?个端口等待下�?个客户端连接
}
while(true)
{
for(int i=0;i<bcolor.length;i++){
bcolor[i].setEnabled(true);
text_cb.setEnabled(true);
start.setEnabled(true);
P_start.setEnabled(true);
P_sava.setEnabled(true);
P_read.setEnabled(true);
js.setEnabled(true);
}
}
}
public static void main(String[] args) throws IOException{
// TODO 自动生成的方法存�?
new Arjmid_Server(2001);
}
public void Sava(String file){ //********************************************
try{
FileOutputStream fout=new FileOutputStream(file);
DataOutputStream dout=new DataOutputStream(fout);
this.x=Double.parseDouble(text_cb.getText());
dout.writeDouble(x); //圈数写入文件
dout.writeInt(color); //颜色按钮数组下标写入文件
dout.writeInt(rgb); //颜色值写入文�?
dout.close();
fout.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch(NumberFormatException nfex){
JOptionPane.showMessageDialog(this,"无法转换成浮点数","提醒",JOptionPane.WARNING_MESSAGE);
}
catch(IOException e1){
e1.printStackTrace();
}
}
public void Read(String file) { //****************************read
try {
FileInputStream fin=new FileInputStream(file);
DataInputStream din1=new DataInputStream(fin);
this.text_cb.setText(din1.readDouble()+""); //读取圈数
this.bcolor[din1.readInt()].setSelected(true);//读取颜色按钮数组下标
rgb=din1.readInt();//读取颜色�?
din1.close();
fin.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e1) {
e1.printStackTrace();
}
}
public void actionPerformed(ActionEvent ev) {
// TODO 自动生成的方法存�?
Color c=null;
if(ev.getSource()==bcolor[0]) {
c=new Color(255,0,0);
rgb=c.getRGB();
color=1;
}
if(ev.getSource()==bcolor[1]){
c=new Color(0,255,0);
rgb=c.getRGB();
color=2;
}
if(ev.getSource()==bcolor[2]){
c=new Color(0,0,255);
rgb=c.getRGB();
color=3;
}
if(ev.getActionCommand().equals("保存数据"))
{
this.file="text_Arjmid";
Sava(this.file); //保存到文�?
}
if(ev.getActionCommand().equals("读取数据"))
{
this.file="text_Arjmid";
Read(this.file);
}
if(ev.getActionCommand().equals("绘制")){ //传输数据
try{
this.x=Double.parseDouble(text_cb.getText());
if(x<0||x>10)
throw new Exception(" ");
for(int i=0;i<4;i++)
{
douts[i].writeDouble(x); //写入圈数
douts[i].writeInt(rgb);
if(start.isSelected())
{
douts[i].writeBoolean(true);
}
else
if(!start.isSelected())
{
douts[i].writeBoolean(false);
}
}
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(this,"无法转换成浮点数","提醒",JOptionPane.WARNING_MESSAGE);
}
catch(Exception ex){
JOptionPane.showMessageDialog(this,"圈数必须�?0~10之间","提醒",JOptionPane.WARNING_MESSAGE);
}
}
}
public void stateChanged(ChangeEvent ev) {
if(ev.getSource().equals(js))
{
int k=(int) js.getValue(); //获取文本行输入数�?
if(k<0)
{
JOptionPane.showMessageDialog(this,"圈数不能小于0","提醒",JOptionPane.WARNING_MESSAGE);
js.setValue(1);
}
if(k>10)
{
JOptionPane.showMessageDialog(this,"圈数不能大于10","提醒",JOptionPane.WARNING_MESSAGE);
js.setValue(10);
}
this.text_cb.setText(""+js.getValue());
}
}
}