Co napisać aby wyswietlała się każdy fragment szubienicy w wisielcu.Mam na razie tylko fragment programu:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
class Haslo
{
Font font;
String haslo;
boolean widoczne[];
int szerokosc;
String losujHaslo(String nazwa,int ile ) throws Exception{
FileInputStream file = new FileInputStream(nazwa);
StreamTokenizer st = new StreamTokenizer(file);
int los = (int)(Math.random()*ile);
for (int i=0;i<los;i++) st.nextToken();
return st.sval;
}
public Haslo(int szer) throws Exception{
szerokosc = szer;
font = new Font("Arial",Font.BOLD,48);
haslo = losujHaslo("test.txt",4);
widoczne = new boolean[haslo.length()];
for (int i=0; i<widoczne.length; i++) widoczne[i] = false;
}
public void rysuj (int x, int y, Graphics g){
g.setColor(new Color(200,200,200));
g.setFont(new Font("Arial", Font.BOLD,48));
StringBuilder tekst = new StringBuilder();
for (int i=0; i<haslo.length();i++)
if (widoczne[i]) tekst.append(haslo.charAt(i)); else tekst.append("-");
g.drawString(tekst.toString(),x,y);
}
public void zgadujZnak (char c) {
for(int i=0;i<haslo.length();i++)
if (Character.toLowerCase(haslo.charAt(i))==Character.toLowerCase(c)) widoczne[i]=true;
}
}
public class Gra extends Frame implements ActionListener
{
Color tlo;
TextField pole;
Label l;
Button zgaduj;
Haslo h;
Gra () throws Exception{
super("Gra w wisielca");
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
h = new Haslo(400);
tlo = new Color(220,220,0);
}
public void paint (Graphics g) {
g.setColor(new Color(200,150,50)); g.fillRect(0,0,600,400);
h.rysuj(120,300,g);
}
public void init () {
resize(600,400);
setLayout(new FlowLayout());
pole = new TextField(1); add(pole);
zgaduj = new Button("Zgadnij"); zgaduj.addActionListener(this);
l = new Label("Tu wpisz znak :");l.setBackground(tlo);
add(l);
add(pole);
add(zgaduj);
}
public void actionPerformed (ActionEvent e) {
h.zgadujZnak(pole.getText().charAt(0));repaint();
}
protected void processWindowEvent (WindowEvent e) {
if (e.getID()==WindowEvent.WINDOW_CLOSING) System.exit(0);
}
public static void main(String args[]) throws Exception{
Gra gra = new Gra();
gra.init();
gra.show();
}
}