okay, i bad selv om det :)
først compilerer jeg denne, (som virker fint)...
//Author: Henrik Høltzer
//Date: 11.maj 2002
//Version: 2.0
//Spil - "Gæt et tal" med GUI
//Distribueret Client/Server
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.io.Serializable;
import java.net.*;
import java.util.ArrayList;
//
//************ AWT - class GUISpilTester********************************************
//
class GUISpilTester extends Frame{
private TopTiConnection topTiConnection = null;
private SpilConnection spilConnection = null;
private Panel topPanel;
private Panel midtPanel;
private Panel bundPanel;
private Label topLabel, topTiLabel, forsøgLabel;
private Label infoLabel1, infoLabel2, infoLabel3, infoLabel4;
private Label navnLabel, gætLabel, svarLabel, antalLabel;
private TextField navnTextField, gætTextField, svarTextField, antalTextField;
private Button nytSpilButton, afslutButton, nulstilButton, gætButton;
private GUITopTiListObserver topTiList;
private java.awt.List forsøgList;
public GUISpilTester(TopTiConnection topTiConnection, SpilConnection spilConnection){
this.topTiConnection = topTiConnection;
this.spilConnection=spilConnection;
topPanel = new Panel();
midtPanel = new Panel();
bundPanel = new Panel();
Font overskrift, label;
add(topPanel);
add(midtPanel);
add(bundPanel);
setLayout(null);
topPanel.setLayout(null);
topPanel.setBounds(0,0,800,75);
midtPanel.setBounds(0,75,800,450);
bundPanel.setBounds(0,525,800,75);
bundPanel.setBackground(Color.blue);
nytSpilButton = new Button("Nyt Spil");
nytSpilButton.addActionListener(new NytSpilListener());
afslutButton = new Button("Afslut");
afslutButton.addActionListener(new AfslutListener());
nulstilButton = new Button("Nulstil TopTi");
gætButton = new Button("Nyt Gæt");
gætButton.setBounds(5,150, 150,25);
gætButton.addActionListener(new GætListener());
midtPanel.add(gætButton);
nytSpilButton.setBounds(10,5,100,40);
afslutButton.setBounds(125,5,100,40);
nulstilButton.setBounds(600,5,150,40);
// nulstilButton.addActionListener(new NulstilListener());
bundPanel.add(nytSpilButton);
bundPanel.add(afslutButton);
topTiLabel = new Label("Top Ti Liste");
midtPanel.add(topTiLabel);
topTiLabel.setBounds(535,5,250,25);
label = new Font("SansSerif", Font.BOLD, 16);
topTiLabel.setFont(label);
topTiLabel.setForeground(Color.white);
topTiLabel.setBackground(Color.gray);
topTiList = new GUITopTiListObserver(10+1, topTiConnection);
midtPanel.add(topTiList);
topTiList.setBounds(535,40,250,350);
forsøgLabel = new Label("Tidligere Gæt");
midtPanel.add(forsøgLabel);
forsøgLabel.setBounds(335,5,150,25);
label = new Font("SansSerif", Font.BOLD, 16);
forsøgLabel.setFont(label);
forsøgLabel.setForeground(Color.white);
forsøgLabel.setBackground(Color.gray);
forsøgList = new java.awt.List(20);
midtPanel.add(forsøgList);
forsøgList.setBounds(335,40,150,350);
bundPanel.add(nulstilButton);
bundPanel.setLayout(null);
overskrift = new Font("SansSerif", Font.BOLD, 24);
topLabel = new Label("SPIL: Gæt et tal mellem 0 og 10.000");
topLabel.setFont(overskrift);
topPanel.add(topLabel);
topLabel.setForeground(Color.white);
topLabel.setBackground(Color.gray);
topLabel.setBounds(5,0,800,75);
midtPanel.setLayout(null);
navnLabel = new Label("Spiller navn");
gætLabel = new Label ("Indtast Gæt");
svarLabel = new Label("Svar");
antalLabel = new Label("Antal gæt");
infoLabel1 = new Label("Vejledning");
infoLabel2 = new Label("Indtast navn, herefter klikkes på Nyt Spil");
infoLabel3 = new Label("Nu kan der indtastes et Gæt, når det er ");
infoLabel4 = new Label("foretaget - klikkes på Nyt Gæt osv.");
label = new Font("SansSerif", Font.BOLD, 16);
infoLabel1.setFont(label);
infoLabel1.setForeground(Color.white);
infoLabel1.setBackground(Color.gray);
infoLabel2.setForeground(Color.white);
infoLabel2.setBackground(Color.gray);
infoLabel3.setForeground(Color.white);
infoLabel3.setBackground(Color.gray);
infoLabel4.setForeground(Color.white);
infoLabel4.setBackground(Color.gray);
navnLabel.setForeground(Color.white);
navnLabel.setBackground(Color.gray);
gætLabel.setForeground(Color.white);
gætLabel.setBackground(Color.gray);
svarLabel.setForeground(Color.white);
svarLabel.setBackground(Color.gray);
antalLabel.setForeground(Color.white);
antalLabel.setBackground(Color.gray);
navnLabel.setBounds(5,5, 150,25);
gætLabel.setBounds(5,40, 150,25);
svarLabel.setBounds(5,70, 150,25);
antalLabel.setBounds(5,100, 150,25);
infoLabel1.setBounds(5, 200, 250, 20);
infoLabel2.setBounds(5, 220, 250, 15);
infoLabel3.setBounds(5, 235, 250, 15);
infoLabel4.setBounds(5, 250, 250, 20);
navnTextField = new TextField("",15);
gætTextField = new TextField(15);
svarTextField = new TextField(15);
antalTextField = new TextField(15);
navnTextField.setBounds(160,5,150,25);
gætTextField.setBounds(160, 40,150,25);
svarTextField.setBounds(160,70,150,25);
antalTextField.setBounds(160,100,150,25);
midtPanel.add(navnLabel);
midtPanel.add(gætLabel);
midtPanel.add(svarLabel);
midtPanel.add(antalLabel);
midtPanel.add(infoLabel1);
midtPanel.add(infoLabel2);
midtPanel.add(infoLabel3);
midtPanel.add(infoLabel4);
midtPanel.add(navnTextField);
midtPanel.add(gætTextField);
midtPanel.add(svarTextField);
midtPanel.add(antalTextField);
show();
new Thread(topTiConnection).start();
}
//
//***************INNERCLASS class GUITopTiList*****************************
//
class GUITopTiListObserver extends java.awt.List implements Observer{
public GUITopTiListObserver(int size, TopTiConnection topTi){super(size); topTi.addObserver(this);};
public void update (Observable obs, Object topTiList){
java.util.List topliste = (java.util.List)topTiList;
Spiller s;
this.removeAll();
for (int i=0; i<topliste.size(); i++){
s = (Spiller)topliste.get(i);
this.add(s.getNavn() + ": " + s.getAntal());
}
}
}
//
//***************INNERCLASS class GUIForsøgListObserver*************************
//
//UDGÅR: Opdateringen af forsøgsliste flyttes ind under GætListener's actionPerformed
//
// class GUIForsøgListObserver extends java.awt.List implements Observer{
//
// public GUIForsøgListObserver(int size){super(size);};
//
// public void update (Observable obs, Object dummy){
// this.add(gætTextField.getText() + " " + svarTextField.getText());
// }
// }
//
//***************INNERCLASS class Lukker*************************
//
class Lukker extends WindowAdapter{
public void windowClosing(WindowEvent e){
spilConnection.close();
System.exit(1);
}
}
//
//***************INNERCLASS class AfslutListener*************************
//
class AfslutListener implements ActionListener{
public void actionPerformed(ActionEvent e){
spilConnection.close();
System.exit(1);
};
}
//
//***************INNERCLASS class NulstilListener*************************
//
// class NulstilListener implements ActionListener{
// public void actionPerformed(ActionEvent e){topTi.nulstil();};
// }
//
//***************INNERCLASS class NytSpilListener*************************
//
class NytSpilListener implements ActionListener {
public void actionPerformed(ActionEvent e){
if ((navnTextField.getText()).equals("")) svarTextField.setText("indtast navn først");
else {
try{
spilConnection.send("nyt" + " " + navnTextField.getText());
forsøgList.removeAll();
svarTextField.setText(spilConnection.receive());
}
catch (IOException IOEx){System.err.println("IOException");};
}
}
}
//
//***************INNERCLASS class GætListener*************************
//
class GætListener implements ActionListener{
private String svar=null;
private int gæt;
public void actionPerformed(ActionEvent e){
if (gætTextField.getText() == null) svarTextField.setText("indtast dit gæt ovenfor");
else {
try{
gæt=Integer.parseInt(gætTextField.getText());
spilConnection.send("guess" + " " + gæt);
svarTextField.setText(spilConnection.receive());
forsøgList.add(gætTextField.getText() + " " + svarTextField.getText());
}
catch (IOException IOEx){}
}
}
}
}
//
//**************** class SpilConnection ******************************
//
class SpilConnection{
private String host = "127.0.0.1";
// private int port = 1026;
private Socket sock = null;
private BufferedReader input = null;
private PrintWriter output = null;
public SpilConnection(int port){
try {
sock = new Socket(host, port);
System.out.println("Spil client forbundet til " + sock.toString());
input = new BufferedReader(
new InputStreamReader( sock.getInputStream() ) );
output = new PrintWriter( sock.getOutputStream(), true );
}
catch (UnknownHostException UnHoEx) { // new Socket
System.err.println("Ukendt host: " + host);
System.err.println(UnHoEx);
}
catch (NoRouteToHostException ex) { // new Socket
System.err.println("Ingen forbindelse til host: " + host);
System.err.println(ex);
}
catch (ConnectException ex) { // new Socket
System.err.println("Kan ikke forbinde til " + host + " port " + port);
System.err.println(ex);
}
catch (SocketException ex) {
System.err.println("Serveren har pludselig afbrudt forbindelsen");
System.err.println(ex);
}
catch (IOException CoEx) { // new Socket
System.err.println("Ukendt fejl ved skabelse af forbindelse");
System.err.println(CoEx);
}
}
public void send(String request) {
System.out.println("SpilConnection: send()");
output.println(request);}
public String receive() throws IOException {return input.readLine();}
public void close(){
try {
if (input != null) input.close();
if (output != null) output.close();
if (sock != null) sock.close();
}
catch (IOException ex) {
System.err.println("Fejl ved lukning af forbindelser");
System.err.println(ex);
}
}
}
//
//****************** class TopTiConnection ***************************
//
class TopTiConnection extends Observable implements Runnable{
private String host = "127.0.0.1";
private int port = 1025;
private Socket sock = null;
private ObjectInputStream input = null;
public TopTiConnection(int portnr) {port=portnr;}
public void run(){
try {
ArrayList toptiliste;
sock = new Socket(host, port);
System.out.println("Spil client forbundet til " + sock.toString());
input = new ObjectInputStream( sock.getInputStream());
while (true) {
try{ toptiliste = (ArrayList) input.readObject();
setChanged();
notifyObservers(toptiliste);}
catch (Exception e){};
}
}
catch (UnknownHostException UnHoEx) { // new Socket
System.err.println("Ukendt host: " + host);
System.err.println(UnHoEx);
}
catch (NoRouteToHostException ex) { // new Socket
System.err.println("Ingen forbindelse til host: " + host);
System.err.println(ex);
}
catch (ConnectException ex) { // new Socket
System.err.println("Kan ikke forbinde til " + host + " port " + port);
System.err.println(ex);
}
catch (SocketException ex) {
System.err.println("Serveren har pludselig afbrudt forbindelsen");
System.err.println(ex);
}
catch (IOException CoEx) { // new Socket
System.err.println("Ukendt fejl ved skabelse af forbindelse");
System.err.println(CoEx);
}
finally { // udføres til sidst uanset exception eller ej
try {
if (input != null) input.close();
if (sock != null) sock.close();
}
catch (IOException ex) {
System.err.println("Fejl ved lukning af forbindelser");
System.err.println(ex);
}
} // finally
}// run
}// class
//
//******************* DRIVERCLASS: class SpilClient *******************
//
public class SpilClient {
public static void main(String[] args){
TopTiConnection topticonnection = new TopTiConnection(1025);
SpilConnection spilconnection = new SpilConnection(1026);
new GUISpilTester(topticonnection, spilconnection);
}
}
//
//********* class spiller**********************************************
//
class Spiller extends Observable implements Serializable{
private String navn;
private int gæt;
private int antal;
private transient Spil spil;
private transient TopTi topTi;
public Spiller(String navn, Spil spil, TopTi topTi){
this.navn = navn;
this.spil=spil;
this.topTi=topTi;
antal=0;
};
public String gæt(int tal){
antal++;
switch (spil.forsøg(tal)){
case -1: return "for lavt";
case 1: return "for højt";
case 2: return "tal er gættet - prøv nyt spil";
case 0: {topTi.checkResultat(this);
return "godt gættet";};
}
return null;
}
public String getNavn(){return navn;};
public int getAntal(){return antal;};
public void setAntal(int nytantal){antal=nytantal;};
public int getGæt(){return gæt;};
public void setGæt(int nytGæt){
gæt=nytGæt;
setChanged();
notifyObservers();
};
public Spil getSpil(){return spil;};
}
okay fik byttet om på dem..
den første kode giver fejlene, og det er denne jeg compilerer først:
import java.net.*; // ServerSocket, Socket
import java.io.*; // BufferedReader, PrintWriter, IOException
import java.util.*;
import java.io.Serializable;
import java.util.ArrayList;
//
//********* class spiller**********************************************
//
class Spiller extends Observable implements Serializable{
private String navn;
private int gæt;
private int antal;
private transient Spil spil;
private transient TopTi topTi;
public Spiller(String navn, Spil spil, TopTi topTi){
this.navn = navn;
this.spil=spil;
this.topTi=topTi;
antal=0;
};
public String gæt(int tal){
antal++;
switch (spil.forsøg(tal)){
case -1: return "for lavt" + antal;
case 1: return "for højt" + antal;
case 2: return "tal er gættet - prøv nyt spil" + antal;
case 0: {topTi.checkResultat(this);
return "godt gættet" + antal;}
}
return "øv" + antal;
}
public String getNavn(){return navn;};
public int getAntal(){return antal;};
public void setAntal(int nytantal){antal=nytantal;};
public int getGæt(){return gæt;};
public void setGæt(int nytGæt){
gæt=nytGæt;
setChanged();
notifyObservers();
};
public Spil getSpil(){return spil;};
}
//
//**********class Spil**********************************************
//
class Spil{
private int tal;
private boolean gættet=true;
public int getTal(){return tal;};
public boolean getGættet(){return gættet;};
public void nytSpil(){
tal=(int)(Math.random()*10000); //genererer et tilfældigt tal mellem 1 og 10.000
System.out.println("Tallet...." + tal);
gættet=false;
}
public int forsøg(int gæt){
if (gættet==false){
if (gæt<tal) return -1;
else if (gæt>tal) return 1;
else {
gættet=true;
return 0;
}
}
else return 2;
}
}
//
//****************** class SpillerComperator******************************
//
class SpillerComparator implements Comparator {
public int compare(Object o1, Object o2){
int antal1 = ((Spiller) o1).getAntal();
int antal2 = ((Spiller) o2).getAntal();
if (antal1 < antal2) return -1;
else if (antal1 > antal2) return 1;
else return 0;
}
}
//
//*******************class TopTi*******************************************
//
class TopTi {
private static int size=10;
private ArrayList topTiListe = new ArrayList(size);
private static SpillerComparator sc = new SpillerComparator();
public synchronized void nulstil(){
topTiListe.clear();
notifyAll();
}
public synchronized void checkResultat(Spiller spiller){
if (topTiListe.size()<size) {
topTiListe.add(spiller);
Collections.sort(topTiListe, sc);
notifyAll();
}
else if (((Spiller)(topTiListe.get(size))).getGæt()>spiller.getGæt()) {
topTiListe.set(size, spiller);
Collections.sort(topTiListe, sc);
notifyAll();
}
}
public synchronized ArrayList nyTopTi(){
try {wait();}
catch (InterruptedException e){};
return topTiListe;
}
public ArrayList getTopTiListe() {return topTiListe;}
}
//
//*************************** class: TopTiService **********************************
//
class TopTiService extends Thread{
private Socket connection;
private TopTi topTi;
public TopTiService(Socket connect, TopTi topTi) {connection = connect; this.topTi=topTi;}
public void run(){
try {
ObjectOutputStream output = new ObjectOutputStream(connection.getOutputStream());
output.writeObject(new ArrayList(topTi.getTopTiListe()));
while(true){
output.writeObject(new ArrayList(topTi.nyTopTi()));
}
}
catch (SocketException SoEx) {
System.err.println("Forbindelsen er gået ned ");
System.err.println(SoEx);
}
catch (IOException IOEx) { // new ServerSocket, accept m.fl.
System.err.println("Ukendt fejl");
System.err.println(IOEx);
}
finally {
// Udføres uanset om metoden afsluttes på normal vis
// eller pga. exception,
// så forbindelsen lukkes under alle omstændigheder.
try {
connection.close();
}
catch (IOException ex) { // close
System.err.println("Den er gal med close");
System.err.println(ex);
}
}
}
}
//
//*********************** class: SpilService *****************************
//
class SpilService extends Thread {
private Socket connection;
private Spiller spiller;
private Spil spil= new Spil();
private TopTi topti;
public SpilService(Socket connect, TopTi topti) {
connection = connect;
this.topti = topti;
}
public static LinkedList intoWords(String line){
LinkedList wordlist = new LinkedList();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {wordlist.add(tokenizer.nextToken());};
return wordlist;
}
private String behandle(String request){
LinkedList ordliste = intoWords(request);
String reply=null;
if (((String)ordliste.get(0)).equals("nyt")) {
spiller = new Spiller((ordliste.get(1)).toString(), spil, topti);
spil.nytSpil();
reply = "Nyt spil: indtast gæt" + spiller.getAntal();
}
if (((ordliste.get(0)).toString()).equals("guess")) {
reply = spiller.gæt(Integer.parseInt((ordliste.get(1)).toString()));
}
return reply;
}
public void run() {
System.out.println("Forbindelse med klient: " + connection.toString());
BufferedReader input= null;
PrintWriter output= null;
try {
input = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );
output = new PrintWriter( connection.getOutputStream(), true );
String request;
while ((request = input.readLine()) != null) {
output.println(behandle(request));
}
}
catch (SocketException SoEx) {
System.err.println("Forbindelsen er gået ned.");
System.err.println(SoEx);
}
catch (IOException IOex) {
System.err.println("Ukendt fejl.");
System.err.println(IOex);
}
finally {
// Udføres uanset om metoden afsluttes på normal vis
// eller pga. exception,
// så forbindelsen lukkes under alle omstændigheder.
try {
input.close();
output.close();
connection.close();
}
catch (IOException ex) { // close
System.err.println("Den er gal med close");
System.err.println(ex);
}
}
}//end_run
}//end_SpilService
//
//**************************** class: Connection **************************
//
class Connection extends Thread {
private static int toptiportnr = 1025;
private static int spilportnr = 1026;
private TopTi topti;
private int portnr;
public Connection(int portNr, TopTi topti) {portnr=portNr; this.topti=topti;}
public void run() {
try {
ServerSocket server;
Socket connection;
TopTiService topTiService;
SpilService spilService;
if (portnr == toptiportnr) {
server = new ServerSocket(portnr);
System.out.println("Topti server startet paa port " + portnr);
while (true) {
connection = server.accept();
topTiService = new TopTiService(connection, topti);
topTiService.start();
}//end_while
} //end_if
else {
server = new ServerSocket(portnr);
System.out.println("Spil server startet paa port " + portnr);
while (true) {
connection = server.accept();
spilService = new SpilService(connection, topti);
spilService.start();
}
}//end_else
} // end_try
catch (BindException BiEx) { // new ServerSocket
System.err.println("Server kan ikke bindes til port " + portnr);
}//end_catch
catch (IOException IOEx) { // new ServerSocket, accept m.fl.
System.err.println("Ukendt fejl");
System.err.println(IOEx);
}//end_catch
} //end_run
}//end_Connection
//
//**************************** class: SpilServer *************************
//
class SpilServer{
public static void main(String [] args) {
TopTi topti = new TopTi();
Connection toptiServer = new Connection(1025, topti);
Connection spilServer = new Connection(1026, topti);
toptiServer.start();
spilServer.start();
}//endmain
}//endSpilServer