import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.*;
/**
*
* Beschreibung.
*
* @version 1.0 vom 19.08.2004
* @author Bernd */
public class UDP_S extends JFrame {
// Anfang Variablen
static final JTextPane jtp1 = new JTextPane();
static final String newline = System.getProperty("line.separator");
private JTextPane jtpeingabe = new JTextPane();
private JButton jbusenden = new JButton();
private JScrollPane jsp1 = new JScrollPane();
private JButton jbuende = new JButton();
private JScrollPane jsp2 = new JScrollPane();
private JButton jbubeenden = new JButton();
private JToolBar tb = new JToolBar();
private JButton b1 = new JButton(new ImageIcon(UDP_S.class.getResource("bart.gif")));
private JButton b2 = new JButton(new ImageIcon(UDP_S.class.getResource("barney.gif")));
private JButton b3 = new JButton(new ImageIcon(UDP_S.class.getResource("mill.gif")));
private JButton b4 = new JButton(new ImageIcon(UDP_S.class.getResource("smile1.gif")));
private JPanel panel = new JPanel();
private JComboBox jcbIP = new JComboBox();
// Ende Variablen
public UDP_S(final String title){
// Frame-Initialisierung
super("Bernd sein Intranet - Chat, Version 1.0");
addWindowListener(new WindowAdapter() {
public void windowClosing(final WindowEvent evt) { System.exit(0); } });
setBounds(20, 20, 600, 525);
Container cp = getContentPane();
cp.setLayout(null);
// Anfang Komponenten
tb.add(b1);
tb.add(b2);
tb.add(b3);
tb.add(b4);
tb.setRollover(true);
tb.setFloatable(false);
//tb.setBounds(330, 50, 50, 50);
panel.add(tb);
panel.setBackground(Color.green);
panel.setBounds(310, 40, 170, 265);
getContentPane().add(panel);
b1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
b1ActionPerformed(evt); } });
b2.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
b2ActionPerformed(evt); } });
b3.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
b3ActionPerformed(evt); } });
b4.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
b4ActionPerformed(evt); } });
jtp1.setBounds(5, 5, 300, 370);
jtp1.setEnabled(false);
jtp1.setContentType("TEXT/RTF");
jtp1.setText("Willkommen im Chat" + newline);
jtpeingabe.setBounds(5, 370, 300, 80);
getContentPane().add(jtp1);
getContentPane().add(jtpeingabe);
jbusenden.setText("senden");
jbusenden.setBounds(5, 455, 80, 25);
getContentPane().add(jbusenden);
jbusenden.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
jbusendenActionPerformed(evt); } });
jsp1.setViewportView(jtp1);
jsp1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
jsp1.setBounds(5, 5, 300, 300);
getContentPane().add(jsp1);
jsp2.setBounds(5, 370, 300, 80);
jsp2.setViewportView(jtpeingabe);
getContentPane().add(jsp2);
jbubeenden.setText("beenden");
jbubeenden.setBounds(480, 455, 100, 25);
getContentPane().add(jbubeenden);
jbubeenden.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
jbubeendenActionPerformed(evt); } });
jcbIP.addItem("");
jcbIP.addItem("192.168.0.2");
jcbIP.addItem("192.168.0.3");
jcbIP.setEditable(true);
jcbIP.setBounds(310, 5, 100, 25);
jcbIP.setToolTipText("IP des Zielrechners eingeben!");
getContentPane().add(jcbIP);
// Ende Komponenten
setVisible(true);
}
// Anfang Ereignisprozeduren
public void b1ActionPerformed(final ActionEvent evt) {
ImageIcon img1 = new ImageIcon(UDP_S.class.getResource("bart.gif"));
jtpeingabe.insertIcon(img1);
jtpeingabe.requestFocus();
}
public void b2ActionPerformed(final ActionEvent evt) {
ImageIcon img2 = new ImageIcon(UDP_S.class.getResource("barney.gif"));
jtpeingabe.insertIcon(img2);
jtpeingabe.requestFocus();
}
public void b3ActionPerformed(final ActionEvent evt) {
ImageIcon img3 = new ImageIcon(UDP_S.class.getResource("mill.gif"));
jtpeingabe.insertIcon(img3);
jtpeingabe.requestFocus();
}
public void b4ActionPerformed(final ActionEvent evt) {
ImageIcon img4 = new ImageIcon(UDP_S.class.getResource("smile1.gif"));
jtpeingabe.insertIcon(img4);
jtpeingabe.requestFocus();
}
public void jbusendenActionPerformed(final ActionEvent evt) {
try
{
DatagramPacket packet;
String ipAdresse = (String) jcbIP.getSelectedItem();
InetAddress ia = InetAddress.getByName( ipAdresse );
String s = jtpeingabe.getText();
packet = new DatagramPacket( s.getBytes(),s.length(),ia,4711 );
DatagramSocket dSocket = new DatagramSocket();
dSocket.send( packet );
String textanzeige = jtp1.getText();
jtp1.setText(textanzeige + "\n" + s);
jtpeingabe.setText("");
jtpeingabe.requestFocus();
dSocket.close();
}
catch ( Exception e )
{
System.out.println( e );
}
}
public void jbubeendenActionPerformed(final ActionEvent evt) {
System.exit(0);
}
// Ende Ereignisprozeduren
public static void main(final String[] args) {
new UDP_S("UDP_S");
try
{
DatagramSocket socket = new DatagramSocket(4711);
DatagramPacket packet;
while (true)
{
// auf Anfrage warten
packet = new DatagramPacket(new byte[1024], 1024);
socket.receive(packet);
//Empfang auslesen
int port = packet.getPort();
int len = packet.getLength();
byte data[] = packet.getData();
String textanzeige = jtp1.getText();
jtp1.setText(textanzeige + "\n" + new String(data, 0, len));
}
}
catch (Exception e)
{
System.out.println(e);
}
}
}