Applets im Internet |
|
Java Applets dürfen Dateien nur auf dem Server öffnen. Das Applet kann mit getDocumentBase() feststellen, aus welcher HTM-Datei es selbst aufgerufen wurde. Daraus kann man dann schliessen, in welchem Verzeichnis etc. das Applet läuft.
Das Einlesen einer Datei erfolgt mittels eines InputStream und eines BufferedReader.
Dateibehandlung muss mit Exception-Handling abgesichert werden.
Der eingelesene Text wird dann an ein Textfeld übergeben: Applet Quelltext
import java.awt.*;
import java.applet.*;
import java.applet.Applet;
import java.net.*;
import java.io.*;
public class Schrift extends Applet{
private final static char TrennZeichen = '/';
private String DateiName = null;
private String datei_lesen(){
String DateiInhalt = new String();
String urlString = getCodeBase().toString();
urlString = urlString.substring(0,urlString.lastIndexOf(TrennZeichen))+TrennZeichen+DateiName;
try{
URL url = new URL( urlString);
InputStream inStream = url.openStream();
BufferedReader dataStream = new BufferedReader( new InputStreamReader( inStream));
String neueZeile = null;
while(( neueZeile = dataStream.readLine()) != null){
DateiInhalt += (neueZeile+"\n");
}
}
catch(MalformedURLException e){
showStatus("Error " + urlString );
}
catch(IOException e ){
showStatus("Error " + e );
}
return DateiInhalt;
}
public void init(){
DateiName = getParameter("DateiName");
setBackground(Color.yellow);
TextArea TextFeld = new TextArea();
TextFeld.setText(datei_lesen());
add(TextFeld);
}
}
import java.applet.Applet;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonDemo extends Applet{
URL url;
Button myButton = new Button("Testseite");
public class ButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent e)
{
if( url != null){
getAppletContext().showDocument(url,"_self");
}
}
}
public void init()
{
setBackground(Color.yellow);
add(myButton);
myButton.addActionListener(new ButtonListener());
try{
url = new URL(getDocumentBase(),"testseite.htm");
}catch( MalformedURLException e){
}
}
}
import java.applet.*;
import java.net.*;
import java.awt.*;
import java.util.*;
public class Bild extends Applet{
Image bild;
URL u;
String S,T,R,I,N,G;
String BildDatei;
String CopyRight;
public void init()
{
BildDatei=getParameter("Bild");
CopyRight=getParameter("CopyRight");
new String( "DOCUMENTBASE von Bild: "+getDocumentBase());
try {
u = new URL(getDocumentBase(), BildDatei);
bild = getImage( u );
MediaTracker Ladekontrolle = new MediaTracker(this);
Ladekontrolle.addImage( bild, 0 );
Ladekontrolle.waitForID(0);
}
catch(InterruptedException e ){
}
catch(MalformedURLException x){
}
I = new String("Man sieht "+u.toString());
repaint();
}
public void paint(Graphics graf)
{
graf.drawImage(bild, 0, 0, this);
setBackground(Color.yellow);
graf.drawString( I, 0,381 );
graf.drawString( CopyRight, 190,395 );
}
}
import java.applet.*;
import java.net.*;
import java.awt.*;
import java.util.*;
public class Klang extends Applet{
URL u ;
String S,T,R;
String SoundDatei;
String CopyRight;
public void init()
{
SoundDatei=getParameter("Songtitel");
CopyRight=getParameter("CopyRight");
try {
u=new URL(getDocumentBase(), SoundDatei);
}
catch(MalformedURLException x){
}
play(u);
S = new String( "es erklingt "+u.toString());
T = new String( "CODEABASE: "+getCodeBase());
R = new String( "DOCUMENTBASE von Klang: "+getDocumentBase());
}
public void paint(Graphics graf)
{
setBackground(Color.yellow);
graf.setColor(Color.black);
graf.drawString( T, 0,21 );
graf.drawString( R, 0,41 );
graf.drawString( S, 0,61 );
graf.drawString( CopyRight, 0, 95 );
}
}