camel.java
来自「使用该APPLET可以让你在WEB里玩MUD」· Java 代码 · 共 441 行
JAVA
441 行
import java.awt.Frame;import java.awt.Panel;import java.awt.Label;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.BorderLayout;import java.awt.MenuBar;import java.awt.Menu;import java.awt.MenuItem;import java.awt.CheckboxMenuItem;import java.awt.MenuShortcut;import java.awt.FileDialog;import java.awt.Dialog;import java.awt.TextField;import java.awt.Button;import java.awt.Dimension;import java.awt.Canvas;import java.awt.List;import java.awt.Image;import java.awt.Graphics;import java.awt.Font;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.util.Hashtable;import java.util.Enumeration;import java.util.Properties;import java.util.zip.GZIPInputStream;import java.util.zip.GZIPOutputStream;import java.io.FileInputStream;import java.io.ObjectInputStream;import java.io.FileOutputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.net.URL;public class camel extends Frame implements ActionListener{ public static void main(String args[]) { new camel(); } protected Hashtable cams; protected Properties props; private String fs = System.getProperty("file.separator"); private String home = System.getProperty("user.home"); public camel() { // initialize frame and menu bar setTitle("WebCam Observer"); Menu m = new Menu("File"); MenuItem mitem = new MenuItem("Open", new MenuShortcut('o')); mitem.setActionCommand("open"); mitem.addActionListener(this); m.add(mitem); mitem = new MenuItem("Save", new MenuShortcut('s')); mitem.setActionCommand("save"); mitem.addActionListener(this); m.add(mitem); mitem = new MenuItem("Preferences", new MenuShortcut('p')); mitem.setActionCommand("prefs"); mitem.addActionListener(this); m.add(mitem); mitem = new MenuItem("Quit", new MenuShortcut('q')); mitem.setActionCommand("quit"); mitem.addActionListener(this); m.add(mitem); MenuBar mbar = new MenuBar(); mbar.add(m); m = new Menu("Camera"); mitem = new MenuItem("New", new MenuShortcut('n')); mitem.setActionCommand("new"); mitem.addActionListener(this); m.add(mitem); mitem = new MenuItem("Remove", new MenuShortcut('r')); mitem.setActionCommand("remove"); mitem.addActionListener(this); m.add(mitem); mbar.add(m); m = new Menu("Help"); mitem = new MenuItem("About"); mitem.setActionCommand("about"); mitem.addActionListener(this); m.add(mitem); mbar.setHelpMenu(m); setMenuBar(mbar); // initialize camel setLayout(new FlowLayout(0, 0, FlowLayout.LEFT)); props = new Properties(); try { props.load(new FileInputStream(home+fs+".camelrc")); } catch(Exception e) { props.put("pictureWidth", "100"); props.put("pictureHeight", "75"); props.put("loadDefault", home+fs+"default.cam"); } if(cams == null) setSize(100, 100); else pack(); show(); if(props.getProperty("loadDefault") != null && ((String)props.getProperty("loadDefault")).length() > 0) openCam((String)props.getProperty("loadDefault")); } public void actionPerformed(ActionEvent evt) { String command = evt.getActionCommand(); if(command.equals("quit")) { try { props.save(new FileOutputStream(home+fs+".camelrc"), "WebCam Observer configuration file"); } catch(Exception e) { System.err.println("Cannot save properties: "+e); } if(props.getProperty("loadDefault") != null && ((String)props.getProperty("loadDefault")).length() > 0) saveCam((String)props.getProperty("loadDefault")); System.exit(0); } else if(command.equals("open")) open(); else if(command.equals("save")) save(); else if(command.equals("prefs")) new prefsDialog(this, "Preferences"); else if(command.equals("new")) new newCamDialog(this, "New Camera"); else if(command.equals("remove")) new removeCamDialog(this, "Remove Camera"); else if(command.equals("about")) new aboutDialog(this, "About WebCam Observer"); } private void open() { FileDialog fd = new FileDialog(this, "Load WebCam's", FileDialog.LOAD); fd.show(); openCam(fd.getFile()); } private void openCam(String filename) { if(filename != null) try { FileInputStream fis = new FileInputStream(filename); GZIPInputStream gzis = new GZIPInputStream(fis); ObjectInputStream in = new ObjectInputStream(gzis); cams = (Hashtable)in.readObject(); in.close(); } catch(Exception e) { System.out.println(e); // e.printStackTrace(); } removeAll(); if(cams != null) { Enumeration enum = cams.elements(); while(enum.hasMoreElements()) { camMonitor cam = ((camStore)enum.nextElement()).getCam(); cam.setSize(new Dimension( Integer.parseInt(props.getProperty("pictureWidth")), Integer.parseInt(props.getProperty("pictureHeight")))); add(cam); pack(); } } } private void save() { FileDialog fd = new FileDialog(this, "Save WebCam's", FileDialog.SAVE); fd.show(); saveCam(fd.getFile()); } private void saveCam(String filename) { if(filename != null) try { FileOutputStream fos = new FileOutputStream(filename); GZIPOutputStream gzos = new GZIPOutputStream(fos); ObjectOutputStream os = new ObjectOutputStream(gzos); os.writeObject(cams); os.flush(); os.close(); } catch(Exception e) { System.out.println(e); // e.printStackTrace(); } } public void addCam(String url, String nick, int update) { if(cams == null) cams = new Hashtable(); camStore cam = new camStore(url, nick, update); cams.put(nick, cam); camMonitor newCam = cam.getCam(); newCam.setSize(new Dimension( Integer.parseInt(props.getProperty("pictureWidth")), Integer.parseInt(props.getProperty("pictureHeight")))); add(cam.getCam()); pack(); }}class camStore implements Serializable { public String url, nick; public int update; transient camMonitor cam; public camStore(String url, String nick, int update) { this.url = url; this.nick = nick; this.update = update; cam = new camMonitor(nick, url, update); } public camMonitor getCam() { if(cam == null) cam = new camMonitor(nick, url, update); return cam; }}class prefsDialog extends Dialog implements ActionListener { Button ok, cancel; TextField loadDefault, width, height; camel camel; public prefsDialog(Frame frame, String title) { super(frame, title); camel = (camel)frame; Panel p = new Panel(new GridLayout(2,2)); p.add(new Label("Default Camera File")); p.add(loadDefault = new TextField( (String)camel.props.getProperty("loadDefault"), 20)); loadDefault.addActionListener(this); p.add(new Label("Picture Size")); Panel sizep = new Panel(new GridLayout(1,4)); sizep.add(new Label("Width")); sizep.add(width=new TextField(camel.props.getProperty("pictureWidth"), 5)); sizep.add(new Label("Height")); sizep.add(height=new TextField(camel.props.getProperty("pictureHeight"),5)); width.addActionListener(this); height.addActionListener(this); p.add(sizep); add("Center", p); p = new Panel(); p.add(ok = new Button("OK")); ok.addActionListener(this); p.add(cancel = new Button("Cancel")); cancel.addActionListener(this); add("South", p); pack(); show(); } public void actionPerformed(ActionEvent evt) { Object source = evt.getSource(); if(source == cancel) setVisible(false); if(source == ok || source == loadDefault || source == width || source == height) { camel.props.put("loadDefault", loadDefault.getText()); setVisible(false); Dimension size = new Dimension(Integer.parseInt(width.getText()), Integer.parseInt(height.getText())); if(!width.getText().equals(camel.props.getProperty("pirctureWidth"))) camel.props.put("pictureWidth", width.getText()); if(!height.getText().equals(camel.props.getProperty("pictureHeight"))) camel.props.put("pictureHeight", height.getText()); Enumeration enum = camel.cams.elements(); while(enum.hasMoreElements()) ((camStore)enum.nextElement()).getCam().setSize(size); camel.pack(); } }} class newCamDialog extends Dialog implements ActionListener { TextField url, nick, update; Button ok, cancel; camel camel; public newCamDialog(Frame frame, String title) { super(frame, title); camel = (camel)frame; Panel p = new Panel(new GridLayout(3, 2)); p.add(new Label("URL")); p.add(url = new TextField(20)); url.addActionListener(this); p.add(new Label("Nick")); p.add(nick = new TextField(20)); nick.addActionListener(this); p.add(new Label("Update (seconds)")); p.add(update = new TextField(5)); update.addActionListener(this); add("Center", p); p = new Panel(); p.add(ok = new Button("OK")); ok.addActionListener(this); p.add(cancel = new Button("Cancel")); cancel.addActionListener(this); add("South", p); pack(); show(); } public void actionPerformed(ActionEvent evt) { Object source = evt.getSource(); if(source == cancel) setVisible(false); else if(source == ok || source == url || source == nick || source == update) { camel.addCam(url.getText(), nick.getText(), Integer.parseInt(update.getText())); setVisible(false); } }}class removeCamDialog extends Dialog implements ActionListener { List camList; Button remove, cancel; camel camel; public removeCamDialog(Frame frame, String title) { super(frame, title); camel = (camel)frame; Panel p = new Panel(); p.add(remove = new Button("Remove")); remove.addActionListener(this); p.add(cancel = new Button("Cancel")); cancel.addActionListener(this); add("South", p); camList = new List(5); camList.setMultipleMode(true); Enumeration enum = camel.cams.elements(); while(enum.hasMoreElements()) { camStore cam = (camStore)enum.nextElement(); camList.add(cam.nick); } add("Center", camList); camList.addActionListener(this); pack(); show(); } public void actionPerformed(ActionEvent evt) { Object source = evt.getSource(); if(source == cancel) setVisible(false); else if(source == remove) { String items[] = camList.getSelectedItems(); for(int i = 0; i < items.length; i++) { camel.remove(((camStore)camel.cams.get(items[i])).getCam()); camel.cams.remove(items[i]); } camel.pack(); setVisible(false); } }}class aboutDialog extends Dialog implements ActionListener { Button ok; class face extends Canvas { Image image; public face() { try { image = this.getToolkit() .getImage(new URL("http://www.mud.de/~leo/leo.jpg")); } catch(Exception e) { e.printStackTrace(); } } public void paint(Graphics g) { g.drawImage(image, 0, 0, this); } public Dimension getSize() { return new Dimension(100, 111); } public Dimension getPreferredSize() { return getSize(); } } public aboutDialog(Frame frame, String title) { super(frame, title); Label line = new Label("About WebCam Observer"); line.setFont(new Font("SansSerif", Font.BOLD, 20)); add("North", line); Panel p = new Panel(new GridLayout(3, 1)); line = new Label("Copyright 1998 Matthias L. Jugel"); line.setFont(new Font("Serif", Font.BOLD & Font.ITALIC, 16)); p.add(line); line = new Label("http://www.mud.de/~leo/"); line.setFont(new Font("Serif", Font.ITALIC, 16)); p.add(line); line = new Label("leo@mud.de"); line.setFont(new Font("Monospaced", Font.BOLD, 16)); p.add(line); add("Center", p); add("West", new face()); p = new Panel(); p.add(ok = new Button("OK")); ok.addActionListener(this); add("South", p); pack(); show(); } public void actionPerformed(ActionEvent evt) { Object source = evt.getSource(); if(source == ok) setVisible(false); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?