Archived
1
0

Grafiken eingefügt

This commit is contained in:
Markus Birth 1998-12-03 19:45:14 +00:00
parent 7efd963367
commit 86f9ec2745

View File

@ -20,10 +20,10 @@ import java.lang.Character.*;
import java.math.*; // eigentlich nur für die eine Zufallszahl import java.math.*; // eigentlich nur für die eine Zufallszahl
import java.io.*; // für Dateioperationen ("Tupfer, Schere ...") import java.io.*; // für Dateioperationen ("Tupfer, Schere ...")
public class hangman extends Frame public class hangman extends Frame {
{
// Globale Variablen // Globale Variablen
final static int WND_B=400, WND_H=400; final static int WND_B=400, WND_H=400;
final int SX=50, SY=50;
RandomAccessFile file; RandomAccessFile file;
int maxdat=0; int maxdat=0;
String words[]; String words[];
@ -37,11 +37,9 @@ public class hangman extends Frame
KL CONTROL; KL CONTROL;
char c; char c;
public hangman() public hangman() {
{
String stmp=new String(); String stmp=new String();
try try {
{
RandomAccessFile f=new RandomAccessFile("hangman.dat","r"); RandomAccessFile f=new RandomAccessFile("hangman.dat","r");
words=new String[50]; words=new String[50];
while ((stmp=f.readLine())!=null && maxdat<50) { while ((stmp=f.readLine())!=null && maxdat<50) {
@ -51,13 +49,11 @@ public class hangman extends Frame
} }
} }
f.close(); f.close();
while (myword==null) while (myword==null) {
{
myword=words[(int)(Math.random()*maxdat)+1]; myword=words[(int)(Math.random()*maxdat)+1];
} }
} }
catch(IOException ioe) catch(IOException ioe) {
{
System.out.println("IOException: "+ioe.toString()); System.out.println("IOException: "+ioe.toString());
} }
CONTROL=new KL(); CONTROL=new KL();
@ -72,26 +68,65 @@ public class hangman extends Frame
} }
} }
public void paint(Graphics g) public void paint(Graphics g) {
{
g.setColor(Color.black); g.setColor(Color.black);
g.fillRect(0,0,WND_B,WND_H); g.fillRect(0,0,WND_B,WND_H);
g.setColor(Color.yellow);
// g.drawString("Datensaetze: "+maxdat,40,350); // g.drawString("Datensaetze: "+maxdat,40,350);
// g.drawString("Wort: "+myword,40,200); // g.drawString("Wort: "+myword,40,200);
// g.drawString("Zeichen: "+c,40,230); // g.drawString("Zeichen: "+c,40,230);
if (mistakes!=6 && mistakes!=-1) {
g.setColor(Color.yellow);
g.drawString("Wort: "+new String(xyword),40,215); g.drawString("Wort: "+new String(xyword),40,215);
g.drawString("alpha: "+new String(probed),40,260); g.drawString("alpha: "+new String(probed),40,260);
g.drawString("mist: "+mistakes,40,230); g.drawString("mist: "+mistakes,40,230);
} }
UpdateHangMan(g);
}
public void UpdateHangMan(Graphics g) {
Toolkit tk=Toolkit.getDefaultToolkit();
class KL implements KeyListener switch(mistakes) {
{ case 6:
g.drawImage(tk.getImage("images/hm6.gif"),SX,SY,this);
g.setColor(Color.red);
g.drawString(">>> VERLOREN <<<",WND_B/2-100,WND_H/2);
g.setColor(Color.white);
g.drawString("Das gesuchte Wort war '"+myword+"'!",WND_B/2-100,WND_H/2+15);
removeKeyListener(CONTROL);
break;
case 5:
g.drawImage(tk.getImage("images/hm5.gif"),SX,SY,this);
break;
case 4:
g.drawImage(tk.getImage("images/hm4.gif"),SX,SY,this);
break;
case 3:
g.drawImage(tk.getImage("images/hm3.gif"),SX,SY,this);
break;
case 2:
g.drawImage(tk.getImage("images/hm2.gif"),SX,SY,this);
break;
case 1:
g.drawImage(tk.getImage("images/hm1.gif"),SX,SY,this);
break;
case 0:
g.drawImage(tk.getImage("images/hm0.gif"),SX,SY,this);
break;
case -1:
g.drawImage(tk.getImage("images/hm.gif"),SX,SY,this);
g.setColor(Color.green);
g.drawString(">>> GEWONNEN <<<",WND_B/2-100,WND_H/2);
removeKeyListener(CONTROL);
break;
}
}
class KL implements KeyListener {
public void keyPressed(KeyEvent e) { } public void keyPressed(KeyEvent e) { }
public void keyReleased(KeyEvent e) { } public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) public void keyTyped(KeyEvent e) {
{
c=e.getKeyChar(); c=e.getKeyChar();
c=Character.toUpperCase(c); c=Character.toUpperCase(c);
int i; int i;
@ -110,35 +145,33 @@ public class hangman extends Frame
} }
if (xyword[i]=='_') underscores++; if (xyword[i]=='_') underscores++;
} }
if (!status && !check) { mistakes++; } if (!status && !check) mistakes++;
if (underscores==0) { if (underscores==0) {
myword="RICHTIG!"; mistakes=-1;
repaint(); System.out.println("Sie haben gewonnen!\n'"+myword+"' war richtig.");
System.out.println("Sie haben gewonnen!");
System.exit(0);
} }
if (mistakes>=6) { if (mistakes>=6) {
myword="VERLOREN!"; mistakes=6;
repaint(); System.out.println("Schön, wie sie da am Galgen baumeln ...\nWieso sind Sie auch nicht auf '"+myword+"' gekommen?");
System.out.println("Schön, wie sie da am Galgen baumeln ...");
System.exit(0);
} }
repaint(); repaint();
} }
} }
public static void main(String args[]) public static void main(String args[]) {
{
Frame frame=new hangman(); Frame frame=new hangman();
frame.addWindowListener(new WindowAdapter() frame.addWindowListener(new WindowAdapter() {
{ public void windowClosing(WindowEvent e) {
public void windowClosing(WindowEvent e)
{
System.exit(0); System.exit(0);
} }
}); });
frame.setTitle("HangMan for Java - \u00a91998 by Markus Birth"); frame.setTitle("HangMan for Java - \u00a91998 by Markus Birth");
frame.setSize(WND_B, WND_H); frame.setSize(WND_B, WND_H);
frame.show(); frame.show();
/* Pictures
Image pic;
pic=Toolkit.getDefaultToolkit().getImage("image.jpg");
g.drawImage(pic,0,0,this);
*/
} }
} }