service(通用服务游览器)

来源:互联网 发布:数学矩阵的用途 编辑:程序博客网 时间:2024/04/29 10:28
import java.rmi.*;//一般的rmi接口public interface ServiceServer extends Remote{Object[] getServiceList()throws RemoteException;Service getService(Object serviceKey)throws RemoteException;}


import javax.swing.*;//gui服务要实现的部分import java.io.*;public interface Service extends Serializable{public JPanel getGuiPanel();}

import java.rmi.*;//远程的实现import java.util.*;import java.rmi.server.*;public class ServiceServerImpl extends UnicastRemoteObject implements ServiceServer{HashMap serviceList;public ServiceServerImpl()throws RemoteException{setUpServices();}private void setUpServices(){serviceList =new HashMap();serviceList.put("Dice Rolling Service",new DiceService());serviceList.put("Day of the Week Service",new DayOfTheWeekService());serviceList.put("Visual Music Service",new MiniMusicService());}public Object[] getServiceList(){System.out.println("in remote");return serviceList.keySet().toArray();//客户端会调用它以取得服务的清单,我们会送出Object数组,只带有HashMap的key,实际服务会到客户要求时才通过getService()送出}public Service getService(Object serviceKey)throws RemoteException{Service theService=(Service)serviceList.get(serviceKey);//这个方法会通过key名称来返回HashMap中相对应的服务return theService;}public static void main(String [] args){try{Naming.rebind("ServiceServer",new ServiceServerImpl());}catch(Exception ex){ex.printStackTrace();}System.out.println("Remote service is running");}}

import java.awt.*;//用户端import javax.swing.*;import java.rmi.*;import java.awt.event.*;public class ServiceBrowser{JPanel mainPanel;JComboBox serviceList;ServiceServer server;public void buildGUI(){JFrame frame=new JFrame("RMI Browser");mainPanel=new JPanel();frame.getContentPane().add(BorderLayout.CENTER,mainPanel);Object []services=getServiceList();//此方法执行RMI registry查询,取得stub并调用getServiceList()serviceList=new JComboBox(services);frame.getContentPane().add(BorderLayout.NORTH,serviceList);serviceList.addActionListener(new MyListListener());frame.setSize(500,500);frame.setVisible(true);}void loadService(Object serviceSelection)//把实际的服务添加在GUI的mainPanel中{try{Service svc=server.getService(serviceSelection);mainPanel.removeAll();mainPanel.add(svc.getGuiPanel());mainPanel.validate();mainPanel.repaint();}catch(Exception ex){ex.printStackTrace();}}Object [] getServiceList(){Object obj=null;Object [] services=null;try{obj=Naming.lookup("rmi://127.0.0.1/ServiceServer");//执行rmi查询,取得stub}catch(Exception ex){ex.printStackTrace();}server=(ServiceServer)obj;try{services=server.getServiceList();}catch(Exception ex){ex.printStackTrace();}return services;}class MyListListener implements ActionListener{public void actionPerformed(ActionEvent ev){Object selection=serviceList.getSelectedItem();loadService(selection);}}public static void main(String [] args){new ServiceBrowser().buildGUI();}}

