📄 guestbook.java
字号:
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
public class guestbook extends Applet implements MouseMotionListener, MouseListener,Runnable, ActionListener {
Thread t; //main thread
Graphics grBuffer; //main graphics object
Image imgBuffer;
int height,width; //height and width of applet
boolean viewing,signing;
String name[] = new String [50000];
String email[] = new String [50000];
String web[] = new String [50000];
String comment[] = new String [50000];
String lines[] = new String [200000];
int entryLines[]=new int [50000];
int finishLines[]=new int [50000];
int numEntries=0;
int numLines=0;
boolean upScrol,downScrol;
int Scrol;
TextArea txtComment;
TextField txtName;
TextField txtWeb;
TextField txtEmail;
Button btnSign;
int paintDelay=0;
static int randomNumber (int low, int high) {
// returns a random number between low and high, inclusive
return (int)(java.lang.Math.random() * (high - low + 1) + low);
} // randomNumber
public void init () {
height = 600; //get height and width
width = 800;
imgBuffer = createImage (width, height); //make buffer image
grBuffer = imgBuffer.getGraphics (); //associate buffer with buffer image
viewing=true;
grBuffer.setFont (new Font ("Courier", Font.PLAIN , 12));
this.addMouseMotionListener (this);
this.addMouseListener (this);
setLayout(null);
txtComment=new TextArea();
txtComment.setBounds(190,320,500,200);
txtName=new TextField();
txtName.setBounds(190,110,500,20);
txtEmail=new TextField();
txtEmail.setBounds (190,180,500,20);
txtWeb=new TextField();
txtWeb.setBounds (190,250,500,20);
btnSign=new Button ("SIGN");
btnSign.setBounds (362,550,75,35);
btnSign.addActionListener(this);
newView ();
} //inint
public void paint (Graphics g) {
int p,t,a;
paintDelay++;
if (paintDelay>=3) {
grBuffer.setColor (Color.gray);
grBuffer.fillRect (0,0,width,height);
grBuffer.setColor (Color.yellow);
grBuffer.fillRect (275,10,100,50);
grBuffer.fillRect (425,10,100,50);
grBuffer.setColor (Color.black);
grBuffer.drawRect (275,10,100,50);
grBuffer.drawRect (425,10,100,50);
grBuffer.drawString ("SIGN",308,40);
grBuffer.drawString ("VIEW",458,40);
grBuffer.drawLine (0,80,800,80);
if (viewing==true) {
grBuffer.setColor (Color.white);
grBuffer.fillRect (20,100,700,480);
grBuffer.setColor (Color.yellow);
grBuffer.fillRect (745,100,30,480);
grBuffer.setColor (Color.black);
grBuffer.drawRect (20,100,700,480);
grBuffer.drawRect (745,100,30,480);
grBuffer.drawLine (745,130,775,130);
grBuffer.drawLine (745,340,775,340);
grBuffer.drawLine (745,550,775,550);
grBuffer.drawLine (760,105,760,125);
grBuffer.drawLine (760,555,760,575);
grBuffer.drawLine (760,105,750,115);
grBuffer.drawLine (760,105,770,115);
grBuffer.drawLine (760,575,750,565);
grBuffer.drawLine (760,575,770,565);
a=0;
p=Scrol;
int v=0;
while (a<33) {
if (p<numLines) {
// if (x>25 && y>103+(a*14) && x<25+(lines[p].length()*7) && y<116+(a*14))
for (v=0;v<numEntries;v++) if (entryLines[v]==p+2) grBuffer.drawLine (25,114+(a*14),25+lines[p].length()*7,114+(a*14));
grBuffer.drawString (lines[p],25,113+(a*14));
}
a=a+1;
if (p<numLines)
for (t=0;t<numEntries;t++) {
if (finishLines[t]==p+1) {
grBuffer.drawLine (25,113+(a*14),720,113+(a*14));
a++;
}//if
}// for t
p=p+1;
}//while
}
if (signing==true) {
grBuffer.setFont (new Font ("Courier", Font.BOLD , 14));
grBuffer.setColor (Color.black);
grBuffer.drawString ("Name:",25,123);
grBuffer.drawString ("Email: (optional)",25,193);
grBuffer.drawString ("Website: (optional)",25,263);
grBuffer.drawString ("Comment:",25,333);
grBuffer.setFont (new Font ("Courier", Font.PLAIN , 12));
}
g.drawImage (imgBuffer, 0, 0, this);
paintDelay=0;
}
}
private void sendData () {
System.out.println ("sending...");
String IPAdd="";
int port=0;
Socket clientsocket;
DataOutputStream out;
IPAdd = getParameter("IPAdd");
port=Integer.parseInt (getParameter("PortNumber"));
try {
clientsocket = new Socket(IPAdd, port); //IPAdd "localHost"
out = new DataOutputStream(clientsocket.getOutputStream());
String msg="";
msg=txtName.getText()+"!@#ZX"+txtEmail.getText()+"!@#ZX"+txtWeb.getText()+"!@#ZX"+txtComment.getText()+"!@#ZX";
out.writeUTF(msg);
out.close();
} catch (Exception e) {
System.out.println ("Couldnt connect to server");
} //exception
}//senddata
private void newView () {
String fileName;
fileName = getParameter("fileName");
int tempInt1=0;
char charRead='a';
String tempString="";
int p,t,a;
numEntries=0;
a=0;
try {
URL url = new URL(getCodeBase(), fileName);
try {
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
do {
tempInt1=in.read();
charRead= (char) (tempInt1);
tempString=tempString + charRead;
if (tempString.endsWith ("!@#ZX")) {
if (a==0)
name[numEntries]=tempString.substring (0,tempString.length()-5);
if (a==1)
email[numEntries]=tempString.substring (0,tempString.length()-5);
if (a==2)
web[numEntries]=tempString.substring (0,tempString.length()-5);
if (a==3)
comment[numEntries]=tempString.substring (0,tempString.length()-5);
a=a+1;
if (a==4) {
a=0;
numEntries++;
}
tempString="";
}//endswith
} while (tempInt1 != -1);
in.close ();
} catch (IOException e) { }
} catch (MalformedURLException e) { }
a=0;
t=0;
int g=0;
for (p=0;p<numEntries;p++){
lines[a++]=name[p];
lines[a++]=email[p];
lines[a++]=web[p];
entryLines[p]=a+1;
g=0;
comment[p]=comment[p]+(char) (10);
for (t=0;t<comment[p].length();t++) {
if ((int) (comment[p].charAt (t)) == 10) {
if (t-g>1) lines[a++]=comment[p].substring (g,t); else {
if (comment[p].charAt(g)!=(char) (13)) lines[a++]=comment[p].substring (g,t); else
lines[a++]=" ";
}
g=t+1;
}//if
}//t
finishLines[p]=a;
}//p
numLines=a;
}//newview
public void run() {
while (true) {
if (downScrol==true) {
if (Scrol+26<numLines) {
Scrol+=1;
if (Scrol>500000) Scrol=500000;
}
}
if (upScrol==true) {
Scrol-=1;
if (Scrol<0) Scrol=0;
}
repaint ();
try {
t.sleep(75);
} catch (InterruptedException e) { }
} //while
} //run
public void start() {
if (t==null) {
t = new Thread(this);
}
t.start();
}
public void stop() {
t = null;
}
public void update (Graphics g) {
paint (g);
}
public void mouseMoved (MouseEvent me) {
}
public void mouseClicked (MouseEvent me) {
int x,y,p,a,t;
x=me.getX();
y=me.getY();
if (viewing==true) {
if (x>745 && x<775 && y>100 && y<340) Scrol--;
if (Scrol+26<numLines) if (x>745 && x<775 && y>340 && y<580) Scrol++;
if (Scrol<0) Scrol=0;
p=Scrol;
a=0;
while (a<33) {
if (p<numLines)
if (x>25 && y>103+(a*14) && x<25+(lines[p].length()*7) && y<116+(a*14)) {
if (lines[p].indexOf ("http://")==0 || lines[p].indexOf ("www.")==0) {
try {URL url = new URL(getCodeBase(),lines[p]);
getAppletContext().showDocument(url, "_new");
} catch (MalformedURLException f) { }
}
if (lines[p].indexOf ("@")!=-1 ) {
try {URL url1 = new URL(getCodeBase(),"mailto:"+lines[p]);
getAppletContext().showDocument(url1, "_new");
} catch (MalformedURLException f) { }
}
}
a=a+1;
if (p<numLines)
for (t=0;t<numEntries;t++) if (entryLines[t]==p+1) a++;
p++;
}//while
} // chooseSymb
if (x>275 && y>10 && x<375 && y<60) {
signing=true;
viewing=false;
add(txtName);
add(txtEmail);
add(txtWeb);
add(txtComment);
add(btnSign);
}
if (x>425 && y>10 && x<525 && y<60) {
newView();
viewing=true;
signing=false;
remove(txtName);
remove(txtComment);
remove(txtEmail);
remove(txtWeb);
remove(btnSign);
}
}
public void mouseDragged (MouseEvent e) {
}
public void mousePressed (MouseEvent me) {
int x,y,t,p;
x=me.getX();
y=me.getY();
if (viewing==true) {
if (x>745 && x<775 && y>100 && y<340)
upScrol=true;
//check for scroll bar
if (x>745 && x<775 && y>340 && y<580)
downScrol=true;
}
}
public void mouseReleased (MouseEvent me) {
downScrol=false;
upScrol=false;
}
public void mouseEntered (MouseEvent me) {
}
public void mouseExited (MouseEvent me) {
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==btnSign) {
sendData ();
viewing=true;
signing=false;
remove(txtName);
remove(txtComment);
remove(txtEmail);
remove(txtWeb);
remove(btnSign);
}
int z=0;
for (int p=0;p<1000000;p++) {
int a=p;
p=p*p;
p=a;
z=z+10/2+p/2;
}
newView ();
}
} //class spacefly
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -