import java.applet.Applet;
import java.applet.AppletContext;
import java.applet.AppletStub;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.AudioFormat.Encoding;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import com.sun.j3d.utils.applet.MainFrame;
public class JavaChat_1 extends Applet implements Runnable
{
public static final int PORT = 1;
Socket socket;
DataInputStream in;
PrintStream out;
TextField inputfield;
TextArea outputarea;
Thread thread;
private static final long serialVersionUID = 4881953554409778241L;
private final HashMap<String, InputStream> appStreams = new HashMap<String, InputStream>();
private final HashMap<String, String> appParams = new HashMap<String, String>();
String name;
public JavaChat_1()
{
File f = new File("F:/FUTUR.PROGRAMS/Chat/Saves.txt");
name = getContents(f);
new chatserver();
inputfield = new TextField();
outputarea = new TextArea();
outputarea.setFont( new Font("Dialog", Font.PLAIN, 12));
outputarea.setEditable(false);
this.setLayout(new BorderLayout());
this.add("South", inputfield);
this.add("Center", outputarea);
this.setBackground(Color.cyan);
this.setForeground(Color.cyan);
inputfield.setBackground(Color.black);
outputarea.setBackground(Color.black);
JMenuBar bar = new JMenuBar();bar.setBackground(Color.black);this.add(bar,BorderLayout.NORTH);
JMenu m = new JMenu("Start");m.setForeground(Color.cyan);bar.add(m);
JMenuItem i = new JMenuItem("Benutzternamen ändern");i.setForeground(Color.cyan);i.setBackground(Color.black);m.add(i);
i.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent evt) {
b1ActionPerformed(evt);
}
private void b1ActionPerformed(ActionEvent evt)
{
new MainFrame(new Correct(),200,250);
}
});
JMenuItem i1 = new JMenuItem("Beenden");i1.setForeground(Color.cyan);i1.setBackground(Color.black);m.add(i1);
i1.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent evt) {
b1ActionPerformed(evt);
}
private void b1ActionPerformed(ActionEvent evt)
{
System.exit(0);
}
});
JTextArea a = new JTextArea();
a.setText(" Java_Chat_1.0");a.setEnabled(false); bar.add(a);a.setBackground(Color.black);
JMenu me = new JMenu("Hilfe");
me.setForeground(Color.cyan);
bar.add(me);
JMenuItem m1 = new JMenuItem("Hilfe aufrufen");
m1.setBackground(Color.black); m1.setForeground(Color.cyan);
me.add(m1);
m1.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent evt) {
b1ActionPerformed(evt);
}
private void b1ActionPerformed(ActionEvent evt)
{
new MainFrame (new Help(),200,250);
}
});
}
public void start()
{
try
{
socket = new Socket(this.getCodeBase().getHost(), PORT);
in = new DataInputStream(socket.getInputStream());
out = new PrintStream(socket.getOutputStream());
} catch (IOException e)
{
this.showStatus(e.toString());
say("Verbindung zum Server fehlgeschlagen!");
}
say(name+" ist der ChatSitzung im Port:"+PORT+" beigetreten.");
if (thread == null)
{
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public void stop()
{
try
{
socket.close();
} catch (IOException e)
{
this.showStatus(e.toString());
}
if ((thread !=null) && thread.isAlive())
{
thread.stop();
thread = null;
}
}
public void run()
{
String line;
try
{
while(true)
{
line = in.readLine();
if(line!=null)
outputarea.appendText(line+'\n' );
}
} catch (IOException e) { say("Verbindung zum Server abgebrochen"); }
}
public boolean action(Event e, Object what)
{
if (e.target==inputfield)
{
String inp=(String)name+": "+ e.arg;
out.println(inp);
inputfield.setText("");
return true;
}
return false;
}
public void say(String msg)
{
outputarea.appendText("*** "+msg+" ***\n");
}
static public String getContents(File aFile)
{
StringBuilder contents = new StringBuilder();
try {
BufferedReader input = new BufferedReader(new FileReader(aFile));
try {
String line = null;
while (( line = input.readLine()) != null){
contents.append(line);
}
}
finally {
input.close();
}
}
catch (IOException ex){
ex.printStackTrace();
}
return contents.toString();
}
public static void main(String ... args)
{
final Frame f = new Frame("Hybrid Application Demo");
final JavaChat_1 applet = new JavaChat_1();
final Enumeration<Applet> appletEnum = new Enumeration<Applet>()
{
public boolean hasMoreElements()
{
return false;
}
public Applet nextElement()
{
return null;
}
};
final AppletContext context = new AppletContext()
{
public Applet getApplet(String name)
{
return null; // not in application mode
}
public Enumeration<Applet> getApplets()
{
return appletEnum;
}
public AudioClip getAudioClip(URL url)
{
try {
final Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
AudioFormat af = ais.getFormat();
if(Encoding.ALAW.equals(af.getEncoding())
|| Encoding.ULAW.equals(af.getEncoding())) {
af = new AudioFormat(
Encoding.PCM_SIGNED,
af.getSampleRate(),
af.getSampleSizeInBits() * 2,
af.getChannels(),
af.getFrameSize() * 2,
af.getFrameRate(),
true
);
ais = AudioSystem.getAudioInputStream(af, ais);
}
clip.open(ais);
return new AudioClip()
{
public void loop()
{
clip.loop(-1);
}
public void play()
{
clip.start();
}
public void stop()
{
clip.stop();
}
};
} catch(LineUnavailableException e) {
e.printStackTrace();
return null;
} catch(IOException e) {
e.printStackTrace();
return null;
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
return null;
}
}
public Image getImage(URL url)
{
return Toolkit.getDefaultToolkit().getImage(url);
}
public InputStream getStream(String key)
{
return applet.appStreams.get(key);
}
public Iterator<String> getStreamKeys()
{
return applet.appStreams.keySet().iterator();
}
public void setStream(String key, InputStream stream)
throws IOException
{
if(key != null && key.length() > 0) {
applet.appStreams.put(key, stream);
}
}
public void showDocument(URL url)
{
// no effect
}
public void showDocument(URL url, String target)
{
// no effect
}
public void showStatus(String status)
{
// no effect
}
};
AppletStub stub = new AppletStub()
{
public void appletResize(int width, int height)
{
}
public AppletContext getAppletContext()
{
return context;
}
public URL getCodeBase()
{
URL rc = JavaChat_1.class.getResource("HybridApplication.class");
return rc;
}
public URL getDocumentBase()
{
URL rc = JavaChat_1.class.getResource("HybridApplication.class");
if(rc.getProtocol().equalsIgnoreCase("jar")) {
try {
String tmp = rc.toString().substring(4, rc.toString().indexOf("!/"));
tmp = rc.toString().replaceAll("jar:", "").replaceAll("file:/", "file://");
tmp = tmp.substring(0, tmp.lastIndexOf("!/"));
rc = new URL(null, tmp);
} catch(MalformedURLException e) {
e.printStackTrace();
rc = null;
}
}
return rc;
}
public String getParameter(String name)
{
return applet.appParams.get(name);
}
public boolean isActive()
{
return f.isActive();
}
};
applet.setStub(stub);
if(args != null && args.length > 0) {
String[] pars;
for(String arg : args) {
pars = arg.split("=");
if(pars.length == 1) pars = new String[] {pars[0], "true"};
applet.appParams.put(pars[0].toLowerCase(), pars[1]);
}
}
f.setLayout(new GridLayout(1, 1, 0, 0));
String arg;
int width, height;
try {
width = ((arg = applet.getParameter("width")) != null)? Integer.parseInt(arg) : 800;
} catch(NumberFormatException e) {
width = 800;
}
try {
height = ((arg = applet.getParameter("height")) != null)? Integer.parseInt(arg) : 600;
} catch(NumberFormatException e) {
height = 600;
}
Dimension d = new Dimension(width, height);
applet.setPreferredSize(d);
applet.setSize(d);
f.addWindowListener(new WindowListener()
{
public void windowActivated(WindowEvent e)
{
applet.start();
}
public void windowClosed(WindowEvent e)
{
System.exit(0);
}
public void windowClosing(WindowEvent e)
{
applet.stop();
applet.destroy();
f.dispose();
}
public void windowDeactivated(WindowEvent e)
{
applet.stop();
}
public void windowDeiconified(WindowEvent e)
{
applet.start();
}
public void windowIconified(WindowEvent e)
{
applet.stop();
}
public void windowOpened(WindowEvent e)
{
}
});
applet.init();
f.add(applet);
f.pack();
f.setVisible(true);
}
}