package tools;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Galgen3 extends JFrame implements ItemListener, ActionListener
{
private static String[] letterArray =
{ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
private JDesktopPane desktopPane;
private JPanel pnlKasten;
private JCheckBox[] checkBoxArray;
private JButton btnStart;
DrawPanel iframe3;
JInternalFrame iframe;
JInternalFrame iframe2;
char Buchstabe;
public void itemStateChanged(ItemEvent e)
{
if (e.getStateChange() == ItemEvent.SELECTED) {
if (e.getSource() instanceof JCheckBox) {
JCheckBox cbClicked = (JCheckBox)e.getSource();
String strLetter = cbClicked.getText();
if (strLetter.length() == 1) {
iframe3.buchstabenEinzeichnen(strLetter);
}
}
}
}
public Galgen3()
{
super("Galgenraten");
initComponents();
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize( 610, 580 );
setVisible(true);
}
private void initComponents()
{
desktopPane = new JDesktopPane();
pnlKasten = new JPanel (new GridLayout(6, 4));
checkBoxArray = new JCheckBox[26];
btnStart = new JButton ("Neu");
for (int index = 0; index < 26; index++) // Erzeugung aller 26 Checkboxen
{
checkBoxArray[index] = new JCheckBox (letterArray[index]);
checkBoxArray[index].addItemListener (this); // ItemListener für CB mit
pnlKasten.add(checkBoxArray[index]); // Adden der CBs auf dem Pannel
}
pnlKasten.add(btnStart); // Neubutton
btnStart.addActionListener (this);
iframe = new JInternalFrame( "Galgenmaenchen", // title, Oberfläche vom
// Bild
false, // resizable
false, // closeable
false, // maximizable
false ); // iconifiable
iframe.setSize(300, 550);
iframe.setBounds(1,1,300,550);
iframe.getContentPane().add( new JScrollPane(new JTextArea()) );
iframe.setVisible( true );
desktopPane.add( iframe ); // adden auf die Gesamtoberfläche
iframe2 = new JInternalFrame( "Buchstaben", // title
false, // resizable
false, // closeable
false, // maximizable
false ); // iconifiable
iframe2.setBounds(300,1,300,300);
iframe2.getContentPane().add(pnlKasten);
iframe2.setVisible( true );
desktopPane.add( iframe2 );
iframe3 = new DrawPanel();
iframe3.setBounds(300,300,300,250);
iframe3.setVisible( true );
desktopPane.add( iframe3 );
getContentPane().add( desktopPane );
}
public void actionPerformed(ActionEvent erg)
{
if(erg.getActionCommand().equals("Neu"))
{ // zurücksetzen
for (int index = 0; index < 26; index++) // Zurücksetzen der 27
{
checkBoxArray[index].setEnabled(true); // Wenn geklickt
checkBoxArray[index].setSelected(false); // Dann ausblenden
}
}
}
public static void main( String[] args )
{
new Galgen3();
}
}
class DrawPanel extends JPanel
{
private String strSuchwort; // Festsetzen der Worte
private String strLetter;
int nPositionX;
public DrawPanel() {
strSuchwort = "AUTOBAHN";
strLetter = "";
}
public void buchstabenEinzeichnen(String strLetter) {
for(int index = 0;index < strSuchwort.length()-1;index++)
{
if ( (strSuchwort.substring(index, index+1)).equalsIgnoreCase(strLetter) )
{
this.strLetter = strLetter;
nPositionX = 40+(15*index);
repaint();
}
}
}
public void paintComponent( Graphics g )
{
super.paintComponent(g);
final int strich=15;
int x1=40, y1=100;
int x2=50, y2=100; // Länge der Striche
super.paintComponent( g );
g.drawString("Lösungswort:",10,30); // Damit schreibt er das Lösungswort
for(int zaehler=1; zaehler<=strSuchwort.length();zaehler++)
{
g.drawLine( x1,y1,x2,y2);
x1+=strich;
x2+=strich;
}
if (!"".equals(strLetter)) {
g.drawString(strLetter, nPositionX, 98);
strLetter = "";
}
}
}