import javax.swing.*;import java.awt.event.*;import java.awt.*;import java.io.*;import java.util.*;import java.text.*;public class DayOfTheWeekService implements Service{JLabel outputLabel;JComboBox month;JTextField day;JTextField year;public JPanel getGuiPanel(){JPanel panel=new JPanel();JButton button=new JButton("Do it!");button.addActionListener(new DoItListener());outputLabel=new JLabel("date appears here");DateFormatSymbols dateStuff=new DateFormatSymbols();month=new JComboBox(dateStuff.getMonths());day=new JTextField(8);year=new JTextField(8);JPanel inputPanel=new JPanel(new GridLayout(3,2));inputPanel.add(new JLabel("Month"));inputPanel.add(month);inputPanel.add(new JLabel("Day"));inputPanel.add(day);inputPanel.add(new JLabel("Year"));inputPanel.add(year);panel.add(inputPanel);panel.add(button);panel.add(outputLabel);return panel;}public class DoItListener implements ActionListener{public void actionPerformed(ActionEvent ev){int monthNum=month.getSelectedIndex();int dayNum=Integer.parseInt(day.getText());int yearNum=Integer.parseInt(year.getText());Calendar c=Calendar.getInstance();c.set(Calendar.MONTH,monthNum);c.set(Calendar.DAY_OF_MONTH,dayNum);c.set(Calendar.YEAR,yearNum);Date date=c.getTime();String dayOfWeek=(new SimpleDateFormat("EEEE")).format(date);outputLabel.setText(dayOfWeek);}}}

import javax.swing.*;import java.awt.event.*;import java.io.*;public class DiceService implements Service{JLabel label;JComboBox numOfDice;public JPanel getGuiPanel(){JPanel panel=new JPanel();JButton button=new JButton("Roll 'em!");String [] choices={"1","2","3","4","5"};numOfDice=new JComboBox(choices);label=new JLabel("dice values here");button.addActionListener(new RollEmListener());panel.add(numOfDice);panel.add(button);panel.add(label);return panel;}public class RollEmListener implements ActionListener{public void actionPerformed(ActionEvent ev){String diceOutput="";String selection=(String) numOfDice.getSelectedItem();int numOfDiceToRoll=Integer.parseInt(selection);for(int i=0;i<numOfDiceToRoll;i++){int r=(int)((Math.random()*6)+1);diceOutput+=(" "+r);}label.setText(diceOutput);}}}


import javax.sound.midi.*;import java.io.*;import javax.swing.*;import java.awt.*;import java.awt.event.*;public class MiniMusicService implements Service{MyDrawPanel myPanel;public JPanel getGuiPanel(){JPanel mainPanel=new JPanel();myPanel=new MyDrawPanel();JButton playItButton=new JButton("Play it");playItButton.addActionListener(new PlayItListener());mainPanel.add(myPanel);mainPanel.add(playItButton);return mainPanel;}public class PlayItListener implements ActionListener{public void actionPerformed(ActionEvent ev){try{Sequencer sequencer=MidiSystem.getSequencer();sequencer.open();sequencer.addControllerEventListener(myPanel,new int[]{127});Sequence seq=new Sequence(Sequence.PPQ,4);Track track=seq.createTrack();for(int i=0;i<100;i+=4){int rNum=(int)((Math.random()*50)+1);if(rNum<38){track.add(makeEvent(144,1,rNum,100,i));track.add(makeEvent(176,1,127,0,i));track.add(makeEvent(128,1,rNum,100,i+2));}}sequencer.setSequence(seq);sequencer.start();sequencer.setTempoInBPM(220);}catch(Exception ex){ex.printStackTrace();}}}public MidiEvent makeEvent(int comd,int chan,int one,int two,int tick){MidiEvent event = null;try{ShortMessage a=new ShortMessage();a.setMessage(comd,chan,one,two);event=new MidiEvent(a,tick);}catch(Exception ex){}return event;}class MyDrawPanel extends JPanel implements ControllerEventListener{boolean msg=false;public void controlChange(ShortMessage event){msg=true;repaint();}public Dimension getPreferredSize(){return new Dimension(300,300);}public void paintComponent(Graphics g){if(msg){Graphics2D g2=(Graphics2D)g;int r=(int)(Math.random()*250);int gr=(int)(Math.random()*250);int b=(int)(Math.random()*250);g.setColor(new Color(r,gr,b));int ht=(int)((Math.random()*120)+10);int width=(int)((Math.random()*120)+10);int x=(int)((Math.random()*40)+10);int y=(int)((Math.random()*40)+10);g.fillRect(x,y,ht,width);msg=false;}}}}



0 0
原创粉丝点击