diff --git a/Software/iud_key_angriffe_vigenere/.gitignore b/Software/iud_key_angriffe_vigenere/.gitignore new file mode 100644 index 0000000..c1f4551 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/.gitignore @@ -0,0 +1,5 @@ +*.class +*.ctxt +*.sh +repo.adoc +*.~lock diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/balkendiagramm.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/balkendiagramm.java new file mode 100644 index 0000000..d64c99b --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/balkendiagramm.java @@ -0,0 +1,76 @@ +import java.awt.*; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import javax.swing.*; + +public class Balkendiagramm extends JPanel { + + private double gesamt; + private double haeufigkeit[]; + private double maxwert; + private int verschiebung = 0; + private double haeufigkeit_de[] = { 0.0651, 0.0189, 0.0306, 0.0508, 0.174, 0.0166, 0.0301, 0.0476,0.0755, 0.0027, 0.0121,0.0344,0.0252,0.0978,0.0251,0.0079,0.0002,0.07,0.0727,0.0615,0.0435,0.067,0.0189,0.0003,0.0004,0.0113}; + + + public void setHaeufigkeit(double haeufigkeit[]) { + + this.haeufigkeit = haeufigkeit; + gesamt = 0; + for (int i = 0; i < 26; i++) { + gesamt += haeufigkeit[i]; + } + for (int i = 0; i < 26; i++) { + haeufigkeit[i] /= gesamt; + } + maxwert = 0.20; + + } + + public void setVerschiebung(int verschiebung) { + if (verschiebung >= 0 && verschiebung < 26) { + this.verschiebung = verschiebung; + + } else { + this.verschiebung = 0; + } // end of if-else + } + + public void paintComponent(Graphics g) { + Graphics2D g2 = (Graphics2D) g; + super.paintComponent(g2); + Dimension d = getSize(null); + //Skalieren auf 100x100 mit y-Invertierung + g2.scale(d.getWidth() / 120.0, -d.getHeight() / 100.0); + // Ursprung verschieben nach (15,-90) + g2.translate(5, -90); + // Linien etwas breiter + g2.setStroke(new BasicStroke(1.5f)); + // x-Achse zeichnen + g2.draw(new Line2D.Double(0, 0, 104, 0)); + // Linien etwas dünner + g2.setStroke(new BasicStroke(0.5f)); + // Deutsche Häufigkeit grau + g2.setColor(Color.darkGray); + // Säulen zeichnen + for (int i = 0; i < 26; i++) { + g2.fill(new Rectangle2D.Double(4 * i+1, 0, 2, haeufigkeit_de[i] / maxwert *100)); + } + // Texthäufigkeit blau + g2.setColor(Color.blue); + // Säulen zeichnen + for (int i = 0; i < 26; i++) { + g2.fill(new Rectangle2D.Double(4 * i, 0, 2, haeufigkeit[(i+verschiebung)%26] / maxwert *100)); + } + // y-Invertierung rückgängig machen + g2.scale(1, -1); + // Beschriftung x-Achse + String xAchse = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + // Fontgröße für Beschriftung ändern + g2.setColor(Color.black); + g2.setFont(g2.getFont().deriveFont(5f)); + for (int i = 0; i < 26; i++) { + g2.drawString(xAchse.substring(i, i + 1), + i * 4, +6); + } + } + } diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/baustein.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/baustein.java new file mode 100644 index 0000000..55954a1 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/baustein.java @@ -0,0 +1,42 @@ +public class Baustein implements Comparable{ + + // Anfang Attribute + private String zeichen; + private int anzahl; + // Ende Attribute + + public Baustein(String zeichen) { + super(); + anzahl = 1; + this.zeichen = zeichen; + } + // Anfang Methoden + + + + public int compareTo(Baustein c) { + + int anz = c.getAnzahl(); + + //ascending order + return anz - this.anzahl; + + //descending order + //return compareQuantity - this.quantity; + + } + public String getZeichen() { + return zeichen; + } + + public int getAnzahl() { + return anzahl; + } + + public void incAnzahl() { + anzahl++; + } + + + // Ende Methoden +} \ No newline at end of file diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/breakvigenere.jar b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/breakvigenere.jar new file mode 100644 index 0000000..c7a2055 Binary files /dev/null and b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/breakvigenere.jar differ diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/breakvigenere.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/breakvigenere.java new file mode 100644 index 0000000..98de0ff --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/breakvigenere.java @@ -0,0 +1,365 @@ +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import javax.swing.event.*; +import javax.swing.table.*; +import java.util.ArrayList; +import java.util.Collections; +import javax.swing.text.DefaultHighlighter; +import javax.swing.text.Highlighter; +import javax.swing.text.Highlighter.HighlightPainter; +import javax.swing.text.BadLocationException; + + +/** + * + * Programm zur Kryptoanalyse + * von monoalphabetischen Substitutionen + * + * @version 1.0 from 23.11.2016 + * @author Thomas Schaller + */ + +public class BreakVigenere extends JFrame { + // Anfang Attribute + private JTextField[] buchstabe; + + private JButton jButton1 = new JButton(); + private JButton jBBuchstabe = new JButton(); + private Balkendiagramm bdText = new Balkendiagramm(); + + private JTextArea jTAKrypto = new JTextArea(""); + private JScrollPane jTAKryptoScrollPane = new JScrollPane(jTAKrypto); + private JTextArea jTAKlartext = new JTextArea(""); + private JScrollPane jTAKlartextScrollPane = new JScrollPane(jTAKlartext); + private JLabel jLKryptotext = new JLabel(); + private JLabel jLKlartext = new JLabel(); + private JLabel jLCopyright = new JLabel(); + private JTextArea jTAKorrelation = new JTextArea(""); + private JScrollPane jTAKorrelationScrollPane = new JScrollPane(jTAKorrelation); + private JSpinner jSpVerschiebung = new JSpinner(); + private SpinnerNumberModel jSpVerschiebungModel = new SpinnerNumberModel(0, 0, 99, 1); + private JNumberField jNFAnzahlTreffer = new JNumberField(); + private JLabel lVerschiebung = new JLabel(); + private JLabel lAnzahlgleicherBuchstaben = new JLabel(); + // Ende Attribute + + public BreakVigenere (String title) { + super (title); + setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); + int frameWidth = 707; + int frameHeight = 649; + setSize(frameWidth, frameHeight); + Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); + int x = (d.width - getSize().width) / 2; + int y = (d.height - getSize().height) / 2; + setLocation(x, y); + Container cp = getContentPane(); + cp.setLayout(null); + // Anfang Komponenten + + jTAKryptoScrollPane.setBounds(16, 24, 665, 105); + jTAKrypto.setLineWrap(true); + jTAKrypto.setFont(new Font("Courier New", Font.BOLD, 16)); + jTAKrypto.setText("JMLQMFNSKIOQNXGYZOQEBJLUKXHFCXGVLGBMREAJZCAEKVIFOWJLMFGJLPQZJIYHQQHYJLAFGFLRLQYESTPMHIAWQZFIPPMZMIZGPDOIIIVGTHIIQVKHLVHQOPLNMIKMSWCYKMUIVBREADEQOXLVVMILSMVWYZLVAONSIIVIKVKIVPOIZIEGXHLHCDILKIVPKYAWKTKRIIVQJMRXQZKVHFBVULHRVQYXYMBTKQPYAUSNHLZUSJUJBQTFHRLEKMUIAUTPHXMUTMZGPQXWWVIONINIAONVPIJQTIUWMONWIRLUMIUAMDQIZTWXEKYEXTOELPQNXMZIFEKGOWJONIYDCDVSSCODGTOMMMTKLKMNKRPRLQSRHGPEKMUIUFUHLZMDLJLRBXOGOXMZHYJLAONPBKMDBSYRIONNLHMYKMUDMXTIUOTMXXLBBNAGOWBMHIUDCYTGOWBQTESTPMHIAMVEKMUIZFGFBPINKVGYOQNIUYVPYSHPTQBIYJONGVLRIXVLHFMFKEBWHGTYADMZJETMBQXJHRLQXHPIXDUKYIAEOZLTWXEESTPMHIAMAONIJLQRLVPIZGTKHFMDKWDEZZUGOIQZLIZXMEBIYJITXIUSPZKWJLTEYISHQQYIYACDJIPQRMNVCSUUZESMMZOWJLMZQVFTBARSNIVSOSCEVNGXAMAFGFLPTMYSJEQZLSYQMUTIZZWYBIYWKTRWZPMDLVLMHGCLSIVPKRRIVZCSYXAAJIYIQZKWRIVZYEADMEBSYKMEILSEOQTKLVVQARKOZKVXVKZMVLPWKTYGOAIONHHPMUILADCQXVHXMZCYYHMZJETETEREAIQZOWJLMEORUWXDILLFMZAXGXEUKFLMABOISWEQOWLZQDZZAMWYTMHTIDKRAETXKWNIPAXGOXLQXXJLBUMOLMBPOIIYKTYXHFMZJIZOMZTWHXHQYFLWBUSQLRLUKVLMPQTJVPOQORKIZPOICIZEILPILQTIUETBNEIIBQGYZHMDZEIYTMGYZKMINPAA\nMDJIUQAEKRZMVPGPSIDQXFYECONXHPAAKMUQIXHIUYBLZAVVLQTLPIZZGGOOTMXXLBBNAGOWBMHIUWWNKKPRVFSEUAQQJIYZWZBSYRMENEUHMXZWPGPEUQPXCYKMUIXQXMVHQEILLTWXEESTPMHIAMAONIZYJEZMAYBUURTMBUSFLMABOISIQZKVWIZUUHLZWZCLYIVPZVPXPQSMBWAUILHYNHKVZGPAHIUIAFGRKEZPGPWLINKXLFMEILYRSFKESWWPOINIEANRAIIXVLHFMFOWJLMDKMOIVRUPNILQXFBGPEZEIIVHKVDIVPKXLFMXREZSJQXIPXAHKVDVNQRXLETBNEIIBQJMLIZMRPLVLUTKZMVHUPBXWDOWJLETRXLFMUJIUQWZUESTPMHIAMAONIUWCNYXPXCFOSUWUQZLVHMZCEYHQQYWJLWZYIPXTMTKLQJXOGOKZGTHZXHXOGOLIFZIKMMEHIYIQFYMTNITXESWWXGRNIDAXXYMBTKQPYAGTHIITXGWVHMDOXHPQQTMZGPQMISIPDZISIWZHEAXQEZEHPJQXXPIUBLSOPMZKVZGPXAKCSZPOIHPXTGFLXMZGGONMIKMSWLDKMVHMDBMLVEDZIYRHGCIJLAQRRHPAYKGOEVUYGOIATOPMWUUZXLPPMZXLIZTOIYDCPOIUEKTOLTFMZGRUXMMRFLVBUYGOIQNKIYJCZJIUIQZKGOMNRXMLVAONIPFMNKWAIPQTHGYUQOWAECEFALMZGTHLRUQZESPAONIPFMZJMLECRKMUIZSKQLMVEGQLRIONWLWQFFIUYVPYSCIZNARKIVEORKHIEYWPGPPOIRPMUTIYIIGLHLVODKVLRLDKLLRSMTRUEPQFYLMVVGLYLCZJIYXABZIYMUVGLYZMDGPSKMYKMUIZFKHLVVQGTVPQFGRPWKTKKLPMTXXLKQABEURQNGXAMAFGHLPTMVSYXIMRFLVBUYZLVEDLISXMMRTOEJQZIBRLNKPSEAAYOLRVIUVAYVPYGOYNPOI\nWSTKGPWLINKXPWKTKGOMNRXMLVCZMQPXDQXAYJMXZITETBNEIIBGTHZGPXYWLPPUKVGYEGXHLETNKVAMAEILLMJQHIUYBLZHPIVMILQILQSFBGPEZEIIVLAHYIPQTAHVQYPEOVODOJMHMDLVHRHAYIIPIUYIKIDUMIUVMBUVAEAUJILECRARKWKTRYNZWDYXHXBPKVHPJQXXPWKTKMIILUKXHFCXGVLGBMJIZXZUZLLQQGYDBZMDCIUHMZJEIIQMHIYEVPKVZETEOQVVQSORHPDQXAYJMXZIHPXTGFLXMQORGYBDGKLRLUKWLVSDETASODGTOMAONWAEZWKZVVAONPHKDUMIUVMEMIYMMFORKIVRUPNIVPKRQEPDNYUHMDZIUMVHKVNIAEKROIQFARKHQQAVZTZZMPPGPHURAVQFNITMCEBSYKMEILSEOQTITIBTUHLACDJIBRBQXHLQVMSIUZQSKRYIKTOJMVMNKOHRVF"); + jTAKrypto.addKeyListener(new KeyAdapter() { + public void keyTyped(KeyEvent evt) { + jTAKrypto_KeyTyped(evt); + } + }); + cp.add(jTAKryptoScrollPane); + + jTAKlartextScrollPane.setBounds(16, 480, 665, 105); + jTAKlartext.setLineWrap(true); + jTAKlartext.setFont(new Font("Courier New", Font.BOLD, 16)); + cp.add(jTAKlartextScrollPane); + jLKryptotext.setText("Kryptotext"); + jLKryptotext.setBounds(16, 3, 110, 20); + cp.add(jLKryptotext); + jLKlartext.setText("Klartext"); + jLKlartext.setBounds(16, 456, 110, 20); + cp.add(jLKlartext); + jLCopyright.setText("(cc) Schaller (ZPG Informatik) - V1.0 (2017)"); + jLCopyright.setBounds(128, 584, 419, 33); + jLCopyright.setHorizontalAlignment(SwingConstants.CENTER); + jLCopyright.setFont(new Font("Dialog", Font.PLAIN, 12)); + cp.add(jLCopyright); + + // -------------------------------- Tab 1 --------------------------------------------- + JPanel tab1 = new JPanel(); + tab1.setLayout(null); + + jTAKorrelationScrollPane.setBounds(16, 144, 665, 57); + jTAKorrelationScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); + jTAKorrelation.setText(""); + jTAKorrelation.setEditable(false); + jTAKorrelation.setEnabled(true); + jTAKorrelation.setFont(new Font("Courier New", Font.BOLD, 16)); + tab1.add(jTAKorrelationScrollPane); + jSpVerschiebung.setBounds(192, 110, 41, 25); + jSpVerschiebung.setValue(1); + jSpVerschiebung.setModel(jSpVerschiebungModel); + jSpVerschiebung.addChangeListener(new ChangeListener() { + public void stateChanged(ChangeEvent evt) { + jSpVerschiebung_StateChanged(evt); + } + }); + tab1.add(jSpVerschiebung); + + jNFAnzahlTreffer.setBounds(192, 140, 41, 25); + jNFAnzahlTreffer.setText(""); + jNFAnzahlTreffer.setEditable(false); + tab1.add(jNFAnzahlTreffer); + lVerschiebung.setBounds(16, 10, 310, 20); + lVerschiebung.setText("Angriff auf die Schlüssellänge mit Autokorrelation"); + tab1.add(lVerschiebung); + JLabel l = new JLabel(); + l.setBounds(14, 165, 640, 40); + l.setText("Suche nach der Verschiebung, bei der möglichst viele Buchstaben übereinstimmen.
Das ist vermutlich die Schlüssellänge."); + tab1.add(l); + l = new JLabel(); + l.setBounds(16, 112, 162, 20); + l.setText("Verschiebung"); + tab1.add(l); + lAnzahlgleicherBuchstaben.setBounds(16, 142, 162, 20); + lAnzahlgleicherBuchstaben.setText("Anzahl gleicher Buchstaben"); + tab1.add(lAnzahlgleicherBuchstaben); + + // ---------------------------------- Tab 2 ------------------------------- + JPanel tab2 = new JPanel(); + tab2.setLayout(null); + + l = new JLabel(); + l.setBounds(16, 10, 310, 20); + l.setText("Angriff auf die Teiltexte mit Häufigkeitsanalyse"); + tab2.add(l); + + l = new JLabel(); + l.setBounds(380, 10, 300, 25); + l.setText("Buchstabenhäufigkeit Deutsch"); + tab2.add(l); + + l = new JLabel(); + l.setBounds(16, 40, 300, 25); + l.setText("Schlüssel"); + tab2.add(l); + + l = new JLabel(); + l.setBounds(16, 100, 300, 100); + l.setText("Versuche den Schlüssel zu knacken, indem du in jedem Teiltext die Häufigkeitsverteilung der Buchstaben mit der üblichen Häufigkeitsverteilung in deutschen Texten in Einklang bringst."); + tab2.add(l); + + double[] h = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + bdText.setBounds(380,40,300,180); + bdText.setHaeufigkeit(h); + tab2.add(bdText); + + jButton1.setBounds(16, 210, 193, 25); + jButton1.setText("Entschlüsselung versuchen"); + jButton1.setMargin(new Insets(2, 2, 2, 2)); + jButton1.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent evt) { + jButton1_ActionPerformed(evt); + } + }); + tab2.add(jButton1); + + + + // Ende Komponenten + buchstabe = new JTextField[26]; + for (int i = 0; i<26 ;i++ ) { + buchstabe[i] = new JTextField(); + buchstabe[i].setBounds(16+i*25, 65, 25, 25); + buchstabe[i].setText("A"); + buchstabe[i].setHorizontalAlignment(SwingConstants.CENTER); + buchstabe[i].setVisible(false); + buchstabe[i].addFocusListener(new FocusAdapter() { + public void focusGained(FocusEvent evt) { + highlightKryptotext(evt); + } + }); + buchstabe[i].addKeyListener(new KeyAdapter() { + public void keyReleased(KeyEvent e) { + e.consume(); + } + + public void keyTyped(KeyEvent e) { + e.consume(); + } + + public void keyPressed(KeyEvent e) { + JTextField textField = (JTextField) e.getSource(); + String text = textField.getText(); + if ((e.getKeyChar()>='a' && e.getKeyChar()<='z') ||(e.getKeyChar()>='A' && e.getKeyChar()<='Z')) { + text = ""+e.getKeyChar(); + } + if (e.getKeyChar()==' ' || e.getKeyChar() == KeyEvent.VK_BACK_SPACE || e.getKeyChar() == KeyEvent.VK_DELETE) { + text = ""; + + } // end of if + textField.setText(text.toUpperCase()); + e.consume(); + if (textField.getText().length()>0) { + bdText.setVerschiebung((int) textField.getText().charAt(0)-65); + + } else { + bdText.setVerschiebung(0); + } // end of if-else + bdText.repaint(); + + } // end of if + + }); + + tab2.add(buchstabe[i]); + } // end of for + + JTabbedPane tabpane = new JTabbedPane + (JTabbedPane.TOP,JTabbedPane.SCROLL_TAB_LAYOUT ); + tabpane.setBounds(10,140,672,290); + + tabpane.addTab("Schritt 1", tab1); + tabpane.addTab("Schritt 2", tab2); + cp.add(tabpane); + + setResizable(false); + setVisible(true); + } + + + + // Anfang Methoden + public void jButton1_ActionPerformed(ActionEvent evt) { + String krypttext = jTAKrypto.getText().toUpperCase(); + String klartext=""; + int c = 0; + int diff = (Integer) jSpVerschiebung.getValue(); + for (int i=0;i=65 && asc<=90) { + h[asc-65]++; + } + } // end of if + c++; + } // end of if + } + bdText.setHaeufigkeit(h); + if (buchstabe[start].getText().length()>0) { + bdText.setVerschiebung((int) buchstabe[start].getText().charAt(0)-65); + + } else { + bdText.setVerschiebung(0); + } // end of if-else + bdText.repaint(); + } catch(BadLocationException e) {} + } + + + public void jSpVerschiebung_StateChanged(ChangeEvent evt) { + String krypto = jTAKrypto.getText().toUpperCase(); + String krypto2=""; + + for (int i=0; iRegula Hoefer-Isenegger +@version 0.1 +*/ +public class LineRenderer { + private Point2D.Double point; + private Turtle turtle; + + LineRenderer(Turtle turtle) { + this.point = new Point2D.Double(); + this.turtle = turtle; + } + + /** Initialisation with coordinates x and + y. + */ + public void init(double x, double y) { + this.point.setLocation(x, y); + } + /** Same as init(double x, double y), but with a Point2D.Double + argument for convenience + */ + public void init(Point2D.Double p) { + this.init(p.x, p.y); + } + /** Get the current x-coordinate in screen coordinates.*/ + private double getX(){ + return (toScreenCoords(point)).getX(); + } + /** Get the current x-coordinate in screen coordinates.*/ + private double getY(){ + return toScreenCoords(point).getY(); + } + /** Calls the clipLineTo and wrapLineTo methods, according + to the turtle's edge behavior. + + Only overwrite this method when working with another + (one that you have defined) edge behaviour.
+ If you mean to change the manner of drawing lines, do this in + the methods clipLineTo() and wrapLineTo(). + @see #clipLineTo + @see #wrapLineTo + */ + protected void internalLineTo(double x, double y){ + Point2D.Double screenPos = toScreenCoords(x, y); + if(turtle.isClip()){ + clipLineTo(screenPos.getX(), screenPos.getY()); + } + if (turtle.isWrap()){ + wrapLineTo(screenPos.getX(), screenPos.getY()); + } + init(x,y); + } + /** Calls the internalLineTo(x,y), which does the actual painting. + */ + public void lineTo(double x, double y) { + internalLineTo(x, y); + } + /** Calls the internalLineTo(x,y), which does the actual painting. + + This method works the same way as lineTo(double x, double y), but + is added for convenience. + */ + public void lineTo(Point2D.Double p) { + internalLineTo(p.getX(), p.getY()); + } + /** Does the actual painting for clip mode. + + It works already with ScreenCoords! + For further comments cf. internalLineTo(double x, double y). + @see #internalLineTo + */ + protected void clipLineTo(double x, double y){ + turtle.getPlayground().lineTo(getX(), getY(), x, y, turtle.getPen()); + } + /** Does the actual painting for wrap mode. + + It works already with ScreenCoords! + For further comments cf. internalLineTo(double x, double y). + @see #internalLineTo + */ + protected void wrapLineTo(double x, double y){ + double dx = getX() - x; + double dy = getY() - y; + Point2D.Double start = new Point2D.Double(x, y); + Point2D.Double end = new Point2D.Double(start.x+dx, start.y+dy); + + intoPanel(start, end); + Point2D.Double tmp; + while ((tmp = calcIntersection(start.x, start.y, end.x, end.y)) != null){ + turtle.getPlayground().lineTo(start.x, start.y, tmp.getX(), tmp.getY(), turtle.getPen()); + start = tmp; + intoPanel(start, end); + dx = end.x - start.x; + dy = end.y - start.y; + } + + turtle.getPlayground().lineTo(start.x, start.y, end.x, end.y, turtle.getPen()); + } + /** Makes the coordinates fit into the Panel. + + Well, this is some sort of modulus calculation. + */ + private void intoPanel(Point2D.Double start, Point2D.Double end){ + int pWidth = turtle.getPlayground().getWidth(); + int pHeight = turtle.getPlayground().getHeight(); + while(start.x < 0){ + start.x += pWidth; + end.x += pWidth; + } + while (start.x > pWidth){ + start.x -= pWidth; + end.x -= pWidth; + } + if(start.x == 0 && end.x < start.x){ + start.x += pWidth; + end.x += pWidth; + } + if(start.x == pWidth && end.x > start.x){ + start.x -= pWidth; + end.x -= pWidth; + } + while(start.y < 0){ + start.y += pHeight; + end.y += pHeight; + } + while (start.y > pHeight){ + start.y -= pHeight; + end.y -= pHeight; + } + if(start.y == 0 && end.y < start.y){ + start.y += pHeight; + end.y += pHeight; + } + if(start.y == pHeight && end.y > start.y){ + start.y -= pHeight; + end.y -= pHeight; + } + + } + /** Intersection line with playground-edges + (startX / startY) MUST lie in the playground! + */ + private Point2D.Double calcIntersection(double startX, double startY, double endX, double endY){ + double dx = endX - startX; + double dy = endY - startY; + double W = turtle.getPlayground().getWidth(); + double H = turtle.getPlayground().getHeight(); + if(endX < 0){ + if((dy/dx <= startY/startX) && (dy/dx >= -(H-startY)/startX)){ // links + return new Point2D.Double(0, startY-startX*dy/dx); + } + } + else if(endX > W){ + if((dy/dx >= -startY/(W-startX)) && (dy/dx <= (H-startY)/(W-startX))){// rechts + return new Point2D.Double(W, startY+(W-startX)*dy/dx); + } + } + if(endY < 0){ // oben + return new Point2D.Double(startX-startY*dx/dy, 0); + } + else if(endY > H){ // unten + return new Point2D.Double(startX+(H-startY)*dx/dy, H); + } + else{ + return null; // Endpoint lies in the window + } + } + /** Calculates the screen coordinates of the turtle's actual + position according to the interpretation of the playground. + */ + private Point2D.Double toScreenCoords(double x, double y){ + return turtle.getPlayground().toScreenCoords(x, y); + } + /** Calculates the screen coordinates of the turtle's actual + position according to the interpretation of the playground. + + Added for convenience. + @see #toScreenCoords(double, double) + */ + private Point2D.Double toScreenCoords(Point2D.Double p){ + return turtle.getPlayground().toScreenCoords(p.x, p.y); + } +} + diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/pen.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/pen.java new file mode 100644 index 0000000..8fcc8b7 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/pen.java @@ -0,0 +1,268 @@ +// Pen.java + +// Copyright 2002 Regula Hoefer-Isenegger +// +// This file is part of The Java Turtle package (TJT) +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package ch.aplu.turtle; + +import ch.aplu.turtle.*; +import java.awt.Color; +import java.awt.BasicStroke; +import java.awt.Stroke; +import java.awt.Font; +import java.awt.GraphicsEnvironment; + +/** The Pen class provides anything used for drawing the lines, such as line width, + pen color, end caps, dashed lines, etc. + + @see java.awt.BasicStroke + + @author Regula Hoefer-Isenegger + @version 0.1.1 +*/ +public class Pen +{ + /* Attributes *********************************************/ + + /** The default font that is used when drawing Text. + + First argument must be one of "Serif", "SansSerif", "Monotyped", "Dialog" or "DialogInput" + to guarantee that this font exists on all systems. + + @see java.awt.Font for more information, e.g. on font styles. + + */ + public static Font DEFAULT_FONT = new Font("SansSerif", Font.PLAIN, 24); + private Color color; + private Color fillColor; + private BasicStroke stroke; + private Font font; + + /* Constructors *******************************************/ + /** Constructor with standard Color and standard Stroke. + + @see java.awt.BasicStroke + */ + public Pen(){ + color = Color.black; + setFillColor(Color.black); + stroke = new BasicStroke(); + font = DEFAULT_FONT; + } + /** Constructor with Color color and standard Stroke. + + @see java.awt.BasicStroke + */ + public Pen(Color color){ + this.color = color; + setFillColor(color); + stroke = new BasicStroke(); + font = DEFAULT_FONT; + } + /* Methods ************************************************/ + /** Query the Pens color.*/ + public Color getColor(){ + return color; + } + /** Set the Pens color.*/ + public void setColor(Color color){ + this.color = color; + } + /** Set the Pens fill color. + */ + public void setFillColor(Color color){ + this.fillColor = color; + } + /** Query the Pens fill color.*/ + public Color getFillColor(){ + return this.fillColor; + } + /** Get the Pens Stroke + + @see BasicStroke + @see Stroke + */ + public Stroke getStroke(){ + return stroke; + } + /** Query the Pens line width*/ + public float getLineWidth(){ + return stroke.getLineWidth(); + } + /** Query the Pens end cap style. + + @see java.awt.BasicStroke + */ + public int getEndCap(){ + return stroke.getEndCap(); + } + /** Query the Pens line join style. + + @see java.awt.BasicStroke + */ + public int getLineJoin(){ + return stroke.getLineJoin(); + } + /** Query the Pens miter limit style. + + @see java.awt.BasicStroke + */ + public float getMiterLimit(){ + return stroke.getMiterLimit(); + } + /** Query the Pens dash array. + + @see java.awt.BasicStroke + */ + public float[] getDashArray(){ + return stroke.getDashArray(); + } + /** Query the Pens dash phase. + + @see java.awt.BasicStroke + */ + public float getDashPhase(){ + return stroke.getDashPhase(); + } + + /** Set the Pens line width. */ + public void setLineWidth(float width){ + stroke = new BasicStroke((float)width, + stroke.getEndCap(), + stroke.getLineJoin(), + stroke.getMiterLimit(), + stroke.getDashArray(), + stroke.getDashPhase()); + } + /** Set the Pens end cap style. + + @see java.awt.BasicStroke + */ + public void setEndCap(int endCap){ + stroke = new BasicStroke(stroke.getLineWidth(), + endCap, + stroke.getLineJoin(), + stroke.getMiterLimit(), + stroke.getDashArray(), + stroke.getDashPhase()); + } + /** Set the Pens line join style. + + @see java.awt.BasicStroke + */ + public void setLineJoin(int join){ + stroke = new BasicStroke(stroke.getLineWidth(), + stroke.getEndCap(), + join, + stroke.getMiterLimit(), + stroke.getDashArray(), + stroke.getDashPhase()); + } + /** Set the Pens miter limit. + + @see java.awt.BasicStroke + */ + public void setMiterLimit(float miterlimit){ + stroke = new BasicStroke(stroke.getLineWidth(), + stroke.getEndCap(), + stroke.getLineJoin(), + miterlimit, + stroke.getDashArray(), + stroke.getDashPhase()); + } + /** Set the Pens dash array. + + @see java.awt.BasicStroke + */ + public void setDash(float[] dashArray){ + stroke = new BasicStroke(stroke.getLineWidth(), + stroke.getEndCap(), + stroke.getLineJoin(), + stroke.getMiterLimit(), + dashArray, + stroke.getDashPhase()); + } + /** Set the Pens dash phase. + + @see java.awt.BasicStroke + */ + public void setDashPhase(float dashPhase){ + stroke = new BasicStroke(stroke.getLineWidth(), + stroke.getEndCap(), + stroke.getLineJoin(), + stroke.getMiterLimit(), + stroke.getDashArray(), + dashPhase); + } + /** Provides information about the currently available font families (e.g. "Roman"). + Each font name is a string packed into a array of strings. + @see java.awt.Font for more information about font attributes etc. + */ + public static String[] getAvailableFontFamilies(){ + GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); + String s[] = ge.getAvailableFontFamilyNames(); + return s; + } + /** Change the font style. + + @see java.awt.Font for possible styles. + */ + public void setFontStyle(int style){ + font = font.deriveFont(style); + } + /** Change the font size (in points). + */ + public void setFontSize(int size){ + font = font.deriveFont((float)size); + } + /** Change the font size (in points). + You will probably only need the int version setFontSize(int). + */ + public void setFontSize(float size){ + font = font.deriveFont(size); + } + /** Query the size (in points, rounded to int) of the current font. + */ + public int getFontSize(){ + return font.getSize() ; + } + /** Change the font to the given one. + */ + public void setFont(Font f){ + font = f; + } + /** Query the current font. + */ + public Font getFont(){ + return font; + } +} + + + + + + + + + + + + + + diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/playground.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/playground.java new file mode 100644 index 0000000..6c0db5b --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/playground.java @@ -0,0 +1,789 @@ +// Playground.java + +// Copyright 2002 Regula Hoefer-Isenegger +// Major code modifications by Aegidius Pluess +// Adaption for the Java-Editor by Gerhard Röhner +// +// This file is part of The Java Turtle package (TJT) +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package ch.aplu.turtle; + +import ch.aplu.turtle.*; +import java.util.*; +import javax.swing.*; +import java.awt.*; +import java.awt.print.*; +import java.awt.geom.*; +import java.awt.image.*; +import java.awt.font.*; + +/** + A Playground is the Turtle's home, i.e. the Turtle lives + and moves in the Playground. + + ThePlayground is responsible for interpreting angle and position of the + Turtle and for choosing the correct turtle image and putting it on the right + spot of the Playground. This means: e.g. whenever you wish to switch the x- and y-axis, you + should do it in this class, and not in the Turtle class. + + @author Regula Hoefer-Isenegger + @version 0.1.1 +*/ + +public class Playground extends JPanel implements Printable { + + /** Hold the Turtles of this Playground. */ + private Vector turtles; + + /** Hold the offscreen buffer and graphics context + * where Turtle traces are drawn. + */ + private BufferedImage traceBuffer = null; + protected Graphics2D traceG2D = null; + private Dimension playgroundSize; + + /** Hold the offscreen buffer and graphics context + * of the Turtles images. + */ + private BufferedImage turtleBuffer = null; + protected Graphics2D turtleG2D = null; + + /** Flag to tell whether we have at least one Turtle shown. */ + private boolean isTurtleVisible = false; + + /** Flag to tell whether we use automatic repainting */ + private boolean isRepainting = true; + + /** The default background color. + */ + protected static Color DEFAULT_BACKGROUND_COLOR = Color.white; + + private double printerScale = 1; // Default printer scaling + private TPrintable traceCanvas; // Store ref to user class + private Graphics2D printerG2D = null; + private boolean isPrintScreen = false; // Indicate we are printing the playground + private double printerScaleFactor = 1.1; // Magnification factor for printer + + /** + * originX is the x-position of the cartesian coodinate system within the playground. + */ + public int originX; + + /** + * originY is the y-position of the cartesian coodinate system within the playground. + */ + public int originY; + + private static Color[] ColorArray = {Color.cyan, Color.red, Color.green, Color.blue, Color.yellow, + Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.black, Color.gray + }; + + /** + * Create a Playground with default background color. + * e.g. creates a new vector (which holds the + * Turtles), + */ + public Playground() { + turtles = new Vector(); + setDoubleBuffered(false); + setBackground(DEFAULT_BACKGROUND_COLOR); + initBuffers(new Dimension(100, 100)); + } + + /** + * Initializes the offscreen buffers and sets the size. + */ + protected void initBuffers(Dimension size) { + Color bkColor = getBackground(); + playgroundSize = size; + traceBuffer = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB); + traceG2D = traceBuffer.createGraphics(); + traceG2D.setColor(bkColor); + traceG2D.fillRect(0, 0, size.width, size.height); + traceG2D.setBackground(bkColor); + + turtleBuffer = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB); + turtleG2D = turtleBuffer.createGraphics(); + turtleG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + originX = size.width/2; + originY = size.height/2; + } + + /** Add a new Turtle to the Playground. + */ + public void add(Turtle turtle) { + turtles.add(turtle); + turtle.setPlayground(this); + int i = turtles.size(); + while (i > 10) { + i = i - 10; + } // end of while + turtle.init(ColorArray[i-1]); + toTop(turtle); + } + + public void setBounds(int x, int y, int width, int height) { + super.setBounds(x, y, width, height); + initBuffers(new Dimension(width, height)); + } + + /** Remove a Turtle from the Playground. + */ + public void remove(Turtle turtle) { + turtles.remove(turtle); + } + + /** Tell current number of Turtles in this Playground. + */ + public int countTurtles() { + return turtles.size(); + } + + /** Return the Turtle at index index. + */ + public Turtle getTurtle(int index) { + return turtles.elementAt(index); + } + + /** Move the given Turtle above all the others, then + paints all turtles. + @see #toTop + */ + public void paintTurtles(Turtle turtle) { + toTop(turtle); + paintTurtles(); + } + + /** Paint all turtles (calling paintComponent()) + */ + public void paintTurtles() { + isTurtleVisible = false; + Graphics2D g2D = getTurtleG2D(); + for (int i = 0; i < countTurtles(); i++) { + Turtle aTurtle = getTurtle(i); + if (!aTurtle.isHidden()) { + paintTurtle(aTurtle); + } + } + + // This is the main repaint call, when the turtle is + // moving (even when all turtles are hidden). + // Strange behaviour an slow Mac machines (pre J2SE 1.4 version): + // It happens that some turtle images are not completely redrawn. + // This is probably due to an improper handling of fast multiple repaint requests. + // Workaround: we wait a small amount of time (and give the thread away) + // (No visible slow down on new machines.) + + paintPlayground(); + if (printerG2D == null) { + //if (isRepainting) + //repaint(); + //paintPlayground(); + } + + if (isTurtleVisible) { + try { + Thread.currentThread().sleep(10); + } + catch (Exception e) {} + } + } + + + /** Paint the given Turtle. + * ( no repaint() ) + */ + public void paintTurtle(Turtle turtle) { + if (turtleBuffer == null){ + turtleBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); + turtleG2D = turtleBuffer.createGraphics(); + } + Graphics2D turtleGraphics = getTurtleG2D(); + turtle.getTurtleRenderer().paint(turtle._getX(), turtle._getY(), turtleGraphics); + isTurtleVisible = true; + } + + /** Put an image of the given Turtle in turtle buffer. + */ + protected void stampTurtle(Turtle turtle) { + turtle.clone(); + isTurtleVisible = true; + if (printerG2D == null) + repaint(); + } + + /** Draw a line from the point (x0, y0) to (x1, y1) + with the color of the given Pen. + */ + protected void lineTo(double x0, double y0, double x1, double y1, Pen pen) { + int ix0 = (int)Math.round(x0); + int iy0 = (int)Math.round(y0); + int ix1 = (int)Math.round(x1); + int iy1 = (int)Math.round(y1); + Color color = pen.getColor(); + + if (traceBuffer == null) { + traceBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); + traceG2D = traceBuffer.createGraphics(); + } + Graphics2D traceG2D = getTraceG2D(); + traceG2D.setColor(color); + traceG2D.setStroke(pen.getStroke()); + traceG2D.drawLine(ix0, iy0, ix1, iy1); + if (printerG2D != null) + printerG2D.drawLine(ix0, iy0, ix1, iy1); + } + + /** A class for convenience. */ + protected class Point extends java.awt.Point { + Point(int x, int y) { + super(x, y); + } + Point() { + super(); + } + Point(Point p) { + super(p.x, p.y); + } + + /** Get a new Point with coordinates (this.x+p.x, this.y+p.y). + */ + protected Point add(Point p) { + return new Point(this.x+p.x, this.y+p.y); + } + + /** Translate by the amounts dx = p.x, dy = p.y. */ + protected void translate(Point p) { + translate(p.x, p.y); + } + + public String toString() { + return "(" + x + "," + y + ")"; + } + } + + /** Fill a region. + The region is defined by the Turtles actual position and + is bounded by any other color than the give background color. + */ + public void fill(Turtle t, Color bgColor) { + final Point[] diff = { new Point(0,-1), new Point(-1,0), new Point(1,0), new Point(0,1)}; + final int N=0; + final int W=1; + final int E=2; + final int S=3; + + int bgcolor = bgColor.getRGB(); + int fillColor = t.getPen().getFillColor().getRGB(); + Vector list = new Vector(); + Point2D.Double p1 = toScreenCoords(t.getPos()); + int startX = (int)Math.round(p1.getX()); + int startY = (int)Math.round(p1.getY()); + Point p = new Point(startX, startY); + if (traceBuffer.getRGB(startX, startY) == bgcolor) { + traceBuffer.setRGB(startX, startY, fillColor); + list.addElement(new Point(startX, startY)); + int d = N; + int back; + while (list.size() > 0) { + while (d <= S) { // forward + Point tmp = p.add(diff[d]); + try { + if (traceBuffer.getRGB(tmp.x, tmp.y) == bgcolor) { + p.translate(diff[d]); + traceBuffer.setRGB(p.x, p.y, fillColor); + if (printerG2D != null) + { + printerG2D.setColor(t.getPen().getFillColor()); + // printerG2D.drawLine(p.x,p.y, p.x, p.y); + BasicStroke stroke = new BasicStroke(2); + printerG2D.setStroke(stroke); + Line2D line = new Line2D.Double(p.x, p.y, p.x, p.y); + printerG2D.draw(line); + } + list.addElement(new Integer(d)); + d=N; + } + else { + d++; + } + } + catch (ArrayIndexOutOfBoundsException e) { + d++; + } + } + Object obj = list.remove(list.size()-1); + try { + d=((Integer)obj).intValue(); // last element + back = S - d; + p.translate(diff[back]); + } + catch (ClassCastException e) { + // the first (zeroest) element in list is the start-point + // just do nothing with it + } + } + } + traceG2D.drawLine(0, 0, 0, 0); // Workaround because on Mac the trace buffer is not drawn without this + if (printerG2D == null) + repaint(); + } + + /** + * Clear the playground with given color. + */ + public void clear(Color color) { + traceG2D.setColor(color); + traceG2D.fillRect(0, 0, getWidth(), getHeight()); + turtleG2D.setColor(color); + turtleG2D.fillRect(0, 0, getWidth(), getHeight()); + isTurtleVisible = true; + if (printerG2D == null) + repaint(); + } + + /** + * Clear playground. + */ + public void clear() { + clear(getBackground()); + } + + /** Paint the Playground. + just a method for convenience. + */ + public void paintComponent() { + paintComponent(getGraphics()); + } + + /** Draw the trace and turtle buffers. + */ + public void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2D = (Graphics2D)g; + g2D.drawImage(traceBuffer, 0, 0, this); + if (isTurtleVisible) + g2D.drawImage(turtleBuffer, 0, 0, this); + } + + public void paintPlayground() { + Graphics g = getGraphics(); + if (g != null) { + super.paintComponent(g); + Graphics2D g2D = (Graphics2D)g; + g2D.drawImage(traceBuffer, 0, 0, this); + if (isTurtleVisible) + g2D.drawImage(turtleBuffer, 0, 0, this); + + } // end of if + } + + /** Remove all turtles from the turtle buffer. + */ + public void clearTurtles() { + for (int i = 0; i < countTurtles(); i++) { + Turtle turtle = getTurtle(i); + clearTurtle(turtle); + } + } + + /** Remove the given turtle from the turtle buffer. + Override this method if you have added a new behaviour (like + wrap or clip) to the turtle. + */ + public void clearTurtle(Turtle turtle) { + if(turtle != null){ + if (!turtle.isHidden()) { + if(turtle.isClip()){ + clearClipTurtle(turtle); + } + else if(turtle.isWrap()){ + clearWrapTurtle(turtle); + } + } + } + } + + /** This method is called when the given Turtle is in wrap mode. + + @see ch.aplu.turtle.Turtle#wrap + */ + protected void clearWrapTurtle(Turtle turtle){ + clearWrapTurtle(turtle, turtleBuffer); + } + + /** Here the actual clearing of a Turtle in wrap mode from the + given image is performed. + */ + protected void clearWrapTurtle(Turtle turtle, Image im){ + Rectangle bounds = getBounds(turtle); + int pWidth = getWidth(); + int pHeight = getHeight(); + int x = bounds.x; + int y = bounds.y; + while (x > pWidth){ + x -= pWidth; + } + while (x < 0){ + x += pWidth; + } + while (y > pHeight){ + y -= pHeight; + } + while (y < 0){ + y += pHeight; + } + x = x % pWidth; + y = y % pHeight; + toAlphaNull(im, new Rectangle(x, y, bounds.width, bounds.height)); // OK + boolean right = (x + bounds.width > getWidth()); + boolean bottom = (y + bounds.height > getHeight()); + if (right) { + toAlphaNull(im, new Rectangle(x-pWidth, y, bounds.width, bounds.height)); + } + if (bottom) { + toAlphaNull(im, new Rectangle(x, y-pHeight, bounds.width, bounds.height)); + } + if (right && bottom) { + toAlphaNull(im, new Rectangle(x-pWidth, y-pHeight, bounds.width, bounds.height)); + } + } + + /** Copy and translate a given Rectangle. + */ + private Rectangle copyAndTranslate(Rectangle rect, int dx, int dy) { + return new Rectangle(rect.x+dx, rect.y+dy, + rect.width, rect.height); + } + + /** This method is called when the given Turtle is in clip mode. + + @see ch.aplu.turtle.Turtle#clip + */ + protected void clearClipTurtle(Turtle turtle) { + clearClipTurtle(turtle, turtleBuffer); + } + + /** Here the actual clearing of a Turtle in clip mode from the + given image is performed. + */ + protected void clearClipTurtle(Turtle turtle, Image im) { + Rectangle bounds = getBounds(turtle); + toAlphaNull(im, bounds); + } + + /** Set the alpha channel of all pixels in the given image + in the given Rectangle to zero (i.e. totally transparent). + + This method is used byte the clearXXXTurtle methods. + */ + private void toAlphaNull(Image im, Rectangle rect) { + Rectangle rim = new Rectangle(0, 0, im.getWidth(this), im.getHeight(this)); + Rectangle r = new Rectangle(); + if (rect.intersects(rim)) { + r=rect.intersection(rim); + } + int size = r.width*r.height; + float[] alphachannel = new float[r.width*r.height]; + ((BufferedImage)im).getAlphaRaster().setPixels(r.x, r.y, r.width, r.height, alphachannel); + } + + /** Puts a Turtle above all others. + */ + public Turtle toTop(Turtle turtle) { + if (turtles.removeElement(turtle)) { + turtles.add(turtle); + } + return turtle; + } + /** Put a Turtle below all others. + */ + public Turtle toBottom(Turtle turtle) { + if (turtles.removeElement(turtle)) { + turtles.add(0,turtle); + } + return turtle; + } + + /** Calculate the screen coordinates of the given point. + */ + public Point2D.Double toScreenCoords(Point2D.Double p) { + return internalToScreenCoords(p.x, p.y); + } + + /** Calculate the screen coordinates of the given point coordinates. + */ + public Point2D.Double toScreenCoords(double x, double y) { + return internalToScreenCoords(x, y); + } + + protected Point2D.Double internalToScreenCoords(double x, double y) { + // reflect at x-axis, then translate to center of Playground + // pixel coordinates coorespond to turtle coordinates, only translation needed + double newX = originX + x; + double newY = originY - y; + return new Point2D.Double(newX, newY); + } + + /** Calculate the turtle coordinates of the given screen coordinates. + */ + public Point2D.Double toTurtleCoords(double x, double y) { + // pixel coordinates coorespond to turtle coordinates, only translation needed + double newX = x - originX; + double newY = originY - y; + return new Point2D.Double(newX, newY); + } + + /** Calculate the turtle coordinates of the given screen point. + */ + public Point2D.Double toTurtleCoords(Point2D.Double p) { + return toTurtleCoords(p.x, p.y); + } + + /** Calculate the screen angle. + I.e. the interpretation of angle. + @param radians The angle in radians. + */ + double toScreenAngle(double radians) { + double sa = radians; + if (sa < Math.PI/2){ + sa += 2*Math.PI; + } + sa -= Math.PI/2; + if (sa != 0) { + sa = Math.PI*2 - sa; + } + return sa; + } + + /** Calculate the bounds of the Turtles picture on the screen. + */ + protected Rectangle getBounds(Turtle turtle) { + Rectangle bounds = turtle.getBounds(); + Point2D.Double tmp = toScreenCoords(new Point2D.Double(bounds.getX(), bounds.getY())); + bounds.setRect(tmp.x-2, tmp.y-2, bounds.width+4, bounds.height+4); + return bounds; + } + + /** Return the graphics context of the turtle buffer. + */ + public Graphics2D getTurtleG2D() { + return turtleG2D; + } + + /** Return the image of the turtle buffer. + */ + public BufferedImage getTurtleBuffer() { + return turtleBuffer; + } + + /** Return the graphics context of the trace buffer. + */ + public Graphics2D getTraceG2D() { + return traceG2D; + } + + /** Return the graphics context of the printer. + */ + public Graphics2D getPrinterG2D() { + return printerG2D; + } + + /** Return the image of the trace buffer. + */ + public BufferedImage getTraceBuffer() { + return traceBuffer; + } + + /** Clean the traces. + All turtles stay how and where they are, only lines, text and stamps will be removed. + */ + void clean() { + Graphics2D g = getTraceG2D(); + g.setColor(getBackground()); + g.fillRect(0,0,getWidth(), getHeight()); + if (printerG2D == null) + repaint(); + } + + /** Draw the text at the current position of the Turtle t. + Drawing a text at some coordinates (x,y) we mean that the bottom left corner of + the text will be at these coordinates. + Font and colour are specified by the Turtle's Pen. + */ + public void label(String text, Turtle t) { + Point2D.Double sc = toScreenCoords(t.getPos()); + int x = (int)Math.round(sc.x); + int y = (int)Math.round(sc.y); + Graphics2D traceG2D = getTraceG2D(); + FontRenderContext frc = traceG2D.getFontRenderContext(); + Font f = t.getFont(); + TextLayout tl = new TextLayout(text, f, frc); + traceG2D.setColor(t.getPen().getColor()); + tl.draw(traceG2D, x, y); + if (printerG2D != null) + { + printerG2D.setColor(t.getPen().getColor()); + tl.draw(printerG2D, x, y); + } + + if (printerG2D == null) + repaint(); + } + + /** Set antialiasing on or off for the turtle trace buffer + * This may result in an better trace quality. + */ + public void setAntiAliasing(boolean on) { + if (on) + traceG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + else + traceG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_OFF); + } + + + /** + * Set the given TPrintable (implementing draw()), + * open a printer dialog and start printing with given scale. + * Return false, if printer dialog is aborted, + * otherwise return true.
+ * If tp == null, the current playground is printed. + * + */ + protected boolean print(TPrintable tp, double scale) { + if (tp == null) + isPrintScreen = true; + else + isPrintScreen = false; + printerScale = scale; + PrinterJob pj = PrinterJob.getPrinterJob(); + pj.setPrintable(this); + traceCanvas = tp; + if (pj.printDialog()) { + try { + pj.print(); + } + catch (PrinterException ex) { + System.out.println(ex); + } + return true; + } + else + return false; + } + + /** + * For internal use only. Implementation of Printable. + * (Callback method called by printing system.) + */ + public int print(Graphics g, PageFormat pf, int pageIndex) { + if (pageIndex != 0) + return NO_SUCH_PAGE; + Graphics2D g2D = (Graphics2D)g; + double printerWidth = pf.getImageableWidth(); + double printerHeight = pf.getImageableHeight(); + double printerSize = printerWidth > printerHeight ? printerWidth : + printerHeight; + double xZero = pf.getImageableX(); + double yZero = pf.getImageableY(); + + printerG2D = g2D; // Indicate also, we are printing now + + // Needed for fill operations: the trace canvas must be empty in order to + // perform the fill algoritm (searching for outline of figure) + if (!isPrintScreen) + clean(); + + g2D.scale(printerScaleFactor * printerScale, printerScaleFactor * printerScale); + g2D.translate(xZero/printerScale, yZero/printerScale); + + if (isPrintScreen) + { + print(g); + } + else // Printing the traceCanvas + { + // Hide all turtles + boolean[] turtleState = new boolean[countTurtles()]; + for (int i = 0; i < countTurtles(); i++) { + Turtle aTurtle = getTurtle(i); + turtleState[i] = aTurtle.isHidden(); + aTurtle.ht(); + } + traceCanvas.draw(); + + // Restore old context + for (int i = 0; i < countTurtles(); i++) { + Turtle aTurtle = getTurtle(i); + if (!turtleState[i]) + aTurtle.st(); + } + } + + printerG2D = null; + return PAGE_EXISTS; + } + + /** Return the color of the pixel at the current turtle position. + */ + public Color getPixelColor(Turtle t) { + Point2D.Double p1 = toScreenCoords(t.getPos()); + int x = (int)Math.round(p1.getX()); + int y = (int)Math.round(p1.getY()); + return new Color(traceBuffer.getRGB(x, y)); + } + + public void enableRepaint(boolean b) { + isRepainting = b; + } + + /** set the background color of the playground + */ + public void setBackground(Color color) { + super.setBackground(color); + if (traceG2D != null) { + clear(color); + } // end of if + } + + /** + * Sets the origin of the cartesian coordinate system within the playground + */ + public void setOrigin(int x, int y) { + for (int i = 0; i < countTurtles(); i++) { + Turtle turtle = getTurtle(i); + Point2D.Double p1 = toScreenCoords(turtle.getPos()); + double newX = p1.getX() - x; + double newY = y - p1.getY(); + turtle.internalSetPos(newX, newY); + } + originX = x; + originY = y; + } + + public int getOriginX() { + return originX; + } + + public int getOriginY() { + return originY; + } + +} + diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/res/bong.wav b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/res/bong.wav new file mode 100644 index 0000000..0d2fef0 Binary files /dev/null and b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/res/bong.wav differ diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/sharedconstants.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/sharedconstants.java new file mode 100644 index 0000000..1bd3955 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/sharedconstants.java @@ -0,0 +1,84 @@ +// SharedConstants.java + +// Copyright 2002 Regula Hoefer-Isenegger +// +// This file is part of The Java Turtle (TJT) package +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +/* History; +V1.36 - Sep 2004: First official release +V1.37 - Nov 2004: Unchanged, modifications in ch.aplu.util +V1.38 - Dec 2004: Unchanged, modifications in ch.aplu.util +V1.39 - Jan 2005: Unchanged, modifications in ch.aplu.util +V1.40 - Mar 2005: Add background color, TurtleKeyAdapter, TurtleArea +V1.41 - May 2005: User defined turtle shape, minor changes in doc and code style +V1.42 - Dec 2005: Unchanged, modifications in ch.aplu.util +V1.43 - Feb 2006: Bug removed: Turtle.turtleFrame was not initialized in all ctors of class Turtle +V1.44 - Mar 2007: Bug removed: stampTurtle did not work properly in wrap mode +V1.45 - Aug 2007: TurtleKeyAdapter: use wait/notify to reduce CPU time (from 100% to 2%) +V1.46 - Aug 2007: synchronize(playground) for forward and rotate animation, + new method bong() using StreamingPlayer +V1.47 - Sept 2007: Unchanged, modifications in ch.aplu.util +V1.48 - Sept 2007: Unchanged, modifications in ch.aplu.util +V1.49 - Oct 2007: Unchanged, modifications in ch.aplu.util +V1.50 - Oct 2007: Unchanged, modifications in ch.aplu.util +V1.51 - Nov 2007: Fixed: correct position of label, when wrapping is on + Fixed: getPos() returns now the wrapped coordinates + Added: _getPos() returns unwrapped coordinates +V1.52 - Nov 2007: Added bean classes in order to use turtles with a Gui builder +V1.53 - Nov 2007: Added TurtlePane visual information when used in Gui builder design mode +V1.54 - Nov 2007: Minor changes to documentation +V1.55 - Dec 2007: Added property enableFocus to GPane, default: setFocusable(false) +V1.56 - Mar 2008: Unchanged, modifications in ch.aplu.util +V1.57 - Mar 2008: Unchanged, modifications in ch.aplu.util +V1.58 - Mar 2008: Modification to fill() (fill(x, y)): + region is defined with pixel color at current position + as opposed to background color +V1.59 - Oct 2008: Added ctors TurtleFrame with window position (ulx, uly) + Added Turtle.getPixelColor() +V2.00 - Nov 2008: Unchanged, modifications in ch.aplu.util + J2SE V1.4 no longer supported +V2.01 - Jan 2009: Unchanged, modifications in ch.aplu.util +V2.02 - Feb 2009 Turtle constructors run in EDT now +V2.03 - Feb 2009 Unchanged, modifications in ch.aplu.util +V2.04 - Feb 2009 Unchanged, modifications in ch.aplu.util +V2.05 - Feb 2009 Unchanged, modifications in ch.aplu.util +V2.06 - Mar 2009 Unchanged, modifications in ch.aplu.util +V2.07 - Mar 2009 All except print methods synchronized, so Turtle package is + now thread-safe +V2.08 - Apr 2009 Unchanged, modifications in ch.aplu.util +V2.09 - Jun 2009 Unchanged, modifications in ch.aplu.util +V2.10 - Jun 2010 Version for the Java-Editor +*/ + +package ch.aplu.turtle; + +interface SharedConstants +{ + int DEBUG_LEVEL_OFF = 0; + int DEBUG_LEVEL_LOW = 1; + int DEBUG_LEVEL_MEDIUM = 2; + int DEBUG_LEVEL_HIGH = 3; + + int DEBUG = DEBUG_LEVEL_OFF; + + String ABOUT = + "Copyright © 2002-2009\nRegula Hoefer-Isenegger, Aegidius Pluess\n, Gerhard Roehner\n" + + "under GNU General Public License\n" + + "http://www.aplu.ch\n" + + "All rights reserved"; + String VERSION = "2.10 - June 2015"; +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/streamingplayer.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/streamingplayer.java new file mode 100644 index 0000000..8a3d5d5 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/streamingplayer.java @@ -0,0 +1,84 @@ +// StreamingPlayer.java + +package ch.aplu.turtle; + + +import javax.sound.sampled.*; +import java.io.*; + +class StreamingPlayer +{ + private class PlayerThread extends Thread + { + public void run() + { + byte buf[] = new byte[20000]; + try + { + int cnt; + while ((cnt = audioInputStream.read(buf, 0, buf.length)) != -1) + { + if (cnt > 0) + sourceDataLine.write(buf, 0, cnt); + } + sourceDataLine.drain(); + sourceDataLine.close(); + } + catch (IOException ex) + { + System.out.println(ex); + System.exit(1); + } + } + } + + private AudioFormat audioFormat; + private AudioInputStream audioInputStream; + private SourceDataLine sourceDataLine; + private PlayerThread playerThread; + + StreamingPlayer(InputStream is) + { + try + { + audioInputStream = + AudioSystem.getAudioInputStream(is); + audioFormat = audioInputStream.getFormat(); + } + catch (Exception ex) + {} + } + + void start(boolean doFinish) + throws LineUnavailableException + { + DataLine.Info dataLineInfo = + new DataLine.Info(SourceDataLine.class, audioFormat); + sourceDataLine = + (SourceDataLine)AudioSystem.getLine(dataLineInfo); + sourceDataLine.open(audioFormat); + sourceDataLine.start(); + playerThread = new PlayerThread(); + playerThread.start(); + if (doFinish) + waitToFinish(); + } + + boolean isPlaying() + { + if (playerThread == null) + return false; + return (playerThread.isAlive()); + } + + void waitToFinish() + { + if (playerThread == null) + return; + try + { + playerThread.join(); + } + catch (InterruptedException ex) {} + } +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/tprintable.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/tprintable.java new file mode 100644 index 0000000..767d22a --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/tprintable.java @@ -0,0 +1,43 @@ +// TPrintable.java + +// Copyright 2002 Regula Hoefer-Isenegger + +// This file is part of The Java Turtle (TJT) package +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +// Added by Aegidius Pluess + +package ch.aplu.turtle; + +/** + * Interface for printing on an attached printer. + * Normally an application uses a Turtle and implements this interface. + * draw() should contain all drawing operations into the + * Turtle's Playground. The printing occures, when Turtle's print() is + * called.

+ */ + + +public interface TPrintable +{ + /** + * This method must perform all drawing operations. + * Be aware that for some undocumented reason + * draw() is called twice. So be sure you initialize it correctly. + */ + + public void draw(); +} \ No newline at end of file diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/turtle.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/turtle.java new file mode 100644 index 0000000..d30ff52 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/turtle.java @@ -0,0 +1,1585 @@ +// Turtle.java +// Modifications by Aegidius Pluess +// Adaption to the Java-Editor by Gerhard Röhner + +// Copyright 2002-2003 Regula Hoefer-Isenegger (Version 1.0) +// +// This file is part of The Java Turtle package (TJT) +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +/* +Screen coordinate system: 0..400 pixels in both directions corresponds to turtle +turtle coordinate system -200..200, so only translation is necessary (no scaling). + +This avoids rounding errors. +*/ + +package ch.aplu.turtle; + +import ch.aplu.turtle.*; +import java.awt.*; +import java.awt.event.*; +import java.awt.geom.*; +import java.awt.image.*; +import java.awt.print.*; +import java.io.*; +import javax.sound.sampled.*; +import javax.swing.*; + +/** + The core class for turtles. + + For a simple example on how to use Turtle, cf. the + Java Turtle Package description. + @author Regula Hoefer-Isenegger + @version 0.1.1 +*/ +public class Turtle implements Cloneable +{ + private double angle; + private Point2D.Double position; + private Playground playground; + private int framesPerSecond; + private double speed; // Pixel/sec + private double angleSpeed; // Radian/sec + private TurtleRenderer turtleRenderer; + private int angleResolution; + private LineRenderer lineRenderer; + private static TurtleFactory turtleFactory; + private boolean penUp; + private boolean showTurtle; + private boolean initialVisibility = true; + private Pen pen; + private Color color; + private int edgeBehaviour; + + + /** Convert from screen coordinates to turtle coordinates + */ + public Point2D.Double toTurtlePos(Point p) { + return playground.toTurtleCoords((double)p.x, (double)p.y); + } + + /** Convert from screen coordinates to turtle coordinates + */ + public Point2D.Double toTurtlePos(int x, int y) { + return playground.toTurtleCoords((double)x, (double)y); + } + + /** Convert from screen coordinates to turtle coordinates + */ + public double toTurtleX(int x) { + return (playground.toTurtleCoords((double)x, 0)).x; + } + + /** Convert from screen coordinates to turtle coordinates + */ + public double toTurtleY(int y) { + return (playground.toTurtleCoords(0, (double)y)).y; + } + + /** Add the specified mouse listener to receive mouse events. + */ + public void addMouseListener(MouseListener l) { + playground.addMouseListener(l); + } + + /** Add the specified mouse motion listener to receive mouse motion events. + */ + public void addMouseMotionListener(MouseMotionListener l) { + playground.addMouseMotionListener(l); + } + + /** + * Emit a beep. Fails if no standard speaker available. Try to use bong() instead. + * @see #bong() + */ + public void beep() { + Toolkit.getDefaultToolkit().beep(); + } + + /** + * Emit a 'bong' via the sound card. Useful on computers without standard + * speaker. + * @see #beep() + */ + public void bong() { + InputStream is = getClass().getResourceAsStream("res/bong.wav"); + StreamingPlayer player = new StreamingPlayer(is); + try + { + player.start(false); + } + catch (LineUnavailableException ex) + { + System.out.println("Error in bong(). Sound card unavailable"); + } + } + + + /** Represent clip mode. + @see #WRAP + @see #clip() + */ + protected final static int CLIP = 0; + /** Represent wrap mode. + + @see #CLIP + @see #wrap() + */ + protected final static int WRAP = 1; + /** Represent the default edge behaviour (i.e. CLIP or WRAP). + + @see #CLIP + @see #WRAP + @see #clip() + @see #wrap() + */ + protected static int DEFAULT_EDGE_BEHAVIOUR = CLIP; + /** Represent the default speed (velocity). + + @see #setSpeed(double) + */ + protected static double DEFAULT_SPEED = 200; + + /** Represent the default angle resolution. + + It specifies how many different turtle pictures + are generated. + */ + protected static int DEFAULT_ANGLE_RESOLUTION = 72; + + /** Specify how many frames per second are used for + turtle animation. + */ + protected static int DEFAULT_FRAMES_PER_SECOND = 10; + + /** Specify the default turtle color. + + @see #setColor(java.awt.Color) + */ + protected static Color DEFAULT_TURTLE_COLOR = Color.cyan; + + /** Specify the default pen color. + + @see #setPenColor(java.awt.Color) + */ + protected static Color DEFAULT_PEN_COLOR = Color.blue; + + /** Create a new Turtle + */ + public Turtle() { + } + + /** init a new Turtle + */ + protected void init(Color color) { + angle = 0; + position = new Point2D.Double(0, 0); + framesPerSecond = DEFAULT_FRAMES_PER_SECOND; + if (initialVisibility) + setSpeed(DEFAULT_SPEED); + else + setSpeed(-1); + showTurtle = initialVisibility; + setAngleResolution(DEFAULT_ANGLE_RESOLUTION); + angleSpeed = getSpeed() * Math.PI * 2 / DEFAULT_SPEED; + pen = new Pen(DEFAULT_PEN_COLOR); + if (getTurtleFactory() == null){ + turtleFactory = createTurtleFactory(); + } + setColor(color); + lineRenderer = createLineRenderer(); + getTurtleRenderer().setAngle(getAngle()); + } + + /** Set the angle resolution for the Turtle's pictures. + + It specifies how many pictures are used. e.g. an angle resolution + of 90 means that you get one picture for every 4 degrees + (= 360/90 degrees). + */ + public void setAngleResolution(int newResolution) { + synchronized(playground) { + angleResolution = newResolution; + } + } + + /** Return the TurtleFactory of this turtle. + + @see ch.aplu.turtle.TurtleFactory + */ + public TurtleFactory getTurtleFactory() { + return this.turtleFactory; + } + + /** Create a LineRenderer which is responsible + for the correct drawing of the lines. + + @return the new LineRenderer + */ + + protected LineRenderer createLineRenderer() { + return new LineRenderer(this); + } + + /** Create a TurtleRenderer which is responsible + for the correct drawing of the Turtle. + + @return the new TurtleRenderer + */ + protected TurtleRenderer createTurtleRenderer() { + return new TurtleRenderer(this); + } + + /** Create a TurtleFactory which provides for + the Turtle pictures. + + @return the new TurtleFactory + */ + + protected TurtleFactory createTurtleFactory() { + return new TurtleFactory(); + } + /** Get the angle resolution. + + @see #setAngleResolution(int) + */ + + protected int getAngleResolution() { + return angleResolution; + } + + /** Get the TurtleRenderer. + */ + TurtleRenderer getTurtleRenderer() { + return turtleRenderer; + } + + /** Get the LineRenderer. + */ + private LineRenderer getLineRenderer() { + return lineRenderer; + } + + /** Get the Playground. + */ + public Playground getPlayground() { + return playground; + } + + /** Set the Playground to the specified + playground. + + The Turtle is removed from the old + Playground and set to the new one. + */ + public void setPlayground(Playground playground) { + Playground pg = getPlayground(); + if(pg != null){ + pg.clearTurtle(this); + pg.remove(this); + pg.paintTurtles(); + } + this.playground = playground; + playground.paintTurtles(this); + } + + /** Get the angle speed. + + (I.e. how fast the Turtle rotation + animation is performed.) + */ + private double getAngleSpeed() { + return angleSpeed; + } + + /** Set the angle speed. + + @see #getAngleSpeed() + */ + private void setAngleSpeed(double newAngleSpeed) { + this.angleSpeed = newAngleSpeed; + } + + /** Get the current angle (heading) of the + Turtle. + */ + private double getAngle() { + return angle; // in radians + } + + /** Get the current speed. + */ + public double getSpeed() { + return speed; + } + + /** Query the Turtle's x-position without wrapping. + * Coordinate are not bound to playground even when + * wrapping is on. + */ + public double _getX() { + synchronized (playground) { + return position.getX(); + } + } + + /** Query the Turtle's y-position without wrapping. + * Coordinate are not bound to playground even when + * wrapping is on. + */ + public double _getY() { + synchronized (playground) { + return position.getY(); + } + } + + /** Query the Turtle's x-position. + * If turtle is outside playground and wrapping is on + * return the coordinate in range -200..200. + * */ + + public double getX() { + synchronized (playground) { + double xPos = _getX(); + return xPos; + } + } + + /** Query the Turtle's x-position. + * If turtle is outside playground and wrapping is on + * return the coordinate in range -200..200. + */ + public double getY() { + synchronized (playground) { + double yPos = _getY(); + return yPos; + } + } + + + /** Query the Turtle's position without wrapping. + * Coordinate are not bound to playground even when + * wrapping is on. + */ + public Point2D.Double _getPos() { + return position; + } + + /** Query the Turtle's position */ + public Point2D.Double getPos() { + return new Point2D.Double(getX(), getY()); + } + + /** Put the Turtle to a new position with the given x-coordinates. + + */ + public void setX(double x) { + synchronized (playground) { + getPlayground().clearTurtle(this); + internalSetX(x); + getPlayground().paintTurtles(this); + } + } + + /** Put the Turtle to a new position with the given screen x-coordinates. + */ + public void setScreenX(int x) { + synchronized (playground) { + setX(toTurtleX(x)); + } + } + + /** Put the Turtle to a new position with the given y-coordinates. + */ + public void setY(double y) { + synchronized (playground) { + getPlayground().clearTurtle(this); + internalSetY(y); + getPlayground().paintTurtles(this); + } + } + + /** Put the Turtle to a new position with the given screen y-coordinates. + */ + public void setScreenY(int y) { + synchronized (playground) { + setY(toTurtleY(y)); + } + } + + /** Set the Turtle's x-Coordinate. + */ + protected void internalSetX(double x) { + synchronized (playground) { + position.setLocation(x, _getY()); + } + } + + /** Set the Turtle's y-Coordinate. + */ + protected void internalSetY(double y) { + position.setLocation(_getX(), y); + } + + /** Set the Turtle's Position. + */ + protected void internalSetPos(double x, double y) { + position.setLocation(x, y); + } + + /** Hide the Turtle. + + @see #st() + If there is only one turte, the speed is set to -1 + so there is no Turtle animation at all. + Hiding the Turtle speeds up the graphics enormously. + */ + public void ht() { + synchronized (playground) { + this.internalHide(); + if (getPlayground().countTurtles() == 1) + setSpeed(-1); + } + } + + /** Hide the Turtle. + + This is the same as ht(). + @see #st() + */ + public void hideTurtle() { + ht(); + } + + /** This is the method called by the public methods ht() and hideTurtle(). + + Here the actual hiding takes place. + @see #ht() + @see #hideTurtle() + */ + protected void internalHide() { + getPlayground().clearTurtle(this); + showTurtle = false; + if (getPlayground().getPrinterG2D() == null) + getPlayground().repaint(); + } + + /** Set the Turtle to show mode. + + That means that the Turtle will be drawn. + @see #ht() + */ + public void st() { + synchronized (playground) { + if (getPlayground().getPrinterG2D() == null) { + getPlayground().paintTurtle(this); + showTurtle = true; + getPlayground().repaint(); + } + } + } + + /** The same as st(). + + @see #st() + */ + public void showTurtle() { + st(); + } + + /** Tell whether the Turtle is hidden or not. + + @return true if the Turtle is hidden, + false otherwise. + */ + public boolean isHidden() { + return !showTurtle; + } + + private int getFramesPerSecond() { + return this.framesPerSecond; + } + + /** Only set the angle attribute. This method does not + invoke any re-painting. + */ + private void setAngle(double radians) { + this.angle = radians; + } + + /** This is the same as setH(double degrees). + + @see #setH(double) + */ + public void setHeading(double degrees) { + synchronized (playground) { + setAngle(Math.toRadians(degrees)); + getTurtleRenderer().setAngle(Math.toRadians(degrees)); + getPlayground().clearTurtle(this); + getPlayground().paintTurtles(this); + } + } + + /** Set the Turtle's heading. + + 0 means facing NORTH.
+ the angles are measured clockwise. + @see #setHeading(double) + */ + public void setH(double degrees) { + setHeading(degrees); + } + /** Query the Turtle's heading. + + @see #setH(double) + */ + public double heading() { + synchronized (playground) { + return Math.toDegrees(getAngle()); + } + } + /** Set the Turtle's heading to the new value. + + @return the old (previous) value. + @see #setH(double) + */ + public double heading(double degrees) { + synchronized (playground) { + double tmp = Math.toDegrees(getAngle()); + setHeading(degrees); + return tmp; + } + } + + /** Set the Turtle's speed. + + If you try to set the speed to 0, it will be set to 1 (very slow). + A negative speed means that moving the Turtle (fd, bk) + will not be animated.
+ The unit is pixels per second (up to certain bounds depending on the CPU etc.).
+ Remark: Dashed lines will only be painted as you expect it with speed set + to -1. + + @see #fd(double) + @see #bk(double) + */ + public void setSpeed(double speed) { + if (speed == 0) + this.speed = 1; + else + this.speed = speed; + } + + + /** This method is responsible for the rotation animation. + */ + private void internalRotate(double angle) { + if(isHidden()){ + synchronized(playground) + { + setAngle(getAngle()+angle); + if (getTurtleRenderer().imageChanged(getAngle())) { + getTurtleRenderer().setAngle(getAngle()); + } + } + return; + } + if (angle != 0) { + int iterations = getAngleIterations(angle); + + double sign = angle/Math.abs(angle); + double increment = sign*getAngleSpeed()/(double)getFramesPerSecond(); + double startAngle = getAngle(); + + for (int index = 0; index < iterations; index++) { + long timeStamp = System.currentTimeMillis(); + + synchronized(playground) { + getPlayground().clearTurtle(this); + + if (index < iterations-1) { + setAngle(getAngle()+increment); + } + else { + setAngle(startAngle+angle); + } + + if (getTurtleRenderer().imageChanged(getAngle())) { + getTurtleRenderer().setAngle(getAngle()); + getPlayground().paintTurtles(this); + } + } + + long newTimeStamp = System.currentTimeMillis(); + Double secs = new Double(1000./getFramesPerSecond()); + long requiredTime = secs.longValue()-newTimeStamp+timeStamp; + + if (requiredTime > 0) { + try { + Thread.sleep(requiredTime); + } + catch (InterruptedException e) { + } + } + } + } + getPlayground().paintTurtles(this); + } + + /** This method is responsible for the moving animation. + */ + private void internalMove(double length) { + if (getSpeed()>0){ + if (length != 0) { + int iterations = getPathIterations(length); + // an angle of 0 means: facing NORTH + double startX = _getX(); + double startY = _getY(); + getLineRenderer().init(startX, startY); + double dx = length * Math.sin(getAngle()); + double dy = length * Math.cos(getAngle()); + double incrementX = dx / iterations; + double incrementY = dy / iterations; + for (int index = 0; index < iterations; index++) { + long timeStamp = System.currentTimeMillis(); + int nX = (int)_getX(); + int nY = (int)_getY(); + synchronized(playground) { + playground.clearTurtle(this); + if (index < iterations-1) { + internalSetX(_getX()+incrementX); + internalSetY(_getY()+incrementY); + } + else { // last step: Calc the "exact" value + internalSetX(startX + dx); + internalSetY(startY + dy); + } + if (nX != (int)_getX() || nY != - (int)_getY() || index == iterations-1){ + if (!isPenUp()) { + getLineRenderer().lineTo(_getX(), _getY()); + } + getPlayground().paintTurtles(this); + } + } + Double frames = new Double(1000./getFramesPerSecond()); + long newTimeStamp = System.currentTimeMillis(); + long requiredTime = frames.longValue()-newTimeStamp+timeStamp; + if (requiredTime > 0) { + try { + Thread.sleep(requiredTime); + } + catch (InterruptedException e) { + } + } + } + } + } + else { // Speed < 0, i.e. no animation + double startX = _getX(); + double startY = _getY(); + getLineRenderer().init(startX, startY); + double dx = length * Math.sin(getAngle()); + double dy = length * Math.cos(getAngle()); + getPlayground().clearTurtle(this); + internalSetX(startX + dx); + internalSetY(startY + dy); + if (!isPenUp()) { + getLineRenderer().lineTo(_getX(), _getY()); + } + getPlayground().paintTurtles(this); + } + } + + /** Turn the Turtle the given angle (in degrees) to the left + + @see #rt(double) + */ + public void lt(double degrees) { + left(degrees); + } + + /** Same as lt(double degrees) + + @see #lt(double) + */ + public void left(double degrees) { + internalRotate(Math.toRadians(-degrees)); + } + + /** Turn the Turtle the given angle (in degrees) to the right. + + @see #rt(double) + */ + public void rt(double degrees) { + right(degrees); + } + + /** Same as rt(double degrees). + + @see #rt(double) + */ + public void right(double degrees) { + internalRotate(Math.toRadians(degrees)); + } + + /** Same as fd(double distance) + + @see #fd(double) + */ + public void forward(double distance) { + internalMove(distance); + } + + /** Move the Turtle forwards. + + Negative values for distance are + allowed. In that case, the Turtle + will move backwards. + + @see #bk(double) + */ + public void fd(double distance) { + forward(distance); + } + + /** Same as bk(double distance). + + @see #bk(double) + */ + public void back(double distance) { + forward(-distance); + } + + /** Move the Turtle backwards. + + Negative values for distance are + allowed. In that case, the Turtle + will move forwards. + + @see #fd(double) + */ + public void bk(double distance) { + back(distance); + } + + /** Query the distance from the current location + to the given one. + */ + public double distance(double x, double y) { + synchronized (playground) { + return this.getPos().distance(x,y); + } + } + + /** Query the distance from the current location + to the given one. + * Actually a polygon with 36 sides is drawn. + */ + public double distance(Point2D.Double p) { + synchronized (playground) { + return this.getPos().distance(p); + } + } + + /** Draw a circle to the left from the current position with + * turtle's heading tangent direction and given radius. + * Actually a polygon with 36 sides is drawn. + */ + public void leftCircle(double radius) { + circle(radius, true); + } + + /** Draw a circle to the right from the current position with + * turtle's heading tangent direction and given radius. + * Actually a polygon with 36 sides is drawn. + */ + public void rightCircle(double radius) { + circle(radius, false); + } + + private void circle(double radius, boolean left) { + double nbSteps = 36; // 36 side polygon + double angle = 2 * Math.PI / nbSteps; + double step = 2 * radius * Math.sin(angle/2); + for (int i = 0; i < 36; i++) { + if (left) + if (i == 0) + lt(180.0 / nbSteps); + else + lt(360.0 / nbSteps); + else + if (i == 0) + rt(180.0 / nbSteps); + else + rt(360.0 / nbSteps); + fd(step); + } + } + + /** Calculate the number of iterations when animating left or right (rotation). + */ + private int getAngleIterations(double dAngle) { + if(getAngleSpeed()<0){ + return 1; + } + if(getAngleSpeed()==0){ + setAngleSpeed(1); + } + double dAbsAngle = Math.abs(dAngle); + Double dValue = new Double(Math.ceil(dAbsAngle/getAngleSpeed()*getFramesPerSecond())); + return dValue.intValue(); + } + + /** Calculate the number of iterations when animating forwards or backwards. + */ + private int getPathIterations(double length) { + if(speed < 0) + return 1; + + if (speed == 0) + setSpeed(1); + + double dAbsLength = Math.abs(length); + Double dValue = new Double(Math.ceil(dAbsLength/getSpeed()*getFramesPerSecond())); + return dValue.intValue(); + } + + /** Lift the Turtle's pen up so it + won't draw a line anymore. + + This is the same as pu(). + + @see #pu() + @see #penDown() + @see #pd() + */ + public void penUp() { + synchronized (playground) { + this.penUp = true; + } + } + + /** Lift the Turtle's pen up so it + won't draw a line anymore. + + This is the same as penUp(). + + @see #penUp() + @see #penDown() + @see #pd() + */ + public void pu() { + this.penUp(); + } + + /** Lower the Turtle's Pen down so it + will draw a line when moving. + + This is the same as pd(). + + @see #pd() + @see #penUp() + @see #pu() + */ + public void penDown() { + synchronized (playground) { + this.penUp = false; + } + } + + /** Lowers the Turtle's pen down so it + will draw a line when moving. + + This is the same as penDown(). + + @see #penDown() + @see #penUp() + @see #pu() + */ + public void pd() { + this.penDown(); + } + + /** Query the Pen's state (up or down). + + @return true if the Pen is + up, false otherwise. + @see #pu() + @see #pd() + */ + public boolean isPenUp() { + return this.penUp; + } + + /** Return the bounds of this Turtle. This is required + by the methods that (return-)paint the Turtles. + */ + Rectangle getBounds() { + Rectangle rect = new Rectangle(); + + Image img = getTurtleRenderer().currentImage(); + int nWidth = img.getWidth(getTurtleRenderer()); + int nHeight = img.getHeight(getTurtleRenderer()); + double x = (_getX()<0)?Math.floor(_getX()):Math.ceil(_getX()); + double y = (_getY()<0)?Math.floor(_getY()):Math.ceil(_getY()); + rect.setBounds((int)x-nWidth/2, (int)y + nHeight/2, nWidth, nHeight); + return rect; + } + + /** Get the Turtle's Pen. + + You need it if you want to change end caps etc. + @see Pen + */ + public Pen getPen() { + return pen; + } + /** Set the line thickness. + + This works only neatly in clip mode (yet). + @see #clip() + @see #wrap() + */ + public void setLineWidth(double lineWidth){ + synchronized (playground) { + setLineWidth((float)lineWidth); + } + } + + /** Set the line thickness. + + This works only neatly in clip mode (yet). + @see #clip() + @see #wrap() + */ + public void setLineWidth(float lineWidth) { + synchronized (playground) { + getPen().setLineWidth(lineWidth); + } + } + + /** Set the Turtle's color to the specified one. + + */ + public void setColor(Color color) { + synchronized (playground) { + internalSetColor(color); + getPlayground().paintTurtles(); + } + } + + private void internalSetColor(Color color) { + this.color = color; + if (getTurtleRenderer()==null) { + turtleRenderer = createTurtleRenderer(); + getTurtleRenderer().init(getTurtleFactory(), getAngleResolution()); + } + else{ + getTurtleRenderer().init(new TurtleFactory(), + this.angleResolution); + } + } + + /** Set the color to the specified one. + + @see #fill() + */ + public void setFillColor(Color color) { + synchronized (playground) { + getPen().setFillColor(color); + } + } + + /** Query the Turtle's current color. + */ + public Color getColor(){ + return color; + } + + /**Set the Turtle's pen color. + + */ + public void setPenColor(Color color) { + synchronized (playground) { + getPen().setColor(color); + } + } + + /** Move the Turtle back "home", i.e. set its position + to the origin, facing NORTH. + + Color, PenColor etc. remain the same. + */ + public void home() { + // first : clean the Turtle! + synchronized (playground) { + getPlayground().clearTurtle(this); + position = new Point2D.Double(0,0); + setHeading(0); + } + } + + /** The Turtle's Pen is + changed to an eraser (which is in fact a pen with + background color). + + This is the same as pe() + @see #pe() + */ + public void penErase() { + synchronized (playground) { + this.internalPenErase(); + } + } + + /** The Turtle's Pen is + changed to an eraser (which is in fact a pen with + background color). + + This is the same as penErase() + @see #penErase() + */ + public void pe() { + penErase(); + } + + protected void internalPenErase(){ + this.setPenColor(getPlayground().getBackground()); + } + + /** Put the Turtle to a new position with specified + x- and y-coordinates. + */ + public void setPos(double x, double y) { + synchronized (playground) { + getPlayground().clearTurtle(this); + internalSetPos(x, y); + getPlayground().paintTurtles(); + } + } + + /** Put the Turtle to a new position. + */ + public void setPos(Point2D.Double p) { + setPos(p.x, p.y); + } + + /** Put the Turtle to a new screen position. + + */ + public void setScreenPos(Point p) { + setPos(toTurtlePos(p)); + } + + + /** Put a Turtle image at the current position. + + */ + public void stampTurtle() { + synchronized (playground) { + this.getPlayground().stampTurtle(this); + } + } + + /** Calculate the direction to a given point. + + @return the direction from the current turtle position + towards the given point, measured in degrees and clockwise + from the vertical upwards position. + */ + public double towards(double x, double y) { + synchronized (playground) { + double dx = x - getX(); + double dy = y - getY(); + double result = Math.toDegrees(Math.atan2(dx, dy)); + return (result < 0) ? result+360 : result; + } + } + + /** Calculate the direction to a given point. + + @return the direction from the current turtle position + towards the given point, measured in degrees and clockwise + from the vertical upwards position. + */ + public double towards(Point2D.Double p) { + return towards(p.getX(), p.getY()); + } + + /** Put the Turtle to the top (i.e. above any other + turtle). + + Just invokes the toTop-Method in Playground. + @see Playground#toTop + @see #internalToBottom + */ + void internalToTop() { + this.getPlayground().toTop(this); + } + /** Put the Turtle to the bottom (i.e. under any other + turtle). + + Just invokes the toBottom-Method in Playground. + @see Playground#toBottom + @see #internalToTop + */ + void internalToBottom() { + this.getPlayground().toBottom(this); + } + + /** Put this turtle to the bottom. + + So any other turtle in the same Playground will be drawn over it. + */ + public void toBottom() { + // this.getPlayground().toBottom(this); + synchronized (playground) { + internalToBottom(); + getPlayground().paintTurtles(); + } + } + + /** Put this turtle to the top. + + So it will be drawn over any other turtle in the same Playground. + */ + public void toTop() { + // this.getPlayground().toTop(this); + synchronized (playground) { + this.getPlayground().paintTurtles(this); + } + } + + /** Set the pen width. + */ + public void penWidth(int newWidth) { + synchronized (playground) { + penWidth((float)newWidth); + } + } + + /** Internal Method for setting the penWidth. + */ + private void penWidth(float newWidth) { + setLineWidth(newWidth); + } + + /** Query the pen width. + */ + public int penWidth(){ + return (int)this.getPen().getLineWidth(); + } + + /** Returns the current edge behaviour. + + @see #CLIP + @see #WRAP + */ + protected int getEdgeBehaviour() { + return edgeBehaviour; + } + + /** Sets the edge behaviour to the specified value; + + @see #CLIP + @see #WRAP + */ + protected void setEdgeBehaviour(int edgeBehaviour) { + synchronized (playground) { + this.edgeBehaviour = edgeBehaviour; + } + } + + /** Set the Turtle to clip-mode. + @see #wrap() + @see #clip() + */ + public void clip() { + synchronized (playground) { + setEdgeBehaviour(CLIP); + } + } + + /** Cause the Turtle to wrap around the playground borders. + + e.g when as the Turtle leaves the Window on one side, + it reappears on the opposite side. + + @see #clip() + */ + public void wrap() { + synchronized (playground) { + setEdgeBehaviour(WRAP); + } + } + + /** Tell whether the Turtle is in clip mode. + + @return true if in clip mode, false otherwise. + @see #clip() + @see #wrap() + */ + public boolean isClip() { + return (getEdgeBehaviour() == CLIP); + } + /** Tell wheter the Turtle is in wrap mode. + + @return true if in wrap mode, false otherwise. + @see #wrap() + @see #clip() + */ + public boolean isWrap() { + return (getEdgeBehaviour() == WRAP); + } + + /** Fill the region the Turtle is in.
+ + A region is bounded by lines + of any color different to the pixel color at the current turtle position + and by the border of the window.
+ (The pen of the Turtle must be down.) + */ + public void fill() { + synchronized (playground) { + getPlayground().fill(this, getPlayground().getPixelColor(this)); + } + } + + /** Fill the region as if the Turtle where at coordinates x and y.
+ A region is bounded by lines + of any color different to the pixel color at the given position and by the border of + the window.
+ (The pen of the Turtle must be down.) + */ + public void fill(double x, double y) { + synchronized (playground) { + double oldX = getX(); + double oldY = getY(); + boolean hidden = isHidden(); + ht(); + setPos(x,y); + getPlayground().fill(this, getPlayground().getPixelColor(this)); + setPos(oldX, oldY); + if(!hidden){ + st(); + } + } + } + + /** Erase all traces and text painted by the turtles, but let all + * turtles where they are. + + */ + public void clean() { + synchronized (playground) { + getPlayground().clean(); + } + } + + /** Paint the specified text at the current turtle position. + + */ + public void label(String text) { + synchronized (playground) { + if (text != null && text.length() > 0) + getPlayground().label(text, this); + } + } + + /** Set the current font as specified. + + @see java.awt.Font + */ + public void setFont(Font font) { + synchronized (playground) { + getPen().setFont(font); + } + } + + /** Change the current font to the specified one. If you want to know what fonts are available on your system, + call #getAvailableFontFamilies() . + + @see #getAvailableFontFamilies + @see java.awt.Font more information about fontName, style and size. + */ + public void setFont(String fontName, int style, int size) { + synchronized (playground) { + getPen().setFont(new Font(fontName, style, size)); + } + } + + /** Set the font size. + + @see #setFontStyle changing the font style + @see #setFont changing the whole font. + */ + public void setFontSize(int size) { + synchronized (playground) { + getPen().setFontSize(size); + } + } + + /** Set the font style. + + This is either java.awt.Font.PLAIN, java.awt.Font.BOLD, java.awt.Font.ITALIC or java.awt.Font.BOLD+java.awt.Font.ITALIC + @see #setFontSize changing the font size + @see #setFont changing the whole font. + */ + public void setFontStyle(int style) { + synchronized (playground) { + getPen().setFontStyle(style); + } + } + + /** Provide information about all font families (e.g. roman) currently available on your system. + Each font name is a string packed into a array of strings. + (This might be useful when you want to change the font.) + @see #setFont(java.awt.Font) + @see #setFont(java.lang.String, int, int) + */ + public static String[] getAvailableFontFamilies() { + return Pen.getAvailableFontFamilies(); + } + + /** Return the current font. + + @see #setFontSize changing the font size + @see #setFontStyle changing the font style + @see #setFont changing the whole font. + */ + public Font getFont() { + return getPen().getFont(); + } + + /** Creates a clone of the Turtle in it's Playground. + Color, position and heading are the same. + (It's visible and pen is down.) + Return the cloned object reference as a Object type, because + it overrides Object.clone(). + (You may cast it to a Turtle.) + */ + public Object clone() + { + synchronized (playground) { + Turtle t = new Turtle(); + t.setColor(getColor()); + t.setPos(getX(), getY()); + t.heading(heading()); + t.showTurtle(); + return t; + } + } + + /** Set antialiasing on or off for the turtle trace buffer + * This may result in an better image quality, especially + * for filling operations (platform dependant). + */ + public void antiAliasing(boolean on) { + synchronized (playground) { + getPlayground().setAntiAliasing(on); + } + } + + + /** + * Print the graphics context to an attached printer with + * the given magnification scale factor. + * scale = 1 will print on standard A4 format paper.
+ * + * The given tp must implement the GPrintable interface, + * e.g. the single method void draw(), where all the + * drawing into the GPanel must occur. + * + * Be aware the turtle(s) state (position, direction, etc.) + * must be reinitialized, because draw() is called several + * times by the printing system. + * + * A standard printer dialog is shown before printing is + * started. Only turte traces are printed. + *
+ * Example:
+ * + import ch.aplu.turtle.*;
+
+ public class PrintTest implements TPrintable
+ {
+ private Turtle t = new Turtle();
+
+ public PrintTest()
+ {
+ draw(); // Draw on screen
+ t.print(this); // Draw on printer
+ }
+
+ public void draw()
+ {
+ t.home(); // Needed for initialization
+ for (int i = 0; i < 5; i++)
+ t.fd(20).rt(90).fd(20).lt(90);
+ }
+
+ public static void main(String[] args)
+ {
+ new PrintTest();
+ }
+ }
+
+ */ + public boolean print(TPrintable tp, double scale) + { + return getPlayground().print(tp, scale); + } + + /** + * Same as print(tp, scale) with scale = 1. + */ + public boolean print(TPrintable tp) + { + return print(tp, 1); + } + + /** + * Print the Turtle's current playground with given scale. + */ + public boolean printScreen(double scale) + { + return print(null, scale); + } + + /** + * Same as printScreen(scale) with scale = 1. + */ + public boolean printScreen() + { + return printScreen(1); + } + + /** + * Clear the Turtle's playground. All turtle images and traces are erased, + * but the turtles remain (invisible) at their positions. + */ + public void clear() + { + synchronized (playground) { + getPlayground().clear(); + } + } + + /** + * Delay execution for the given amount of time ( in ms ). + */ + public static void sleep(int time) + { + try + { + Thread.currentThread().sleep(time); + } + catch (Exception e) {} + } + + /** + * Return version information + */ + public String version() + { + return SharedConstants.VERSION; + } + + /** + * Enable/disable automatic repainting. + * If disabled, call repaint() to perform repainting. + * Disabling automatic repainting and hiding the + * Turtle speeds up the graphics enormously. + */ + public void enableRepaint(boolean b) + { + getPlayground().enableRepaint(b); + } + + /** + * Perform manual repainting when automatic repainting is + * disabled. + */ + public void repaint() { + getPlayground().repaint(); + } + + /** + * Return the color of the pixel at the current turtle position. + */ + public Color getPixelColor() { + synchronized (playground) { + return getPlayground().getPixelColor(this); + } + } + + protected Graphics getGraphics() { + return playground.getGraphics(); + } + + /** + * Transforms the gui-position to mouse-coordinates. + */ + public void setBounds(int x, int y, int b, int h) { + int ox = playground.getOriginX(); + int oy = playground.getOriginY(); + x = -ox + x + 10; + y = oy - y - 13; + setPos(x, y); + } + + /** + * Shows or hides the turtle. + */ + public void setVisible(boolean show) { + if (show) { + showTurtle(); + } else { + hideTurtle(); + } // end of if-else + } + + /** + * Sets clipping-mode on/off. + */ + public void setClip(boolean clip) { + if (clip) { + clip(); + } else { + wrap(); + } // end of if-else + } + + /** + * Sets pen-position up/down. + */ + public void setPenUp(boolean penup) { + if (penup) { + penUp(); + } else { + penDown(); + } // end of if-else + } + + /** + * Sets angle of the turtle. + */ + public void setAngle(int angle) { + setHeading(angle); + } +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/turtlefactory.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/turtlefactory.java new file mode 100644 index 0000000..e9d700e --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/turtlefactory.java @@ -0,0 +1,66 @@ +// TurtleFactory.java + +// Copyright 2002 Regula Hoefer-Isenegger +// +// This file is part of The Java Turtle (TJT) package +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package ch.aplu.turtle; + +import java.awt.image.*; +import java.awt.*; + +class TurtleFactory +{ + /** Generates the shape of the turtle with color, + * angle angle, width w + * and height h. + */ + protected Image turtleImage(Color color, double angle, int w, int h) + { + BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR); + Graphics2D g = (Graphics2D)bi.getGraphics(); + // Origin in center + g.translate(w/2, h/2); + // angle = 0 is direction east (as usual in mathematics) + g.rotate(Math.PI/2 - angle); + g.setColor(color); + + // Body + g.fillOval((int)( -0.35 * w), (int)( -0.35 * h), + (int)(0.7 * w), (int)(0.7 * h)); + + // Head + g.fillOval((int)( -0.1 * w), (int)( -0.5 * h), + (int)(0.2 * w), (int)(0.2 * h)); + + // Tail + int[] xcoords = + {(int)( -0.05 * w), 0, (int)(0.05 * w)}; + int[] ycoords = + {(int)(0.35 * h), (int)(0.45 * h), (int)(0.35 * h)}; + g.fillPolygon(xcoords, ycoords, 3); + + // Feet + for (int i = 0; i < 4; i++) + { + g.rotate(Math.PI / 2); + g.fillOval((int)( -0.35 * w), (int)( -0.35 * h), + (int)(0.125 * w), (int)(0.125 * h)); + } + return (Image)bi; + } +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/turtlerenderer.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/turtlerenderer.java new file mode 100644 index 0000000..9489e78 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/ch/aplu/turtle/turtlerenderer.java @@ -0,0 +1,312 @@ +// TurtleRenderer.java + +// Copyright 2002 Regula Hoefer-Isenegger +// Some modifications by Aegidius Pluess + +// This file is part of The Java Turtle (TJT) package +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package ch.aplu.turtle; + +import ch.aplu.turtle.*; +import java.awt.image.*; +import java.awt.*; +import javax.swing.*; +import java.awt.geom.*; +import java.util.*; + + +/** This class is responsible for creating and choosing the correct Turtle picture. + @author Regula Hoefer-Isenegger + @version 0.1 +*/ +public class TurtleRenderer implements ImageObserver { + /** Holds the current image */ + private Image currentImage; + /** Holds all images */ + private Vector images; + /** Tells how many pictures are needed*/ + private int resolution; + /** A reference to the Turtle */ + private Turtle turtle; + /** Holds the current Angle */ + private double currentAngle; + + private final int turtleSize = 29; + + /***/ + public TurtleRenderer(Turtle turtle) { + this.currentImage = null; + this.images = new Vector(); + this.turtle = turtle; + currentAngle = 0; + } + + /** As an image stays unchanged, there's no need to ever update it. So this method returns always false. + + For further information cf. java.awt.image.ImageObserver. + @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int) + */ + public boolean imageUpdate(Image img, int infoflags, + int x, int y, int width, int height) { + return false; + } + + /** Return the current image. + */ + public Image currentImage() { + return this.currentImage; + } + + /** Tell whether the image has changed. + */ + public boolean imageChanged(double angle) { + return (this.currentImage != getImage(angle)); + } + + /** Set the current image to the specified one. */ + private void setCurrentImage(Image image) { + currentImage = image; + } + + /** Choose the image for the angle angle. + */ + private Image getImage(double angle) { + while (angle < 0) { + angle += 2*Math.PI; + } + while (angle >= 2*Math.PI) { + angle -= 2*Math.PI; + } + double res = 2*Math.PI/(double)this.resolution; + int index = (int)(angle/res); + return image(index); + } + + /** Set the current image to the one corresponding to the angle angle. + */ + public void setAngle(double angle) { + currentAngle = angle; + setCurrentImage(getImage(angle)); + } + + /** @return the current angle. */ + protected double getAngle(){ + return currentAngle; + } + + /** Create the images. There are resolution images (i.e. two subsequent + images contain an angle of 2π/ or 360/resolution degrees). + */ + public void init(TurtleFactory factory, int resolution) { + this.resolution = resolution; + Integer res = new Integer(resolution); + double incRes = Math.PI*2/res.doubleValue(); + double angle = 0; + images = new Vector(); + for (int i = 0; i < resolution; i++) { + images.add(factory.turtleImage(turtle.getColor(), + turtle.getPlayground().toScreenAngle(angle), + turtleSize, turtleSize)); + angle += incRes; + } + setCurrentImage(getImage(currentAngle)); + } + + /** Tell how many images this TurtleRenderer holds */ + private int countImages() { + return this.images.size(); + } + + /** Get the image at index */ + private Image image(int index) { + return (Image)this.images.elementAt(index); + } + + /** This method is responsible for painting the turtle onto the + playground at (x, y). + */ + public final void paint(double x, double y) { + internalPaint(x, y, turtle.getPlayground().getGraphics()); + } + + /** This method is responsible for painting the turtle onto the + playground at p. + */ + public final void paint(Point2D.Double p) { + internalPaint(p.x, p.y, turtle.getPlayground().getGraphics()); + } + + /** This method is responsible for painting the Turtle + at (x, y).
+ The Graphics argument tells where to paint. + */ + public final void paint(double x, double y, Graphics g) { + internalPaint(x, y, g); + } + + /** This method is responsible for painting the Turtle + at p.
+ The Graphics argument tells where to paint. + */ + public void paint(Point2D.Double p, Graphics g){ + internalPaint(p.x, p.y, g); + } + + /** Call clipPaint and wrapPaint(). + + You should override this method only, if you add a new (edge) + behaviour to the turtle. I recommend to you then to first call this + method from the overriding one. +
+ If you want to change anything about the real painting, override + clipPaint or wrapPaint. + + @see #clipPaint(int, int, Graphics2D) + @see #wrapPaint(int, int, Graphics2D) + */ + protected void internalPaint(double x, double y, Graphics g) { + if(turtle.isClip()){ + Point2D.Double p = + calcTopLeftCorner(turtle.getPlayground().toScreenCoords(x, y)); + clipPaint((int)p.x, (int)p.y, (Graphics2D)g); + } + else if(turtle.isWrap()){ + Point2D.Double p = + calcTopLeftCorner(turtle.getPlayground().toScreenCoords(x, y)); + wrapPaint((int)p.x, (int)p.y, (Graphics2D)g); + } + } + + /** Define how to paint in clip mode and do it */ + protected void clipPaint(int x, int y, Graphics2D g2D) { + g2D.drawImage(currentImage, x, y, this); + } + + /** Define how to paint in wrap mode and do it */ + protected void wrapPaint(int x, int y, Graphics2D g2D) { + int pWidth = turtle.getPlayground().getWidth(); + int pHeight = turtle.getPlayground().getHeight(); + int paintX = x; + while (paintX > pWidth) { + paintX -= pWidth; + } + while (paintX < 0) { + paintX += pWidth; + } + int paintY = y; + while (paintY > pHeight) { + paintY -= pHeight; + } + while (paintY < 0) { + paintY += pHeight; + } + g2D.drawImage(currentImage, paintX, paintY, this); + int nWidth = currentImage.getWidth(this); + int nHeight = currentImage.getHeight(this); + boolean right = (paintX+nWidth > pWidth); + boolean bottom = (paintY+nHeight > pHeight); + if(right) { + g2D.drawImage(currentImage, + paintX-pWidth, + paintY, + this); + } + if(bottom) { + g2D.drawImage(currentImage, + paintX, + paintY-pHeight, + this); + } + if(right && bottom) { + g2D.drawImage(currentImage, + paintX-pWidth, + paintY-pHeight, + this); + } + } + + /** Compute the x-coordinate of the top left corner of the Turtle image + (it depends on the specified x-coordinate and the image width). + */ + protected int calcTopLeftCornerX(double x) { + int intX = (int)x; + int nWidth; + if(currentImage == null) { + setCurrentImage(new TurtleFactory(). + turtleImage(turtle.getColor(), getAngle(), turtleSize, turtleSize)); + } + nWidth = this.currentImage.getWidth(this); + // the center of the turtle lies on the turtle's location: + intX -= nWidth/2; + return intX; // top left corner of the Turtle's image + } + + /** Compute the y-coordinate of the top left corner of the Turtle image + (it depends on the specified y-coordinate and the image height). + */ + protected int calcTopLeftCornerY(double y) { + int intY = (int)y; + if(currentImage == null) { + setCurrentImage(new TurtleFactory(). + turtleImage(turtle.getColor(), getAngle(), turtleSize, turtleSize)); + } + int nHeight = currentImage.getHeight(this); + // the center of the turtle lies on the turtle's location: + intY -= nHeight/2; + + return intY; // top left corner of the Turtle's image + } + /** Compute the top left corner of the Turtle image + (dependent on the specified x- and y-coordinate and the image + width and height. + */ + protected Point2D.Double calcTopLeftCorner(double x, double y) { + if(currentImage == null) { + setCurrentImage(new TurtleFactory(). + turtleImage(turtle.getColor(), getAngle(), turtleSize, turtleSize)); + } + int w = currentImage.getWidth(this); + int h = currentImage.getHeight(this); + return new Point2D.Double(x-w/2, y-h/2); + } + + /** Compute the top left corner of the Turtle image + (dependent on the specified point p and the image + width and height. + */ + protected Point2D.Double calcTopLeftCorner(Point2D.Double p) { + return calcTopLeftCorner(p.x, p.y); + } +} + + + + + + + + + + + + + + + + + + diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/eval.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/eval.java new file mode 100644 index 0000000..97c8ff0 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/eval.java @@ -0,0 +1,50 @@ +public class Eval { + + public static Wrap getOb(Object o) { + return new Wrap(o); + } + + protected static Object getOb(final String s) { + return new Object() {public String result = s;}; + } + + protected static Object getOb(final boolean b) { + return new Object() {public boolean result = b;}; + } + + protected static Object getOb(final byte b) { + return new Object() {public byte result = b;}; + } + + protected static Object getOb(final char c) { + return new Object() {public char result = c;}; + } + + protected static Object getOb(final double d) { + return new Object() {public double result = d;}; + } + + protected static Object getOb(final float f) { + return new Object() {public float result = f;}; + } + + protected static Object getOb(final int i) { + return new Object() {public int result = i;}; + } + + protected static Object getOb(final long l) { + return new Object() {public long result = l;}; + } + protected static Object getOb(final short s) { + return new Object() {public short result = s;}; + } +} + + +class Wrap { + public Object result; + + Wrap(Object result) { + this.result = result; + } +} \ No newline at end of file diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/inout.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/inout.java new file mode 100644 index 0000000..d702d54 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/inout.java @@ -0,0 +1,98 @@ +import java.io.*; + +/** +Support class for console input and output of numeric values.

+ +Example for input:
+ int age = InOut.readInt("Your age: ");

+ +Example for output:
+ System.out.println("price: " + InOut.format2(prize) + "Euro");
+ System.out.println("percent: " + InOut.formatN(percent, 1) + " %"); +*/ + + +public class InOut { + + /** Formats a double-value with 2 decimal digits. */ + public static String format2(double d) { + return String.format("%.2f", d); + } + + /** Formats a double-value with N decimal digits. */ + public static String formatN(double d, int N) { + return String.format("%." + N + "f", d); + } + + /** Reads a boolean-value from console. */ + public static boolean readBoolean(String prompt) { + final String[] trueValues = + { "1", "y", "t", "j", "w", "yes", "true", "ja", "wahr", "ok" }; + System.out.print(prompt); + String input = readln().toLowerCase(); + for (int i = 0; i < trueValues.length; ++i) + if (trueValues[i].equals(input)) + return true; + return false; + } + + /** Reads a char-value from console. */ + public static char readChar(String prompt) { + System.out.print(prompt); + return readln().charAt(0); + } + + /** Reads a double-value from console. */ + public static double readDouble(String prompt) { + System.out.print(prompt); + return Double.parseDouble(readln()); + } + + /** Reads a float-value from console. */ + public static float readFloat(String prompt) { + System.out.print(prompt); + return Float.parseFloat(readln()); + } + + /** Reads an int-value from console. */ + public static int readInt(String prompt) { + System.out.print(prompt); + return Integer.parseInt(readln()); + } + + /** Reads a long-value from console. */ + public static long readLong(String prompt) { + System.out.print(prompt); + return Long.parseLong(readln()); + } + + /** Reads a string-value from console. */ + public static String readString(String prompt) { + System.out.print(prompt); + return readln(); + } + + /** Reads a string-value from console without prompt. + For use at the end of a console program. */ + public static String readln() { + try { + return Input.readLine(); + } catch(Exception e) { + return ""; + } + } + + private static BufferedReader Input; + + static { + try { + Input = new BufferedReader(new InputStreamReader(System.in)); + } + catch (Exception e) { + System.out.println("console input not possible."); + } + } + + + +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/je/util/turtle.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/je/util/turtle.java new file mode 100644 index 0000000..4a704ad --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/je/util/turtle.java @@ -0,0 +1,311 @@ +package je.util; +/* + * Class : Turtle + * Copyright: (c) Gerhard Röhner + * + * History + * -------- + * 3.00 2000.10.10 first version as java class + * 3.01 2002.10.22 DrawDynamic + * 3.02 2010.10.11 sleepTime for paint delay + * 3.03 2012.03.20 cartesian coordinate system, used with originX, originY, setOrigin + * 3.04 2015.01.24 english version, integrated component + */ + + +import java.awt.*; +import java.awt.image.*; +import java.awt.event.*; + +/** + * This class is a turtle component for simple graphic programming. + * + * @see + * @author Gerhard Röhner + * @version 3.03 20/03/2012 + */ + + +public class Turtle extends Canvas { + + // -- private Attribute ------------------------------------------------------ + + private BufferedImage myBufferedImage; + private Graphics myBufferedGraphics; + private Color foreground; + private Color background; + private static final double piDurch180 = Math.PI / 180; + + // -- public attributes ----------------------------------------------------- + + /** + * turtleX is the x-coordinate of the turtle + *

+ * Example:

myTurtle.turtleX = 100;
+ */ + public double turtleX; + + /** + * turtleY is the y-coordinate of the turtle. + *

+ * Example:

myTurtle.turtleY = 200;
+ */ + public double turtleY; + + /** + * turtleW is the current angle of the turtle in the range form 0 to 360 degrees. + *

+ * Example:

myTurtle.turtleW = 180;
+ */ + public double turtleW; + + /** + * originX is the x-position of the cartesian coodinate system within the turtle canvas. + *

+ * Example:

myTurtle.originX = 0;
+ */ + public double originX; + + /** + * originY is the y-position of the cartesian coodinate system within the turtle canvas. + *

+ * Example:

myTurtle.originY = 100;
+ */ + public double originY; + + /** + * If drawDynamic is true you can watch the drawing of the turtle. + *

+ * Example:

myTurtle.drawDynamic = true;
+ */ + public boolean drawDynamic; + + /** + * For drawDynamic = true you set the delay in milliseconds for drawing. + *

+ * Example:

myTurtle.sleepTime = 500;
+ */ + public int sleepTime = 0; + + // --- constructor ----------------------------------------------------------- + + /** + * Creates a Turtle with a canvas. + * At the beginning the Turtle is placed in the middle of it's canvas. + * The start angle is 0 degree, which means the Turtle looks to the right. + * The background color is white, the drawing color black. + *

+ * The turtle position can easily be changed by clicking into the canvas. + *

+ * The size of the canvas can easily be changed with the mouse. + *

+ * Example:

Turtle myTurtle = new Turtle();
+ */ + + public Turtle() { + myBufferedImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); + myBufferedGraphics = myBufferedImage.getGraphics(); + + setForeground(Color.black); + setBackground(Color.white); + drawDynamic = true; + + addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent evt) { + turtleMouseClicked(evt);}}); + addComponentListener(new ComponentAdapter() { + public void componentResized(ComponentEvent evt) { + turtleResized(evt);}}); + setOrigin(50, 50); + } + + private void turtleMouseClicked(MouseEvent evt) { + turtleX = evt.getX() - originX; + turtleY = originY - evt.getY(); + turtleW = 0; + } + + private void turtleResized(ComponentEvent evt) { + int width = getWidth(); + int height = getHeight(); + + BufferedImage newBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics newGraphics = newBufferedImage.getGraphics(); + newGraphics.setColor(background); + newGraphics.fillRect(0, 0, width, height); + newGraphics.setColor(foreground); + newGraphics.drawImage(myBufferedImage, 0, 0, this); + + turtleX = 0; + turtleY = 0; + turtleW = 0; + setOrigin(width / 2, height / 2); + + myBufferedImage = newBufferedImage; + myBufferedGraphics = newGraphics; + } + + public boolean isDoubleBuffered() { + return true; + } + + private void wTurtleMod360() { + while (turtleW >= 360) + turtleW = turtleW - 360; + while (turtleW < 0) + turtleW = turtleW + 360; + } + + // --- angle and turns ------------------------------------------------------- + + /** + * Turns the angle of the turtle relativ by the parameter angle. + * Positive values means a turn right, negative a turn left. + *

+ * Example:

myTurtle.turn(-90);
+ */ + public void turn(double angle) { + turtleW = turtleW + angle; + wTurtleMod360(); + } + + /** + * Sets the angle of the turtle absolute by the parameter angle. + * The angle increases counterclockwise, therefore + * right = 0, top = 90, left = 180, bottom = 270. + *

+ * Example:

myTurtle.turnto(270);
+ */ + public void turnto(double angle) { + turtleW = angle; + wTurtleMod360(); + } + + // --- Drawing --------------------------------------------------------------- + + /** + * The Turtle draws a line of the length specified in the current direction. + *

+ * Example:

myTurtle.draw(100);
+ */ + public void draw(double length) { + drawto(turtleX + length * Math.cos(turtleW * piDurch180), + turtleY + length * Math.sin(turtleW * piDurch180)); + } + + /** + * The Turtle draws a line form the current position (turtleX, turtleY) to the + * position (x, y) relativ to the cartesian coordinate system. + *

+ * Example:

myTurtle.drawto(200, 300);
+ */ + public void drawto(double x, double y) { + int x1 = (int) (originX + turtleX); + int x2 = (int) (originX + x); + int y1 = (int) (originY - turtleY); + int y2 = (int) (originY - y); + + myBufferedGraphics.drawLine(x1, y1, x2, y2); + if (drawDynamic){ + getGraphics().drawLine(x1, y1, x2, y2); + try { + Thread.currentThread().sleep(sleepTime); + } catch(InterruptedException e) { + } + } else + repaint(); + + turtleX = x; + turtleY = y; + } + + // --- Moving ---------------------------------------------------------------- + + /** + * The Turtle moves without drawing the length specified in the current + * direction. + *

+ * Example:

myTurtle.move(100);
+ */ + public void move(double length) { + turtleX = turtleX + length * Math.cos (turtleW * piDurch180); + turtleY = turtleY + length * Math.sin (turtleW * piDurch180); + } + + /** + * The Turtle moves without drawing to position (x, y) relativ to the + * cartesian coordinate system. + *

+ * Example:

myTurtle.moveto(100, 200);
+ */ + public void moveto(double x, double y) { + turtleX = x; + turtleY = y; + } + + // --- set origin ----------------------------------------------------------- + + /** + * Sets the origin of the cartesian coordinate system within the turtle's canvas. + *

+ * Example:

myTurtle.setOrigin(100, 200);
+ */ + public void setOrigin(double x, double y) { + originX = x; + originY = y; + } + + // --- fore- and background color -------------------------------------------- + + /** + * Sets the drawing color of the Turtle to color c. + *

+ * Example:

myTurtle.setForeground(Color.red);
+ */ + public void setForeground(Color c) { + foreground = c; + myBufferedGraphics.setColor(foreground); + super.setForeground(foreground); + } + + /** + * Sets the canvas color of the Turtle to color c. + *

+ * Example:

myTurtle.setBackground(Color.blue); 
+ */ + public void setBackground(Color c) { + background = c; + myBufferedGraphics.setColor(background); + myBufferedGraphics.fillRect(0, 0, getWidth(), getHeight()); + myBufferedGraphics.setColor(foreground); + repaint(); + } + + /** + * Clear the Turtle's canvas with the background color. + *

+ * Example:

myTurtle.clear();
+ */ + public void clear() { + setBackground(background); + getGraphics().drawImage(myBufferedImage, 0, 0, this); + repaint(); + } + + // --- Showing -------------------------------------------------------------- + + /** + * Shows the Turtle's canvas. + */ + public void paint(Graphics g) { + g.drawImage(myBufferedImage, 0, 0, this); + } + + /** + * Updates the Turtle's canvas. + */ + public void update(Graphics g) { + paint(g); + } + +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses.jar b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses.jar new file mode 100644 index 0000000..7fa4892 Binary files /dev/null and b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses.jar differ diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/package.bluej b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/package.bluej new file mode 100644 index 0000000..e69de29 diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/linerenderer.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/linerenderer.java new file mode 100644 index 0000000..52eceef --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/linerenderer.java @@ -0,0 +1,210 @@ +// LineRenderer.java + +// Copyright 2002 Regula Hoefer-Isenegger +// +// This file is part of The Java Turtle (TJT) package +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package ch.aplu.turtle; + +import ch.aplu.turtle.*; +import java.awt.geom.*; +/** This class is responsible for drawing the turtle's lines. + +@author
Regula Hoefer-Isenegger +@version 0.1 +*/ +public class LineRenderer { + private Point2D.Double point; + private Turtle turtle; + + LineRenderer(Turtle turtle) { + this.point = new Point2D.Double(); + this.turtle = turtle; + } + + /** Initialisation with coordinates x and + y. + */ + public void init(double x, double y) { + this.point.setLocation(x, y); + } + /** Same as init(double x, double y), but with a Point2D.Double + argument for convenience + */ + public void init(Point2D.Double p) { + this.init(p.x, p.y); + } + /** Get the current x-coordinate in screen coordinates.*/ + private double getX(){ + return (toScreenCoords(point)).getX(); + } + /** Get the current x-coordinate in screen coordinates.*/ + private double getY(){ + return toScreenCoords(point).getY(); + } + /** Calls the clipLineTo and wrapLineTo methods, according + to the turtle's edge behavior. + + Only overwrite this method when working with another + (one that you have defined) edge behaviour.
+ If you mean to change the manner of drawing lines, do this in + the methods clipLineTo() and wrapLineTo(). + @see #clipLineTo + @see #wrapLineTo + */ + protected void internalLineTo(double x, double y){ + Point2D.Double screenPos = toScreenCoords(x, y); + if(turtle.isClip()){ + clipLineTo(screenPos.getX(), screenPos.getY()); + } + if (turtle.isWrap()){ + wrapLineTo(screenPos.getX(), screenPos.getY()); + } + init(x,y); + } + /** Calls the internalLineTo(x,y), which does the actual painting. + */ + public void lineTo(double x, double y) { + internalLineTo(x, y); + } + /** Calls the internalLineTo(x,y), which does the actual painting. + + This method works the same way as lineTo(double x, double y), but + is added for convenience. + */ + public void lineTo(Point2D.Double p) { + internalLineTo(p.getX(), p.getY()); + } + /** Does the actual painting for clip mode. + + It works already with ScreenCoords! + For further comments cf. internalLineTo(double x, double y). + @see #internalLineTo + */ + protected void clipLineTo(double x, double y){ + turtle.getPlayground().lineTo(getX(), getY(), x, y, turtle.getPen()); + } + /** Does the actual painting for wrap mode. + + It works already with ScreenCoords! + For further comments cf. internalLineTo(double x, double y). + @see #internalLineTo + */ + protected void wrapLineTo(double x, double y){ + double dx = getX() - x; + double dy = getY() - y; + Point2D.Double start = new Point2D.Double(x, y); + Point2D.Double end = new Point2D.Double(start.x+dx, start.y+dy); + + intoPanel(start, end); + Point2D.Double tmp; + while ((tmp = calcIntersection(start.x, start.y, end.x, end.y)) != null){ + turtle.getPlayground().lineTo(start.x, start.y, tmp.getX(), tmp.getY(), turtle.getPen()); + start = tmp; + intoPanel(start, end); + dx = end.x - start.x; + dy = end.y - start.y; + } + + turtle.getPlayground().lineTo(start.x, start.y, end.x, end.y, turtle.getPen()); + } + /** Makes the coordinates fit into the Panel. + + Well, this is some sort of modulus calculation. + */ + private void intoPanel(Point2D.Double start, Point2D.Double end){ + int pWidth = turtle.getPlayground().getWidth(); + int pHeight = turtle.getPlayground().getHeight(); + while(start.x < 0){ + start.x += pWidth; + end.x += pWidth; + } + while (start.x > pWidth){ + start.x -= pWidth; + end.x -= pWidth; + } + if(start.x == 0 && end.x < start.x){ + start.x += pWidth; + end.x += pWidth; + } + if(start.x == pWidth && end.x > start.x){ + start.x -= pWidth; + end.x -= pWidth; + } + while(start.y < 0){ + start.y += pHeight; + end.y += pHeight; + } + while (start.y > pHeight){ + start.y -= pHeight; + end.y -= pHeight; + } + if(start.y == 0 && end.y < start.y){ + start.y += pHeight; + end.y += pHeight; + } + if(start.y == pHeight && end.y > start.y){ + start.y -= pHeight; + end.y -= pHeight; + } + + } + /** Intersection line with playground-edges + (startX / startY) MUST lie in the playground! + */ + private Point2D.Double calcIntersection(double startX, double startY, double endX, double endY){ + double dx = endX - startX; + double dy = endY - startY; + double W = turtle.getPlayground().getWidth(); + double H = turtle.getPlayground().getHeight(); + if(endX < 0){ + if((dy/dx <= startY/startX) && (dy/dx >= -(H-startY)/startX)){ // links + return new Point2D.Double(0, startY-startX*dy/dx); + } + } + else if(endX > W){ + if((dy/dx >= -startY/(W-startX)) && (dy/dx <= (H-startY)/(W-startX))){// rechts + return new Point2D.Double(W, startY+(W-startX)*dy/dx); + } + } + if(endY < 0){ // oben + return new Point2D.Double(startX-startY*dx/dy, 0); + } + else if(endY > H){ // unten + return new Point2D.Double(startX+(H-startY)*dx/dy, H); + } + else{ + return null; // Endpoint lies in the window + } + } + /** Calculates the screen coordinates of the turtle's actual + position according to the interpretation of the playground. + */ + private Point2D.Double toScreenCoords(double x, double y){ + return turtle.getPlayground().toScreenCoords(x, y); + } + /** Calculates the screen coordinates of the turtle's actual + position according to the interpretation of the playground. + + Added for convenience. + @see #toScreenCoords(double, double) + */ + private Point2D.Double toScreenCoords(Point2D.Double p){ + return turtle.getPlayground().toScreenCoords(p.x, p.y); + } +} + diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/package.bluej b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/package.bluej new file mode 100644 index 0000000..e69de29 diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/pen.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/pen.java new file mode 100644 index 0000000..8fcc8b7 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/pen.java @@ -0,0 +1,268 @@ +// Pen.java + +// Copyright 2002 Regula Hoefer-Isenegger +// +// This file is part of The Java Turtle package (TJT) +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package ch.aplu.turtle; + +import ch.aplu.turtle.*; +import java.awt.Color; +import java.awt.BasicStroke; +import java.awt.Stroke; +import java.awt.Font; +import java.awt.GraphicsEnvironment; + +/** The Pen class provides anything used for drawing the lines, such as line width, + pen color, end caps, dashed lines, etc. + + @see java.awt.BasicStroke + + @author Regula Hoefer-Isenegger + @version 0.1.1 +*/ +public class Pen +{ + /* Attributes *********************************************/ + + /** The default font that is used when drawing Text. + + First argument must be one of "Serif", "SansSerif", "Monotyped", "Dialog" or "DialogInput" + to guarantee that this font exists on all systems. + + @see java.awt.Font for more information, e.g. on font styles. + + */ + public static Font DEFAULT_FONT = new Font("SansSerif", Font.PLAIN, 24); + private Color color; + private Color fillColor; + private BasicStroke stroke; + private Font font; + + /* Constructors *******************************************/ + /** Constructor with standard Color and standard Stroke. + + @see java.awt.BasicStroke + */ + public Pen(){ + color = Color.black; + setFillColor(Color.black); + stroke = new BasicStroke(); + font = DEFAULT_FONT; + } + /** Constructor with Color color and standard Stroke. + + @see java.awt.BasicStroke + */ + public Pen(Color color){ + this.color = color; + setFillColor(color); + stroke = new BasicStroke(); + font = DEFAULT_FONT; + } + /* Methods ************************************************/ + /** Query the Pens color.*/ + public Color getColor(){ + return color; + } + /** Set the Pens color.*/ + public void setColor(Color color){ + this.color = color; + } + /** Set the Pens fill color. + */ + public void setFillColor(Color color){ + this.fillColor = color; + } + /** Query the Pens fill color.*/ + public Color getFillColor(){ + return this.fillColor; + } + /** Get the Pens Stroke + + @see BasicStroke + @see Stroke + */ + public Stroke getStroke(){ + return stroke; + } + /** Query the Pens line width*/ + public float getLineWidth(){ + return stroke.getLineWidth(); + } + /** Query the Pens end cap style. + + @see java.awt.BasicStroke + */ + public int getEndCap(){ + return stroke.getEndCap(); + } + /** Query the Pens line join style. + + @see java.awt.BasicStroke + */ + public int getLineJoin(){ + return stroke.getLineJoin(); + } + /** Query the Pens miter limit style. + + @see java.awt.BasicStroke + */ + public float getMiterLimit(){ + return stroke.getMiterLimit(); + } + /** Query the Pens dash array. + + @see java.awt.BasicStroke + */ + public float[] getDashArray(){ + return stroke.getDashArray(); + } + /** Query the Pens dash phase. + + @see java.awt.BasicStroke + */ + public float getDashPhase(){ + return stroke.getDashPhase(); + } + + /** Set the Pens line width. */ + public void setLineWidth(float width){ + stroke = new BasicStroke((float)width, + stroke.getEndCap(), + stroke.getLineJoin(), + stroke.getMiterLimit(), + stroke.getDashArray(), + stroke.getDashPhase()); + } + /** Set the Pens end cap style. + + @see java.awt.BasicStroke + */ + public void setEndCap(int endCap){ + stroke = new BasicStroke(stroke.getLineWidth(), + endCap, + stroke.getLineJoin(), + stroke.getMiterLimit(), + stroke.getDashArray(), + stroke.getDashPhase()); + } + /** Set the Pens line join style. + + @see java.awt.BasicStroke + */ + public void setLineJoin(int join){ + stroke = new BasicStroke(stroke.getLineWidth(), + stroke.getEndCap(), + join, + stroke.getMiterLimit(), + stroke.getDashArray(), + stroke.getDashPhase()); + } + /** Set the Pens miter limit. + + @see java.awt.BasicStroke + */ + public void setMiterLimit(float miterlimit){ + stroke = new BasicStroke(stroke.getLineWidth(), + stroke.getEndCap(), + stroke.getLineJoin(), + miterlimit, + stroke.getDashArray(), + stroke.getDashPhase()); + } + /** Set the Pens dash array. + + @see java.awt.BasicStroke + */ + public void setDash(float[] dashArray){ + stroke = new BasicStroke(stroke.getLineWidth(), + stroke.getEndCap(), + stroke.getLineJoin(), + stroke.getMiterLimit(), + dashArray, + stroke.getDashPhase()); + } + /** Set the Pens dash phase. + + @see java.awt.BasicStroke + */ + public void setDashPhase(float dashPhase){ + stroke = new BasicStroke(stroke.getLineWidth(), + stroke.getEndCap(), + stroke.getLineJoin(), + stroke.getMiterLimit(), + stroke.getDashArray(), + dashPhase); + } + /** Provides information about the currently available font families (e.g. "Roman"). + Each font name is a string packed into a array of strings. + @see java.awt.Font for more information about font attributes etc. + */ + public static String[] getAvailableFontFamilies(){ + GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); + String s[] = ge.getAvailableFontFamilyNames(); + return s; + } + /** Change the font style. + + @see java.awt.Font for possible styles. + */ + public void setFontStyle(int style){ + font = font.deriveFont(style); + } + /** Change the font size (in points). + */ + public void setFontSize(int size){ + font = font.deriveFont((float)size); + } + /** Change the font size (in points). + You will probably only need the int version setFontSize(int). + */ + public void setFontSize(float size){ + font = font.deriveFont(size); + } + /** Query the size (in points, rounded to int) of the current font. + */ + public int getFontSize(){ + return font.getSize() ; + } + /** Change the font to the given one. + */ + public void setFont(Font f){ + font = f; + } + /** Query the current font. + */ + public Font getFont(){ + return font; + } +} + + + + + + + + + + + + + + diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/playground.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/playground.java new file mode 100644 index 0000000..6c0db5b --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/playground.java @@ -0,0 +1,789 @@ +// Playground.java + +// Copyright 2002 Regula Hoefer-Isenegger +// Major code modifications by Aegidius Pluess +// Adaption for the Java-Editor by Gerhard Röhner +// +// This file is part of The Java Turtle package (TJT) +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package ch.aplu.turtle; + +import ch.aplu.turtle.*; +import java.util.*; +import javax.swing.*; +import java.awt.*; +import java.awt.print.*; +import java.awt.geom.*; +import java.awt.image.*; +import java.awt.font.*; + +/** + A Playground is the Turtle's home, i.e. the Turtle lives + and moves in the Playground. + + ThePlayground is responsible for interpreting angle and position of the + Turtle and for choosing the correct turtle image and putting it on the right + spot of the Playground. This means: e.g. whenever you wish to switch the x- and y-axis, you + should do it in this class, and not in the Turtle class. + + @author Regula Hoefer-Isenegger + @version 0.1.1 +*/ + +public class Playground extends JPanel implements Printable { + + /** Hold the Turtles of this Playground. */ + private Vector turtles; + + /** Hold the offscreen buffer and graphics context + * where Turtle traces are drawn. + */ + private BufferedImage traceBuffer = null; + protected Graphics2D traceG2D = null; + private Dimension playgroundSize; + + /** Hold the offscreen buffer and graphics context + * of the Turtles images. + */ + private BufferedImage turtleBuffer = null; + protected Graphics2D turtleG2D = null; + + /** Flag to tell whether we have at least one Turtle shown. */ + private boolean isTurtleVisible = false; + + /** Flag to tell whether we use automatic repainting */ + private boolean isRepainting = true; + + /** The default background color. + */ + protected static Color DEFAULT_BACKGROUND_COLOR = Color.white; + + private double printerScale = 1; // Default printer scaling + private TPrintable traceCanvas; // Store ref to user class + private Graphics2D printerG2D = null; + private boolean isPrintScreen = false; // Indicate we are printing the playground + private double printerScaleFactor = 1.1; // Magnification factor for printer + + /** + * originX is the x-position of the cartesian coodinate system within the playground. + */ + public int originX; + + /** + * originY is the y-position of the cartesian coodinate system within the playground. + */ + public int originY; + + private static Color[] ColorArray = {Color.cyan, Color.red, Color.green, Color.blue, Color.yellow, + Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.black, Color.gray + }; + + /** + * Create a Playground with default background color. + * e.g. creates a new vector (which holds the + * Turtles), + */ + public Playground() { + turtles = new Vector(); + setDoubleBuffered(false); + setBackground(DEFAULT_BACKGROUND_COLOR); + initBuffers(new Dimension(100, 100)); + } + + /** + * Initializes the offscreen buffers and sets the size. + */ + protected void initBuffers(Dimension size) { + Color bkColor = getBackground(); + playgroundSize = size; + traceBuffer = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB); + traceG2D = traceBuffer.createGraphics(); + traceG2D.setColor(bkColor); + traceG2D.fillRect(0, 0, size.width, size.height); + traceG2D.setBackground(bkColor); + + turtleBuffer = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB); + turtleG2D = turtleBuffer.createGraphics(); + turtleG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + originX = size.width/2; + originY = size.height/2; + } + + /** Add a new Turtle to the Playground. + */ + public void add(Turtle turtle) { + turtles.add(turtle); + turtle.setPlayground(this); + int i = turtles.size(); + while (i > 10) { + i = i - 10; + } // end of while + turtle.init(ColorArray[i-1]); + toTop(turtle); + } + + public void setBounds(int x, int y, int width, int height) { + super.setBounds(x, y, width, height); + initBuffers(new Dimension(width, height)); + } + + /** Remove a Turtle from the Playground. + */ + public void remove(Turtle turtle) { + turtles.remove(turtle); + } + + /** Tell current number of Turtles in this Playground. + */ + public int countTurtles() { + return turtles.size(); + } + + /** Return the Turtle at index index. + */ + public Turtle getTurtle(int index) { + return turtles.elementAt(index); + } + + /** Move the given Turtle above all the others, then + paints all turtles. + @see #toTop + */ + public void paintTurtles(Turtle turtle) { + toTop(turtle); + paintTurtles(); + } + + /** Paint all turtles (calling paintComponent()) + */ + public void paintTurtles() { + isTurtleVisible = false; + Graphics2D g2D = getTurtleG2D(); + for (int i = 0; i < countTurtles(); i++) { + Turtle aTurtle = getTurtle(i); + if (!aTurtle.isHidden()) { + paintTurtle(aTurtle); + } + } + + // This is the main repaint call, when the turtle is + // moving (even when all turtles are hidden). + // Strange behaviour an slow Mac machines (pre J2SE 1.4 version): + // It happens that some turtle images are not completely redrawn. + // This is probably due to an improper handling of fast multiple repaint requests. + // Workaround: we wait a small amount of time (and give the thread away) + // (No visible slow down on new machines.) + + paintPlayground(); + if (printerG2D == null) { + //if (isRepainting) + //repaint(); + //paintPlayground(); + } + + if (isTurtleVisible) { + try { + Thread.currentThread().sleep(10); + } + catch (Exception e) {} + } + } + + + /** Paint the given Turtle. + * ( no repaint() ) + */ + public void paintTurtle(Turtle turtle) { + if (turtleBuffer == null){ + turtleBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); + turtleG2D = turtleBuffer.createGraphics(); + } + Graphics2D turtleGraphics = getTurtleG2D(); + turtle.getTurtleRenderer().paint(turtle._getX(), turtle._getY(), turtleGraphics); + isTurtleVisible = true; + } + + /** Put an image of the given Turtle in turtle buffer. + */ + protected void stampTurtle(Turtle turtle) { + turtle.clone(); + isTurtleVisible = true; + if (printerG2D == null) + repaint(); + } + + /** Draw a line from the point (x0, y0) to (x1, y1) + with the color of the given Pen. + */ + protected void lineTo(double x0, double y0, double x1, double y1, Pen pen) { + int ix0 = (int)Math.round(x0); + int iy0 = (int)Math.round(y0); + int ix1 = (int)Math.round(x1); + int iy1 = (int)Math.round(y1); + Color color = pen.getColor(); + + if (traceBuffer == null) { + traceBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); + traceG2D = traceBuffer.createGraphics(); + } + Graphics2D traceG2D = getTraceG2D(); + traceG2D.setColor(color); + traceG2D.setStroke(pen.getStroke()); + traceG2D.drawLine(ix0, iy0, ix1, iy1); + if (printerG2D != null) + printerG2D.drawLine(ix0, iy0, ix1, iy1); + } + + /** A class for convenience. */ + protected class Point extends java.awt.Point { + Point(int x, int y) { + super(x, y); + } + Point() { + super(); + } + Point(Point p) { + super(p.x, p.y); + } + + /** Get a new Point with coordinates (this.x+p.x, this.y+p.y). + */ + protected Point add(Point p) { + return new Point(this.x+p.x, this.y+p.y); + } + + /** Translate by the amounts dx = p.x, dy = p.y. */ + protected void translate(Point p) { + translate(p.x, p.y); + } + + public String toString() { + return "(" + x + "," + y + ")"; + } + } + + /** Fill a region. + The region is defined by the Turtles actual position and + is bounded by any other color than the give background color. + */ + public void fill(Turtle t, Color bgColor) { + final Point[] diff = { new Point(0,-1), new Point(-1,0), new Point(1,0), new Point(0,1)}; + final int N=0; + final int W=1; + final int E=2; + final int S=3; + + int bgcolor = bgColor.getRGB(); + int fillColor = t.getPen().getFillColor().getRGB(); + Vector list = new Vector(); + Point2D.Double p1 = toScreenCoords(t.getPos()); + int startX = (int)Math.round(p1.getX()); + int startY = (int)Math.round(p1.getY()); + Point p = new Point(startX, startY); + if (traceBuffer.getRGB(startX, startY) == bgcolor) { + traceBuffer.setRGB(startX, startY, fillColor); + list.addElement(new Point(startX, startY)); + int d = N; + int back; + while (list.size() > 0) { + while (d <= S) { // forward + Point tmp = p.add(diff[d]); + try { + if (traceBuffer.getRGB(tmp.x, tmp.y) == bgcolor) { + p.translate(diff[d]); + traceBuffer.setRGB(p.x, p.y, fillColor); + if (printerG2D != null) + { + printerG2D.setColor(t.getPen().getFillColor()); + // printerG2D.drawLine(p.x,p.y, p.x, p.y); + BasicStroke stroke = new BasicStroke(2); + printerG2D.setStroke(stroke); + Line2D line = new Line2D.Double(p.x, p.y, p.x, p.y); + printerG2D.draw(line); + } + list.addElement(new Integer(d)); + d=N; + } + else { + d++; + } + } + catch (ArrayIndexOutOfBoundsException e) { + d++; + } + } + Object obj = list.remove(list.size()-1); + try { + d=((Integer)obj).intValue(); // last element + back = S - d; + p.translate(diff[back]); + } + catch (ClassCastException e) { + // the first (zeroest) element in list is the start-point + // just do nothing with it + } + } + } + traceG2D.drawLine(0, 0, 0, 0); // Workaround because on Mac the trace buffer is not drawn without this + if (printerG2D == null) + repaint(); + } + + /** + * Clear the playground with given color. + */ + public void clear(Color color) { + traceG2D.setColor(color); + traceG2D.fillRect(0, 0, getWidth(), getHeight()); + turtleG2D.setColor(color); + turtleG2D.fillRect(0, 0, getWidth(), getHeight()); + isTurtleVisible = true; + if (printerG2D == null) + repaint(); + } + + /** + * Clear playground. + */ + public void clear() { + clear(getBackground()); + } + + /** Paint the Playground. + just a method for convenience. + */ + public void paintComponent() { + paintComponent(getGraphics()); + } + + /** Draw the trace and turtle buffers. + */ + public void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2D = (Graphics2D)g; + g2D.drawImage(traceBuffer, 0, 0, this); + if (isTurtleVisible) + g2D.drawImage(turtleBuffer, 0, 0, this); + } + + public void paintPlayground() { + Graphics g = getGraphics(); + if (g != null) { + super.paintComponent(g); + Graphics2D g2D = (Graphics2D)g; + g2D.drawImage(traceBuffer, 0, 0, this); + if (isTurtleVisible) + g2D.drawImage(turtleBuffer, 0, 0, this); + + } // end of if + } + + /** Remove all turtles from the turtle buffer. + */ + public void clearTurtles() { + for (int i = 0; i < countTurtles(); i++) { + Turtle turtle = getTurtle(i); + clearTurtle(turtle); + } + } + + /** Remove the given turtle from the turtle buffer. + Override this method if you have added a new behaviour (like + wrap or clip) to the turtle. + */ + public void clearTurtle(Turtle turtle) { + if(turtle != null){ + if (!turtle.isHidden()) { + if(turtle.isClip()){ + clearClipTurtle(turtle); + } + else if(turtle.isWrap()){ + clearWrapTurtle(turtle); + } + } + } + } + + /** This method is called when the given Turtle is in wrap mode. + + @see ch.aplu.turtle.Turtle#wrap + */ + protected void clearWrapTurtle(Turtle turtle){ + clearWrapTurtle(turtle, turtleBuffer); + } + + /** Here the actual clearing of a Turtle in wrap mode from the + given image is performed. + */ + protected void clearWrapTurtle(Turtle turtle, Image im){ + Rectangle bounds = getBounds(turtle); + int pWidth = getWidth(); + int pHeight = getHeight(); + int x = bounds.x; + int y = bounds.y; + while (x > pWidth){ + x -= pWidth; + } + while (x < 0){ + x += pWidth; + } + while (y > pHeight){ + y -= pHeight; + } + while (y < 0){ + y += pHeight; + } + x = x % pWidth; + y = y % pHeight; + toAlphaNull(im, new Rectangle(x, y, bounds.width, bounds.height)); // OK + boolean right = (x + bounds.width > getWidth()); + boolean bottom = (y + bounds.height > getHeight()); + if (right) { + toAlphaNull(im, new Rectangle(x-pWidth, y, bounds.width, bounds.height)); + } + if (bottom) { + toAlphaNull(im, new Rectangle(x, y-pHeight, bounds.width, bounds.height)); + } + if (right && bottom) { + toAlphaNull(im, new Rectangle(x-pWidth, y-pHeight, bounds.width, bounds.height)); + } + } + + /** Copy and translate a given Rectangle. + */ + private Rectangle copyAndTranslate(Rectangle rect, int dx, int dy) { + return new Rectangle(rect.x+dx, rect.y+dy, + rect.width, rect.height); + } + + /** This method is called when the given Turtle is in clip mode. + + @see ch.aplu.turtle.Turtle#clip + */ + protected void clearClipTurtle(Turtle turtle) { + clearClipTurtle(turtle, turtleBuffer); + } + + /** Here the actual clearing of a Turtle in clip mode from the + given image is performed. + */ + protected void clearClipTurtle(Turtle turtle, Image im) { + Rectangle bounds = getBounds(turtle); + toAlphaNull(im, bounds); + } + + /** Set the alpha channel of all pixels in the given image + in the given Rectangle to zero (i.e. totally transparent). + + This method is used byte the clearXXXTurtle methods. + */ + private void toAlphaNull(Image im, Rectangle rect) { + Rectangle rim = new Rectangle(0, 0, im.getWidth(this), im.getHeight(this)); + Rectangle r = new Rectangle(); + if (rect.intersects(rim)) { + r=rect.intersection(rim); + } + int size = r.width*r.height; + float[] alphachannel = new float[r.width*r.height]; + ((BufferedImage)im).getAlphaRaster().setPixels(r.x, r.y, r.width, r.height, alphachannel); + } + + /** Puts a Turtle above all others. + */ + public Turtle toTop(Turtle turtle) { + if (turtles.removeElement(turtle)) { + turtles.add(turtle); + } + return turtle; + } + /** Put a Turtle below all others. + */ + public Turtle toBottom(Turtle turtle) { + if (turtles.removeElement(turtle)) { + turtles.add(0,turtle); + } + return turtle; + } + + /** Calculate the screen coordinates of the given point. + */ + public Point2D.Double toScreenCoords(Point2D.Double p) { + return internalToScreenCoords(p.x, p.y); + } + + /** Calculate the screen coordinates of the given point coordinates. + */ + public Point2D.Double toScreenCoords(double x, double y) { + return internalToScreenCoords(x, y); + } + + protected Point2D.Double internalToScreenCoords(double x, double y) { + // reflect at x-axis, then translate to center of Playground + // pixel coordinates coorespond to turtle coordinates, only translation needed + double newX = originX + x; + double newY = originY - y; + return new Point2D.Double(newX, newY); + } + + /** Calculate the turtle coordinates of the given screen coordinates. + */ + public Point2D.Double toTurtleCoords(double x, double y) { + // pixel coordinates coorespond to turtle coordinates, only translation needed + double newX = x - originX; + double newY = originY - y; + return new Point2D.Double(newX, newY); + } + + /** Calculate the turtle coordinates of the given screen point. + */ + public Point2D.Double toTurtleCoords(Point2D.Double p) { + return toTurtleCoords(p.x, p.y); + } + + /** Calculate the screen angle. + I.e. the interpretation of angle. + @param radians The angle in radians. + */ + double toScreenAngle(double radians) { + double sa = radians; + if (sa < Math.PI/2){ + sa += 2*Math.PI; + } + sa -= Math.PI/2; + if (sa != 0) { + sa = Math.PI*2 - sa; + } + return sa; + } + + /** Calculate the bounds of the Turtles picture on the screen. + */ + protected Rectangle getBounds(Turtle turtle) { + Rectangle bounds = turtle.getBounds(); + Point2D.Double tmp = toScreenCoords(new Point2D.Double(bounds.getX(), bounds.getY())); + bounds.setRect(tmp.x-2, tmp.y-2, bounds.width+4, bounds.height+4); + return bounds; + } + + /** Return the graphics context of the turtle buffer. + */ + public Graphics2D getTurtleG2D() { + return turtleG2D; + } + + /** Return the image of the turtle buffer. + */ + public BufferedImage getTurtleBuffer() { + return turtleBuffer; + } + + /** Return the graphics context of the trace buffer. + */ + public Graphics2D getTraceG2D() { + return traceG2D; + } + + /** Return the graphics context of the printer. + */ + public Graphics2D getPrinterG2D() { + return printerG2D; + } + + /** Return the image of the trace buffer. + */ + public BufferedImage getTraceBuffer() { + return traceBuffer; + } + + /** Clean the traces. + All turtles stay how and where they are, only lines, text and stamps will be removed. + */ + void clean() { + Graphics2D g = getTraceG2D(); + g.setColor(getBackground()); + g.fillRect(0,0,getWidth(), getHeight()); + if (printerG2D == null) + repaint(); + } + + /** Draw the text at the current position of the Turtle t. + Drawing a text at some coordinates (x,y) we mean that the bottom left corner of + the text will be at these coordinates. + Font and colour are specified by the Turtle's Pen. + */ + public void label(String text, Turtle t) { + Point2D.Double sc = toScreenCoords(t.getPos()); + int x = (int)Math.round(sc.x); + int y = (int)Math.round(sc.y); + Graphics2D traceG2D = getTraceG2D(); + FontRenderContext frc = traceG2D.getFontRenderContext(); + Font f = t.getFont(); + TextLayout tl = new TextLayout(text, f, frc); + traceG2D.setColor(t.getPen().getColor()); + tl.draw(traceG2D, x, y); + if (printerG2D != null) + { + printerG2D.setColor(t.getPen().getColor()); + tl.draw(printerG2D, x, y); + } + + if (printerG2D == null) + repaint(); + } + + /** Set antialiasing on or off for the turtle trace buffer + * This may result in an better trace quality. + */ + public void setAntiAliasing(boolean on) { + if (on) + traceG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + else + traceG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_OFF); + } + + + /** + * Set the given TPrintable (implementing draw()), + * open a printer dialog and start printing with given scale. + * Return false, if printer dialog is aborted, + * otherwise return true.
+ * If tp == null, the current playground is printed. + * + */ + protected boolean print(TPrintable tp, double scale) { + if (tp == null) + isPrintScreen = true; + else + isPrintScreen = false; + printerScale = scale; + PrinterJob pj = PrinterJob.getPrinterJob(); + pj.setPrintable(this); + traceCanvas = tp; + if (pj.printDialog()) { + try { + pj.print(); + } + catch (PrinterException ex) { + System.out.println(ex); + } + return true; + } + else + return false; + } + + /** + * For internal use only. Implementation of Printable. + * (Callback method called by printing system.) + */ + public int print(Graphics g, PageFormat pf, int pageIndex) { + if (pageIndex != 0) + return NO_SUCH_PAGE; + Graphics2D g2D = (Graphics2D)g; + double printerWidth = pf.getImageableWidth(); + double printerHeight = pf.getImageableHeight(); + double printerSize = printerWidth > printerHeight ? printerWidth : + printerHeight; + double xZero = pf.getImageableX(); + double yZero = pf.getImageableY(); + + printerG2D = g2D; // Indicate also, we are printing now + + // Needed for fill operations: the trace canvas must be empty in order to + // perform the fill algoritm (searching for outline of figure) + if (!isPrintScreen) + clean(); + + g2D.scale(printerScaleFactor * printerScale, printerScaleFactor * printerScale); + g2D.translate(xZero/printerScale, yZero/printerScale); + + if (isPrintScreen) + { + print(g); + } + else // Printing the traceCanvas + { + // Hide all turtles + boolean[] turtleState = new boolean[countTurtles()]; + for (int i = 0; i < countTurtles(); i++) { + Turtle aTurtle = getTurtle(i); + turtleState[i] = aTurtle.isHidden(); + aTurtle.ht(); + } + traceCanvas.draw(); + + // Restore old context + for (int i = 0; i < countTurtles(); i++) { + Turtle aTurtle = getTurtle(i); + if (!turtleState[i]) + aTurtle.st(); + } + } + + printerG2D = null; + return PAGE_EXISTS; + } + + /** Return the color of the pixel at the current turtle position. + */ + public Color getPixelColor(Turtle t) { + Point2D.Double p1 = toScreenCoords(t.getPos()); + int x = (int)Math.round(p1.getX()); + int y = (int)Math.round(p1.getY()); + return new Color(traceBuffer.getRGB(x, y)); + } + + public void enableRepaint(boolean b) { + isRepainting = b; + } + + /** set the background color of the playground + */ + public void setBackground(Color color) { + super.setBackground(color); + if (traceG2D != null) { + clear(color); + } // end of if + } + + /** + * Sets the origin of the cartesian coordinate system within the playground + */ + public void setOrigin(int x, int y) { + for (int i = 0; i < countTurtles(); i++) { + Turtle turtle = getTurtle(i); + Point2D.Double p1 = toScreenCoords(turtle.getPos()); + double newX = p1.getX() - x; + double newY = y - p1.getY(); + turtle.internalSetPos(newX, newY); + } + originX = x; + originY = y; + } + + public int getOriginX() { + return originX; + } + + public int getOriginY() { + return originY; + } + +} + diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/res/bong.wav b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/res/bong.wav new file mode 100644 index 0000000..0d2fef0 Binary files /dev/null and b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/res/bong.wav differ diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/sharedconstants.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/sharedconstants.java new file mode 100644 index 0000000..1bd3955 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/sharedconstants.java @@ -0,0 +1,84 @@ +// SharedConstants.java + +// Copyright 2002 Regula Hoefer-Isenegger +// +// This file is part of The Java Turtle (TJT) package +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +/* History; +V1.36 - Sep 2004: First official release +V1.37 - Nov 2004: Unchanged, modifications in ch.aplu.util +V1.38 - Dec 2004: Unchanged, modifications in ch.aplu.util +V1.39 - Jan 2005: Unchanged, modifications in ch.aplu.util +V1.40 - Mar 2005: Add background color, TurtleKeyAdapter, TurtleArea +V1.41 - May 2005: User defined turtle shape, minor changes in doc and code style +V1.42 - Dec 2005: Unchanged, modifications in ch.aplu.util +V1.43 - Feb 2006: Bug removed: Turtle.turtleFrame was not initialized in all ctors of class Turtle +V1.44 - Mar 2007: Bug removed: stampTurtle did not work properly in wrap mode +V1.45 - Aug 2007: TurtleKeyAdapter: use wait/notify to reduce CPU time (from 100% to 2%) +V1.46 - Aug 2007: synchronize(playground) for forward and rotate animation, + new method bong() using StreamingPlayer +V1.47 - Sept 2007: Unchanged, modifications in ch.aplu.util +V1.48 - Sept 2007: Unchanged, modifications in ch.aplu.util +V1.49 - Oct 2007: Unchanged, modifications in ch.aplu.util +V1.50 - Oct 2007: Unchanged, modifications in ch.aplu.util +V1.51 - Nov 2007: Fixed: correct position of label, when wrapping is on + Fixed: getPos() returns now the wrapped coordinates + Added: _getPos() returns unwrapped coordinates +V1.52 - Nov 2007: Added bean classes in order to use turtles with a Gui builder +V1.53 - Nov 2007: Added TurtlePane visual information when used in Gui builder design mode +V1.54 - Nov 2007: Minor changes to documentation +V1.55 - Dec 2007: Added property enableFocus to GPane, default: setFocusable(false) +V1.56 - Mar 2008: Unchanged, modifications in ch.aplu.util +V1.57 - Mar 2008: Unchanged, modifications in ch.aplu.util +V1.58 - Mar 2008: Modification to fill() (fill(x, y)): + region is defined with pixel color at current position + as opposed to background color +V1.59 - Oct 2008: Added ctors TurtleFrame with window position (ulx, uly) + Added Turtle.getPixelColor() +V2.00 - Nov 2008: Unchanged, modifications in ch.aplu.util + J2SE V1.4 no longer supported +V2.01 - Jan 2009: Unchanged, modifications in ch.aplu.util +V2.02 - Feb 2009 Turtle constructors run in EDT now +V2.03 - Feb 2009 Unchanged, modifications in ch.aplu.util +V2.04 - Feb 2009 Unchanged, modifications in ch.aplu.util +V2.05 - Feb 2009 Unchanged, modifications in ch.aplu.util +V2.06 - Mar 2009 Unchanged, modifications in ch.aplu.util +V2.07 - Mar 2009 All except print methods synchronized, so Turtle package is + now thread-safe +V2.08 - Apr 2009 Unchanged, modifications in ch.aplu.util +V2.09 - Jun 2009 Unchanged, modifications in ch.aplu.util +V2.10 - Jun 2010 Version for the Java-Editor +*/ + +package ch.aplu.turtle; + +interface SharedConstants +{ + int DEBUG_LEVEL_OFF = 0; + int DEBUG_LEVEL_LOW = 1; + int DEBUG_LEVEL_MEDIUM = 2; + int DEBUG_LEVEL_HIGH = 3; + + int DEBUG = DEBUG_LEVEL_OFF; + + String ABOUT = + "Copyright © 2002-2009\nRegula Hoefer-Isenegger, Aegidius Pluess\n, Gerhard Roehner\n" + + "under GNU General Public License\n" + + "http://www.aplu.ch\n" + + "All rights reserved"; + String VERSION = "2.10 - June 2015"; +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/streamingplayer.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/streamingplayer.java new file mode 100644 index 0000000..8a3d5d5 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/streamingplayer.java @@ -0,0 +1,84 @@ +// StreamingPlayer.java + +package ch.aplu.turtle; + + +import javax.sound.sampled.*; +import java.io.*; + +class StreamingPlayer +{ + private class PlayerThread extends Thread + { + public void run() + { + byte buf[] = new byte[20000]; + try + { + int cnt; + while ((cnt = audioInputStream.read(buf, 0, buf.length)) != -1) + { + if (cnt > 0) + sourceDataLine.write(buf, 0, cnt); + } + sourceDataLine.drain(); + sourceDataLine.close(); + } + catch (IOException ex) + { + System.out.println(ex); + System.exit(1); + } + } + } + + private AudioFormat audioFormat; + private AudioInputStream audioInputStream; + private SourceDataLine sourceDataLine; + private PlayerThread playerThread; + + StreamingPlayer(InputStream is) + { + try + { + audioInputStream = + AudioSystem.getAudioInputStream(is); + audioFormat = audioInputStream.getFormat(); + } + catch (Exception ex) + {} + } + + void start(boolean doFinish) + throws LineUnavailableException + { + DataLine.Info dataLineInfo = + new DataLine.Info(SourceDataLine.class, audioFormat); + sourceDataLine = + (SourceDataLine)AudioSystem.getLine(dataLineInfo); + sourceDataLine.open(audioFormat); + sourceDataLine.start(); + playerThread = new PlayerThread(); + playerThread.start(); + if (doFinish) + waitToFinish(); + } + + boolean isPlaying() + { + if (playerThread == null) + return false; + return (playerThread.isAlive()); + } + + void waitToFinish() + { + if (playerThread == null) + return; + try + { + playerThread.join(); + } + catch (InterruptedException ex) {} + } +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/tprintable.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/tprintable.java new file mode 100644 index 0000000..767d22a --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/tprintable.java @@ -0,0 +1,43 @@ +// TPrintable.java + +// Copyright 2002 Regula Hoefer-Isenegger + +// This file is part of The Java Turtle (TJT) package +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +// Added by Aegidius Pluess + +package ch.aplu.turtle; + +/** + * Interface for printing on an attached printer. + * Normally an application uses a Turtle and implements this interface. + * draw() should contain all drawing operations into the + * Turtle's Playground. The printing occures, when Turtle's print() is + * called.

+ */ + + +public interface TPrintable +{ + /** + * This method must perform all drawing operations. + * Be aware that for some undocumented reason + * draw() is called twice. So be sure you initialize it correctly. + */ + + public void draw(); +} \ No newline at end of file diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/turtle.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/turtle.java new file mode 100644 index 0000000..d30ff52 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/turtle.java @@ -0,0 +1,1585 @@ +// Turtle.java +// Modifications by Aegidius Pluess +// Adaption to the Java-Editor by Gerhard Röhner + +// Copyright 2002-2003 Regula Hoefer-Isenegger (Version 1.0) +// +// This file is part of The Java Turtle package (TJT) +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +/* +Screen coordinate system: 0..400 pixels in both directions corresponds to turtle +turtle coordinate system -200..200, so only translation is necessary (no scaling). + +This avoids rounding errors. +*/ + +package ch.aplu.turtle; + +import ch.aplu.turtle.*; +import java.awt.*; +import java.awt.event.*; +import java.awt.geom.*; +import java.awt.image.*; +import java.awt.print.*; +import java.io.*; +import javax.sound.sampled.*; +import javax.swing.*; + +/** + The core class for turtles. + + For a simple example on how to use Turtle, cf. the + Java Turtle Package description. + @author Regula Hoefer-Isenegger + @version 0.1.1 +*/ +public class Turtle implements Cloneable +{ + private double angle; + private Point2D.Double position; + private Playground playground; + private int framesPerSecond; + private double speed; // Pixel/sec + private double angleSpeed; // Radian/sec + private TurtleRenderer turtleRenderer; + private int angleResolution; + private LineRenderer lineRenderer; + private static TurtleFactory turtleFactory; + private boolean penUp; + private boolean showTurtle; + private boolean initialVisibility = true; + private Pen pen; + private Color color; + private int edgeBehaviour; + + + /** Convert from screen coordinates to turtle coordinates + */ + public Point2D.Double toTurtlePos(Point p) { + return playground.toTurtleCoords((double)p.x, (double)p.y); + } + + /** Convert from screen coordinates to turtle coordinates + */ + public Point2D.Double toTurtlePos(int x, int y) { + return playground.toTurtleCoords((double)x, (double)y); + } + + /** Convert from screen coordinates to turtle coordinates + */ + public double toTurtleX(int x) { + return (playground.toTurtleCoords((double)x, 0)).x; + } + + /** Convert from screen coordinates to turtle coordinates + */ + public double toTurtleY(int y) { + return (playground.toTurtleCoords(0, (double)y)).y; + } + + /** Add the specified mouse listener to receive mouse events. + */ + public void addMouseListener(MouseListener l) { + playground.addMouseListener(l); + } + + /** Add the specified mouse motion listener to receive mouse motion events. + */ + public void addMouseMotionListener(MouseMotionListener l) { + playground.addMouseMotionListener(l); + } + + /** + * Emit a beep. Fails if no standard speaker available. Try to use bong() instead. + * @see #bong() + */ + public void beep() { + Toolkit.getDefaultToolkit().beep(); + } + + /** + * Emit a 'bong' via the sound card. Useful on computers without standard + * speaker. + * @see #beep() + */ + public void bong() { + InputStream is = getClass().getResourceAsStream("res/bong.wav"); + StreamingPlayer player = new StreamingPlayer(is); + try + { + player.start(false); + } + catch (LineUnavailableException ex) + { + System.out.println("Error in bong(). Sound card unavailable"); + } + } + + + /** Represent clip mode. + @see #WRAP + @see #clip() + */ + protected final static int CLIP = 0; + /** Represent wrap mode. + + @see #CLIP + @see #wrap() + */ + protected final static int WRAP = 1; + /** Represent the default edge behaviour (i.e. CLIP or WRAP). + + @see #CLIP + @see #WRAP + @see #clip() + @see #wrap() + */ + protected static int DEFAULT_EDGE_BEHAVIOUR = CLIP; + /** Represent the default speed (velocity). + + @see #setSpeed(double) + */ + protected static double DEFAULT_SPEED = 200; + + /** Represent the default angle resolution. + + It specifies how many different turtle pictures + are generated. + */ + protected static int DEFAULT_ANGLE_RESOLUTION = 72; + + /** Specify how many frames per second are used for + turtle animation. + */ + protected static int DEFAULT_FRAMES_PER_SECOND = 10; + + /** Specify the default turtle color. + + @see #setColor(java.awt.Color) + */ + protected static Color DEFAULT_TURTLE_COLOR = Color.cyan; + + /** Specify the default pen color. + + @see #setPenColor(java.awt.Color) + */ + protected static Color DEFAULT_PEN_COLOR = Color.blue; + + /** Create a new Turtle + */ + public Turtle() { + } + + /** init a new Turtle + */ + protected void init(Color color) { + angle = 0; + position = new Point2D.Double(0, 0); + framesPerSecond = DEFAULT_FRAMES_PER_SECOND; + if (initialVisibility) + setSpeed(DEFAULT_SPEED); + else + setSpeed(-1); + showTurtle = initialVisibility; + setAngleResolution(DEFAULT_ANGLE_RESOLUTION); + angleSpeed = getSpeed() * Math.PI * 2 / DEFAULT_SPEED; + pen = new Pen(DEFAULT_PEN_COLOR); + if (getTurtleFactory() == null){ + turtleFactory = createTurtleFactory(); + } + setColor(color); + lineRenderer = createLineRenderer(); + getTurtleRenderer().setAngle(getAngle()); + } + + /** Set the angle resolution for the Turtle's pictures. + + It specifies how many pictures are used. e.g. an angle resolution + of 90 means that you get one picture for every 4 degrees + (= 360/90 degrees). + */ + public void setAngleResolution(int newResolution) { + synchronized(playground) { + angleResolution = newResolution; + } + } + + /** Return the TurtleFactory of this turtle. + + @see ch.aplu.turtle.TurtleFactory + */ + public TurtleFactory getTurtleFactory() { + return this.turtleFactory; + } + + /** Create a LineRenderer which is responsible + for the correct drawing of the lines. + + @return the new LineRenderer + */ + + protected LineRenderer createLineRenderer() { + return new LineRenderer(this); + } + + /** Create a TurtleRenderer which is responsible + for the correct drawing of the Turtle. + + @return the new TurtleRenderer + */ + protected TurtleRenderer createTurtleRenderer() { + return new TurtleRenderer(this); + } + + /** Create a TurtleFactory which provides for + the Turtle pictures. + + @return the new TurtleFactory + */ + + protected TurtleFactory createTurtleFactory() { + return new TurtleFactory(); + } + /** Get the angle resolution. + + @see #setAngleResolution(int) + */ + + protected int getAngleResolution() { + return angleResolution; + } + + /** Get the TurtleRenderer. + */ + TurtleRenderer getTurtleRenderer() { + return turtleRenderer; + } + + /** Get the LineRenderer. + */ + private LineRenderer getLineRenderer() { + return lineRenderer; + } + + /** Get the Playground. + */ + public Playground getPlayground() { + return playground; + } + + /** Set the Playground to the specified + playground. + + The Turtle is removed from the old + Playground and set to the new one. + */ + public void setPlayground(Playground playground) { + Playground pg = getPlayground(); + if(pg != null){ + pg.clearTurtle(this); + pg.remove(this); + pg.paintTurtles(); + } + this.playground = playground; + playground.paintTurtles(this); + } + + /** Get the angle speed. + + (I.e. how fast the Turtle rotation + animation is performed.) + */ + private double getAngleSpeed() { + return angleSpeed; + } + + /** Set the angle speed. + + @see #getAngleSpeed() + */ + private void setAngleSpeed(double newAngleSpeed) { + this.angleSpeed = newAngleSpeed; + } + + /** Get the current angle (heading) of the + Turtle. + */ + private double getAngle() { + return angle; // in radians + } + + /** Get the current speed. + */ + public double getSpeed() { + return speed; + } + + /** Query the Turtle's x-position without wrapping. + * Coordinate are not bound to playground even when + * wrapping is on. + */ + public double _getX() { + synchronized (playground) { + return position.getX(); + } + } + + /** Query the Turtle's y-position without wrapping. + * Coordinate are not bound to playground even when + * wrapping is on. + */ + public double _getY() { + synchronized (playground) { + return position.getY(); + } + } + + /** Query the Turtle's x-position. + * If turtle is outside playground and wrapping is on + * return the coordinate in range -200..200. + * */ + + public double getX() { + synchronized (playground) { + double xPos = _getX(); + return xPos; + } + } + + /** Query the Turtle's x-position. + * If turtle is outside playground and wrapping is on + * return the coordinate in range -200..200. + */ + public double getY() { + synchronized (playground) { + double yPos = _getY(); + return yPos; + } + } + + + /** Query the Turtle's position without wrapping. + * Coordinate are not bound to playground even when + * wrapping is on. + */ + public Point2D.Double _getPos() { + return position; + } + + /** Query the Turtle's position */ + public Point2D.Double getPos() { + return new Point2D.Double(getX(), getY()); + } + + /** Put the Turtle to a new position with the given x-coordinates. + + */ + public void setX(double x) { + synchronized (playground) { + getPlayground().clearTurtle(this); + internalSetX(x); + getPlayground().paintTurtles(this); + } + } + + /** Put the Turtle to a new position with the given screen x-coordinates. + */ + public void setScreenX(int x) { + synchronized (playground) { + setX(toTurtleX(x)); + } + } + + /** Put the Turtle to a new position with the given y-coordinates. + */ + public void setY(double y) { + synchronized (playground) { + getPlayground().clearTurtle(this); + internalSetY(y); + getPlayground().paintTurtles(this); + } + } + + /** Put the Turtle to a new position with the given screen y-coordinates. + */ + public void setScreenY(int y) { + synchronized (playground) { + setY(toTurtleY(y)); + } + } + + /** Set the Turtle's x-Coordinate. + */ + protected void internalSetX(double x) { + synchronized (playground) { + position.setLocation(x, _getY()); + } + } + + /** Set the Turtle's y-Coordinate. + */ + protected void internalSetY(double y) { + position.setLocation(_getX(), y); + } + + /** Set the Turtle's Position. + */ + protected void internalSetPos(double x, double y) { + position.setLocation(x, y); + } + + /** Hide the Turtle. + + @see #st() + If there is only one turte, the speed is set to -1 + so there is no Turtle animation at all. + Hiding the Turtle speeds up the graphics enormously. + */ + public void ht() { + synchronized (playground) { + this.internalHide(); + if (getPlayground().countTurtles() == 1) + setSpeed(-1); + } + } + + /** Hide the Turtle. + + This is the same as ht(). + @see #st() + */ + public void hideTurtle() { + ht(); + } + + /** This is the method called by the public methods ht() and hideTurtle(). + + Here the actual hiding takes place. + @see #ht() + @see #hideTurtle() + */ + protected void internalHide() { + getPlayground().clearTurtle(this); + showTurtle = false; + if (getPlayground().getPrinterG2D() == null) + getPlayground().repaint(); + } + + /** Set the Turtle to show mode. + + That means that the Turtle will be drawn. + @see #ht() + */ + public void st() { + synchronized (playground) { + if (getPlayground().getPrinterG2D() == null) { + getPlayground().paintTurtle(this); + showTurtle = true; + getPlayground().repaint(); + } + } + } + + /** The same as st(). + + @see #st() + */ + public void showTurtle() { + st(); + } + + /** Tell whether the Turtle is hidden or not. + + @return true if the Turtle is hidden, + false otherwise. + */ + public boolean isHidden() { + return !showTurtle; + } + + private int getFramesPerSecond() { + return this.framesPerSecond; + } + + /** Only set the angle attribute. This method does not + invoke any re-painting. + */ + private void setAngle(double radians) { + this.angle = radians; + } + + /** This is the same as setH(double degrees). + + @see #setH(double) + */ + public void setHeading(double degrees) { + synchronized (playground) { + setAngle(Math.toRadians(degrees)); + getTurtleRenderer().setAngle(Math.toRadians(degrees)); + getPlayground().clearTurtle(this); + getPlayground().paintTurtles(this); + } + } + + /** Set the Turtle's heading. + + 0 means facing NORTH.
+ the angles are measured clockwise. + @see #setHeading(double) + */ + public void setH(double degrees) { + setHeading(degrees); + } + /** Query the Turtle's heading. + + @see #setH(double) + */ + public double heading() { + synchronized (playground) { + return Math.toDegrees(getAngle()); + } + } + /** Set the Turtle's heading to the new value. + + @return the old (previous) value. + @see #setH(double) + */ + public double heading(double degrees) { + synchronized (playground) { + double tmp = Math.toDegrees(getAngle()); + setHeading(degrees); + return tmp; + } + } + + /** Set the Turtle's speed. + + If you try to set the speed to 0, it will be set to 1 (very slow). + A negative speed means that moving the Turtle (fd, bk) + will not be animated.
+ The unit is pixels per second (up to certain bounds depending on the CPU etc.).
+ Remark: Dashed lines will only be painted as you expect it with speed set + to -1. + + @see #fd(double) + @see #bk(double) + */ + public void setSpeed(double speed) { + if (speed == 0) + this.speed = 1; + else + this.speed = speed; + } + + + /** This method is responsible for the rotation animation. + */ + private void internalRotate(double angle) { + if(isHidden()){ + synchronized(playground) + { + setAngle(getAngle()+angle); + if (getTurtleRenderer().imageChanged(getAngle())) { + getTurtleRenderer().setAngle(getAngle()); + } + } + return; + } + if (angle != 0) { + int iterations = getAngleIterations(angle); + + double sign = angle/Math.abs(angle); + double increment = sign*getAngleSpeed()/(double)getFramesPerSecond(); + double startAngle = getAngle(); + + for (int index = 0; index < iterations; index++) { + long timeStamp = System.currentTimeMillis(); + + synchronized(playground) { + getPlayground().clearTurtle(this); + + if (index < iterations-1) { + setAngle(getAngle()+increment); + } + else { + setAngle(startAngle+angle); + } + + if (getTurtleRenderer().imageChanged(getAngle())) { + getTurtleRenderer().setAngle(getAngle()); + getPlayground().paintTurtles(this); + } + } + + long newTimeStamp = System.currentTimeMillis(); + Double secs = new Double(1000./getFramesPerSecond()); + long requiredTime = secs.longValue()-newTimeStamp+timeStamp; + + if (requiredTime > 0) { + try { + Thread.sleep(requiredTime); + } + catch (InterruptedException e) { + } + } + } + } + getPlayground().paintTurtles(this); + } + + /** This method is responsible for the moving animation. + */ + private void internalMove(double length) { + if (getSpeed()>0){ + if (length != 0) { + int iterations = getPathIterations(length); + // an angle of 0 means: facing NORTH + double startX = _getX(); + double startY = _getY(); + getLineRenderer().init(startX, startY); + double dx = length * Math.sin(getAngle()); + double dy = length * Math.cos(getAngle()); + double incrementX = dx / iterations; + double incrementY = dy / iterations; + for (int index = 0; index < iterations; index++) { + long timeStamp = System.currentTimeMillis(); + int nX = (int)_getX(); + int nY = (int)_getY(); + synchronized(playground) { + playground.clearTurtle(this); + if (index < iterations-1) { + internalSetX(_getX()+incrementX); + internalSetY(_getY()+incrementY); + } + else { // last step: Calc the "exact" value + internalSetX(startX + dx); + internalSetY(startY + dy); + } + if (nX != (int)_getX() || nY != - (int)_getY() || index == iterations-1){ + if (!isPenUp()) { + getLineRenderer().lineTo(_getX(), _getY()); + } + getPlayground().paintTurtles(this); + } + } + Double frames = new Double(1000./getFramesPerSecond()); + long newTimeStamp = System.currentTimeMillis(); + long requiredTime = frames.longValue()-newTimeStamp+timeStamp; + if (requiredTime > 0) { + try { + Thread.sleep(requiredTime); + } + catch (InterruptedException e) { + } + } + } + } + } + else { // Speed < 0, i.e. no animation + double startX = _getX(); + double startY = _getY(); + getLineRenderer().init(startX, startY); + double dx = length * Math.sin(getAngle()); + double dy = length * Math.cos(getAngle()); + getPlayground().clearTurtle(this); + internalSetX(startX + dx); + internalSetY(startY + dy); + if (!isPenUp()) { + getLineRenderer().lineTo(_getX(), _getY()); + } + getPlayground().paintTurtles(this); + } + } + + /** Turn the Turtle the given angle (in degrees) to the left + + @see #rt(double) + */ + public void lt(double degrees) { + left(degrees); + } + + /** Same as lt(double degrees) + + @see #lt(double) + */ + public void left(double degrees) { + internalRotate(Math.toRadians(-degrees)); + } + + /** Turn the Turtle the given angle (in degrees) to the right. + + @see #rt(double) + */ + public void rt(double degrees) { + right(degrees); + } + + /** Same as rt(double degrees). + + @see #rt(double) + */ + public void right(double degrees) { + internalRotate(Math.toRadians(degrees)); + } + + /** Same as fd(double distance) + + @see #fd(double) + */ + public void forward(double distance) { + internalMove(distance); + } + + /** Move the Turtle forwards. + + Negative values for distance are + allowed. In that case, the Turtle + will move backwards. + + @see #bk(double) + */ + public void fd(double distance) { + forward(distance); + } + + /** Same as bk(double distance). + + @see #bk(double) + */ + public void back(double distance) { + forward(-distance); + } + + /** Move the Turtle backwards. + + Negative values for distance are + allowed. In that case, the Turtle + will move forwards. + + @see #fd(double) + */ + public void bk(double distance) { + back(distance); + } + + /** Query the distance from the current location + to the given one. + */ + public double distance(double x, double y) { + synchronized (playground) { + return this.getPos().distance(x,y); + } + } + + /** Query the distance from the current location + to the given one. + * Actually a polygon with 36 sides is drawn. + */ + public double distance(Point2D.Double p) { + synchronized (playground) { + return this.getPos().distance(p); + } + } + + /** Draw a circle to the left from the current position with + * turtle's heading tangent direction and given radius. + * Actually a polygon with 36 sides is drawn. + */ + public void leftCircle(double radius) { + circle(radius, true); + } + + /** Draw a circle to the right from the current position with + * turtle's heading tangent direction and given radius. + * Actually a polygon with 36 sides is drawn. + */ + public void rightCircle(double radius) { + circle(radius, false); + } + + private void circle(double radius, boolean left) { + double nbSteps = 36; // 36 side polygon + double angle = 2 * Math.PI / nbSteps; + double step = 2 * radius * Math.sin(angle/2); + for (int i = 0; i < 36; i++) { + if (left) + if (i == 0) + lt(180.0 / nbSteps); + else + lt(360.0 / nbSteps); + else + if (i == 0) + rt(180.0 / nbSteps); + else + rt(360.0 / nbSteps); + fd(step); + } + } + + /** Calculate the number of iterations when animating left or right (rotation). + */ + private int getAngleIterations(double dAngle) { + if(getAngleSpeed()<0){ + return 1; + } + if(getAngleSpeed()==0){ + setAngleSpeed(1); + } + double dAbsAngle = Math.abs(dAngle); + Double dValue = new Double(Math.ceil(dAbsAngle/getAngleSpeed()*getFramesPerSecond())); + return dValue.intValue(); + } + + /** Calculate the number of iterations when animating forwards or backwards. + */ + private int getPathIterations(double length) { + if(speed < 0) + return 1; + + if (speed == 0) + setSpeed(1); + + double dAbsLength = Math.abs(length); + Double dValue = new Double(Math.ceil(dAbsLength/getSpeed()*getFramesPerSecond())); + return dValue.intValue(); + } + + /** Lift the Turtle's pen up so it + won't draw a line anymore. + + This is the same as pu(). + + @see #pu() + @see #penDown() + @see #pd() + */ + public void penUp() { + synchronized (playground) { + this.penUp = true; + } + } + + /** Lift the Turtle's pen up so it + won't draw a line anymore. + + This is the same as penUp(). + + @see #penUp() + @see #penDown() + @see #pd() + */ + public void pu() { + this.penUp(); + } + + /** Lower the Turtle's Pen down so it + will draw a line when moving. + + This is the same as pd(). + + @see #pd() + @see #penUp() + @see #pu() + */ + public void penDown() { + synchronized (playground) { + this.penUp = false; + } + } + + /** Lowers the Turtle's pen down so it + will draw a line when moving. + + This is the same as penDown(). + + @see #penDown() + @see #penUp() + @see #pu() + */ + public void pd() { + this.penDown(); + } + + /** Query the Pen's state (up or down). + + @return true if the Pen is + up, false otherwise. + @see #pu() + @see #pd() + */ + public boolean isPenUp() { + return this.penUp; + } + + /** Return the bounds of this Turtle. This is required + by the methods that (return-)paint the Turtles. + */ + Rectangle getBounds() { + Rectangle rect = new Rectangle(); + + Image img = getTurtleRenderer().currentImage(); + int nWidth = img.getWidth(getTurtleRenderer()); + int nHeight = img.getHeight(getTurtleRenderer()); + double x = (_getX()<0)?Math.floor(_getX()):Math.ceil(_getX()); + double y = (_getY()<0)?Math.floor(_getY()):Math.ceil(_getY()); + rect.setBounds((int)x-nWidth/2, (int)y + nHeight/2, nWidth, nHeight); + return rect; + } + + /** Get the Turtle's Pen. + + You need it if you want to change end caps etc. + @see Pen + */ + public Pen getPen() { + return pen; + } + /** Set the line thickness. + + This works only neatly in clip mode (yet). + @see #clip() + @see #wrap() + */ + public void setLineWidth(double lineWidth){ + synchronized (playground) { + setLineWidth((float)lineWidth); + } + } + + /** Set the line thickness. + + This works only neatly in clip mode (yet). + @see #clip() + @see #wrap() + */ + public void setLineWidth(float lineWidth) { + synchronized (playground) { + getPen().setLineWidth(lineWidth); + } + } + + /** Set the Turtle's color to the specified one. + + */ + public void setColor(Color color) { + synchronized (playground) { + internalSetColor(color); + getPlayground().paintTurtles(); + } + } + + private void internalSetColor(Color color) { + this.color = color; + if (getTurtleRenderer()==null) { + turtleRenderer = createTurtleRenderer(); + getTurtleRenderer().init(getTurtleFactory(), getAngleResolution()); + } + else{ + getTurtleRenderer().init(new TurtleFactory(), + this.angleResolution); + } + } + + /** Set the color to the specified one. + + @see #fill() + */ + public void setFillColor(Color color) { + synchronized (playground) { + getPen().setFillColor(color); + } + } + + /** Query the Turtle's current color. + */ + public Color getColor(){ + return color; + } + + /**Set the Turtle's pen color. + + */ + public void setPenColor(Color color) { + synchronized (playground) { + getPen().setColor(color); + } + } + + /** Move the Turtle back "home", i.e. set its position + to the origin, facing NORTH. + + Color, PenColor etc. remain the same. + */ + public void home() { + // first : clean the Turtle! + synchronized (playground) { + getPlayground().clearTurtle(this); + position = new Point2D.Double(0,0); + setHeading(0); + } + } + + /** The Turtle's Pen is + changed to an eraser (which is in fact a pen with + background color). + + This is the same as pe() + @see #pe() + */ + public void penErase() { + synchronized (playground) { + this.internalPenErase(); + } + } + + /** The Turtle's Pen is + changed to an eraser (which is in fact a pen with + background color). + + This is the same as penErase() + @see #penErase() + */ + public void pe() { + penErase(); + } + + protected void internalPenErase(){ + this.setPenColor(getPlayground().getBackground()); + } + + /** Put the Turtle to a new position with specified + x- and y-coordinates. + */ + public void setPos(double x, double y) { + synchronized (playground) { + getPlayground().clearTurtle(this); + internalSetPos(x, y); + getPlayground().paintTurtles(); + } + } + + /** Put the Turtle to a new position. + */ + public void setPos(Point2D.Double p) { + setPos(p.x, p.y); + } + + /** Put the Turtle to a new screen position. + + */ + public void setScreenPos(Point p) { + setPos(toTurtlePos(p)); + } + + + /** Put a Turtle image at the current position. + + */ + public void stampTurtle() { + synchronized (playground) { + this.getPlayground().stampTurtle(this); + } + } + + /** Calculate the direction to a given point. + + @return the direction from the current turtle position + towards the given point, measured in degrees and clockwise + from the vertical upwards position. + */ + public double towards(double x, double y) { + synchronized (playground) { + double dx = x - getX(); + double dy = y - getY(); + double result = Math.toDegrees(Math.atan2(dx, dy)); + return (result < 0) ? result+360 : result; + } + } + + /** Calculate the direction to a given point. + + @return the direction from the current turtle position + towards the given point, measured in degrees and clockwise + from the vertical upwards position. + */ + public double towards(Point2D.Double p) { + return towards(p.getX(), p.getY()); + } + + /** Put the Turtle to the top (i.e. above any other + turtle). + + Just invokes the toTop-Method in Playground. + @see Playground#toTop + @see #internalToBottom + */ + void internalToTop() { + this.getPlayground().toTop(this); + } + /** Put the Turtle to the bottom (i.e. under any other + turtle). + + Just invokes the toBottom-Method in Playground. + @see Playground#toBottom + @see #internalToTop + */ + void internalToBottom() { + this.getPlayground().toBottom(this); + } + + /** Put this turtle to the bottom. + + So any other turtle in the same Playground will be drawn over it. + */ + public void toBottom() { + // this.getPlayground().toBottom(this); + synchronized (playground) { + internalToBottom(); + getPlayground().paintTurtles(); + } + } + + /** Put this turtle to the top. + + So it will be drawn over any other turtle in the same Playground. + */ + public void toTop() { + // this.getPlayground().toTop(this); + synchronized (playground) { + this.getPlayground().paintTurtles(this); + } + } + + /** Set the pen width. + */ + public void penWidth(int newWidth) { + synchronized (playground) { + penWidth((float)newWidth); + } + } + + /** Internal Method for setting the penWidth. + */ + private void penWidth(float newWidth) { + setLineWidth(newWidth); + } + + /** Query the pen width. + */ + public int penWidth(){ + return (int)this.getPen().getLineWidth(); + } + + /** Returns the current edge behaviour. + + @see #CLIP + @see #WRAP + */ + protected int getEdgeBehaviour() { + return edgeBehaviour; + } + + /** Sets the edge behaviour to the specified value; + + @see #CLIP + @see #WRAP + */ + protected void setEdgeBehaviour(int edgeBehaviour) { + synchronized (playground) { + this.edgeBehaviour = edgeBehaviour; + } + } + + /** Set the Turtle to clip-mode. + @see #wrap() + @see #clip() + */ + public void clip() { + synchronized (playground) { + setEdgeBehaviour(CLIP); + } + } + + /** Cause the Turtle to wrap around the playground borders. + + e.g when as the Turtle leaves the Window on one side, + it reappears on the opposite side. + + @see #clip() + */ + public void wrap() { + synchronized (playground) { + setEdgeBehaviour(WRAP); + } + } + + /** Tell whether the Turtle is in clip mode. + + @return true if in clip mode, false otherwise. + @see #clip() + @see #wrap() + */ + public boolean isClip() { + return (getEdgeBehaviour() == CLIP); + } + /** Tell wheter the Turtle is in wrap mode. + + @return true if in wrap mode, false otherwise. + @see #wrap() + @see #clip() + */ + public boolean isWrap() { + return (getEdgeBehaviour() == WRAP); + } + + /** Fill the region the Turtle is in.
+ + A region is bounded by lines + of any color different to the pixel color at the current turtle position + and by the border of the window.
+ (The pen of the Turtle must be down.) + */ + public void fill() { + synchronized (playground) { + getPlayground().fill(this, getPlayground().getPixelColor(this)); + } + } + + /** Fill the region as if the Turtle where at coordinates x and y.
+ A region is bounded by lines + of any color different to the pixel color at the given position and by the border of + the window.
+ (The pen of the Turtle must be down.) + */ + public void fill(double x, double y) { + synchronized (playground) { + double oldX = getX(); + double oldY = getY(); + boolean hidden = isHidden(); + ht(); + setPos(x,y); + getPlayground().fill(this, getPlayground().getPixelColor(this)); + setPos(oldX, oldY); + if(!hidden){ + st(); + } + } + } + + /** Erase all traces and text painted by the turtles, but let all + * turtles where they are. + + */ + public void clean() { + synchronized (playground) { + getPlayground().clean(); + } + } + + /** Paint the specified text at the current turtle position. + + */ + public void label(String text) { + synchronized (playground) { + if (text != null && text.length() > 0) + getPlayground().label(text, this); + } + } + + /** Set the current font as specified. + + @see java.awt.Font + */ + public void setFont(Font font) { + synchronized (playground) { + getPen().setFont(font); + } + } + + /** Change the current font to the specified one. If you want to know what fonts are available on your system, + call #getAvailableFontFamilies() . + + @see #getAvailableFontFamilies + @see java.awt.Font more information about fontName, style and size. + */ + public void setFont(String fontName, int style, int size) { + synchronized (playground) { + getPen().setFont(new Font(fontName, style, size)); + } + } + + /** Set the font size. + + @see #setFontStyle changing the font style + @see #setFont changing the whole font. + */ + public void setFontSize(int size) { + synchronized (playground) { + getPen().setFontSize(size); + } + } + + /** Set the font style. + + This is either java.awt.Font.PLAIN, java.awt.Font.BOLD, java.awt.Font.ITALIC or java.awt.Font.BOLD+java.awt.Font.ITALIC + @see #setFontSize changing the font size + @see #setFont changing the whole font. + */ + public void setFontStyle(int style) { + synchronized (playground) { + getPen().setFontStyle(style); + } + } + + /** Provide information about all font families (e.g. roman) currently available on your system. + Each font name is a string packed into a array of strings. + (This might be useful when you want to change the font.) + @see #setFont(java.awt.Font) + @see #setFont(java.lang.String, int, int) + */ + public static String[] getAvailableFontFamilies() { + return Pen.getAvailableFontFamilies(); + } + + /** Return the current font. + + @see #setFontSize changing the font size + @see #setFontStyle changing the font style + @see #setFont changing the whole font. + */ + public Font getFont() { + return getPen().getFont(); + } + + /** Creates a clone of the Turtle in it's Playground. + Color, position and heading are the same. + (It's visible and pen is down.) + Return the cloned object reference as a Object type, because + it overrides Object.clone(). + (You may cast it to a Turtle.) + */ + public Object clone() + { + synchronized (playground) { + Turtle t = new Turtle(); + t.setColor(getColor()); + t.setPos(getX(), getY()); + t.heading(heading()); + t.showTurtle(); + return t; + } + } + + /** Set antialiasing on or off for the turtle trace buffer + * This may result in an better image quality, especially + * for filling operations (platform dependant). + */ + public void antiAliasing(boolean on) { + synchronized (playground) { + getPlayground().setAntiAliasing(on); + } + } + + + /** + * Print the graphics context to an attached printer with + * the given magnification scale factor. + * scale = 1 will print on standard A4 format paper.
+ * + * The given tp must implement the GPrintable interface, + * e.g. the single method void draw(), where all the + * drawing into the GPanel must occur. + * + * Be aware the turtle(s) state (position, direction, etc.) + * must be reinitialized, because draw() is called several + * times by the printing system. + * + * A standard printer dialog is shown before printing is + * started. Only turte traces are printed. + *
+ * Example:
+ * + import ch.aplu.turtle.*;
+
+ public class PrintTest implements TPrintable
+ {
+ private Turtle t = new Turtle();
+
+ public PrintTest()
+ {
+ draw(); // Draw on screen
+ t.print(this); // Draw on printer
+ }
+
+ public void draw()
+ {
+ t.home(); // Needed for initialization
+ for (int i = 0; i < 5; i++)
+ t.fd(20).rt(90).fd(20).lt(90);
+ }
+
+ public static void main(String[] args)
+ {
+ new PrintTest();
+ }
+ }
+
+ */ + public boolean print(TPrintable tp, double scale) + { + return getPlayground().print(tp, scale); + } + + /** + * Same as print(tp, scale) with scale = 1. + */ + public boolean print(TPrintable tp) + { + return print(tp, 1); + } + + /** + * Print the Turtle's current playground with given scale. + */ + public boolean printScreen(double scale) + { + return print(null, scale); + } + + /** + * Same as printScreen(scale) with scale = 1. + */ + public boolean printScreen() + { + return printScreen(1); + } + + /** + * Clear the Turtle's playground. All turtle images and traces are erased, + * but the turtles remain (invisible) at their positions. + */ + public void clear() + { + synchronized (playground) { + getPlayground().clear(); + } + } + + /** + * Delay execution for the given amount of time ( in ms ). + */ + public static void sleep(int time) + { + try + { + Thread.currentThread().sleep(time); + } + catch (Exception e) {} + } + + /** + * Return version information + */ + public String version() + { + return SharedConstants.VERSION; + } + + /** + * Enable/disable automatic repainting. + * If disabled, call repaint() to perform repainting. + * Disabling automatic repainting and hiding the + * Turtle speeds up the graphics enormously. + */ + public void enableRepaint(boolean b) + { + getPlayground().enableRepaint(b); + } + + /** + * Perform manual repainting when automatic repainting is + * disabled. + */ + public void repaint() { + getPlayground().repaint(); + } + + /** + * Return the color of the pixel at the current turtle position. + */ + public Color getPixelColor() { + synchronized (playground) { + return getPlayground().getPixelColor(this); + } + } + + protected Graphics getGraphics() { + return playground.getGraphics(); + } + + /** + * Transforms the gui-position to mouse-coordinates. + */ + public void setBounds(int x, int y, int b, int h) { + int ox = playground.getOriginX(); + int oy = playground.getOriginY(); + x = -ox + x + 10; + y = oy - y - 13; + setPos(x, y); + } + + /** + * Shows or hides the turtle. + */ + public void setVisible(boolean show) { + if (show) { + showTurtle(); + } else { + hideTurtle(); + } // end of if-else + } + + /** + * Sets clipping-mode on/off. + */ + public void setClip(boolean clip) { + if (clip) { + clip(); + } else { + wrap(); + } // end of if-else + } + + /** + * Sets pen-position up/down. + */ + public void setPenUp(boolean penup) { + if (penup) { + penUp(); + } else { + penDown(); + } // end of if-else + } + + /** + * Sets angle of the turtle. + */ + public void setAngle(int angle) { + setHeading(angle); + } +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/turtlefactory.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/turtlefactory.java new file mode 100644 index 0000000..e9d700e --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/turtlefactory.java @@ -0,0 +1,66 @@ +// TurtleFactory.java + +// Copyright 2002 Regula Hoefer-Isenegger +// +// This file is part of The Java Turtle (TJT) package +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package ch.aplu.turtle; + +import java.awt.image.*; +import java.awt.*; + +class TurtleFactory +{ + /** Generates the shape of the turtle with color, + * angle angle, width w + * and height h. + */ + protected Image turtleImage(Color color, double angle, int w, int h) + { + BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR); + Graphics2D g = (Graphics2D)bi.getGraphics(); + // Origin in center + g.translate(w/2, h/2); + // angle = 0 is direction east (as usual in mathematics) + g.rotate(Math.PI/2 - angle); + g.setColor(color); + + // Body + g.fillOval((int)( -0.35 * w), (int)( -0.35 * h), + (int)(0.7 * w), (int)(0.7 * h)); + + // Head + g.fillOval((int)( -0.1 * w), (int)( -0.5 * h), + (int)(0.2 * w), (int)(0.2 * h)); + + // Tail + int[] xcoords = + {(int)( -0.05 * w), 0, (int)(0.05 * w)}; + int[] ycoords = + {(int)(0.35 * h), (int)(0.45 * h), (int)(0.35 * h)}; + g.fillPolygon(xcoords, ycoords, 3); + + // Feet + for (int i = 0; i < 4; i++) + { + g.rotate(Math.PI / 2); + g.fillOval((int)( -0.35 * w), (int)( -0.35 * h), + (int)(0.125 * w), (int)(0.125 * h)); + } + return (Image)bi; + } +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/turtlerenderer.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/turtlerenderer.java new file mode 100644 index 0000000..9489e78 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/aplu/turtle/turtlerenderer.java @@ -0,0 +1,312 @@ +// TurtleRenderer.java + +// Copyright 2002 Regula Hoefer-Isenegger +// Some modifications by Aegidius Pluess + +// This file is part of The Java Turtle (TJT) package +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package ch.aplu.turtle; + +import ch.aplu.turtle.*; +import java.awt.image.*; +import java.awt.*; +import javax.swing.*; +import java.awt.geom.*; +import java.util.*; + + +/** This class is responsible for creating and choosing the correct Turtle picture. + @author Regula Hoefer-Isenegger + @version 0.1 +*/ +public class TurtleRenderer implements ImageObserver { + /** Holds the current image */ + private Image currentImage; + /** Holds all images */ + private Vector images; + /** Tells how many pictures are needed*/ + private int resolution; + /** A reference to the Turtle */ + private Turtle turtle; + /** Holds the current Angle */ + private double currentAngle; + + private final int turtleSize = 29; + + /***/ + public TurtleRenderer(Turtle turtle) { + this.currentImage = null; + this.images = new Vector(); + this.turtle = turtle; + currentAngle = 0; + } + + /** As an image stays unchanged, there's no need to ever update it. So this method returns always false. + + For further information cf. java.awt.image.ImageObserver. + @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int) + */ + public boolean imageUpdate(Image img, int infoflags, + int x, int y, int width, int height) { + return false; + } + + /** Return the current image. + */ + public Image currentImage() { + return this.currentImage; + } + + /** Tell whether the image has changed. + */ + public boolean imageChanged(double angle) { + return (this.currentImage != getImage(angle)); + } + + /** Set the current image to the specified one. */ + private void setCurrentImage(Image image) { + currentImage = image; + } + + /** Choose the image for the angle angle. + */ + private Image getImage(double angle) { + while (angle < 0) { + angle += 2*Math.PI; + } + while (angle >= 2*Math.PI) { + angle -= 2*Math.PI; + } + double res = 2*Math.PI/(double)this.resolution; + int index = (int)(angle/res); + return image(index); + } + + /** Set the current image to the one corresponding to the angle angle. + */ + public void setAngle(double angle) { + currentAngle = angle; + setCurrentImage(getImage(angle)); + } + + /** @return the current angle. */ + protected double getAngle(){ + return currentAngle; + } + + /** Create the images. There are resolution images (i.e. two subsequent + images contain an angle of 2π/ or 360/resolution degrees). + */ + public void init(TurtleFactory factory, int resolution) { + this.resolution = resolution; + Integer res = new Integer(resolution); + double incRes = Math.PI*2/res.doubleValue(); + double angle = 0; + images = new Vector(); + for (int i = 0; i < resolution; i++) { + images.add(factory.turtleImage(turtle.getColor(), + turtle.getPlayground().toScreenAngle(angle), + turtleSize, turtleSize)); + angle += incRes; + } + setCurrentImage(getImage(currentAngle)); + } + + /** Tell how many images this TurtleRenderer holds */ + private int countImages() { + return this.images.size(); + } + + /** Get the image at index */ + private Image image(int index) { + return (Image)this.images.elementAt(index); + } + + /** This method is responsible for painting the turtle onto the + playground at (x, y). + */ + public final void paint(double x, double y) { + internalPaint(x, y, turtle.getPlayground().getGraphics()); + } + + /** This method is responsible for painting the turtle onto the + playground at p. + */ + public final void paint(Point2D.Double p) { + internalPaint(p.x, p.y, turtle.getPlayground().getGraphics()); + } + + /** This method is responsible for painting the Turtle + at (x, y).
+ The Graphics argument tells where to paint. + */ + public final void paint(double x, double y, Graphics g) { + internalPaint(x, y, g); + } + + /** This method is responsible for painting the Turtle + at p.
+ The Graphics argument tells where to paint. + */ + public void paint(Point2D.Double p, Graphics g){ + internalPaint(p.x, p.y, g); + } + + /** Call clipPaint and wrapPaint(). + + You should override this method only, if you add a new (edge) + behaviour to the turtle. I recommend to you then to first call this + method from the overriding one. +
+ If you want to change anything about the real painting, override + clipPaint or wrapPaint. + + @see #clipPaint(int, int, Graphics2D) + @see #wrapPaint(int, int, Graphics2D) + */ + protected void internalPaint(double x, double y, Graphics g) { + if(turtle.isClip()){ + Point2D.Double p = + calcTopLeftCorner(turtle.getPlayground().toScreenCoords(x, y)); + clipPaint((int)p.x, (int)p.y, (Graphics2D)g); + } + else if(turtle.isWrap()){ + Point2D.Double p = + calcTopLeftCorner(turtle.getPlayground().toScreenCoords(x, y)); + wrapPaint((int)p.x, (int)p.y, (Graphics2D)g); + } + } + + /** Define how to paint in clip mode and do it */ + protected void clipPaint(int x, int y, Graphics2D g2D) { + g2D.drawImage(currentImage, x, y, this); + } + + /** Define how to paint in wrap mode and do it */ + protected void wrapPaint(int x, int y, Graphics2D g2D) { + int pWidth = turtle.getPlayground().getWidth(); + int pHeight = turtle.getPlayground().getHeight(); + int paintX = x; + while (paintX > pWidth) { + paintX -= pWidth; + } + while (paintX < 0) { + paintX += pWidth; + } + int paintY = y; + while (paintY > pHeight) { + paintY -= pHeight; + } + while (paintY < 0) { + paintY += pHeight; + } + g2D.drawImage(currentImage, paintX, paintY, this); + int nWidth = currentImage.getWidth(this); + int nHeight = currentImage.getHeight(this); + boolean right = (paintX+nWidth > pWidth); + boolean bottom = (paintY+nHeight > pHeight); + if(right) { + g2D.drawImage(currentImage, + paintX-pWidth, + paintY, + this); + } + if(bottom) { + g2D.drawImage(currentImage, + paintX, + paintY-pHeight, + this); + } + if(right && bottom) { + g2D.drawImage(currentImage, + paintX-pWidth, + paintY-pHeight, + this); + } + } + + /** Compute the x-coordinate of the top left corner of the Turtle image + (it depends on the specified x-coordinate and the image width). + */ + protected int calcTopLeftCornerX(double x) { + int intX = (int)x; + int nWidth; + if(currentImage == null) { + setCurrentImage(new TurtleFactory(). + turtleImage(turtle.getColor(), getAngle(), turtleSize, turtleSize)); + } + nWidth = this.currentImage.getWidth(this); + // the center of the turtle lies on the turtle's location: + intX -= nWidth/2; + return intX; // top left corner of the Turtle's image + } + + /** Compute the y-coordinate of the top left corner of the Turtle image + (it depends on the specified y-coordinate and the image height). + */ + protected int calcTopLeftCornerY(double y) { + int intY = (int)y; + if(currentImage == null) { + setCurrentImage(new TurtleFactory(). + turtleImage(turtle.getColor(), getAngle(), turtleSize, turtleSize)); + } + int nHeight = currentImage.getHeight(this); + // the center of the turtle lies on the turtle's location: + intY -= nHeight/2; + + return intY; // top left corner of the Turtle's image + } + /** Compute the top left corner of the Turtle image + (dependent on the specified x- and y-coordinate and the image + width and height. + */ + protected Point2D.Double calcTopLeftCorner(double x, double y) { + if(currentImage == null) { + setCurrentImage(new TurtleFactory(). + turtleImage(turtle.getColor(), getAngle(), turtleSize, turtleSize)); + } + int w = currentImage.getWidth(this); + int h = currentImage.getHeight(this); + return new Point2D.Double(x-w/2, y-h/2); + } + + /** Compute the top left corner of the Turtle image + (dependent on the specified point p and the image + width and height. + */ + protected Point2D.Double calcTopLeftCorner(Point2D.Double p) { + return calcTopLeftCorner(p.x, p.y); + } +} + + + + + + + + + + + + + + + + + + diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/package.bluej b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/package.bluej new file mode 100644 index 0000000..4843072 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/ch/package.bluej @@ -0,0 +1,26 @@ +#BlueJ package file +objectbench.height=155 +objectbench.width=760 +package.divider.horizontal=0.6 +package.divider.vertical=0.6766467065868264 +package.editor.height=332 +package.editor.width=649 +package.editor.x=379 +package.editor.y=72 +package.frame.height=600 +package.frame.width=800 +package.numDependencies=0 +package.numTargets=1 +package.showExtends=true +package.showUses=true +readme.height=58 +readme.name=@README +readme.width=47 +readme.x=10 +readme.y=10 +target1.height=62 +target1.name=aplu +target1.type=PackageTarget +target1.width=80 +target1.x=180 +target1.y=10 diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/eval.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/eval.java new file mode 100644 index 0000000..97c8ff0 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/eval.java @@ -0,0 +1,50 @@ +public class Eval { + + public static Wrap getOb(Object o) { + return new Wrap(o); + } + + protected static Object getOb(final String s) { + return new Object() {public String result = s;}; + } + + protected static Object getOb(final boolean b) { + return new Object() {public boolean result = b;}; + } + + protected static Object getOb(final byte b) { + return new Object() {public byte result = b;}; + } + + protected static Object getOb(final char c) { + return new Object() {public char result = c;}; + } + + protected static Object getOb(final double d) { + return new Object() {public double result = d;}; + } + + protected static Object getOb(final float f) { + return new Object() {public float result = f;}; + } + + protected static Object getOb(final int i) { + return new Object() {public int result = i;}; + } + + protected static Object getOb(final long l) { + return new Object() {public long result = l;}; + } + protected static Object getOb(final short s) { + return new Object() {public short result = s;}; + } +} + + +class Wrap { + public Object result; + + Wrap(Object result) { + this.result = result; + } +} \ No newline at end of file diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/inout.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/inout.java new file mode 100644 index 0000000..d702d54 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/inout.java @@ -0,0 +1,98 @@ +import java.io.*; + +/** +Support class for console input and output of numeric values.

+ +Example for input:
+ int age = InOut.readInt("Your age: ");

+ +Example for output:
+ System.out.println("price: " + InOut.format2(prize) + "Euro");
+ System.out.println("percent: " + InOut.formatN(percent, 1) + " %"); +*/ + + +public class InOut { + + /** Formats a double-value with 2 decimal digits. */ + public static String format2(double d) { + return String.format("%.2f", d); + } + + /** Formats a double-value with N decimal digits. */ + public static String formatN(double d, int N) { + return String.format("%." + N + "f", d); + } + + /** Reads a boolean-value from console. */ + public static boolean readBoolean(String prompt) { + final String[] trueValues = + { "1", "y", "t", "j", "w", "yes", "true", "ja", "wahr", "ok" }; + System.out.print(prompt); + String input = readln().toLowerCase(); + for (int i = 0; i < trueValues.length; ++i) + if (trueValues[i].equals(input)) + return true; + return false; + } + + /** Reads a char-value from console. */ + public static char readChar(String prompt) { + System.out.print(prompt); + return readln().charAt(0); + } + + /** Reads a double-value from console. */ + public static double readDouble(String prompt) { + System.out.print(prompt); + return Double.parseDouble(readln()); + } + + /** Reads a float-value from console. */ + public static float readFloat(String prompt) { + System.out.print(prompt); + return Float.parseFloat(readln()); + } + + /** Reads an int-value from console. */ + public static int readInt(String prompt) { + System.out.print(prompt); + return Integer.parseInt(readln()); + } + + /** Reads a long-value from console. */ + public static long readLong(String prompt) { + System.out.print(prompt); + return Long.parseLong(readln()); + } + + /** Reads a string-value from console. */ + public static String readString(String prompt) { + System.out.print(prompt); + return readln(); + } + + /** Reads a string-value from console without prompt. + For use at the end of a console program. */ + public static String readln() { + try { + return Input.readLine(); + } catch(Exception e) { + return ""; + } + } + + private static BufferedReader Input; + + static { + try { + Input = new BufferedReader(new InputStreamReader(System.in)); + } + catch (Exception e) { + System.out.println("console input not possible."); + } + } + + + +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/je/package.bluej b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/je/package.bluej new file mode 100644 index 0000000..9f468a7 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/je/package.bluej @@ -0,0 +1,26 @@ +#BlueJ package file +objectbench.height=155 +objectbench.width=760 +package.divider.horizontal=0.6 +package.divider.vertical=0.6766467065868264 +package.editor.height=332 +package.editor.width=649 +package.editor.x=379 +package.editor.y=72 +package.frame.height=600 +package.frame.width=800 +package.numDependencies=0 +package.numTargets=1 +package.showExtends=true +package.showUses=true +readme.height=58 +readme.name=@README +readme.width=47 +readme.x=10 +readme.y=10 +target1.height=62 +target1.name=util +target1.type=PackageTarget +target1.width=80 +target1.x=180 +target1.y=10 diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/je/util/package.bluej b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/je/util/package.bluej new file mode 100644 index 0000000..5742d82 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/je/util/package.bluej @@ -0,0 +1,27 @@ +#BlueJ package file +objectbench.height=155 +objectbench.width=760 +package.divider.horizontal=0.6 +package.divider.vertical=0.6766467065868264 +package.editor.height=332 +package.editor.width=649 +package.editor.x=399 +package.editor.y=92 +package.frame.height=600 +package.frame.width=800 +package.numDependencies=0 +package.numTargets=1 +package.showExtends=true +package.showUses=true +readme.height=58 +readme.name=@README +readme.width=47 +readme.x=10 +readme.y=10 +target1.height=50 +target1.name=Turtle +target1.showInterface=false +target1.type=ClassTarget +target1.width=80 +target1.x=180 +target1.y=10 diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/je/util/turtle.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/je/util/turtle.java new file mode 100644 index 0000000..4a704ad --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/je/util/turtle.java @@ -0,0 +1,311 @@ +package je.util; +/* + * Class : Turtle + * Copyright: (c) Gerhard Röhner + * + * History + * -------- + * 3.00 2000.10.10 first version as java class + * 3.01 2002.10.22 DrawDynamic + * 3.02 2010.10.11 sleepTime for paint delay + * 3.03 2012.03.20 cartesian coordinate system, used with originX, originY, setOrigin + * 3.04 2015.01.24 english version, integrated component + */ + + +import java.awt.*; +import java.awt.image.*; +import java.awt.event.*; + +/** + * This class is a turtle component for simple graphic programming. + * + * @see + * @author Gerhard Röhner + * @version 3.03 20/03/2012 + */ + + +public class Turtle extends Canvas { + + // -- private Attribute ------------------------------------------------------ + + private BufferedImage myBufferedImage; + private Graphics myBufferedGraphics; + private Color foreground; + private Color background; + private static final double piDurch180 = Math.PI / 180; + + // -- public attributes ----------------------------------------------------- + + /** + * turtleX is the x-coordinate of the turtle + *

+ * Example:

myTurtle.turtleX = 100;
+ */ + public double turtleX; + + /** + * turtleY is the y-coordinate of the turtle. + *

+ * Example:

myTurtle.turtleY = 200;
+ */ + public double turtleY; + + /** + * turtleW is the current angle of the turtle in the range form 0 to 360 degrees. + *

+ * Example:

myTurtle.turtleW = 180;
+ */ + public double turtleW; + + /** + * originX is the x-position of the cartesian coodinate system within the turtle canvas. + *

+ * Example:

myTurtle.originX = 0;
+ */ + public double originX; + + /** + * originY is the y-position of the cartesian coodinate system within the turtle canvas. + *

+ * Example:

myTurtle.originY = 100;
+ */ + public double originY; + + /** + * If drawDynamic is true you can watch the drawing of the turtle. + *

+ * Example:

myTurtle.drawDynamic = true;
+ */ + public boolean drawDynamic; + + /** + * For drawDynamic = true you set the delay in milliseconds for drawing. + *

+ * Example:

myTurtle.sleepTime = 500;
+ */ + public int sleepTime = 0; + + // --- constructor ----------------------------------------------------------- + + /** + * Creates a Turtle with a canvas. + * At the beginning the Turtle is placed in the middle of it's canvas. + * The start angle is 0 degree, which means the Turtle looks to the right. + * The background color is white, the drawing color black. + *

+ * The turtle position can easily be changed by clicking into the canvas. + *

+ * The size of the canvas can easily be changed with the mouse. + *

+ * Example:

Turtle myTurtle = new Turtle();
+ */ + + public Turtle() { + myBufferedImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); + myBufferedGraphics = myBufferedImage.getGraphics(); + + setForeground(Color.black); + setBackground(Color.white); + drawDynamic = true; + + addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent evt) { + turtleMouseClicked(evt);}}); + addComponentListener(new ComponentAdapter() { + public void componentResized(ComponentEvent evt) { + turtleResized(evt);}}); + setOrigin(50, 50); + } + + private void turtleMouseClicked(MouseEvent evt) { + turtleX = evt.getX() - originX; + turtleY = originY - evt.getY(); + turtleW = 0; + } + + private void turtleResized(ComponentEvent evt) { + int width = getWidth(); + int height = getHeight(); + + BufferedImage newBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics newGraphics = newBufferedImage.getGraphics(); + newGraphics.setColor(background); + newGraphics.fillRect(0, 0, width, height); + newGraphics.setColor(foreground); + newGraphics.drawImage(myBufferedImage, 0, 0, this); + + turtleX = 0; + turtleY = 0; + turtleW = 0; + setOrigin(width / 2, height / 2); + + myBufferedImage = newBufferedImage; + myBufferedGraphics = newGraphics; + } + + public boolean isDoubleBuffered() { + return true; + } + + private void wTurtleMod360() { + while (turtleW >= 360) + turtleW = turtleW - 360; + while (turtleW < 0) + turtleW = turtleW + 360; + } + + // --- angle and turns ------------------------------------------------------- + + /** + * Turns the angle of the turtle relativ by the parameter angle. + * Positive values means a turn right, negative a turn left. + *

+ * Example:

myTurtle.turn(-90);
+ */ + public void turn(double angle) { + turtleW = turtleW + angle; + wTurtleMod360(); + } + + /** + * Sets the angle of the turtle absolute by the parameter angle. + * The angle increases counterclockwise, therefore + * right = 0, top = 90, left = 180, bottom = 270. + *

+ * Example:

myTurtle.turnto(270);
+ */ + public void turnto(double angle) { + turtleW = angle; + wTurtleMod360(); + } + + // --- Drawing --------------------------------------------------------------- + + /** + * The Turtle draws a line of the length specified in the current direction. + *

+ * Example:

myTurtle.draw(100);
+ */ + public void draw(double length) { + drawto(turtleX + length * Math.cos(turtleW * piDurch180), + turtleY + length * Math.sin(turtleW * piDurch180)); + } + + /** + * The Turtle draws a line form the current position (turtleX, turtleY) to the + * position (x, y) relativ to the cartesian coordinate system. + *

+ * Example:

myTurtle.drawto(200, 300);
+ */ + public void drawto(double x, double y) { + int x1 = (int) (originX + turtleX); + int x2 = (int) (originX + x); + int y1 = (int) (originY - turtleY); + int y2 = (int) (originY - y); + + myBufferedGraphics.drawLine(x1, y1, x2, y2); + if (drawDynamic){ + getGraphics().drawLine(x1, y1, x2, y2); + try { + Thread.currentThread().sleep(sleepTime); + } catch(InterruptedException e) { + } + } else + repaint(); + + turtleX = x; + turtleY = y; + } + + // --- Moving ---------------------------------------------------------------- + + /** + * The Turtle moves without drawing the length specified in the current + * direction. + *

+ * Example:

myTurtle.move(100);
+ */ + public void move(double length) { + turtleX = turtleX + length * Math.cos (turtleW * piDurch180); + turtleY = turtleY + length * Math.sin (turtleW * piDurch180); + } + + /** + * The Turtle moves without drawing to position (x, y) relativ to the + * cartesian coordinate system. + *

+ * Example:

myTurtle.moveto(100, 200);
+ */ + public void moveto(double x, double y) { + turtleX = x; + turtleY = y; + } + + // --- set origin ----------------------------------------------------------- + + /** + * Sets the origin of the cartesian coordinate system within the turtle's canvas. + *

+ * Example:

myTurtle.setOrigin(100, 200);
+ */ + public void setOrigin(double x, double y) { + originX = x; + originY = y; + } + + // --- fore- and background color -------------------------------------------- + + /** + * Sets the drawing color of the Turtle to color c. + *

+ * Example:

myTurtle.setForeground(Color.red);
+ */ + public void setForeground(Color c) { + foreground = c; + myBufferedGraphics.setColor(foreground); + super.setForeground(foreground); + } + + /** + * Sets the canvas color of the Turtle to color c. + *

+ * Example:

myTurtle.setBackground(Color.blue); 
+ */ + public void setBackground(Color c) { + background = c; + myBufferedGraphics.setColor(background); + myBufferedGraphics.fillRect(0, 0, getWidth(), getHeight()); + myBufferedGraphics.setColor(foreground); + repaint(); + } + + /** + * Clear the Turtle's canvas with the background color. + *

+ * Example:

myTurtle.clear();
+ */ + public void clear() { + setBackground(background); + getGraphics().drawImage(myBufferedImage, 0, 0, this); + repaint(); + } + + // --- Showing -------------------------------------------------------------- + + /** + * Shows the Turtle's canvas. + */ + public void paint(Graphics g) { + g.drawImage(myBufferedImage, 0, 0, this); + } + + /** + * Updates the Turtle's canvas. + */ + public void update(Graphics g) { + paint(g); + } + +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/jnumberfield.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/jnumberfield.java new file mode 100644 index 0000000..fd41a78 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/jnumberfield.java @@ -0,0 +1,129 @@ +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import java.util.*; + +/** +Swing-component for input and output of numeric values. +*/ + +public class JNumberField extends JTextField { + + /** constructor for a JNumberField */ + public JNumberField() { + enableEvents(AWTEvent.KEY_EVENT_MASK); + } + + /** Gets a double-value from the JNumberField. */ + public double getDouble() { + Double d = new Double(getText()); + return d.doubleValue(); + } + + /** Gets a float-value from the JNumberField. */ + public float getFloat() { + Double d = new Double(getText()); + return d.floatValue(); + } + + /** Gets an int-value from the JNumberField. */ + public int getInt() { + Double d = new Double(getText()); + return d.intValue(); + } + + /** Gets a long-value from the JNumberField. */ + public long getLong() { + Double d = new Double(getText()); + return d.longValue(); + } + + /** Checks wether the JNumberField contains a valid numeric value. */ + public boolean isNumeric() { + final String Digits = "(\\p{Digit}+)"; + final String HexDigits = "(\\p{XDigit}+)"; + // an exponent is 'e' or 'E' followed by an optionally + // signed decimal integer. + final String Exp = "[eE][+-]?"+Digits; + final String fpRegex = + ("[\\x00-\\x20]*"+ // Optional leading "whitespace" + "[+-]?(" + // Optional sign character + "NaN|" + // "NaN" string + "Infinity|" + // "Infinity" string + + // A decimal floating-point string representing a finite positive + // number without a leading sign has at most five basic pieces: + // Digits . Digits ExponentPart FloatTypeSuffix + // + // Since this method allows integer-only strings as input + // in addition to strings of floating-point literals, the + // two sub-patterns below are simplifications of the grammar + // productions from the Java Language Specification, 2nd + // edition, section 3.10.2. + + // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt + "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+ + + // . Digits ExponentPart_opt FloatTypeSuffix_opt + "(\\.("+Digits+")("+Exp+")?)|"+ + + // Hexadecimal strings + "((" + + // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt + "(0[xX]" + HexDigits + "(\\.)?)|" + + + // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt + "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" + + + ")[pP][+-]?" + Digits + "))" + + "[fFdD]?))" + + "[\\x00-\\x20]*");// Optional trailing "whitespace" + + return java.util.regex.Pattern.matches(fpRegex, getText()); + } + + /** Sets a double-value into the JNumberField. */ + public void setDouble(double d) { + setText(String.valueOf(d)); + } + + /** Sets a double-value with N digits into the JNumberField. */ + public void setDouble(double d, int N) { + setText(String.format(Locale.ENGLISH, "%." + N + "f", d)); + } + + /** Sets a float-value into the JNumberField. */ + public void setFloat(float f) { + setText(String.valueOf(f)); + } + + /** Sets a float-value with N digits into the JNumberField. */ + public void setFloat(float f, int N) { + setText(String.format(Locale.ENGLISH, "%." + N + "f", f)); + } + + /** Sets an int-value into the JNumberField. */ + public void setInt(int i) { + setText(String.valueOf(i)); + } + + /** Sets a long-value into the JNumberField. */ + public void setLong(long l) { + setText(String.valueOf(l)); + } + + /** Clears the JNumberField */ + public void clear() { + setText(""); + } + + protected void processKeyEvent(KeyEvent e) { + super.processKeyEvent(e); + if (isNumeric() || getText().equals("-") || + getText().equals("") || getText().equals(".")) + setBackground(Color.white); + else + setBackground(Color.red); + } + +} \ No newline at end of file diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/numberfield.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/numberfield.java new file mode 100644 index 0000000..ca7ef01 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/numberfield.java @@ -0,0 +1,128 @@ +import java.awt.*; +import java.awt.event.*; +import java.util.*; + +/** +AWT-component for input and output of numeric values. +*/ + +public class NumberField extends TextField { + + /** constructor for a NumberField */ + public NumberField() { + enableEvents(AWTEvent.KEY_EVENT_MASK); + } + + /** Gets a double-value from the NumberField. */ + public double getDouble() { + Double d = new Double(getText()); + return d.doubleValue(); + } + + /** Gets a float-value from the NumberField. */ + public float getFloat() { + Double d = new Double(getText()); + return d.floatValue(); + } + + /** Gets an int-value from the NumberField. */ + public int getInt() { + Double d = new Double(getText()); + return d.intValue(); + } + + /** Gets a long-value from the NumberField. */ + public long getLong() { + Double d = new Double(getText()); + return d.longValue(); + } + + /** Checks wether the NumberField contains a valid numeric value. */ + public boolean isNumeric() { + final String Digits = "(\\p{Digit}+)"; + final String HexDigits = "(\\p{XDigit}+)"; + // an exponent is 'e' or 'E' followed by an optionally + // signed decimal integer. + final String Exp = "[eE][+-]?"+Digits; + final String fpRegex = + ("[\\x00-\\x20]*"+ // Optional leading "whitespace" + "[+-]?(" + // Optional sign character + "NaN|" + // "NaN" string + "Infinity|" + // "Infinity" string + + // A decimal floating-point string representing a finite positive + // number without a leading sign has at most five basic pieces: + // Digits . Digits ExponentPart FloatTypeSuffix + // + // Since this method allows integer-only strings as input + // in addition to strings of floating-point literals, the + // two sub-patterns below are simplifications of the grammar + // productions from the Java Language Specification, 2nd + // edition, section 3.10.2. + + // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt + "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+ + + // . Digits ExponentPart_opt FloatTypeSuffix_opt + "(\\.("+Digits+")("+Exp+")?)|"+ + + // Hexadecimal strings + "((" + + // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt + "(0[xX]" + HexDigits + "(\\.)?)|" + + + // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt + "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" + + + ")[pP][+-]?" + Digits + "))" + + "[fFdD]?))" + + "[\\x00-\\x20]*");// Optional trailing "whitespace" + + return java.util.regex.Pattern.matches(fpRegex, getText()); + } + + /** Sets a double-value into the NumberField. */ + public void setDouble(double d) { + setText(String.valueOf(d)); + } + + /** Sets a double-value with N digits into the NumberField. */ + public void setDouble(double d, int N) { + setText(String.format(Locale.ENGLISH, "%." + N + "f", d)); + } + + /** Sets a float-value into the NumberField. */ + public void setFloat(float f) { + setText(String.valueOf(f)); + } + + /** Sets a float-value with N digits into the NumberField. */ + public void setFloat(float f, int N) { + setText(String.format(Locale.ENGLISH, "%." + N + "f", f)); + } + + /** Sets an int-value into the NumberField. */ + public void setInt(int i) { + setText(String.valueOf(i)); + } + + /** Sets a long-value into the NumberField. */ + public void setLong(long l) { + setText(String.valueOf(l)); + } + + /** Clears the NumberField */ + public void clear() { + setText(""); + } + + protected void processKeyEvent(KeyEvent e) { + super.processKeyEvent(e); + if (isNumeric() || getText().equals("-") || + getText().equals("") || getText().equals(".")) + setBackground(Color.white); + else + setBackground(Color.red); + } + +} \ No newline at end of file diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/package.bluej b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/package.bluej new file mode 100644 index 0000000..7c6db23 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jeclasses/package.bluej @@ -0,0 +1,79 @@ +#BlueJ package file +editor.fx.0.height=738 +editor.fx.0.width=816 +editor.fx.0.x=275 +editor.fx.0.y=0 +objectbench.height=93 +objectbench.width=760 +package.divider.horizontal=0.6 +package.divider.vertical=0.8003992015968064 +package.editor.height=394 +package.editor.width=649 +package.editor.x=359 +package.editor.y=52 +package.frame.height=600 +package.frame.width=800 +package.numDependencies=0 +package.numTargets=8 +package.showExtends=true +package.showUses=true +project.charset=windows-1252 +readme.height=58 +readme.name=@README +readme.width=47 +readme.x=10 +readme.y=10 +target1.height=50 +target1.name=JEClassLoader +target1.showInterface=false +target1.type=ClassTarget +target1.width=110 +target1.x=10 +target1.y=90 +target2.height=62 +target2.name=ch +target2.type=PackageTarget +target2.width=80 +target2.x=110 +target2.y=10 +target3.height=62 +target3.name=je +target3.type=PackageTarget +target3.width=80 +target3.x=200 +target3.y=10 +target4.height=50 +target4.name=JEControl +target4.showInterface=false +target4.type=ClassTarget +target4.width=80 +target4.x=120 +target4.y=90 +target5.height=50 +target5.name=Eval +target5.showInterface=false +target5.type=ClassTarget +target5.width=80 +target5.x=10 +target5.y=150 +target6.height=50 +target6.name=InOut +target6.showInterface=false +target6.type=ClassTarget +target6.width=80 +target6.x=100 +target6.y=150 +target7.height=50 +target7.name=NumberField +target7.showInterface=false +target7.type=ClassTarget +target7.width=110 +target7.x=190 +target7.y=150 +target8.height=50 +target8.name=JNumberField +target8.showInterface=false +target8.type=ClassTarget +target8.width=110 +target8.x=10 +target8.y=210 diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jnumberfield.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jnumberfield.java new file mode 100644 index 0000000..fd41a78 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/jnumberfield.java @@ -0,0 +1,129 @@ +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import java.util.*; + +/** +Swing-component for input and output of numeric values. +*/ + +public class JNumberField extends JTextField { + + /** constructor for a JNumberField */ + public JNumberField() { + enableEvents(AWTEvent.KEY_EVENT_MASK); + } + + /** Gets a double-value from the JNumberField. */ + public double getDouble() { + Double d = new Double(getText()); + return d.doubleValue(); + } + + /** Gets a float-value from the JNumberField. */ + public float getFloat() { + Double d = new Double(getText()); + return d.floatValue(); + } + + /** Gets an int-value from the JNumberField. */ + public int getInt() { + Double d = new Double(getText()); + return d.intValue(); + } + + /** Gets a long-value from the JNumberField. */ + public long getLong() { + Double d = new Double(getText()); + return d.longValue(); + } + + /** Checks wether the JNumberField contains a valid numeric value. */ + public boolean isNumeric() { + final String Digits = "(\\p{Digit}+)"; + final String HexDigits = "(\\p{XDigit}+)"; + // an exponent is 'e' or 'E' followed by an optionally + // signed decimal integer. + final String Exp = "[eE][+-]?"+Digits; + final String fpRegex = + ("[\\x00-\\x20]*"+ // Optional leading "whitespace" + "[+-]?(" + // Optional sign character + "NaN|" + // "NaN" string + "Infinity|" + // "Infinity" string + + // A decimal floating-point string representing a finite positive + // number without a leading sign has at most five basic pieces: + // Digits . Digits ExponentPart FloatTypeSuffix + // + // Since this method allows integer-only strings as input + // in addition to strings of floating-point literals, the + // two sub-patterns below are simplifications of the grammar + // productions from the Java Language Specification, 2nd + // edition, section 3.10.2. + + // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt + "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+ + + // . Digits ExponentPart_opt FloatTypeSuffix_opt + "(\\.("+Digits+")("+Exp+")?)|"+ + + // Hexadecimal strings + "((" + + // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt + "(0[xX]" + HexDigits + "(\\.)?)|" + + + // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt + "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" + + + ")[pP][+-]?" + Digits + "))" + + "[fFdD]?))" + + "[\\x00-\\x20]*");// Optional trailing "whitespace" + + return java.util.regex.Pattern.matches(fpRegex, getText()); + } + + /** Sets a double-value into the JNumberField. */ + public void setDouble(double d) { + setText(String.valueOf(d)); + } + + /** Sets a double-value with N digits into the JNumberField. */ + public void setDouble(double d, int N) { + setText(String.format(Locale.ENGLISH, "%." + N + "f", d)); + } + + /** Sets a float-value into the JNumberField. */ + public void setFloat(float f) { + setText(String.valueOf(f)); + } + + /** Sets a float-value with N digits into the JNumberField. */ + public void setFloat(float f, int N) { + setText(String.format(Locale.ENGLISH, "%." + N + "f", f)); + } + + /** Sets an int-value into the JNumberField. */ + public void setInt(int i) { + setText(String.valueOf(i)); + } + + /** Sets a long-value into the JNumberField. */ + public void setLong(long l) { + setText(String.valueOf(l)); + } + + /** Clears the JNumberField */ + public void clear() { + setText(""); + } + + protected void processKeyEvent(KeyEvent e) { + super.processKeyEvent(e); + if (isNumeric() || getText().equals("-") || + getText().equals("") || getText().equals(".")) + setBackground(Color.white); + else + setBackground(Color.red); + } + +} \ No newline at end of file diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/meta-inf/manifest.mf b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/meta-inf/manifest.mf new file mode 100644 index 0000000..39b10ac --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/meta-inf/manifest.mf @@ -0,0 +1,5 @@ +Manifest-Version: 1.0 +Class-Path: JEClasses.jar jdom-1.1.3.jar +Created-By: 1.8.0_161 (Oracle Corporation) +Main-Class: BreakVigenere + diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/numberfield.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/numberfield.java new file mode 100644 index 0000000..ca7ef01 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_autokorrelation/numberfield.java @@ -0,0 +1,128 @@ +import java.awt.*; +import java.awt.event.*; +import java.util.*; + +/** +AWT-component for input and output of numeric values. +*/ + +public class NumberField extends TextField { + + /** constructor for a NumberField */ + public NumberField() { + enableEvents(AWTEvent.KEY_EVENT_MASK); + } + + /** Gets a double-value from the NumberField. */ + public double getDouble() { + Double d = new Double(getText()); + return d.doubleValue(); + } + + /** Gets a float-value from the NumberField. */ + public float getFloat() { + Double d = new Double(getText()); + return d.floatValue(); + } + + /** Gets an int-value from the NumberField. */ + public int getInt() { + Double d = new Double(getText()); + return d.intValue(); + } + + /** Gets a long-value from the NumberField. */ + public long getLong() { + Double d = new Double(getText()); + return d.longValue(); + } + + /** Checks wether the NumberField contains a valid numeric value. */ + public boolean isNumeric() { + final String Digits = "(\\p{Digit}+)"; + final String HexDigits = "(\\p{XDigit}+)"; + // an exponent is 'e' or 'E' followed by an optionally + // signed decimal integer. + final String Exp = "[eE][+-]?"+Digits; + final String fpRegex = + ("[\\x00-\\x20]*"+ // Optional leading "whitespace" + "[+-]?(" + // Optional sign character + "NaN|" + // "NaN" string + "Infinity|" + // "Infinity" string + + // A decimal floating-point string representing a finite positive + // number without a leading sign has at most five basic pieces: + // Digits . Digits ExponentPart FloatTypeSuffix + // + // Since this method allows integer-only strings as input + // in addition to strings of floating-point literals, the + // two sub-patterns below are simplifications of the grammar + // productions from the Java Language Specification, 2nd + // edition, section 3.10.2. + + // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt + "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+ + + // . Digits ExponentPart_opt FloatTypeSuffix_opt + "(\\.("+Digits+")("+Exp+")?)|"+ + + // Hexadecimal strings + "((" + + // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt + "(0[xX]" + HexDigits + "(\\.)?)|" + + + // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt + "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" + + + ")[pP][+-]?" + Digits + "))" + + "[fFdD]?))" + + "[\\x00-\\x20]*");// Optional trailing "whitespace" + + return java.util.regex.Pattern.matches(fpRegex, getText()); + } + + /** Sets a double-value into the NumberField. */ + public void setDouble(double d) { + setText(String.valueOf(d)); + } + + /** Sets a double-value with N digits into the NumberField. */ + public void setDouble(double d, int N) { + setText(String.format(Locale.ENGLISH, "%." + N + "f", d)); + } + + /** Sets a float-value into the NumberField. */ + public void setFloat(float f) { + setText(String.valueOf(f)); + } + + /** Sets a float-value with N digits into the NumberField. */ + public void setFloat(float f, int N) { + setText(String.format(Locale.ENGLISH, "%." + N + "f", f)); + } + + /** Sets an int-value into the NumberField. */ + public void setInt(int i) { + setText(String.valueOf(i)); + } + + /** Sets a long-value into the NumberField. */ + public void setLong(long l) { + setText(String.valueOf(l)); + } + + /** Clears the NumberField */ + public void clear() { + setText(""); + } + + protected void processKeyEvent(KeyEvent e) { + super.processKeyEvent(e); + if (isNumeric() || getText().equals("-") || + getText().equals("") || getText().equals(".")) + setBackground(Color.white); + else + setBackground(Color.red); + } + +} \ No newline at end of file diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/balkendiagramm.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/balkendiagramm.java new file mode 100644 index 0000000..d64c99b --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/balkendiagramm.java @@ -0,0 +1,76 @@ +import java.awt.*; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import javax.swing.*; + +public class Balkendiagramm extends JPanel { + + private double gesamt; + private double haeufigkeit[]; + private double maxwert; + private int verschiebung = 0; + private double haeufigkeit_de[] = { 0.0651, 0.0189, 0.0306, 0.0508, 0.174, 0.0166, 0.0301, 0.0476,0.0755, 0.0027, 0.0121,0.0344,0.0252,0.0978,0.0251,0.0079,0.0002,0.07,0.0727,0.0615,0.0435,0.067,0.0189,0.0003,0.0004,0.0113}; + + + public void setHaeufigkeit(double haeufigkeit[]) { + + this.haeufigkeit = haeufigkeit; + gesamt = 0; + for (int i = 0; i < 26; i++) { + gesamt += haeufigkeit[i]; + } + for (int i = 0; i < 26; i++) { + haeufigkeit[i] /= gesamt; + } + maxwert = 0.20; + + } + + public void setVerschiebung(int verschiebung) { + if (verschiebung >= 0 && verschiebung < 26) { + this.verschiebung = verschiebung; + + } else { + this.verschiebung = 0; + } // end of if-else + } + + public void paintComponent(Graphics g) { + Graphics2D g2 = (Graphics2D) g; + super.paintComponent(g2); + Dimension d = getSize(null); + //Skalieren auf 100x100 mit y-Invertierung + g2.scale(d.getWidth() / 120.0, -d.getHeight() / 100.0); + // Ursprung verschieben nach (15,-90) + g2.translate(5, -90); + // Linien etwas breiter + g2.setStroke(new BasicStroke(1.5f)); + // x-Achse zeichnen + g2.draw(new Line2D.Double(0, 0, 104, 0)); + // Linien etwas dünner + g2.setStroke(new BasicStroke(0.5f)); + // Deutsche Häufigkeit grau + g2.setColor(Color.darkGray); + // Säulen zeichnen + for (int i = 0; i < 26; i++) { + g2.fill(new Rectangle2D.Double(4 * i+1, 0, 2, haeufigkeit_de[i] / maxwert *100)); + } + // Texthäufigkeit blau + g2.setColor(Color.blue); + // Säulen zeichnen + for (int i = 0; i < 26; i++) { + g2.fill(new Rectangle2D.Double(4 * i, 0, 2, haeufigkeit[(i+verschiebung)%26] / maxwert *100)); + } + // y-Invertierung rückgängig machen + g2.scale(1, -1); + // Beschriftung x-Achse + String xAchse = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + // Fontgröße für Beschriftung ändern + g2.setColor(Color.black); + g2.setFont(g2.getFont().deriveFont(5f)); + for (int i = 0; i < 26; i++) { + g2.drawString(xAchse.substring(i, i + 1), + i * 4, +6); + } + } + } diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/baustein.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/baustein.java new file mode 100644 index 0000000..55954a1 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/baustein.java @@ -0,0 +1,42 @@ +public class Baustein implements Comparable{ + + // Anfang Attribute + private String zeichen; + private int anzahl; + // Ende Attribute + + public Baustein(String zeichen) { + super(); + anzahl = 1; + this.zeichen = zeichen; + } + // Anfang Methoden + + + + public int compareTo(Baustein c) { + + int anz = c.getAnzahl(); + + //ascending order + return anz - this.anzahl; + + //descending order + //return compareQuantity - this.quantity; + + } + public String getZeichen() { + return zeichen; + } + + public int getAnzahl() { + return anzahl; + } + + public void incAnzahl() { + anzahl++; + } + + + // Ende Methoden +} \ No newline at end of file diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/breakvigenere.jar b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/breakvigenere.jar new file mode 100644 index 0000000..37e51fd Binary files /dev/null and b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/breakvigenere.jar differ diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/breakvigenere.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/breakvigenere.java new file mode 100644 index 0000000..d150a48 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/breakvigenere.java @@ -0,0 +1,498 @@ +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import javax.swing.event.*; +import javax.swing.table.*; +import java.util.ArrayList; +import java.util.Collections; +import javax.swing.text.DefaultHighlighter; +import javax.swing.text.Highlighter; +import javax.swing.text.Highlighter.HighlightPainter; +import javax.swing.text.BadLocationException; + + +/** + * + * Programm zur Kryptoanalyse + * von monoalphabetischen Substitutionen + * + * @version 1.0 from 23.11.2016 + * @author Thomas Schaller + */ + +public class BreakVigenere extends JFrame { + // Anfang Attribute + private JTextField[] buchstabe; + + private JButton jButton1 = new JButton(); + private JButton jBBuchstabe = new JButton(); + private Balkendiagramm bdText = new Balkendiagramm(); + + private JTextArea jTAKrypto = new JTextArea(""); + private JScrollPane jTAKryptoScrollPane = new JScrollPane(jTAKrypto); + private JTextArea jTAKlartext = new JTextArea(""); + private JScrollPane jTAKlartextScrollPane = new JScrollPane(jTAKlartext); + private JLabel jLKryptotext = new JLabel(); + private JLabel jLKlartext = new JLabel(); + private JLabel jLCopyright = new JLabel(); + private JTextArea jTAKorrelation = new JTextArea(""); + private JScrollPane jTAKorrelationScrollPane = new JScrollPane(jTAKorrelation); + private JTextArea jTAKorrelation2 = new JTextArea(""); + private JScrollPane jTAKorrelation2ScrollPane = new JScrollPane(jTAKorrelation2); + private JSpinner jSpVerschiebung = new JSpinner(); + private SpinnerNumberModel jSpVerschiebungModel = new SpinnerNumberModel(0, 0, 99, 1); + private JNumberField jNFAnzahlTreffer = new JNumberField(); + private JLabel lVerschiebung = new JLabel(); + private JLabel lAnzahlgleicherBuchstaben = new JLabel(); + private JButton jButtonKas = new JButton(); // neu mkl + private int lv, lb, rv, rb; + // Ende Attribute + + public BreakVigenere (String title) { + super (title); + setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); + int frameWidth = 1013; + int frameHeight = 662; + setSize(frameWidth, frameHeight); + Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); + int x = (d.width - getSize().width) / 2; + int y = (d.height - getSize().height) / 2; + setLocation(x, y); + Container cp = getContentPane(); + cp.setLayout(null); + // Anfang Komponenten + + jTAKryptoScrollPane.setBounds(16, 24, 665, 105); + jTAKryptoScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); + + jTAKrypto.setLineWrap(true); + jTAKrypto.setFont(new Font("Courier New", Font.BOLD, 16)); + jTAKrypto.setText("JMLQMFNSKIOQNXGYZOQEBJLUKXHFCXGVLGBMREAJZCAEKVIFOWJLMFGJLPQZJIYHQQHYJLAFGFLRLQYESTPMHIAWQZFIPPMZMIZGPDOIIIVGTHIIQVKHLVHQOPLNMIKMSWCYKMUIVBREADEQOXLVVMILSMVWYZLVAONSIIVIKVKIVPOIZIEGXHLHCDILKIVPKYAWKTKRIIVQJMRXQZKVHFBVULHRVQYXYMBTKQPYAUSNHLZUSJUJBQTFHRLEKMUIAUTPHXMUTMZGPQXWWVIONINIAONVPIJQTIUWMONWIRLUMIUAMDQIZTWXEKYEXTOELPQNXMZIFEKGOWJONIYDCDVSSCODGTOMMMTKLKMNKRPRLQSRHGPEKMUIUFUHLZMDLJLRBXOGOXMZHYJLAONPBKMDBSYRIONNLHMYKMUDMXTIUOTMXXLBBNAGOWBMHIUDCYTGOWBQTESTPMHIAMVEKMUIZFGFBPINKVGYOQNIUYVPYSHPTQBIYJONGVLRIXVLHFMFKEBWHGTYADMZJETMBQXJHRLQXHPIXDUKYIAEOZLTWXEESTPMHIAMAONIJLQRLVPIZGTKHFMDKWDEZZUGOIQZLIZXMEBIYJITXIUSPZKWJLTEYISHQQYIYACDJIPQRMNVCSUUZESMMZOWJLMZQVFTBARSNIVSOSCEVNGXAMAFGFLPTMYSJEQZLSYQMUTIZZWYBIYWKTRWZPMDLVLMHGCLSIVPKRRIVZCSYXAAJIYIQZKWRIVZYEADMEBSYKMEILSEOQTKLVVQARKOZKVXVKZMVLPWKTYGOAIONHHPMUILADCQXVHXMZCYYHMZJETETEREAIQZOWJLMEORUWXDILLFMZAXGXEUKFLMABOISWEQOWLZQDZZAMWYTMHTIDKRAETXKWNIPAXGOXLQXXJLBUMOLMBPOIIYKTYXHFMZJIZOMZTWHXHQYFLWBUSQLRLUKVLMPQTJVPOQORKIZPOICIZEILPILQTIUETBNEIIBQGYZHMDZEIYTMGYZKMINPAAMDJIUQAEKRZMVPGPSIDQXFYECONXHPAAKMUQIXHIUYBLZAVVLQTLPIZZGGOOTMXXLBBNAGOWBMHIUWWNKKPRVFSEUAQQJIYZWZBSYRMENEUHMXZWPGPEUQPXCYKMUIXQXMVHQEILLTWXEESTPMHIAMAONIZYJEZMAYBUURTMBUSFLMABOISIQZKVWIZUUHLZWZCLYIVPZVPXPQSMBWAUILHYNHKVZGPAHIUIAFGRKEZPGPWLINKXLFMEILYRSFKESWWPOINIEANRAIIXVLHFMFOWJLMDKMOIVRUPNILQXFBGPEZEIIVHKVDIVPKXLFMXREZSJQXIPXAHKVDVNQRXLETBNEIIBQJMLIZMRPLVLUTKZMVHUPBXWDOWJLETRXLFMUJIUQWZUESTPMHIAMAONIUWCNYXPXCFOSUWUQZLVHMZCEYHQQYWJLWZYIPXTMTKLQJXOGOKZGTHZXHXOGOLIFZIKMMEHIYIQFYMTNITXESWWXGRNIDAXXYMBTKQPYAGTHIITXGWVHMDOXHPQQTMZGPQMISIPDZISIWZHEAXQEZEHPJQXXPIUBLSOPMZKVZGPXAKCSZPOIHPXTGFLXMZGGONMIKMSWLDKMVHMDBMLVEDZIYRHGCIJLAQRRHPAYKGOEVUYGOIATOPMWUUZXLPPMZXLIZTOIYDCPOIUEKTOLTFMZGRUXMMRFLVBUYGOIQNKIYJCZJIUIQZKGOMNRXMLVAONIPFMNKWAIPQTHGYUQOWAECEFALMZGTHLRUQZESPAONIPFMZJMLECRKMUIZSKQLMVEGQLRIONWLWQFFIUYVPYSCIZNARKIVEORKHIEYWPGPPOIRPMUTIYIIGLHLVODKVLRLDKLLRSMTRUEPQFYLMVVGLYLCZJIYXABZIYMUVGLYZMDGPSKMYKMUIZFKHLVVQGTVPQFGRPWKTKKLPMTXXLKQABEURQNGXAMAFGHLPTMVSYXIMRFLVBUYZLVEDLISXMMRTOEJQZIBRLNKPSEAAYOLRVIUVAYVPYGOYNPOIWSTKGPWLINKXPWKTKGOMNRXMLVCZMQPXDQXAYJMXZITETBNEIIBGTHZGPXYWLPPUKVGYEGXHLETNKVAMAEILLMJQHIUYBLZHPIVMILQILQSFBGPEZEIIVLAHYIPQTAHVQYPEOVODOJMHMDLVHRHAYIIPIUYIKIDUMIUVMBUVAEAUJILECRARKWKTRYNZWDYXHXBPKVHPJQXXPWKTKMIILUKXHFCXGVLGBMJIZXZUZLLQQGYDBZMDCIUHMZJEIIQMHIYEVPKVZETEOQVVQSORHPDQXAYJMXZIHPXTGFLXMQORGYBDGKLRLUKWLVSDETASODGTOMAONWAEZWKZVVAONPHKDUMIUVMEMIYMMFORKIVRUPNIVPKRQEPDNYUHMDZIUMVHKVNIAEKROIQFARKHQQAVZTZZMPPGPHURAVQFNITMCEBSYKMEILSEOQTITIBTUHLACDJIBRBQXHLQVMSIUZQSKRYIKTOJMVMNKOHRVF"); + jTAKrypto.addKeyListener(new KeyAdapter() { + public void keyTyped(KeyEvent evt) { + jTAKrypto_KeyTyped(evt); + } + }); + cp.add(jTAKryptoScrollPane); + + jTAKlartextScrollPane.setBounds(16, 480, 665, 105); + jTAKlartext.setLineWrap(true); + jTAKlartext.setFont(new Font("Courier New", Font.BOLD, 16)); + cp.add(jTAKlartextScrollPane); + jLKryptotext.setText("Kryptotext"); + jLKryptotext.setBounds(16, 3, 110, 20); + cp.add(jLKryptotext); + jLKlartext.setText("Klartext"); + jLKlartext.setBounds(16, 456, 110, 20); + cp.add(jLKlartext); + jLCopyright.setText("(cc) Schaller (ZPG Informatik) - V1.0 (2017)"); + jLCopyright.setBounds(128, 584, 419, 33); + jLCopyright.setHorizontalAlignment(SwingConstants.CENTER); + jLCopyright.setFont(new Font("Dialog", Font.PLAIN, 12)); + cp.add(jLCopyright); + + // -------------------------------- Tab 1 --------------------------------------------- + JPanel tab1 = new JPanel(); + tab1.setLayout(null); + lv = -1; lb = -1; rv = -1; rb=-1; + jTAKorrelationScrollPane.setBounds(16, 40, 320, 110); + jTAKorrelationScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); + jTAKorrelation.setText(jTAKrypto.getText()); + jTAKorrelation.setLineWrap(true); + jTAKorrelation.setEditable(false); + jTAKorrelation.setEnabled(true); + jTAKorrelation.setFont(new Font("Courier New", Font.BOLD, 16)); + jTAKorrelation.addMouseListener(new MouseListener() { + public void mousePressed(MouseEvent e) { + removeSearchHighlight(); + } + + public void mouseReleased(MouseEvent e) { + addSearchHighlight(); + } + + public void mouseEntered(MouseEvent e) { + } + + public void mouseExited(MouseEvent e) { + } + + public void mouseClicked(MouseEvent e) { + } + + }); + + tab1.add(jTAKorrelationScrollPane); + jTAKorrelation2ScrollPane.setBounds(340, 40, 320, 110); + jTAKorrelation2ScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); + jTAKorrelation2.setText(jTAKrypto.getText()); + jTAKorrelation2.setLineWrap(true); + jTAKorrelation2.setEditable(false); + jTAKorrelation2.setEnabled(true); + jTAKorrelation2.setFont(new Font("Courier New", Font.BOLD, 16)); + jTAKorrelation2.addMouseListener(new MouseListener() { + public void mousePressed(MouseEvent e) { + //removeSearchHighlight(); + } + + public void mouseReleased(MouseEvent e) { + //addSearchHighlight(); + } + + public void mouseEntered(MouseEvent e) { + } + + public void mouseExited(MouseEvent e) { + } + + public void mouseClicked(MouseEvent e) { + selectHighlight(); + } + + }); + + tab1.add(jTAKorrelation2ScrollPane); + + + JLabel l = new JLabel(); + l.setBounds(350, 155, 162, 20); + l.setText("Vermutete Schlüssellänge:"); + tab1.add(l); + + jSpVerschiebung.setBounds(520, 152, 41, 25); + jSpVerschiebung.setValue(1); + jSpVerschiebung.setModel(jSpVerschiebungModel); + jSpVerschiebung.addChangeListener(new ChangeListener() { + public void stateChanged(ChangeEvent evt) { + jSpVerschiebung_StateChanged(evt); + } + }); + tab1.add(jSpVerschiebung); + + + lVerschiebung.setBounds(200, 10, 310, 20); + lVerschiebung.setText("Angriff auf die Schlüssellänge mit Kasiski-Test"); + tab1.add(lVerschiebung); + l = new JLabel(); + l.setBounds(14, 177, 640, 80); + l.setText("Markiere im linken Textfeld eine Buchstabenfolge. Wird diese dunkel kommt sie mehrmals im Text vor. Suche sie im rechten Textfeld und wähle ein Auftreten aus. Du siehst dann, wie weit die beiden auseinander liegen. Wiederhole dies mehrfach. Die Primfaktoren, die häufig vorkommen, stecken dann vermutlich auch in der Schlüssellänge. Stelle die Schlüssellänge entsprechend ein und gehe zu Schritt 2."); + tab1.add(l); + lAnzahlgleicherBuchstaben.setBounds(16, 155, 300, 20); + lAnzahlgleicherBuchstaben.setText("Abstand: "); + lAnzahlgleicherBuchstaben.setHorizontalAlignment(JLabel.CENTER); + tab1.add(lAnzahlgleicherBuchstaben); + + jButtonKas.setBounds(16, 10, 150, 25); // neu mkl + jButtonKas.setText("Text übernehmen"); + jButtonKas.setMargin(new Insets(2, 2, 2, 2)); + jButtonKas.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent evt) { + jButtonKas_ActionPerformed(evt); + } + }); + tab1.add(jButtonKas); + // ---------------------------------- Tab 2 ------------------------------- + JPanel tab2 = new JPanel(); + tab2.setLayout(null); + + l = new JLabel(); + l.setBounds(16, 10, 310, 20); + l.setText("Angriff auf die Teiltexte mit Häufigkeitsanalyse"); + tab2.add(l); + + l = new JLabel(); + l.setBounds(380, 10, 300, 25); + l.setText("Buchstabenhäufigkeit Deutsch"); + tab2.add(l); + + l = new JLabel(); + l.setBounds(16, 40, 300, 25); + l.setText("Schlüssel"); + tab2.add(l); + + l = new JLabel(); + l.setBounds(16, 100, 300, 100); + l.setText("Versuche den Schlüssel zu knacken, indem du in jedem Teiltext die Häufigkeitsverteilung der Buchstaben mit der üblichen Häufigkeitsverteilung in deutschen Texten in Einklang bringst."); + tab2.add(l); + + double[] h = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + bdText.setBounds(380,40,300,180); + bdText.setHaeufigkeit(h); + tab2.add(bdText); + + jButton1.setBounds(16, 210, 193, 25); + jButton1.setText("Entschlüsselung versuchen"); + jButton1.setMargin(new Insets(2, 2, 2, 2)); + jButton1.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent evt) { + jButton1_ActionPerformed(evt); + } + }); + tab2.add(jButton1); + + + + // Ende Komponenten + buchstabe = new JTextField[26]; + for (int i = 0; i<26 ;i++ ) { + buchstabe[i] = new JTextField(); + buchstabe[i].setBounds(16+i*25, 65, 25, 25); + buchstabe[i].setText("A"); + buchstabe[i].setHorizontalAlignment(SwingConstants.CENTER); + buchstabe[i].setVisible(false); + buchstabe[i].addFocusListener(new FocusAdapter() { + public void focusGained(FocusEvent evt) { + highlightKryptotext(evt); + } + }); + buchstabe[i].addKeyListener(new KeyAdapter() { + public void keyReleased(KeyEvent e) { + e.consume(); + } + + public void keyTyped(KeyEvent e) { + e.consume(); + } + + public void keyPressed(KeyEvent e) { + JTextField textField = (JTextField) e.getSource(); + String text = textField.getText(); + if ((e.getKeyChar()>='a' && e.getKeyChar()<='z') ||(e.getKeyChar()>='A' && e.getKeyChar()<='Z')) { + text = ""+e.getKeyChar(); + } + if (e.getKeyChar()==' ' || e.getKeyChar() == KeyEvent.VK_BACK_SPACE || e.getKeyChar() == KeyEvent.VK_DELETE) { + text = ""; + + } // end of if + textField.setText(text.toUpperCase()); + e.consume(); + if (textField.getText().length()>0) { + bdText.setVerschiebung((int) textField.getText().charAt(0)-65); + + } else { + bdText.setVerschiebung(0); + } // end of if-else + bdText.repaint(); + + } // end of if + + }); + + tab2.add(buchstabe[i]); + } // end of for + + JTabbedPane tabpane = new JTabbedPane + (JTabbedPane.TOP,JTabbedPane.SCROLL_TAB_LAYOUT ); + tabpane.setBounds(10,140,672,290); + + tabpane.addTab("Schritt 1", tab1); + tabpane.addTab("Schritt 2", tab2); + cp.add(tabpane); + + setResizable(false); + setVisible(true); + } + + + + // Anfang Methoden + + public void jButtonKas_ActionPerformed(ActionEvent evt) { // neu mkl + //String krypttext = jTAKrypto.getText().toUpperCase(); + jTAKorrelation.setText(jTAKrypto.getText().toUpperCase()); + jTAKorrelation2.setText(jTAKrypto.getText().toUpperCase()); + jTAKlartext.setText(""); + } + + public void jButton1_ActionPerformed(ActionEvent evt) { + String krypttext = jTAKrypto.getText().toUpperCase(); + String klartext=""; + int c = 0; + int diff = (Integer) jSpVerschiebung.getValue(); + for (int i=0;i=65 && asc<=90) { + h[asc-65]++; + } + } // end of if + c++; + } // end of if + } + bdText.setHaeufigkeit(h); + if (buchstabe[start].getText().length()>0) { + bdText.setVerschiebung((int) buchstabe[start].getText().charAt(0)-65); + + } else { + bdText.setVerschiebung(0); + } // end of if-else + bdText.repaint(); + } catch(BadLocationException e) {} + } + + + public void addSearchHighlight() { + String search = jTAKorrelation.getSelectedText(); + if (search !=null) { + lv = jTAKorrelation.getSelectionStart(); + lb = jTAKorrelation.getSelectionEnd(); + Highlighter h2 = jTAKorrelation.getHighlighter(); + h2.removeAllHighlights(); + try{ + + search = search.toUpperCase(); + System.out.println(search); + + Highlighter h1 = jTAKorrelation2.getHighlighter(); + h1.removeAllHighlights(); + String krypto = jTAKorrelation.getText().toUpperCase(); + + int c = krypto.indexOf(search,0); + int anz = 0; + while (c>=0) { + h1.addHighlight(c, c+search.length(), + new DefaultHighlighter.DefaultHighlightPainter(new Color(200,200,255))); + anz++; + c = krypto.indexOf(search, c+1); + } // end of while + if (anz>1){ + h2.addHighlight(lv, lv+search.length(), + new DefaultHighlighter.DefaultHighlightPainter(new Color(100,100,255))); + } else { + h2.addHighlight(lv, lv+search.length(), + new DefaultHighlighter.DefaultHighlightPainter(new Color(200,200,255))); + } // end of if-else + + + + } catch(BadLocationException e) {} + + } + + } + + public ArrayList primfaktoren(int zahl) { + int w = 2; + ArrayList list = new ArrayList(); + while (zahl != 1) { + if(zahl % w == 0) { + list.add(w); + zahl /= w; + } + else { + w++; + } // end of if-else + + + } // end of while + return list; + } + + + public void selectHighlight() { + int caretpos = jTAKorrelation2.getCaretPosition(); + Highlighter h2 = jTAKorrelation2.getHighlighter(); + Highlighter.Highlight[] highlights = h2.getHighlights(); + for (Highlighter.Highlight h : highlights) { + if(caretpos >= h.getStartOffset() && caretpos < h.getEndOffset()) + { + int diff = Math.abs(lv - h.getStartOffset()); + if (diff == 0) { + lAnzahlgleicherBuchstaben.setText("Abstand: "+diff); + + } else { + lAnzahlgleicherBuchstaben.setText("Abstand: "+diff+" => Primfaktoren "+ primfaktoren(diff).toString()); + + } // end of if-else + + } + + } // end of for + } + + + public void removeSearchHighlight() { + Highlighter h1 = jTAKorrelation.getHighlighter(); + h1.removeAllHighlights(); + lAnzahlgleicherBuchstaben.setText("Abstand: "); + } + + + public void jSpVerschiebung_StateChanged(ChangeEvent evt) { + + int v = (Integer) (jSpVerschiebung.getValue()); + + + for (int i=0; iRegula Hoefer-Isenegger
+@version 0.1 +*/ +public class LineRenderer { + private Point2D.Double point; + private Turtle turtle; + + LineRenderer(Turtle turtle) { + this.point = new Point2D.Double(); + this.turtle = turtle; + } + + /** Initialisation with coordinates x and + y. + */ + public void init(double x, double y) { + this.point.setLocation(x, y); + } + /** Same as init(double x, double y), but with a Point2D.Double + argument for convenience + */ + public void init(Point2D.Double p) { + this.init(p.x, p.y); + } + /** Get the current x-coordinate in screen coordinates.*/ + private double getX(){ + return (toScreenCoords(point)).getX(); + } + /** Get the current x-coordinate in screen coordinates.*/ + private double getY(){ + return toScreenCoords(point).getY(); + } + /** Calls the clipLineTo and wrapLineTo methods, according + to the turtle's edge behavior. + + Only overwrite this method when working with another + (one that you have defined) edge behaviour.
+ If you mean to change the manner of drawing lines, do this in + the methods clipLineTo() and wrapLineTo(). + @see #clipLineTo + @see #wrapLineTo + */ + protected void internalLineTo(double x, double y){ + Point2D.Double screenPos = toScreenCoords(x, y); + if(turtle.isClip()){ + clipLineTo(screenPos.getX(), screenPos.getY()); + } + if (turtle.isWrap()){ + wrapLineTo(screenPos.getX(), screenPos.getY()); + } + init(x,y); + } + /** Calls the internalLineTo(x,y), which does the actual painting. + */ + public void lineTo(double x, double y) { + internalLineTo(x, y); + } + /** Calls the internalLineTo(x,y), which does the actual painting. + + This method works the same way as lineTo(double x, double y), but + is added for convenience. + */ + public void lineTo(Point2D.Double p) { + internalLineTo(p.getX(), p.getY()); + } + /** Does the actual painting for clip mode. + + It works already with ScreenCoords! + For further comments cf. internalLineTo(double x, double y). + @see #internalLineTo + */ + protected void clipLineTo(double x, double y){ + turtle.getPlayground().lineTo(getX(), getY(), x, y, turtle.getPen()); + } + /** Does the actual painting for wrap mode. + + It works already with ScreenCoords! + For further comments cf. internalLineTo(double x, double y). + @see #internalLineTo + */ + protected void wrapLineTo(double x, double y){ + double dx = getX() - x; + double dy = getY() - y; + Point2D.Double start = new Point2D.Double(x, y); + Point2D.Double end = new Point2D.Double(start.x+dx, start.y+dy); + + intoPanel(start, end); + Point2D.Double tmp; + while ((tmp = calcIntersection(start.x, start.y, end.x, end.y)) != null){ + turtle.getPlayground().lineTo(start.x, start.y, tmp.getX(), tmp.getY(), turtle.getPen()); + start = tmp; + intoPanel(start, end); + dx = end.x - start.x; + dy = end.y - start.y; + } + + turtle.getPlayground().lineTo(start.x, start.y, end.x, end.y, turtle.getPen()); + } + /** Makes the coordinates fit into the Panel. + + Well, this is some sort of modulus calculation. + */ + private void intoPanel(Point2D.Double start, Point2D.Double end){ + int pWidth = turtle.getPlayground().getWidth(); + int pHeight = turtle.getPlayground().getHeight(); + while(start.x < 0){ + start.x += pWidth; + end.x += pWidth; + } + while (start.x > pWidth){ + start.x -= pWidth; + end.x -= pWidth; + } + if(start.x == 0 && end.x < start.x){ + start.x += pWidth; + end.x += pWidth; + } + if(start.x == pWidth && end.x > start.x){ + start.x -= pWidth; + end.x -= pWidth; + } + while(start.y < 0){ + start.y += pHeight; + end.y += pHeight; + } + while (start.y > pHeight){ + start.y -= pHeight; + end.y -= pHeight; + } + if(start.y == 0 && end.y < start.y){ + start.y += pHeight; + end.y += pHeight; + } + if(start.y == pHeight && end.y > start.y){ + start.y -= pHeight; + end.y -= pHeight; + } + + } + /** Intersection line with playground-edges + (startX / startY) MUST lie in the playground! + */ + private Point2D.Double calcIntersection(double startX, double startY, double endX, double endY){ + double dx = endX - startX; + double dy = endY - startY; + double W = turtle.getPlayground().getWidth(); + double H = turtle.getPlayground().getHeight(); + if(endX < 0){ + if((dy/dx <= startY/startX) && (dy/dx >= -(H-startY)/startX)){ // links + return new Point2D.Double(0, startY-startX*dy/dx); + } + } + else if(endX > W){ + if((dy/dx >= -startY/(W-startX)) && (dy/dx <= (H-startY)/(W-startX))){// rechts + return new Point2D.Double(W, startY+(W-startX)*dy/dx); + } + } + if(endY < 0){ // oben + return new Point2D.Double(startX-startY*dx/dy, 0); + } + else if(endY > H){ // unten + return new Point2D.Double(startX+(H-startY)*dx/dy, H); + } + else{ + return null; // Endpoint lies in the window + } + } + /** Calculates the screen coordinates of the turtle's actual + position according to the interpretation of the playground. + */ + private Point2D.Double toScreenCoords(double x, double y){ + return turtle.getPlayground().toScreenCoords(x, y); + } + /** Calculates the screen coordinates of the turtle's actual + position according to the interpretation of the playground. + + Added for convenience. + @see #toScreenCoords(double, double) + */ + private Point2D.Double toScreenCoords(Point2D.Double p){ + return turtle.getPlayground().toScreenCoords(p.x, p.y); + } +} + diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/pen.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/pen.java new file mode 100644 index 0000000..8fcc8b7 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/pen.java @@ -0,0 +1,268 @@ +// Pen.java + +// Copyright 2002 Regula Hoefer-Isenegger +// +// This file is part of The Java Turtle package (TJT) +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package ch.aplu.turtle; + +import ch.aplu.turtle.*; +import java.awt.Color; +import java.awt.BasicStroke; +import java.awt.Stroke; +import java.awt.Font; +import java.awt.GraphicsEnvironment; + +/** The Pen class provides anything used for drawing the lines, such as line width, + pen color, end caps, dashed lines, etc. + + @see java.awt.BasicStroke + + @author Regula Hoefer-Isenegger + @version 0.1.1 +*/ +public class Pen +{ + /* Attributes *********************************************/ + + /** The default font that is used when drawing Text. + + First argument must be one of "Serif", "SansSerif", "Monotyped", "Dialog" or "DialogInput" + to guarantee that this font exists on all systems. + + @see java.awt.Font for more information, e.g. on font styles. + + */ + public static Font DEFAULT_FONT = new Font("SansSerif", Font.PLAIN, 24); + private Color color; + private Color fillColor; + private BasicStroke stroke; + private Font font; + + /* Constructors *******************************************/ + /** Constructor with standard Color and standard Stroke. + + @see java.awt.BasicStroke + */ + public Pen(){ + color = Color.black; + setFillColor(Color.black); + stroke = new BasicStroke(); + font = DEFAULT_FONT; + } + /** Constructor with Color color and standard Stroke. + + @see java.awt.BasicStroke + */ + public Pen(Color color){ + this.color = color; + setFillColor(color); + stroke = new BasicStroke(); + font = DEFAULT_FONT; + } + /* Methods ************************************************/ + /** Query the Pens color.*/ + public Color getColor(){ + return color; + } + /** Set the Pens color.*/ + public void setColor(Color color){ + this.color = color; + } + /** Set the Pens fill color. + */ + public void setFillColor(Color color){ + this.fillColor = color; + } + /** Query the Pens fill color.*/ + public Color getFillColor(){ + return this.fillColor; + } + /** Get the Pens Stroke + + @see BasicStroke + @see Stroke + */ + public Stroke getStroke(){ + return stroke; + } + /** Query the Pens line width*/ + public float getLineWidth(){ + return stroke.getLineWidth(); + } + /** Query the Pens end cap style. + + @see java.awt.BasicStroke + */ + public int getEndCap(){ + return stroke.getEndCap(); + } + /** Query the Pens line join style. + + @see java.awt.BasicStroke + */ + public int getLineJoin(){ + return stroke.getLineJoin(); + } + /** Query the Pens miter limit style. + + @see java.awt.BasicStroke + */ + public float getMiterLimit(){ + return stroke.getMiterLimit(); + } + /** Query the Pens dash array. + + @see java.awt.BasicStroke + */ + public float[] getDashArray(){ + return stroke.getDashArray(); + } + /** Query the Pens dash phase. + + @see java.awt.BasicStroke + */ + public float getDashPhase(){ + return stroke.getDashPhase(); + } + + /** Set the Pens line width. */ + public void setLineWidth(float width){ + stroke = new BasicStroke((float)width, + stroke.getEndCap(), + stroke.getLineJoin(), + stroke.getMiterLimit(), + stroke.getDashArray(), + stroke.getDashPhase()); + } + /** Set the Pens end cap style. + + @see java.awt.BasicStroke + */ + public void setEndCap(int endCap){ + stroke = new BasicStroke(stroke.getLineWidth(), + endCap, + stroke.getLineJoin(), + stroke.getMiterLimit(), + stroke.getDashArray(), + stroke.getDashPhase()); + } + /** Set the Pens line join style. + + @see java.awt.BasicStroke + */ + public void setLineJoin(int join){ + stroke = new BasicStroke(stroke.getLineWidth(), + stroke.getEndCap(), + join, + stroke.getMiterLimit(), + stroke.getDashArray(), + stroke.getDashPhase()); + } + /** Set the Pens miter limit. + + @see java.awt.BasicStroke + */ + public void setMiterLimit(float miterlimit){ + stroke = new BasicStroke(stroke.getLineWidth(), + stroke.getEndCap(), + stroke.getLineJoin(), + miterlimit, + stroke.getDashArray(), + stroke.getDashPhase()); + } + /** Set the Pens dash array. + + @see java.awt.BasicStroke + */ + public void setDash(float[] dashArray){ + stroke = new BasicStroke(stroke.getLineWidth(), + stroke.getEndCap(), + stroke.getLineJoin(), + stroke.getMiterLimit(), + dashArray, + stroke.getDashPhase()); + } + /** Set the Pens dash phase. + + @see java.awt.BasicStroke + */ + public void setDashPhase(float dashPhase){ + stroke = new BasicStroke(stroke.getLineWidth(), + stroke.getEndCap(), + stroke.getLineJoin(), + stroke.getMiterLimit(), + stroke.getDashArray(), + dashPhase); + } + /** Provides information about the currently available font families (e.g. "Roman"). + Each font name is a string packed into a array of strings. + @see java.awt.Font for more information about font attributes etc. + */ + public static String[] getAvailableFontFamilies(){ + GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); + String s[] = ge.getAvailableFontFamilyNames(); + return s; + } + /** Change the font style. + + @see java.awt.Font for possible styles. + */ + public void setFontStyle(int style){ + font = font.deriveFont(style); + } + /** Change the font size (in points). + */ + public void setFontSize(int size){ + font = font.deriveFont((float)size); + } + /** Change the font size (in points). + You will probably only need the int version setFontSize(int). + */ + public void setFontSize(float size){ + font = font.deriveFont(size); + } + /** Query the size (in points, rounded to int) of the current font. + */ + public int getFontSize(){ + return font.getSize() ; + } + /** Change the font to the given one. + */ + public void setFont(Font f){ + font = f; + } + /** Query the current font. + */ + public Font getFont(){ + return font; + } +} + + + + + + + + + + + + + + diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/playground.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/playground.java new file mode 100644 index 0000000..6c0db5b --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/playground.java @@ -0,0 +1,789 @@ +// Playground.java + +// Copyright 2002 Regula Hoefer-Isenegger +// Major code modifications by Aegidius Pluess +// Adaption for the Java-Editor by Gerhard Röhner +// +// This file is part of The Java Turtle package (TJT) +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package ch.aplu.turtle; + +import ch.aplu.turtle.*; +import java.util.*; +import javax.swing.*; +import java.awt.*; +import java.awt.print.*; +import java.awt.geom.*; +import java.awt.image.*; +import java.awt.font.*; + +/** + A Playground is the Turtle's home, i.e. the Turtle lives + and moves in the Playground. + + ThePlayground is responsible for interpreting angle and position of the + Turtle and for choosing the correct turtle image and putting it on the right + spot of the Playground. This means: e.g. whenever you wish to switch the x- and y-axis, you + should do it in this class, and not in the Turtle class. + + @author Regula Hoefer-Isenegger + @version 0.1.1 +*/ + +public class Playground extends JPanel implements Printable { + + /** Hold the Turtles of this Playground. */ + private Vector turtles; + + /** Hold the offscreen buffer and graphics context + * where Turtle traces are drawn. + */ + private BufferedImage traceBuffer = null; + protected Graphics2D traceG2D = null; + private Dimension playgroundSize; + + /** Hold the offscreen buffer and graphics context + * of the Turtles images. + */ + private BufferedImage turtleBuffer = null; + protected Graphics2D turtleG2D = null; + + /** Flag to tell whether we have at least one Turtle shown. */ + private boolean isTurtleVisible = false; + + /** Flag to tell whether we use automatic repainting */ + private boolean isRepainting = true; + + /** The default background color. + */ + protected static Color DEFAULT_BACKGROUND_COLOR = Color.white; + + private double printerScale = 1; // Default printer scaling + private TPrintable traceCanvas; // Store ref to user class + private Graphics2D printerG2D = null; + private boolean isPrintScreen = false; // Indicate we are printing the playground + private double printerScaleFactor = 1.1; // Magnification factor for printer + + /** + * originX is the x-position of the cartesian coodinate system within the playground. + */ + public int originX; + + /** + * originY is the y-position of the cartesian coodinate system within the playground. + */ + public int originY; + + private static Color[] ColorArray = {Color.cyan, Color.red, Color.green, Color.blue, Color.yellow, + Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.black, Color.gray + }; + + /** + * Create a Playground with default background color. + * e.g. creates a new vector (which holds the + * Turtles), + */ + public Playground() { + turtles = new Vector(); + setDoubleBuffered(false); + setBackground(DEFAULT_BACKGROUND_COLOR); + initBuffers(new Dimension(100, 100)); + } + + /** + * Initializes the offscreen buffers and sets the size. + */ + protected void initBuffers(Dimension size) { + Color bkColor = getBackground(); + playgroundSize = size; + traceBuffer = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB); + traceG2D = traceBuffer.createGraphics(); + traceG2D.setColor(bkColor); + traceG2D.fillRect(0, 0, size.width, size.height); + traceG2D.setBackground(bkColor); + + turtleBuffer = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB); + turtleG2D = turtleBuffer.createGraphics(); + turtleG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + originX = size.width/2; + originY = size.height/2; + } + + /** Add a new Turtle to the Playground. + */ + public void add(Turtle turtle) { + turtles.add(turtle); + turtle.setPlayground(this); + int i = turtles.size(); + while (i > 10) { + i = i - 10; + } // end of while + turtle.init(ColorArray[i-1]); + toTop(turtle); + } + + public void setBounds(int x, int y, int width, int height) { + super.setBounds(x, y, width, height); + initBuffers(new Dimension(width, height)); + } + + /** Remove a Turtle from the Playground. + */ + public void remove(Turtle turtle) { + turtles.remove(turtle); + } + + /** Tell current number of Turtles in this Playground. + */ + public int countTurtles() { + return turtles.size(); + } + + /** Return the Turtle at index index. + */ + public Turtle getTurtle(int index) { + return turtles.elementAt(index); + } + + /** Move the given Turtle above all the others, then + paints all turtles. + @see #toTop + */ + public void paintTurtles(Turtle turtle) { + toTop(turtle); + paintTurtles(); + } + + /** Paint all turtles (calling paintComponent()) + */ + public void paintTurtles() { + isTurtleVisible = false; + Graphics2D g2D = getTurtleG2D(); + for (int i = 0; i < countTurtles(); i++) { + Turtle aTurtle = getTurtle(i); + if (!aTurtle.isHidden()) { + paintTurtle(aTurtle); + } + } + + // This is the main repaint call, when the turtle is + // moving (even when all turtles are hidden). + // Strange behaviour an slow Mac machines (pre J2SE 1.4 version): + // It happens that some turtle images are not completely redrawn. + // This is probably due to an improper handling of fast multiple repaint requests. + // Workaround: we wait a small amount of time (and give the thread away) + // (No visible slow down on new machines.) + + paintPlayground(); + if (printerG2D == null) { + //if (isRepainting) + //repaint(); + //paintPlayground(); + } + + if (isTurtleVisible) { + try { + Thread.currentThread().sleep(10); + } + catch (Exception e) {} + } + } + + + /** Paint the given Turtle. + * ( no repaint() ) + */ + public void paintTurtle(Turtle turtle) { + if (turtleBuffer == null){ + turtleBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); + turtleG2D = turtleBuffer.createGraphics(); + } + Graphics2D turtleGraphics = getTurtleG2D(); + turtle.getTurtleRenderer().paint(turtle._getX(), turtle._getY(), turtleGraphics); + isTurtleVisible = true; + } + + /** Put an image of the given Turtle in turtle buffer. + */ + protected void stampTurtle(Turtle turtle) { + turtle.clone(); + isTurtleVisible = true; + if (printerG2D == null) + repaint(); + } + + /** Draw a line from the point (x0, y0) to (x1, y1) + with the color of the given Pen. + */ + protected void lineTo(double x0, double y0, double x1, double y1, Pen pen) { + int ix0 = (int)Math.round(x0); + int iy0 = (int)Math.round(y0); + int ix1 = (int)Math.round(x1); + int iy1 = (int)Math.round(y1); + Color color = pen.getColor(); + + if (traceBuffer == null) { + traceBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); + traceG2D = traceBuffer.createGraphics(); + } + Graphics2D traceG2D = getTraceG2D(); + traceG2D.setColor(color); + traceG2D.setStroke(pen.getStroke()); + traceG2D.drawLine(ix0, iy0, ix1, iy1); + if (printerG2D != null) + printerG2D.drawLine(ix0, iy0, ix1, iy1); + } + + /** A class for convenience. */ + protected class Point extends java.awt.Point { + Point(int x, int y) { + super(x, y); + } + Point() { + super(); + } + Point(Point p) { + super(p.x, p.y); + } + + /** Get a new Point with coordinates (this.x+p.x, this.y+p.y). + */ + protected Point add(Point p) { + return new Point(this.x+p.x, this.y+p.y); + } + + /** Translate by the amounts dx = p.x, dy = p.y. */ + protected void translate(Point p) { + translate(p.x, p.y); + } + + public String toString() { + return "(" + x + "," + y + ")"; + } + } + + /** Fill a region. + The region is defined by the Turtles actual position and + is bounded by any other color than the give background color. + */ + public void fill(Turtle t, Color bgColor) { + final Point[] diff = { new Point(0,-1), new Point(-1,0), new Point(1,0), new Point(0,1)}; + final int N=0; + final int W=1; + final int E=2; + final int S=3; + + int bgcolor = bgColor.getRGB(); + int fillColor = t.getPen().getFillColor().getRGB(); + Vector list = new Vector(); + Point2D.Double p1 = toScreenCoords(t.getPos()); + int startX = (int)Math.round(p1.getX()); + int startY = (int)Math.round(p1.getY()); + Point p = new Point(startX, startY); + if (traceBuffer.getRGB(startX, startY) == bgcolor) { + traceBuffer.setRGB(startX, startY, fillColor); + list.addElement(new Point(startX, startY)); + int d = N; + int back; + while (list.size() > 0) { + while (d <= S) { // forward + Point tmp = p.add(diff[d]); + try { + if (traceBuffer.getRGB(tmp.x, tmp.y) == bgcolor) { + p.translate(diff[d]); + traceBuffer.setRGB(p.x, p.y, fillColor); + if (printerG2D != null) + { + printerG2D.setColor(t.getPen().getFillColor()); + // printerG2D.drawLine(p.x,p.y, p.x, p.y); + BasicStroke stroke = new BasicStroke(2); + printerG2D.setStroke(stroke); + Line2D line = new Line2D.Double(p.x, p.y, p.x, p.y); + printerG2D.draw(line); + } + list.addElement(new Integer(d)); + d=N; + } + else { + d++; + } + } + catch (ArrayIndexOutOfBoundsException e) { + d++; + } + } + Object obj = list.remove(list.size()-1); + try { + d=((Integer)obj).intValue(); // last element + back = S - d; + p.translate(diff[back]); + } + catch (ClassCastException e) { + // the first (zeroest) element in list is the start-point + // just do nothing with it + } + } + } + traceG2D.drawLine(0, 0, 0, 0); // Workaround because on Mac the trace buffer is not drawn without this + if (printerG2D == null) + repaint(); + } + + /** + * Clear the playground with given color. + */ + public void clear(Color color) { + traceG2D.setColor(color); + traceG2D.fillRect(0, 0, getWidth(), getHeight()); + turtleG2D.setColor(color); + turtleG2D.fillRect(0, 0, getWidth(), getHeight()); + isTurtleVisible = true; + if (printerG2D == null) + repaint(); + } + + /** + * Clear playground. + */ + public void clear() { + clear(getBackground()); + } + + /** Paint the Playground. + just a method for convenience. + */ + public void paintComponent() { + paintComponent(getGraphics()); + } + + /** Draw the trace and turtle buffers. + */ + public void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2D = (Graphics2D)g; + g2D.drawImage(traceBuffer, 0, 0, this); + if (isTurtleVisible) + g2D.drawImage(turtleBuffer, 0, 0, this); + } + + public void paintPlayground() { + Graphics g = getGraphics(); + if (g != null) { + super.paintComponent(g); + Graphics2D g2D = (Graphics2D)g; + g2D.drawImage(traceBuffer, 0, 0, this); + if (isTurtleVisible) + g2D.drawImage(turtleBuffer, 0, 0, this); + + } // end of if + } + + /** Remove all turtles from the turtle buffer. + */ + public void clearTurtles() { + for (int i = 0; i < countTurtles(); i++) { + Turtle turtle = getTurtle(i); + clearTurtle(turtle); + } + } + + /** Remove the given turtle from the turtle buffer. + Override this method if you have added a new behaviour (like + wrap or clip) to the turtle. + */ + public void clearTurtle(Turtle turtle) { + if(turtle != null){ + if (!turtle.isHidden()) { + if(turtle.isClip()){ + clearClipTurtle(turtle); + } + else if(turtle.isWrap()){ + clearWrapTurtle(turtle); + } + } + } + } + + /** This method is called when the given Turtle is in wrap mode. + + @see ch.aplu.turtle.Turtle#wrap + */ + protected void clearWrapTurtle(Turtle turtle){ + clearWrapTurtle(turtle, turtleBuffer); + } + + /** Here the actual clearing of a Turtle in wrap mode from the + given image is performed. + */ + protected void clearWrapTurtle(Turtle turtle, Image im){ + Rectangle bounds = getBounds(turtle); + int pWidth = getWidth(); + int pHeight = getHeight(); + int x = bounds.x; + int y = bounds.y; + while (x > pWidth){ + x -= pWidth; + } + while (x < 0){ + x += pWidth; + } + while (y > pHeight){ + y -= pHeight; + } + while (y < 0){ + y += pHeight; + } + x = x % pWidth; + y = y % pHeight; + toAlphaNull(im, new Rectangle(x, y, bounds.width, bounds.height)); // OK + boolean right = (x + bounds.width > getWidth()); + boolean bottom = (y + bounds.height > getHeight()); + if (right) { + toAlphaNull(im, new Rectangle(x-pWidth, y, bounds.width, bounds.height)); + } + if (bottom) { + toAlphaNull(im, new Rectangle(x, y-pHeight, bounds.width, bounds.height)); + } + if (right && bottom) { + toAlphaNull(im, new Rectangle(x-pWidth, y-pHeight, bounds.width, bounds.height)); + } + } + + /** Copy and translate a given Rectangle. + */ + private Rectangle copyAndTranslate(Rectangle rect, int dx, int dy) { + return new Rectangle(rect.x+dx, rect.y+dy, + rect.width, rect.height); + } + + /** This method is called when the given Turtle is in clip mode. + + @see ch.aplu.turtle.Turtle#clip + */ + protected void clearClipTurtle(Turtle turtle) { + clearClipTurtle(turtle, turtleBuffer); + } + + /** Here the actual clearing of a Turtle in clip mode from the + given image is performed. + */ + protected void clearClipTurtle(Turtle turtle, Image im) { + Rectangle bounds = getBounds(turtle); + toAlphaNull(im, bounds); + } + + /** Set the alpha channel of all pixels in the given image + in the given Rectangle to zero (i.e. totally transparent). + + This method is used byte the clearXXXTurtle methods. + */ + private void toAlphaNull(Image im, Rectangle rect) { + Rectangle rim = new Rectangle(0, 0, im.getWidth(this), im.getHeight(this)); + Rectangle r = new Rectangle(); + if (rect.intersects(rim)) { + r=rect.intersection(rim); + } + int size = r.width*r.height; + float[] alphachannel = new float[r.width*r.height]; + ((BufferedImage)im).getAlphaRaster().setPixels(r.x, r.y, r.width, r.height, alphachannel); + } + + /** Puts a Turtle above all others. + */ + public Turtle toTop(Turtle turtle) { + if (turtles.removeElement(turtle)) { + turtles.add(turtle); + } + return turtle; + } + /** Put a Turtle below all others. + */ + public Turtle toBottom(Turtle turtle) { + if (turtles.removeElement(turtle)) { + turtles.add(0,turtle); + } + return turtle; + } + + /** Calculate the screen coordinates of the given point. + */ + public Point2D.Double toScreenCoords(Point2D.Double p) { + return internalToScreenCoords(p.x, p.y); + } + + /** Calculate the screen coordinates of the given point coordinates. + */ + public Point2D.Double toScreenCoords(double x, double y) { + return internalToScreenCoords(x, y); + } + + protected Point2D.Double internalToScreenCoords(double x, double y) { + // reflect at x-axis, then translate to center of Playground + // pixel coordinates coorespond to turtle coordinates, only translation needed + double newX = originX + x; + double newY = originY - y; + return new Point2D.Double(newX, newY); + } + + /** Calculate the turtle coordinates of the given screen coordinates. + */ + public Point2D.Double toTurtleCoords(double x, double y) { + // pixel coordinates coorespond to turtle coordinates, only translation needed + double newX = x - originX; + double newY = originY - y; + return new Point2D.Double(newX, newY); + } + + /** Calculate the turtle coordinates of the given screen point. + */ + public Point2D.Double toTurtleCoords(Point2D.Double p) { + return toTurtleCoords(p.x, p.y); + } + + /** Calculate the screen angle. + I.e. the interpretation of angle. + @param radians The angle in radians. + */ + double toScreenAngle(double radians) { + double sa = radians; + if (sa < Math.PI/2){ + sa += 2*Math.PI; + } + sa -= Math.PI/2; + if (sa != 0) { + sa = Math.PI*2 - sa; + } + return sa; + } + + /** Calculate the bounds of the Turtles picture on the screen. + */ + protected Rectangle getBounds(Turtle turtle) { + Rectangle bounds = turtle.getBounds(); + Point2D.Double tmp = toScreenCoords(new Point2D.Double(bounds.getX(), bounds.getY())); + bounds.setRect(tmp.x-2, tmp.y-2, bounds.width+4, bounds.height+4); + return bounds; + } + + /** Return the graphics context of the turtle buffer. + */ + public Graphics2D getTurtleG2D() { + return turtleG2D; + } + + /** Return the image of the turtle buffer. + */ + public BufferedImage getTurtleBuffer() { + return turtleBuffer; + } + + /** Return the graphics context of the trace buffer. + */ + public Graphics2D getTraceG2D() { + return traceG2D; + } + + /** Return the graphics context of the printer. + */ + public Graphics2D getPrinterG2D() { + return printerG2D; + } + + /** Return the image of the trace buffer. + */ + public BufferedImage getTraceBuffer() { + return traceBuffer; + } + + /** Clean the traces. + All turtles stay how and where they are, only lines, text and stamps will be removed. + */ + void clean() { + Graphics2D g = getTraceG2D(); + g.setColor(getBackground()); + g.fillRect(0,0,getWidth(), getHeight()); + if (printerG2D == null) + repaint(); + } + + /** Draw the text at the current position of the Turtle t. + Drawing a text at some coordinates (x,y) we mean that the bottom left corner of + the text will be at these coordinates. + Font and colour are specified by the Turtle's Pen. + */ + public void label(String text, Turtle t) { + Point2D.Double sc = toScreenCoords(t.getPos()); + int x = (int)Math.round(sc.x); + int y = (int)Math.round(sc.y); + Graphics2D traceG2D = getTraceG2D(); + FontRenderContext frc = traceG2D.getFontRenderContext(); + Font f = t.getFont(); + TextLayout tl = new TextLayout(text, f, frc); + traceG2D.setColor(t.getPen().getColor()); + tl.draw(traceG2D, x, y); + if (printerG2D != null) + { + printerG2D.setColor(t.getPen().getColor()); + tl.draw(printerG2D, x, y); + } + + if (printerG2D == null) + repaint(); + } + + /** Set antialiasing on or off for the turtle trace buffer + * This may result in an better trace quality. + */ + public void setAntiAliasing(boolean on) { + if (on) + traceG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + else + traceG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_OFF); + } + + + /** + * Set the given TPrintable (implementing draw()), + * open a printer dialog and start printing with given scale. + * Return false, if printer dialog is aborted, + * otherwise return true.
+ * If tp == null, the current playground is printed. + * + */ + protected boolean print(TPrintable tp, double scale) { + if (tp == null) + isPrintScreen = true; + else + isPrintScreen = false; + printerScale = scale; + PrinterJob pj = PrinterJob.getPrinterJob(); + pj.setPrintable(this); + traceCanvas = tp; + if (pj.printDialog()) { + try { + pj.print(); + } + catch (PrinterException ex) { + System.out.println(ex); + } + return true; + } + else + return false; + } + + /** + * For internal use only. Implementation of Printable. + * (Callback method called by printing system.) + */ + public int print(Graphics g, PageFormat pf, int pageIndex) { + if (pageIndex != 0) + return NO_SUCH_PAGE; + Graphics2D g2D = (Graphics2D)g; + double printerWidth = pf.getImageableWidth(); + double printerHeight = pf.getImageableHeight(); + double printerSize = printerWidth > printerHeight ? printerWidth : + printerHeight; + double xZero = pf.getImageableX(); + double yZero = pf.getImageableY(); + + printerG2D = g2D; // Indicate also, we are printing now + + // Needed for fill operations: the trace canvas must be empty in order to + // perform the fill algoritm (searching for outline of figure) + if (!isPrintScreen) + clean(); + + g2D.scale(printerScaleFactor * printerScale, printerScaleFactor * printerScale); + g2D.translate(xZero/printerScale, yZero/printerScale); + + if (isPrintScreen) + { + print(g); + } + else // Printing the traceCanvas + { + // Hide all turtles + boolean[] turtleState = new boolean[countTurtles()]; + for (int i = 0; i < countTurtles(); i++) { + Turtle aTurtle = getTurtle(i); + turtleState[i] = aTurtle.isHidden(); + aTurtle.ht(); + } + traceCanvas.draw(); + + // Restore old context + for (int i = 0; i < countTurtles(); i++) { + Turtle aTurtle = getTurtle(i); + if (!turtleState[i]) + aTurtle.st(); + } + } + + printerG2D = null; + return PAGE_EXISTS; + } + + /** Return the color of the pixel at the current turtle position. + */ + public Color getPixelColor(Turtle t) { + Point2D.Double p1 = toScreenCoords(t.getPos()); + int x = (int)Math.round(p1.getX()); + int y = (int)Math.round(p1.getY()); + return new Color(traceBuffer.getRGB(x, y)); + } + + public void enableRepaint(boolean b) { + isRepainting = b; + } + + /** set the background color of the playground + */ + public void setBackground(Color color) { + super.setBackground(color); + if (traceG2D != null) { + clear(color); + } // end of if + } + + /** + * Sets the origin of the cartesian coordinate system within the playground + */ + public void setOrigin(int x, int y) { + for (int i = 0; i < countTurtles(); i++) { + Turtle turtle = getTurtle(i); + Point2D.Double p1 = toScreenCoords(turtle.getPos()); + double newX = p1.getX() - x; + double newY = y - p1.getY(); + turtle.internalSetPos(newX, newY); + } + originX = x; + originY = y; + } + + public int getOriginX() { + return originX; + } + + public int getOriginY() { + return originY; + } + +} + diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/res/bong.wav b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/res/bong.wav new file mode 100644 index 0000000..0d2fef0 Binary files /dev/null and b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/res/bong.wav differ diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/sharedconstants.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/sharedconstants.java new file mode 100644 index 0000000..1bd3955 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/sharedconstants.java @@ -0,0 +1,84 @@ +// SharedConstants.java + +// Copyright 2002 Regula Hoefer-Isenegger +// +// This file is part of The Java Turtle (TJT) package +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +/* History; +V1.36 - Sep 2004: First official release +V1.37 - Nov 2004: Unchanged, modifications in ch.aplu.util +V1.38 - Dec 2004: Unchanged, modifications in ch.aplu.util +V1.39 - Jan 2005: Unchanged, modifications in ch.aplu.util +V1.40 - Mar 2005: Add background color, TurtleKeyAdapter, TurtleArea +V1.41 - May 2005: User defined turtle shape, minor changes in doc and code style +V1.42 - Dec 2005: Unchanged, modifications in ch.aplu.util +V1.43 - Feb 2006: Bug removed: Turtle.turtleFrame was not initialized in all ctors of class Turtle +V1.44 - Mar 2007: Bug removed: stampTurtle did not work properly in wrap mode +V1.45 - Aug 2007: TurtleKeyAdapter: use wait/notify to reduce CPU time (from 100% to 2%) +V1.46 - Aug 2007: synchronize(playground) for forward and rotate animation, + new method bong() using StreamingPlayer +V1.47 - Sept 2007: Unchanged, modifications in ch.aplu.util +V1.48 - Sept 2007: Unchanged, modifications in ch.aplu.util +V1.49 - Oct 2007: Unchanged, modifications in ch.aplu.util +V1.50 - Oct 2007: Unchanged, modifications in ch.aplu.util +V1.51 - Nov 2007: Fixed: correct position of label, when wrapping is on + Fixed: getPos() returns now the wrapped coordinates + Added: _getPos() returns unwrapped coordinates +V1.52 - Nov 2007: Added bean classes in order to use turtles with a Gui builder +V1.53 - Nov 2007: Added TurtlePane visual information when used in Gui builder design mode +V1.54 - Nov 2007: Minor changes to documentation +V1.55 - Dec 2007: Added property enableFocus to GPane, default: setFocusable(false) +V1.56 - Mar 2008: Unchanged, modifications in ch.aplu.util +V1.57 - Mar 2008: Unchanged, modifications in ch.aplu.util +V1.58 - Mar 2008: Modification to fill() (fill(x, y)): + region is defined with pixel color at current position + as opposed to background color +V1.59 - Oct 2008: Added ctors TurtleFrame with window position (ulx, uly) + Added Turtle.getPixelColor() +V2.00 - Nov 2008: Unchanged, modifications in ch.aplu.util + J2SE V1.4 no longer supported +V2.01 - Jan 2009: Unchanged, modifications in ch.aplu.util +V2.02 - Feb 2009 Turtle constructors run in EDT now +V2.03 - Feb 2009 Unchanged, modifications in ch.aplu.util +V2.04 - Feb 2009 Unchanged, modifications in ch.aplu.util +V2.05 - Feb 2009 Unchanged, modifications in ch.aplu.util +V2.06 - Mar 2009 Unchanged, modifications in ch.aplu.util +V2.07 - Mar 2009 All except print methods synchronized, so Turtle package is + now thread-safe +V2.08 - Apr 2009 Unchanged, modifications in ch.aplu.util +V2.09 - Jun 2009 Unchanged, modifications in ch.aplu.util +V2.10 - Jun 2010 Version for the Java-Editor +*/ + +package ch.aplu.turtle; + +interface SharedConstants +{ + int DEBUG_LEVEL_OFF = 0; + int DEBUG_LEVEL_LOW = 1; + int DEBUG_LEVEL_MEDIUM = 2; + int DEBUG_LEVEL_HIGH = 3; + + int DEBUG = DEBUG_LEVEL_OFF; + + String ABOUT = + "Copyright © 2002-2009\nRegula Hoefer-Isenegger, Aegidius Pluess\n, Gerhard Roehner\n" + + "under GNU General Public License\n" + + "http://www.aplu.ch\n" + + "All rights reserved"; + String VERSION = "2.10 - June 2015"; +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/streamingplayer.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/streamingplayer.java new file mode 100644 index 0000000..8a3d5d5 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/streamingplayer.java @@ -0,0 +1,84 @@ +// StreamingPlayer.java + +package ch.aplu.turtle; + + +import javax.sound.sampled.*; +import java.io.*; + +class StreamingPlayer +{ + private class PlayerThread extends Thread + { + public void run() + { + byte buf[] = new byte[20000]; + try + { + int cnt; + while ((cnt = audioInputStream.read(buf, 0, buf.length)) != -1) + { + if (cnt > 0) + sourceDataLine.write(buf, 0, cnt); + } + sourceDataLine.drain(); + sourceDataLine.close(); + } + catch (IOException ex) + { + System.out.println(ex); + System.exit(1); + } + } + } + + private AudioFormat audioFormat; + private AudioInputStream audioInputStream; + private SourceDataLine sourceDataLine; + private PlayerThread playerThread; + + StreamingPlayer(InputStream is) + { + try + { + audioInputStream = + AudioSystem.getAudioInputStream(is); + audioFormat = audioInputStream.getFormat(); + } + catch (Exception ex) + {} + } + + void start(boolean doFinish) + throws LineUnavailableException + { + DataLine.Info dataLineInfo = + new DataLine.Info(SourceDataLine.class, audioFormat); + sourceDataLine = + (SourceDataLine)AudioSystem.getLine(dataLineInfo); + sourceDataLine.open(audioFormat); + sourceDataLine.start(); + playerThread = new PlayerThread(); + playerThread.start(); + if (doFinish) + waitToFinish(); + } + + boolean isPlaying() + { + if (playerThread == null) + return false; + return (playerThread.isAlive()); + } + + void waitToFinish() + { + if (playerThread == null) + return; + try + { + playerThread.join(); + } + catch (InterruptedException ex) {} + } +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/tprintable.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/tprintable.java new file mode 100644 index 0000000..767d22a --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/tprintable.java @@ -0,0 +1,43 @@ +// TPrintable.java + +// Copyright 2002 Regula Hoefer-Isenegger + +// This file is part of The Java Turtle (TJT) package +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +// Added by Aegidius Pluess + +package ch.aplu.turtle; + +/** + * Interface for printing on an attached printer. + * Normally an application uses a Turtle and implements this interface. + * draw() should contain all drawing operations into the + * Turtle's Playground. The printing occures, when Turtle's print() is + * called.

+ */ + + +public interface TPrintable +{ + /** + * This method must perform all drawing operations. + * Be aware that for some undocumented reason + * draw() is called twice. So be sure you initialize it correctly. + */ + + public void draw(); +} \ No newline at end of file diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/turtle.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/turtle.java new file mode 100644 index 0000000..d30ff52 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/turtle.java @@ -0,0 +1,1585 @@ +// Turtle.java +// Modifications by Aegidius Pluess +// Adaption to the Java-Editor by Gerhard Röhner + +// Copyright 2002-2003 Regula Hoefer-Isenegger (Version 1.0) +// +// This file is part of The Java Turtle package (TJT) +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +/* +Screen coordinate system: 0..400 pixels in both directions corresponds to turtle +turtle coordinate system -200..200, so only translation is necessary (no scaling). + +This avoids rounding errors. +*/ + +package ch.aplu.turtle; + +import ch.aplu.turtle.*; +import java.awt.*; +import java.awt.event.*; +import java.awt.geom.*; +import java.awt.image.*; +import java.awt.print.*; +import java.io.*; +import javax.sound.sampled.*; +import javax.swing.*; + +/** + The core class for turtles. + + For a simple example on how to use Turtle, cf. the + Java Turtle Package description. + @author Regula Hoefer-Isenegger + @version 0.1.1 +*/ +public class Turtle implements Cloneable +{ + private double angle; + private Point2D.Double position; + private Playground playground; + private int framesPerSecond; + private double speed; // Pixel/sec + private double angleSpeed; // Radian/sec + private TurtleRenderer turtleRenderer; + private int angleResolution; + private LineRenderer lineRenderer; + private static TurtleFactory turtleFactory; + private boolean penUp; + private boolean showTurtle; + private boolean initialVisibility = true; + private Pen pen; + private Color color; + private int edgeBehaviour; + + + /** Convert from screen coordinates to turtle coordinates + */ + public Point2D.Double toTurtlePos(Point p) { + return playground.toTurtleCoords((double)p.x, (double)p.y); + } + + /** Convert from screen coordinates to turtle coordinates + */ + public Point2D.Double toTurtlePos(int x, int y) { + return playground.toTurtleCoords((double)x, (double)y); + } + + /** Convert from screen coordinates to turtle coordinates + */ + public double toTurtleX(int x) { + return (playground.toTurtleCoords((double)x, 0)).x; + } + + /** Convert from screen coordinates to turtle coordinates + */ + public double toTurtleY(int y) { + return (playground.toTurtleCoords(0, (double)y)).y; + } + + /** Add the specified mouse listener to receive mouse events. + */ + public void addMouseListener(MouseListener l) { + playground.addMouseListener(l); + } + + /** Add the specified mouse motion listener to receive mouse motion events. + */ + public void addMouseMotionListener(MouseMotionListener l) { + playground.addMouseMotionListener(l); + } + + /** + * Emit a beep. Fails if no standard speaker available. Try to use bong() instead. + * @see #bong() + */ + public void beep() { + Toolkit.getDefaultToolkit().beep(); + } + + /** + * Emit a 'bong' via the sound card. Useful on computers without standard + * speaker. + * @see #beep() + */ + public void bong() { + InputStream is = getClass().getResourceAsStream("res/bong.wav"); + StreamingPlayer player = new StreamingPlayer(is); + try + { + player.start(false); + } + catch (LineUnavailableException ex) + { + System.out.println("Error in bong(). Sound card unavailable"); + } + } + + + /** Represent clip mode. + @see #WRAP + @see #clip() + */ + protected final static int CLIP = 0; + /** Represent wrap mode. + + @see #CLIP + @see #wrap() + */ + protected final static int WRAP = 1; + /** Represent the default edge behaviour (i.e. CLIP or WRAP). + + @see #CLIP + @see #WRAP + @see #clip() + @see #wrap() + */ + protected static int DEFAULT_EDGE_BEHAVIOUR = CLIP; + /** Represent the default speed (velocity). + + @see #setSpeed(double) + */ + protected static double DEFAULT_SPEED = 200; + + /** Represent the default angle resolution. + + It specifies how many different turtle pictures + are generated. + */ + protected static int DEFAULT_ANGLE_RESOLUTION = 72; + + /** Specify how many frames per second are used for + turtle animation. + */ + protected static int DEFAULT_FRAMES_PER_SECOND = 10; + + /** Specify the default turtle color. + + @see #setColor(java.awt.Color) + */ + protected static Color DEFAULT_TURTLE_COLOR = Color.cyan; + + /** Specify the default pen color. + + @see #setPenColor(java.awt.Color) + */ + protected static Color DEFAULT_PEN_COLOR = Color.blue; + + /** Create a new Turtle + */ + public Turtle() { + } + + /** init a new Turtle + */ + protected void init(Color color) { + angle = 0; + position = new Point2D.Double(0, 0); + framesPerSecond = DEFAULT_FRAMES_PER_SECOND; + if (initialVisibility) + setSpeed(DEFAULT_SPEED); + else + setSpeed(-1); + showTurtle = initialVisibility; + setAngleResolution(DEFAULT_ANGLE_RESOLUTION); + angleSpeed = getSpeed() * Math.PI * 2 / DEFAULT_SPEED; + pen = new Pen(DEFAULT_PEN_COLOR); + if (getTurtleFactory() == null){ + turtleFactory = createTurtleFactory(); + } + setColor(color); + lineRenderer = createLineRenderer(); + getTurtleRenderer().setAngle(getAngle()); + } + + /** Set the angle resolution for the Turtle's pictures. + + It specifies how many pictures are used. e.g. an angle resolution + of 90 means that you get one picture for every 4 degrees + (= 360/90 degrees). + */ + public void setAngleResolution(int newResolution) { + synchronized(playground) { + angleResolution = newResolution; + } + } + + /** Return the TurtleFactory of this turtle. + + @see ch.aplu.turtle.TurtleFactory + */ + public TurtleFactory getTurtleFactory() { + return this.turtleFactory; + } + + /** Create a LineRenderer which is responsible + for the correct drawing of the lines. + + @return the new LineRenderer + */ + + protected LineRenderer createLineRenderer() { + return new LineRenderer(this); + } + + /** Create a TurtleRenderer which is responsible + for the correct drawing of the Turtle. + + @return the new TurtleRenderer + */ + protected TurtleRenderer createTurtleRenderer() { + return new TurtleRenderer(this); + } + + /** Create a TurtleFactory which provides for + the Turtle pictures. + + @return the new TurtleFactory + */ + + protected TurtleFactory createTurtleFactory() { + return new TurtleFactory(); + } + /** Get the angle resolution. + + @see #setAngleResolution(int) + */ + + protected int getAngleResolution() { + return angleResolution; + } + + /** Get the TurtleRenderer. + */ + TurtleRenderer getTurtleRenderer() { + return turtleRenderer; + } + + /** Get the LineRenderer. + */ + private LineRenderer getLineRenderer() { + return lineRenderer; + } + + /** Get the Playground. + */ + public Playground getPlayground() { + return playground; + } + + /** Set the Playground to the specified + playground. + + The Turtle is removed from the old + Playground and set to the new one. + */ + public void setPlayground(Playground playground) { + Playground pg = getPlayground(); + if(pg != null){ + pg.clearTurtle(this); + pg.remove(this); + pg.paintTurtles(); + } + this.playground = playground; + playground.paintTurtles(this); + } + + /** Get the angle speed. + + (I.e. how fast the Turtle rotation + animation is performed.) + */ + private double getAngleSpeed() { + return angleSpeed; + } + + /** Set the angle speed. + + @see #getAngleSpeed() + */ + private void setAngleSpeed(double newAngleSpeed) { + this.angleSpeed = newAngleSpeed; + } + + /** Get the current angle (heading) of the + Turtle. + */ + private double getAngle() { + return angle; // in radians + } + + /** Get the current speed. + */ + public double getSpeed() { + return speed; + } + + /** Query the Turtle's x-position without wrapping. + * Coordinate are not bound to playground even when + * wrapping is on. + */ + public double _getX() { + synchronized (playground) { + return position.getX(); + } + } + + /** Query the Turtle's y-position without wrapping. + * Coordinate are not bound to playground even when + * wrapping is on. + */ + public double _getY() { + synchronized (playground) { + return position.getY(); + } + } + + /** Query the Turtle's x-position. + * If turtle is outside playground and wrapping is on + * return the coordinate in range -200..200. + * */ + + public double getX() { + synchronized (playground) { + double xPos = _getX(); + return xPos; + } + } + + /** Query the Turtle's x-position. + * If turtle is outside playground and wrapping is on + * return the coordinate in range -200..200. + */ + public double getY() { + synchronized (playground) { + double yPos = _getY(); + return yPos; + } + } + + + /** Query the Turtle's position without wrapping. + * Coordinate are not bound to playground even when + * wrapping is on. + */ + public Point2D.Double _getPos() { + return position; + } + + /** Query the Turtle's position */ + public Point2D.Double getPos() { + return new Point2D.Double(getX(), getY()); + } + + /** Put the Turtle to a new position with the given x-coordinates. + + */ + public void setX(double x) { + synchronized (playground) { + getPlayground().clearTurtle(this); + internalSetX(x); + getPlayground().paintTurtles(this); + } + } + + /** Put the Turtle to a new position with the given screen x-coordinates. + */ + public void setScreenX(int x) { + synchronized (playground) { + setX(toTurtleX(x)); + } + } + + /** Put the Turtle to a new position with the given y-coordinates. + */ + public void setY(double y) { + synchronized (playground) { + getPlayground().clearTurtle(this); + internalSetY(y); + getPlayground().paintTurtles(this); + } + } + + /** Put the Turtle to a new position with the given screen y-coordinates. + */ + public void setScreenY(int y) { + synchronized (playground) { + setY(toTurtleY(y)); + } + } + + /** Set the Turtle's x-Coordinate. + */ + protected void internalSetX(double x) { + synchronized (playground) { + position.setLocation(x, _getY()); + } + } + + /** Set the Turtle's y-Coordinate. + */ + protected void internalSetY(double y) { + position.setLocation(_getX(), y); + } + + /** Set the Turtle's Position. + */ + protected void internalSetPos(double x, double y) { + position.setLocation(x, y); + } + + /** Hide the Turtle. + + @see #st() + If there is only one turte, the speed is set to -1 + so there is no Turtle animation at all. + Hiding the Turtle speeds up the graphics enormously. + */ + public void ht() { + synchronized (playground) { + this.internalHide(); + if (getPlayground().countTurtles() == 1) + setSpeed(-1); + } + } + + /** Hide the Turtle. + + This is the same as ht(). + @see #st() + */ + public void hideTurtle() { + ht(); + } + + /** This is the method called by the public methods ht() and hideTurtle(). + + Here the actual hiding takes place. + @see #ht() + @see #hideTurtle() + */ + protected void internalHide() { + getPlayground().clearTurtle(this); + showTurtle = false; + if (getPlayground().getPrinterG2D() == null) + getPlayground().repaint(); + } + + /** Set the Turtle to show mode. + + That means that the Turtle will be drawn. + @see #ht() + */ + public void st() { + synchronized (playground) { + if (getPlayground().getPrinterG2D() == null) { + getPlayground().paintTurtle(this); + showTurtle = true; + getPlayground().repaint(); + } + } + } + + /** The same as st(). + + @see #st() + */ + public void showTurtle() { + st(); + } + + /** Tell whether the Turtle is hidden or not. + + @return true if the Turtle is hidden, + false otherwise. + */ + public boolean isHidden() { + return !showTurtle; + } + + private int getFramesPerSecond() { + return this.framesPerSecond; + } + + /** Only set the angle attribute. This method does not + invoke any re-painting. + */ + private void setAngle(double radians) { + this.angle = radians; + } + + /** This is the same as setH(double degrees). + + @see #setH(double) + */ + public void setHeading(double degrees) { + synchronized (playground) { + setAngle(Math.toRadians(degrees)); + getTurtleRenderer().setAngle(Math.toRadians(degrees)); + getPlayground().clearTurtle(this); + getPlayground().paintTurtles(this); + } + } + + /** Set the Turtle's heading. + + 0 means facing NORTH.
+ the angles are measured clockwise. + @see #setHeading(double) + */ + public void setH(double degrees) { + setHeading(degrees); + } + /** Query the Turtle's heading. + + @see #setH(double) + */ + public double heading() { + synchronized (playground) { + return Math.toDegrees(getAngle()); + } + } + /** Set the Turtle's heading to the new value. + + @return the old (previous) value. + @see #setH(double) + */ + public double heading(double degrees) { + synchronized (playground) { + double tmp = Math.toDegrees(getAngle()); + setHeading(degrees); + return tmp; + } + } + + /** Set the Turtle's speed. + + If you try to set the speed to 0, it will be set to 1 (very slow). + A negative speed means that moving the Turtle (fd, bk) + will not be animated.
+ The unit is pixels per second (up to certain bounds depending on the CPU etc.).
+ Remark: Dashed lines will only be painted as you expect it with speed set + to -1. + + @see #fd(double) + @see #bk(double) + */ + public void setSpeed(double speed) { + if (speed == 0) + this.speed = 1; + else + this.speed = speed; + } + + + /** This method is responsible for the rotation animation. + */ + private void internalRotate(double angle) { + if(isHidden()){ + synchronized(playground) + { + setAngle(getAngle()+angle); + if (getTurtleRenderer().imageChanged(getAngle())) { + getTurtleRenderer().setAngle(getAngle()); + } + } + return; + } + if (angle != 0) { + int iterations = getAngleIterations(angle); + + double sign = angle/Math.abs(angle); + double increment = sign*getAngleSpeed()/(double)getFramesPerSecond(); + double startAngle = getAngle(); + + for (int index = 0; index < iterations; index++) { + long timeStamp = System.currentTimeMillis(); + + synchronized(playground) { + getPlayground().clearTurtle(this); + + if (index < iterations-1) { + setAngle(getAngle()+increment); + } + else { + setAngle(startAngle+angle); + } + + if (getTurtleRenderer().imageChanged(getAngle())) { + getTurtleRenderer().setAngle(getAngle()); + getPlayground().paintTurtles(this); + } + } + + long newTimeStamp = System.currentTimeMillis(); + Double secs = new Double(1000./getFramesPerSecond()); + long requiredTime = secs.longValue()-newTimeStamp+timeStamp; + + if (requiredTime > 0) { + try { + Thread.sleep(requiredTime); + } + catch (InterruptedException e) { + } + } + } + } + getPlayground().paintTurtles(this); + } + + /** This method is responsible for the moving animation. + */ + private void internalMove(double length) { + if (getSpeed()>0){ + if (length != 0) { + int iterations = getPathIterations(length); + // an angle of 0 means: facing NORTH + double startX = _getX(); + double startY = _getY(); + getLineRenderer().init(startX, startY); + double dx = length * Math.sin(getAngle()); + double dy = length * Math.cos(getAngle()); + double incrementX = dx / iterations; + double incrementY = dy / iterations; + for (int index = 0; index < iterations; index++) { + long timeStamp = System.currentTimeMillis(); + int nX = (int)_getX(); + int nY = (int)_getY(); + synchronized(playground) { + playground.clearTurtle(this); + if (index < iterations-1) { + internalSetX(_getX()+incrementX); + internalSetY(_getY()+incrementY); + } + else { // last step: Calc the "exact" value + internalSetX(startX + dx); + internalSetY(startY + dy); + } + if (nX != (int)_getX() || nY != - (int)_getY() || index == iterations-1){ + if (!isPenUp()) { + getLineRenderer().lineTo(_getX(), _getY()); + } + getPlayground().paintTurtles(this); + } + } + Double frames = new Double(1000./getFramesPerSecond()); + long newTimeStamp = System.currentTimeMillis(); + long requiredTime = frames.longValue()-newTimeStamp+timeStamp; + if (requiredTime > 0) { + try { + Thread.sleep(requiredTime); + } + catch (InterruptedException e) { + } + } + } + } + } + else { // Speed < 0, i.e. no animation + double startX = _getX(); + double startY = _getY(); + getLineRenderer().init(startX, startY); + double dx = length * Math.sin(getAngle()); + double dy = length * Math.cos(getAngle()); + getPlayground().clearTurtle(this); + internalSetX(startX + dx); + internalSetY(startY + dy); + if (!isPenUp()) { + getLineRenderer().lineTo(_getX(), _getY()); + } + getPlayground().paintTurtles(this); + } + } + + /** Turn the Turtle the given angle (in degrees) to the left + + @see #rt(double) + */ + public void lt(double degrees) { + left(degrees); + } + + /** Same as lt(double degrees) + + @see #lt(double) + */ + public void left(double degrees) { + internalRotate(Math.toRadians(-degrees)); + } + + /** Turn the Turtle the given angle (in degrees) to the right. + + @see #rt(double) + */ + public void rt(double degrees) { + right(degrees); + } + + /** Same as rt(double degrees). + + @see #rt(double) + */ + public void right(double degrees) { + internalRotate(Math.toRadians(degrees)); + } + + /** Same as fd(double distance) + + @see #fd(double) + */ + public void forward(double distance) { + internalMove(distance); + } + + /** Move the Turtle forwards. + + Negative values for distance are + allowed. In that case, the Turtle + will move backwards. + + @see #bk(double) + */ + public void fd(double distance) { + forward(distance); + } + + /** Same as bk(double distance). + + @see #bk(double) + */ + public void back(double distance) { + forward(-distance); + } + + /** Move the Turtle backwards. + + Negative values for distance are + allowed. In that case, the Turtle + will move forwards. + + @see #fd(double) + */ + public void bk(double distance) { + back(distance); + } + + /** Query the distance from the current location + to the given one. + */ + public double distance(double x, double y) { + synchronized (playground) { + return this.getPos().distance(x,y); + } + } + + /** Query the distance from the current location + to the given one. + * Actually a polygon with 36 sides is drawn. + */ + public double distance(Point2D.Double p) { + synchronized (playground) { + return this.getPos().distance(p); + } + } + + /** Draw a circle to the left from the current position with + * turtle's heading tangent direction and given radius. + * Actually a polygon with 36 sides is drawn. + */ + public void leftCircle(double radius) { + circle(radius, true); + } + + /** Draw a circle to the right from the current position with + * turtle's heading tangent direction and given radius. + * Actually a polygon with 36 sides is drawn. + */ + public void rightCircle(double radius) { + circle(radius, false); + } + + private void circle(double radius, boolean left) { + double nbSteps = 36; // 36 side polygon + double angle = 2 * Math.PI / nbSteps; + double step = 2 * radius * Math.sin(angle/2); + for (int i = 0; i < 36; i++) { + if (left) + if (i == 0) + lt(180.0 / nbSteps); + else + lt(360.0 / nbSteps); + else + if (i == 0) + rt(180.0 / nbSteps); + else + rt(360.0 / nbSteps); + fd(step); + } + } + + /** Calculate the number of iterations when animating left or right (rotation). + */ + private int getAngleIterations(double dAngle) { + if(getAngleSpeed()<0){ + return 1; + } + if(getAngleSpeed()==0){ + setAngleSpeed(1); + } + double dAbsAngle = Math.abs(dAngle); + Double dValue = new Double(Math.ceil(dAbsAngle/getAngleSpeed()*getFramesPerSecond())); + return dValue.intValue(); + } + + /** Calculate the number of iterations when animating forwards or backwards. + */ + private int getPathIterations(double length) { + if(speed < 0) + return 1; + + if (speed == 0) + setSpeed(1); + + double dAbsLength = Math.abs(length); + Double dValue = new Double(Math.ceil(dAbsLength/getSpeed()*getFramesPerSecond())); + return dValue.intValue(); + } + + /** Lift the Turtle's pen up so it + won't draw a line anymore. + + This is the same as pu(). + + @see #pu() + @see #penDown() + @see #pd() + */ + public void penUp() { + synchronized (playground) { + this.penUp = true; + } + } + + /** Lift the Turtle's pen up so it + won't draw a line anymore. + + This is the same as penUp(). + + @see #penUp() + @see #penDown() + @see #pd() + */ + public void pu() { + this.penUp(); + } + + /** Lower the Turtle's Pen down so it + will draw a line when moving. + + This is the same as pd(). + + @see #pd() + @see #penUp() + @see #pu() + */ + public void penDown() { + synchronized (playground) { + this.penUp = false; + } + } + + /** Lowers the Turtle's pen down so it + will draw a line when moving. + + This is the same as penDown(). + + @see #penDown() + @see #penUp() + @see #pu() + */ + public void pd() { + this.penDown(); + } + + /** Query the Pen's state (up or down). + + @return true if the Pen is + up, false otherwise. + @see #pu() + @see #pd() + */ + public boolean isPenUp() { + return this.penUp; + } + + /** Return the bounds of this Turtle. This is required + by the methods that (return-)paint the Turtles. + */ + Rectangle getBounds() { + Rectangle rect = new Rectangle(); + + Image img = getTurtleRenderer().currentImage(); + int nWidth = img.getWidth(getTurtleRenderer()); + int nHeight = img.getHeight(getTurtleRenderer()); + double x = (_getX()<0)?Math.floor(_getX()):Math.ceil(_getX()); + double y = (_getY()<0)?Math.floor(_getY()):Math.ceil(_getY()); + rect.setBounds((int)x-nWidth/2, (int)y + nHeight/2, nWidth, nHeight); + return rect; + } + + /** Get the Turtle's Pen. + + You need it if you want to change end caps etc. + @see Pen + */ + public Pen getPen() { + return pen; + } + /** Set the line thickness. + + This works only neatly in clip mode (yet). + @see #clip() + @see #wrap() + */ + public void setLineWidth(double lineWidth){ + synchronized (playground) { + setLineWidth((float)lineWidth); + } + } + + /** Set the line thickness. + + This works only neatly in clip mode (yet). + @see #clip() + @see #wrap() + */ + public void setLineWidth(float lineWidth) { + synchronized (playground) { + getPen().setLineWidth(lineWidth); + } + } + + /** Set the Turtle's color to the specified one. + + */ + public void setColor(Color color) { + synchronized (playground) { + internalSetColor(color); + getPlayground().paintTurtles(); + } + } + + private void internalSetColor(Color color) { + this.color = color; + if (getTurtleRenderer()==null) { + turtleRenderer = createTurtleRenderer(); + getTurtleRenderer().init(getTurtleFactory(), getAngleResolution()); + } + else{ + getTurtleRenderer().init(new TurtleFactory(), + this.angleResolution); + } + } + + /** Set the color to the specified one. + + @see #fill() + */ + public void setFillColor(Color color) { + synchronized (playground) { + getPen().setFillColor(color); + } + } + + /** Query the Turtle's current color. + */ + public Color getColor(){ + return color; + } + + /**Set the Turtle's pen color. + + */ + public void setPenColor(Color color) { + synchronized (playground) { + getPen().setColor(color); + } + } + + /** Move the Turtle back "home", i.e. set its position + to the origin, facing NORTH. + + Color, PenColor etc. remain the same. + */ + public void home() { + // first : clean the Turtle! + synchronized (playground) { + getPlayground().clearTurtle(this); + position = new Point2D.Double(0,0); + setHeading(0); + } + } + + /** The Turtle's Pen is + changed to an eraser (which is in fact a pen with + background color). + + This is the same as pe() + @see #pe() + */ + public void penErase() { + synchronized (playground) { + this.internalPenErase(); + } + } + + /** The Turtle's Pen is + changed to an eraser (which is in fact a pen with + background color). + + This is the same as penErase() + @see #penErase() + */ + public void pe() { + penErase(); + } + + protected void internalPenErase(){ + this.setPenColor(getPlayground().getBackground()); + } + + /** Put the Turtle to a new position with specified + x- and y-coordinates. + */ + public void setPos(double x, double y) { + synchronized (playground) { + getPlayground().clearTurtle(this); + internalSetPos(x, y); + getPlayground().paintTurtles(); + } + } + + /** Put the Turtle to a new position. + */ + public void setPos(Point2D.Double p) { + setPos(p.x, p.y); + } + + /** Put the Turtle to a new screen position. + + */ + public void setScreenPos(Point p) { + setPos(toTurtlePos(p)); + } + + + /** Put a Turtle image at the current position. + + */ + public void stampTurtle() { + synchronized (playground) { + this.getPlayground().stampTurtle(this); + } + } + + /** Calculate the direction to a given point. + + @return the direction from the current turtle position + towards the given point, measured in degrees and clockwise + from the vertical upwards position. + */ + public double towards(double x, double y) { + synchronized (playground) { + double dx = x - getX(); + double dy = y - getY(); + double result = Math.toDegrees(Math.atan2(dx, dy)); + return (result < 0) ? result+360 : result; + } + } + + /** Calculate the direction to a given point. + + @return the direction from the current turtle position + towards the given point, measured in degrees and clockwise + from the vertical upwards position. + */ + public double towards(Point2D.Double p) { + return towards(p.getX(), p.getY()); + } + + /** Put the Turtle to the top (i.e. above any other + turtle). + + Just invokes the toTop-Method in Playground. + @see Playground#toTop + @see #internalToBottom + */ + void internalToTop() { + this.getPlayground().toTop(this); + } + /** Put the Turtle to the bottom (i.e. under any other + turtle). + + Just invokes the toBottom-Method in Playground. + @see Playground#toBottom + @see #internalToTop + */ + void internalToBottom() { + this.getPlayground().toBottom(this); + } + + /** Put this turtle to the bottom. + + So any other turtle in the same Playground will be drawn over it. + */ + public void toBottom() { + // this.getPlayground().toBottom(this); + synchronized (playground) { + internalToBottom(); + getPlayground().paintTurtles(); + } + } + + /** Put this turtle to the top. + + So it will be drawn over any other turtle in the same Playground. + */ + public void toTop() { + // this.getPlayground().toTop(this); + synchronized (playground) { + this.getPlayground().paintTurtles(this); + } + } + + /** Set the pen width. + */ + public void penWidth(int newWidth) { + synchronized (playground) { + penWidth((float)newWidth); + } + } + + /** Internal Method for setting the penWidth. + */ + private void penWidth(float newWidth) { + setLineWidth(newWidth); + } + + /** Query the pen width. + */ + public int penWidth(){ + return (int)this.getPen().getLineWidth(); + } + + /** Returns the current edge behaviour. + + @see #CLIP + @see #WRAP + */ + protected int getEdgeBehaviour() { + return edgeBehaviour; + } + + /** Sets the edge behaviour to the specified value; + + @see #CLIP + @see #WRAP + */ + protected void setEdgeBehaviour(int edgeBehaviour) { + synchronized (playground) { + this.edgeBehaviour = edgeBehaviour; + } + } + + /** Set the Turtle to clip-mode. + @see #wrap() + @see #clip() + */ + public void clip() { + synchronized (playground) { + setEdgeBehaviour(CLIP); + } + } + + /** Cause the Turtle to wrap around the playground borders. + + e.g when as the Turtle leaves the Window on one side, + it reappears on the opposite side. + + @see #clip() + */ + public void wrap() { + synchronized (playground) { + setEdgeBehaviour(WRAP); + } + } + + /** Tell whether the Turtle is in clip mode. + + @return true if in clip mode, false otherwise. + @see #clip() + @see #wrap() + */ + public boolean isClip() { + return (getEdgeBehaviour() == CLIP); + } + /** Tell wheter the Turtle is in wrap mode. + + @return true if in wrap mode, false otherwise. + @see #wrap() + @see #clip() + */ + public boolean isWrap() { + return (getEdgeBehaviour() == WRAP); + } + + /** Fill the region the Turtle is in.
+ + A region is bounded by lines + of any color different to the pixel color at the current turtle position + and by the border of the window.
+ (The pen of the Turtle must be down.) + */ + public void fill() { + synchronized (playground) { + getPlayground().fill(this, getPlayground().getPixelColor(this)); + } + } + + /** Fill the region as if the Turtle where at coordinates x and y.
+ A region is bounded by lines + of any color different to the pixel color at the given position and by the border of + the window.
+ (The pen of the Turtle must be down.) + */ + public void fill(double x, double y) { + synchronized (playground) { + double oldX = getX(); + double oldY = getY(); + boolean hidden = isHidden(); + ht(); + setPos(x,y); + getPlayground().fill(this, getPlayground().getPixelColor(this)); + setPos(oldX, oldY); + if(!hidden){ + st(); + } + } + } + + /** Erase all traces and text painted by the turtles, but let all + * turtles where they are. + + */ + public void clean() { + synchronized (playground) { + getPlayground().clean(); + } + } + + /** Paint the specified text at the current turtle position. + + */ + public void label(String text) { + synchronized (playground) { + if (text != null && text.length() > 0) + getPlayground().label(text, this); + } + } + + /** Set the current font as specified. + + @see java.awt.Font + */ + public void setFont(Font font) { + synchronized (playground) { + getPen().setFont(font); + } + } + + /** Change the current font to the specified one. If you want to know what fonts are available on your system, + call #getAvailableFontFamilies() . + + @see #getAvailableFontFamilies + @see java.awt.Font more information about fontName, style and size. + */ + public void setFont(String fontName, int style, int size) { + synchronized (playground) { + getPen().setFont(new Font(fontName, style, size)); + } + } + + /** Set the font size. + + @see #setFontStyle changing the font style + @see #setFont changing the whole font. + */ + public void setFontSize(int size) { + synchronized (playground) { + getPen().setFontSize(size); + } + } + + /** Set the font style. + + This is either java.awt.Font.PLAIN, java.awt.Font.BOLD, java.awt.Font.ITALIC or java.awt.Font.BOLD+java.awt.Font.ITALIC + @see #setFontSize changing the font size + @see #setFont changing the whole font. + */ + public void setFontStyle(int style) { + synchronized (playground) { + getPen().setFontStyle(style); + } + } + + /** Provide information about all font families (e.g. roman) currently available on your system. + Each font name is a string packed into a array of strings. + (This might be useful when you want to change the font.) + @see #setFont(java.awt.Font) + @see #setFont(java.lang.String, int, int) + */ + public static String[] getAvailableFontFamilies() { + return Pen.getAvailableFontFamilies(); + } + + /** Return the current font. + + @see #setFontSize changing the font size + @see #setFontStyle changing the font style + @see #setFont changing the whole font. + */ + public Font getFont() { + return getPen().getFont(); + } + + /** Creates a clone of the Turtle in it's Playground. + Color, position and heading are the same. + (It's visible and pen is down.) + Return the cloned object reference as a Object type, because + it overrides Object.clone(). + (You may cast it to a Turtle.) + */ + public Object clone() + { + synchronized (playground) { + Turtle t = new Turtle(); + t.setColor(getColor()); + t.setPos(getX(), getY()); + t.heading(heading()); + t.showTurtle(); + return t; + } + } + + /** Set antialiasing on or off for the turtle trace buffer + * This may result in an better image quality, especially + * for filling operations (platform dependant). + */ + public void antiAliasing(boolean on) { + synchronized (playground) { + getPlayground().setAntiAliasing(on); + } + } + + + /** + * Print the graphics context to an attached printer with + * the given magnification scale factor. + * scale = 1 will print on standard A4 format paper.
+ * + * The given tp must implement the GPrintable interface, + * e.g. the single method void draw(), where all the + * drawing into the GPanel must occur. + * + * Be aware the turtle(s) state (position, direction, etc.) + * must be reinitialized, because draw() is called several + * times by the printing system. + * + * A standard printer dialog is shown before printing is + * started. Only turte traces are printed. + *
+ * Example:
+ * + import ch.aplu.turtle.*;
+
+ public class PrintTest implements TPrintable
+ {
+ private Turtle t = new Turtle();
+
+ public PrintTest()
+ {
+ draw(); // Draw on screen
+ t.print(this); // Draw on printer
+ }
+
+ public void draw()
+ {
+ t.home(); // Needed for initialization
+ for (int i = 0; i < 5; i++)
+ t.fd(20).rt(90).fd(20).lt(90);
+ }
+
+ public static void main(String[] args)
+ {
+ new PrintTest();
+ }
+ }
+
+ */ + public boolean print(TPrintable tp, double scale) + { + return getPlayground().print(tp, scale); + } + + /** + * Same as print(tp, scale) with scale = 1. + */ + public boolean print(TPrintable tp) + { + return print(tp, 1); + } + + /** + * Print the Turtle's current playground with given scale. + */ + public boolean printScreen(double scale) + { + return print(null, scale); + } + + /** + * Same as printScreen(scale) with scale = 1. + */ + public boolean printScreen() + { + return printScreen(1); + } + + /** + * Clear the Turtle's playground. All turtle images and traces are erased, + * but the turtles remain (invisible) at their positions. + */ + public void clear() + { + synchronized (playground) { + getPlayground().clear(); + } + } + + /** + * Delay execution for the given amount of time ( in ms ). + */ + public static void sleep(int time) + { + try + { + Thread.currentThread().sleep(time); + } + catch (Exception e) {} + } + + /** + * Return version information + */ + public String version() + { + return SharedConstants.VERSION; + } + + /** + * Enable/disable automatic repainting. + * If disabled, call repaint() to perform repainting. + * Disabling automatic repainting and hiding the + * Turtle speeds up the graphics enormously. + */ + public void enableRepaint(boolean b) + { + getPlayground().enableRepaint(b); + } + + /** + * Perform manual repainting when automatic repainting is + * disabled. + */ + public void repaint() { + getPlayground().repaint(); + } + + /** + * Return the color of the pixel at the current turtle position. + */ + public Color getPixelColor() { + synchronized (playground) { + return getPlayground().getPixelColor(this); + } + } + + protected Graphics getGraphics() { + return playground.getGraphics(); + } + + /** + * Transforms the gui-position to mouse-coordinates. + */ + public void setBounds(int x, int y, int b, int h) { + int ox = playground.getOriginX(); + int oy = playground.getOriginY(); + x = -ox + x + 10; + y = oy - y - 13; + setPos(x, y); + } + + /** + * Shows or hides the turtle. + */ + public void setVisible(boolean show) { + if (show) { + showTurtle(); + } else { + hideTurtle(); + } // end of if-else + } + + /** + * Sets clipping-mode on/off. + */ + public void setClip(boolean clip) { + if (clip) { + clip(); + } else { + wrap(); + } // end of if-else + } + + /** + * Sets pen-position up/down. + */ + public void setPenUp(boolean penup) { + if (penup) { + penUp(); + } else { + penDown(); + } // end of if-else + } + + /** + * Sets angle of the turtle. + */ + public void setAngle(int angle) { + setHeading(angle); + } +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/turtlefactory.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/turtlefactory.java new file mode 100644 index 0000000..e9d700e --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/turtlefactory.java @@ -0,0 +1,66 @@ +// TurtleFactory.java + +// Copyright 2002 Regula Hoefer-Isenegger +// +// This file is part of The Java Turtle (TJT) package +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package ch.aplu.turtle; + +import java.awt.image.*; +import java.awt.*; + +class TurtleFactory +{ + /** Generates the shape of the turtle with color, + * angle angle, width w + * and height h. + */ + protected Image turtleImage(Color color, double angle, int w, int h) + { + BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR); + Graphics2D g = (Graphics2D)bi.getGraphics(); + // Origin in center + g.translate(w/2, h/2); + // angle = 0 is direction east (as usual in mathematics) + g.rotate(Math.PI/2 - angle); + g.setColor(color); + + // Body + g.fillOval((int)( -0.35 * w), (int)( -0.35 * h), + (int)(0.7 * w), (int)(0.7 * h)); + + // Head + g.fillOval((int)( -0.1 * w), (int)( -0.5 * h), + (int)(0.2 * w), (int)(0.2 * h)); + + // Tail + int[] xcoords = + {(int)( -0.05 * w), 0, (int)(0.05 * w)}; + int[] ycoords = + {(int)(0.35 * h), (int)(0.45 * h), (int)(0.35 * h)}; + g.fillPolygon(xcoords, ycoords, 3); + + // Feet + for (int i = 0; i < 4; i++) + { + g.rotate(Math.PI / 2); + g.fillOval((int)( -0.35 * w), (int)( -0.35 * h), + (int)(0.125 * w), (int)(0.125 * h)); + } + return (Image)bi; + } +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/turtlerenderer.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/turtlerenderer.java new file mode 100644 index 0000000..9489e78 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/ch/aplu/turtle/turtlerenderer.java @@ -0,0 +1,312 @@ +// TurtleRenderer.java + +// Copyright 2002 Regula Hoefer-Isenegger +// Some modifications by Aegidius Pluess + +// This file is part of The Java Turtle (TJT) package +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package ch.aplu.turtle; + +import ch.aplu.turtle.*; +import java.awt.image.*; +import java.awt.*; +import javax.swing.*; +import java.awt.geom.*; +import java.util.*; + + +/** This class is responsible for creating and choosing the correct Turtle picture. + @author Regula Hoefer-Isenegger + @version 0.1 +*/ +public class TurtleRenderer implements ImageObserver { + /** Holds the current image */ + private Image currentImage; + /** Holds all images */ + private Vector images; + /** Tells how many pictures are needed*/ + private int resolution; + /** A reference to the Turtle */ + private Turtle turtle; + /** Holds the current Angle */ + private double currentAngle; + + private final int turtleSize = 29; + + /***/ + public TurtleRenderer(Turtle turtle) { + this.currentImage = null; + this.images = new Vector(); + this.turtle = turtle; + currentAngle = 0; + } + + /** As an image stays unchanged, there's no need to ever update it. So this method returns always false. + + For further information cf. java.awt.image.ImageObserver. + @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int) + */ + public boolean imageUpdate(Image img, int infoflags, + int x, int y, int width, int height) { + return false; + } + + /** Return the current image. + */ + public Image currentImage() { + return this.currentImage; + } + + /** Tell whether the image has changed. + */ + public boolean imageChanged(double angle) { + return (this.currentImage != getImage(angle)); + } + + /** Set the current image to the specified one. */ + private void setCurrentImage(Image image) { + currentImage = image; + } + + /** Choose the image for the angle angle. + */ + private Image getImage(double angle) { + while (angle < 0) { + angle += 2*Math.PI; + } + while (angle >= 2*Math.PI) { + angle -= 2*Math.PI; + } + double res = 2*Math.PI/(double)this.resolution; + int index = (int)(angle/res); + return image(index); + } + + /** Set the current image to the one corresponding to the angle angle. + */ + public void setAngle(double angle) { + currentAngle = angle; + setCurrentImage(getImage(angle)); + } + + /** @return the current angle. */ + protected double getAngle(){ + return currentAngle; + } + + /** Create the images. There are resolution images (i.e. two subsequent + images contain an angle of 2π/ or 360/resolution degrees). + */ + public void init(TurtleFactory factory, int resolution) { + this.resolution = resolution; + Integer res = new Integer(resolution); + double incRes = Math.PI*2/res.doubleValue(); + double angle = 0; + images = new Vector(); + for (int i = 0; i < resolution; i++) { + images.add(factory.turtleImage(turtle.getColor(), + turtle.getPlayground().toScreenAngle(angle), + turtleSize, turtleSize)); + angle += incRes; + } + setCurrentImage(getImage(currentAngle)); + } + + /** Tell how many images this TurtleRenderer holds */ + private int countImages() { + return this.images.size(); + } + + /** Get the image at index */ + private Image image(int index) { + return (Image)this.images.elementAt(index); + } + + /** This method is responsible for painting the turtle onto the + playground at (x, y). + */ + public final void paint(double x, double y) { + internalPaint(x, y, turtle.getPlayground().getGraphics()); + } + + /** This method is responsible for painting the turtle onto the + playground at p. + */ + public final void paint(Point2D.Double p) { + internalPaint(p.x, p.y, turtle.getPlayground().getGraphics()); + } + + /** This method is responsible for painting the Turtle + at (x, y).
+ The Graphics argument tells where to paint. + */ + public final void paint(double x, double y, Graphics g) { + internalPaint(x, y, g); + } + + /** This method is responsible for painting the Turtle + at p.
+ The Graphics argument tells where to paint. + */ + public void paint(Point2D.Double p, Graphics g){ + internalPaint(p.x, p.y, g); + } + + /** Call clipPaint and wrapPaint(). + + You should override this method only, if you add a new (edge) + behaviour to the turtle. I recommend to you then to first call this + method from the overriding one. +
+ If you want to change anything about the real painting, override + clipPaint or wrapPaint. + + @see #clipPaint(int, int, Graphics2D) + @see #wrapPaint(int, int, Graphics2D) + */ + protected void internalPaint(double x, double y, Graphics g) { + if(turtle.isClip()){ + Point2D.Double p = + calcTopLeftCorner(turtle.getPlayground().toScreenCoords(x, y)); + clipPaint((int)p.x, (int)p.y, (Graphics2D)g); + } + else if(turtle.isWrap()){ + Point2D.Double p = + calcTopLeftCorner(turtle.getPlayground().toScreenCoords(x, y)); + wrapPaint((int)p.x, (int)p.y, (Graphics2D)g); + } + } + + /** Define how to paint in clip mode and do it */ + protected void clipPaint(int x, int y, Graphics2D g2D) { + g2D.drawImage(currentImage, x, y, this); + } + + /** Define how to paint in wrap mode and do it */ + protected void wrapPaint(int x, int y, Graphics2D g2D) { + int pWidth = turtle.getPlayground().getWidth(); + int pHeight = turtle.getPlayground().getHeight(); + int paintX = x; + while (paintX > pWidth) { + paintX -= pWidth; + } + while (paintX < 0) { + paintX += pWidth; + } + int paintY = y; + while (paintY > pHeight) { + paintY -= pHeight; + } + while (paintY < 0) { + paintY += pHeight; + } + g2D.drawImage(currentImage, paintX, paintY, this); + int nWidth = currentImage.getWidth(this); + int nHeight = currentImage.getHeight(this); + boolean right = (paintX+nWidth > pWidth); + boolean bottom = (paintY+nHeight > pHeight); + if(right) { + g2D.drawImage(currentImage, + paintX-pWidth, + paintY, + this); + } + if(bottom) { + g2D.drawImage(currentImage, + paintX, + paintY-pHeight, + this); + } + if(right && bottom) { + g2D.drawImage(currentImage, + paintX-pWidth, + paintY-pHeight, + this); + } + } + + /** Compute the x-coordinate of the top left corner of the Turtle image + (it depends on the specified x-coordinate and the image width). + */ + protected int calcTopLeftCornerX(double x) { + int intX = (int)x; + int nWidth; + if(currentImage == null) { + setCurrentImage(new TurtleFactory(). + turtleImage(turtle.getColor(), getAngle(), turtleSize, turtleSize)); + } + nWidth = this.currentImage.getWidth(this); + // the center of the turtle lies on the turtle's location: + intX -= nWidth/2; + return intX; // top left corner of the Turtle's image + } + + /** Compute the y-coordinate of the top left corner of the Turtle image + (it depends on the specified y-coordinate and the image height). + */ + protected int calcTopLeftCornerY(double y) { + int intY = (int)y; + if(currentImage == null) { + setCurrentImage(new TurtleFactory(). + turtleImage(turtle.getColor(), getAngle(), turtleSize, turtleSize)); + } + int nHeight = currentImage.getHeight(this); + // the center of the turtle lies on the turtle's location: + intY -= nHeight/2; + + return intY; // top left corner of the Turtle's image + } + /** Compute the top left corner of the Turtle image + (dependent on the specified x- and y-coordinate and the image + width and height. + */ + protected Point2D.Double calcTopLeftCorner(double x, double y) { + if(currentImage == null) { + setCurrentImage(new TurtleFactory(). + turtleImage(turtle.getColor(), getAngle(), turtleSize, turtleSize)); + } + int w = currentImage.getWidth(this); + int h = currentImage.getHeight(this); + return new Point2D.Double(x-w/2, y-h/2); + } + + /** Compute the top left corner of the Turtle image + (dependent on the specified point p and the image + width and height. + */ + protected Point2D.Double calcTopLeftCorner(Point2D.Double p) { + return calcTopLeftCorner(p.x, p.y); + } +} + + + + + + + + + + + + + + + + + + diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/eval.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/eval.java new file mode 100644 index 0000000..97c8ff0 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/eval.java @@ -0,0 +1,50 @@ +public class Eval { + + public static Wrap getOb(Object o) { + return new Wrap(o); + } + + protected static Object getOb(final String s) { + return new Object() {public String result = s;}; + } + + protected static Object getOb(final boolean b) { + return new Object() {public boolean result = b;}; + } + + protected static Object getOb(final byte b) { + return new Object() {public byte result = b;}; + } + + protected static Object getOb(final char c) { + return new Object() {public char result = c;}; + } + + protected static Object getOb(final double d) { + return new Object() {public double result = d;}; + } + + protected static Object getOb(final float f) { + return new Object() {public float result = f;}; + } + + protected static Object getOb(final int i) { + return new Object() {public int result = i;}; + } + + protected static Object getOb(final long l) { + return new Object() {public long result = l;}; + } + protected static Object getOb(final short s) { + return new Object() {public short result = s;}; + } +} + + +class Wrap { + public Object result; + + Wrap(Object result) { + this.result = result; + } +} \ No newline at end of file diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/inout.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/inout.java new file mode 100644 index 0000000..d702d54 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/inout.java @@ -0,0 +1,98 @@ +import java.io.*; + +/** +Support class for console input and output of numeric values.

+ +Example for input:
+ int age = InOut.readInt("Your age: ");

+ +Example for output:
+ System.out.println("price: " + InOut.format2(prize) + "Euro");
+ System.out.println("percent: " + InOut.formatN(percent, 1) + " %"); +*/ + + +public class InOut { + + /** Formats a double-value with 2 decimal digits. */ + public static String format2(double d) { + return String.format("%.2f", d); + } + + /** Formats a double-value with N decimal digits. */ + public static String formatN(double d, int N) { + return String.format("%." + N + "f", d); + } + + /** Reads a boolean-value from console. */ + public static boolean readBoolean(String prompt) { + final String[] trueValues = + { "1", "y", "t", "j", "w", "yes", "true", "ja", "wahr", "ok" }; + System.out.print(prompt); + String input = readln().toLowerCase(); + for (int i = 0; i < trueValues.length; ++i) + if (trueValues[i].equals(input)) + return true; + return false; + } + + /** Reads a char-value from console. */ + public static char readChar(String prompt) { + System.out.print(prompt); + return readln().charAt(0); + } + + /** Reads a double-value from console. */ + public static double readDouble(String prompt) { + System.out.print(prompt); + return Double.parseDouble(readln()); + } + + /** Reads a float-value from console. */ + public static float readFloat(String prompt) { + System.out.print(prompt); + return Float.parseFloat(readln()); + } + + /** Reads an int-value from console. */ + public static int readInt(String prompt) { + System.out.print(prompt); + return Integer.parseInt(readln()); + } + + /** Reads a long-value from console. */ + public static long readLong(String prompt) { + System.out.print(prompt); + return Long.parseLong(readln()); + } + + /** Reads a string-value from console. */ + public static String readString(String prompt) { + System.out.print(prompt); + return readln(); + } + + /** Reads a string-value from console without prompt. + For use at the end of a console program. */ + public static String readln() { + try { + return Input.readLine(); + } catch(Exception e) { + return ""; + } + } + + private static BufferedReader Input; + + static { + try { + Input = new BufferedReader(new InputStreamReader(System.in)); + } + catch (Exception e) { + System.out.println("console input not possible."); + } + } + + + +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/je/util/turtle.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/je/util/turtle.java new file mode 100644 index 0000000..4a704ad --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/je/util/turtle.java @@ -0,0 +1,311 @@ +package je.util; +/* + * Class : Turtle + * Copyright: (c) Gerhard Röhner + * + * History + * -------- + * 3.00 2000.10.10 first version as java class + * 3.01 2002.10.22 DrawDynamic + * 3.02 2010.10.11 sleepTime for paint delay + * 3.03 2012.03.20 cartesian coordinate system, used with originX, originY, setOrigin + * 3.04 2015.01.24 english version, integrated component + */ + + +import java.awt.*; +import java.awt.image.*; +import java.awt.event.*; + +/** + * This class is a turtle component for simple graphic programming. + * + * @see + * @author Gerhard Röhner + * @version 3.03 20/03/2012 + */ + + +public class Turtle extends Canvas { + + // -- private Attribute ------------------------------------------------------ + + private BufferedImage myBufferedImage; + private Graphics myBufferedGraphics; + private Color foreground; + private Color background; + private static final double piDurch180 = Math.PI / 180; + + // -- public attributes ----------------------------------------------------- + + /** + * turtleX is the x-coordinate of the turtle + *

+ * Example:

myTurtle.turtleX = 100;
+ */ + public double turtleX; + + /** + * turtleY is the y-coordinate of the turtle. + *

+ * Example:

myTurtle.turtleY = 200;
+ */ + public double turtleY; + + /** + * turtleW is the current angle of the turtle in the range form 0 to 360 degrees. + *

+ * Example:

myTurtle.turtleW = 180;
+ */ + public double turtleW; + + /** + * originX is the x-position of the cartesian coodinate system within the turtle canvas. + *

+ * Example:

myTurtle.originX = 0;
+ */ + public double originX; + + /** + * originY is the y-position of the cartesian coodinate system within the turtle canvas. + *

+ * Example:

myTurtle.originY = 100;
+ */ + public double originY; + + /** + * If drawDynamic is true you can watch the drawing of the turtle. + *

+ * Example:

myTurtle.drawDynamic = true;
+ */ + public boolean drawDynamic; + + /** + * For drawDynamic = true you set the delay in milliseconds for drawing. + *

+ * Example:

myTurtle.sleepTime = 500;
+ */ + public int sleepTime = 0; + + // --- constructor ----------------------------------------------------------- + + /** + * Creates a Turtle with a canvas. + * At the beginning the Turtle is placed in the middle of it's canvas. + * The start angle is 0 degree, which means the Turtle looks to the right. + * The background color is white, the drawing color black. + *

+ * The turtle position can easily be changed by clicking into the canvas. + *

+ * The size of the canvas can easily be changed with the mouse. + *

+ * Example:

Turtle myTurtle = new Turtle();
+ */ + + public Turtle() { + myBufferedImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); + myBufferedGraphics = myBufferedImage.getGraphics(); + + setForeground(Color.black); + setBackground(Color.white); + drawDynamic = true; + + addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent evt) { + turtleMouseClicked(evt);}}); + addComponentListener(new ComponentAdapter() { + public void componentResized(ComponentEvent evt) { + turtleResized(evt);}}); + setOrigin(50, 50); + } + + private void turtleMouseClicked(MouseEvent evt) { + turtleX = evt.getX() - originX; + turtleY = originY - evt.getY(); + turtleW = 0; + } + + private void turtleResized(ComponentEvent evt) { + int width = getWidth(); + int height = getHeight(); + + BufferedImage newBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics newGraphics = newBufferedImage.getGraphics(); + newGraphics.setColor(background); + newGraphics.fillRect(0, 0, width, height); + newGraphics.setColor(foreground); + newGraphics.drawImage(myBufferedImage, 0, 0, this); + + turtleX = 0; + turtleY = 0; + turtleW = 0; + setOrigin(width / 2, height / 2); + + myBufferedImage = newBufferedImage; + myBufferedGraphics = newGraphics; + } + + public boolean isDoubleBuffered() { + return true; + } + + private void wTurtleMod360() { + while (turtleW >= 360) + turtleW = turtleW - 360; + while (turtleW < 0) + turtleW = turtleW + 360; + } + + // --- angle and turns ------------------------------------------------------- + + /** + * Turns the angle of the turtle relativ by the parameter angle. + * Positive values means a turn right, negative a turn left. + *

+ * Example:

myTurtle.turn(-90);
+ */ + public void turn(double angle) { + turtleW = turtleW + angle; + wTurtleMod360(); + } + + /** + * Sets the angle of the turtle absolute by the parameter angle. + * The angle increases counterclockwise, therefore + * right = 0, top = 90, left = 180, bottom = 270. + *

+ * Example:

myTurtle.turnto(270);
+ */ + public void turnto(double angle) { + turtleW = angle; + wTurtleMod360(); + } + + // --- Drawing --------------------------------------------------------------- + + /** + * The Turtle draws a line of the length specified in the current direction. + *

+ * Example:

myTurtle.draw(100);
+ */ + public void draw(double length) { + drawto(turtleX + length * Math.cos(turtleW * piDurch180), + turtleY + length * Math.sin(turtleW * piDurch180)); + } + + /** + * The Turtle draws a line form the current position (turtleX, turtleY) to the + * position (x, y) relativ to the cartesian coordinate system. + *

+ * Example:

myTurtle.drawto(200, 300);
+ */ + public void drawto(double x, double y) { + int x1 = (int) (originX + turtleX); + int x2 = (int) (originX + x); + int y1 = (int) (originY - turtleY); + int y2 = (int) (originY - y); + + myBufferedGraphics.drawLine(x1, y1, x2, y2); + if (drawDynamic){ + getGraphics().drawLine(x1, y1, x2, y2); + try { + Thread.currentThread().sleep(sleepTime); + } catch(InterruptedException e) { + } + } else + repaint(); + + turtleX = x; + turtleY = y; + } + + // --- Moving ---------------------------------------------------------------- + + /** + * The Turtle moves without drawing the length specified in the current + * direction. + *

+ * Example:

myTurtle.move(100);
+ */ + public void move(double length) { + turtleX = turtleX + length * Math.cos (turtleW * piDurch180); + turtleY = turtleY + length * Math.sin (turtleW * piDurch180); + } + + /** + * The Turtle moves without drawing to position (x, y) relativ to the + * cartesian coordinate system. + *

+ * Example:

myTurtle.moveto(100, 200);
+ */ + public void moveto(double x, double y) { + turtleX = x; + turtleY = y; + } + + // --- set origin ----------------------------------------------------------- + + /** + * Sets the origin of the cartesian coordinate system within the turtle's canvas. + *

+ * Example:

myTurtle.setOrigin(100, 200);
+ */ + public void setOrigin(double x, double y) { + originX = x; + originY = y; + } + + // --- fore- and background color -------------------------------------------- + + /** + * Sets the drawing color of the Turtle to color c. + *

+ * Example:

myTurtle.setForeground(Color.red);
+ */ + public void setForeground(Color c) { + foreground = c; + myBufferedGraphics.setColor(foreground); + super.setForeground(foreground); + } + + /** + * Sets the canvas color of the Turtle to color c. + *

+ * Example:

myTurtle.setBackground(Color.blue); 
+ */ + public void setBackground(Color c) { + background = c; + myBufferedGraphics.setColor(background); + myBufferedGraphics.fillRect(0, 0, getWidth(), getHeight()); + myBufferedGraphics.setColor(foreground); + repaint(); + } + + /** + * Clear the Turtle's canvas with the background color. + *

+ * Example:

myTurtle.clear();
+ */ + public void clear() { + setBackground(background); + getGraphics().drawImage(myBufferedImage, 0, 0, this); + repaint(); + } + + // --- Showing -------------------------------------------------------------- + + /** + * Shows the Turtle's canvas. + */ + public void paint(Graphics g) { + g.drawImage(myBufferedImage, 0, 0, this); + } + + /** + * Updates the Turtle's canvas. + */ + public void update(Graphics g) { + paint(g); + } + +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/jeclasses.jar b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/jeclasses.jar new file mode 100644 index 0000000..7fa4892 Binary files /dev/null and b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/jeclasses.jar differ diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/jnumberfield.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/jnumberfield.java new file mode 100644 index 0000000..fd41a78 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/jnumberfield.java @@ -0,0 +1,129 @@ +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import java.util.*; + +/** +Swing-component for input and output of numeric values. +*/ + +public class JNumberField extends JTextField { + + /** constructor for a JNumberField */ + public JNumberField() { + enableEvents(AWTEvent.KEY_EVENT_MASK); + } + + /** Gets a double-value from the JNumberField. */ + public double getDouble() { + Double d = new Double(getText()); + return d.doubleValue(); + } + + /** Gets a float-value from the JNumberField. */ + public float getFloat() { + Double d = new Double(getText()); + return d.floatValue(); + } + + /** Gets an int-value from the JNumberField. */ + public int getInt() { + Double d = new Double(getText()); + return d.intValue(); + } + + /** Gets a long-value from the JNumberField. */ + public long getLong() { + Double d = new Double(getText()); + return d.longValue(); + } + + /** Checks wether the JNumberField contains a valid numeric value. */ + public boolean isNumeric() { + final String Digits = "(\\p{Digit}+)"; + final String HexDigits = "(\\p{XDigit}+)"; + // an exponent is 'e' or 'E' followed by an optionally + // signed decimal integer. + final String Exp = "[eE][+-]?"+Digits; + final String fpRegex = + ("[\\x00-\\x20]*"+ // Optional leading "whitespace" + "[+-]?(" + // Optional sign character + "NaN|" + // "NaN" string + "Infinity|" + // "Infinity" string + + // A decimal floating-point string representing a finite positive + // number without a leading sign has at most five basic pieces: + // Digits . Digits ExponentPart FloatTypeSuffix + // + // Since this method allows integer-only strings as input + // in addition to strings of floating-point literals, the + // two sub-patterns below are simplifications of the grammar + // productions from the Java Language Specification, 2nd + // edition, section 3.10.2. + + // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt + "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+ + + // . Digits ExponentPart_opt FloatTypeSuffix_opt + "(\\.("+Digits+")("+Exp+")?)|"+ + + // Hexadecimal strings + "((" + + // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt + "(0[xX]" + HexDigits + "(\\.)?)|" + + + // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt + "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" + + + ")[pP][+-]?" + Digits + "))" + + "[fFdD]?))" + + "[\\x00-\\x20]*");// Optional trailing "whitespace" + + return java.util.regex.Pattern.matches(fpRegex, getText()); + } + + /** Sets a double-value into the JNumberField. */ + public void setDouble(double d) { + setText(String.valueOf(d)); + } + + /** Sets a double-value with N digits into the JNumberField. */ + public void setDouble(double d, int N) { + setText(String.format(Locale.ENGLISH, "%." + N + "f", d)); + } + + /** Sets a float-value into the JNumberField. */ + public void setFloat(float f) { + setText(String.valueOf(f)); + } + + /** Sets a float-value with N digits into the JNumberField. */ + public void setFloat(float f, int N) { + setText(String.format(Locale.ENGLISH, "%." + N + "f", f)); + } + + /** Sets an int-value into the JNumberField. */ + public void setInt(int i) { + setText(String.valueOf(i)); + } + + /** Sets a long-value into the JNumberField. */ + public void setLong(long l) { + setText(String.valueOf(l)); + } + + /** Clears the JNumberField */ + public void clear() { + setText(""); + } + + protected void processKeyEvent(KeyEvent e) { + super.processKeyEvent(e); + if (isNumeric() || getText().equals("-") || + getText().equals("") || getText().equals(".")) + setBackground(Color.white); + else + setBackground(Color.red); + } + +} \ No newline at end of file diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/meta-inf/manifest.mf b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/meta-inf/manifest.mf new file mode 100644 index 0000000..4e2b00e --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/meta-inf/manifest.mf @@ -0,0 +1,5 @@ +Manifest-Version: 1.0 +Class-Path: JEClasses.jar +Created-By: 1.8.0_161 (Oracle Corporation) +Main-Class: BreakVigenere + diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/numberfield.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/numberfield.java new file mode 100644 index 0000000..ca7ef01 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_kasiskitest/numberfield.java @@ -0,0 +1,128 @@ +import java.awt.*; +import java.awt.event.*; +import java.util.*; + +/** +AWT-component for input and output of numeric values. +*/ + +public class NumberField extends TextField { + + /** constructor for a NumberField */ + public NumberField() { + enableEvents(AWTEvent.KEY_EVENT_MASK); + } + + /** Gets a double-value from the NumberField. */ + public double getDouble() { + Double d = new Double(getText()); + return d.doubleValue(); + } + + /** Gets a float-value from the NumberField. */ + public float getFloat() { + Double d = new Double(getText()); + return d.floatValue(); + } + + /** Gets an int-value from the NumberField. */ + public int getInt() { + Double d = new Double(getText()); + return d.intValue(); + } + + /** Gets a long-value from the NumberField. */ + public long getLong() { + Double d = new Double(getText()); + return d.longValue(); + } + + /** Checks wether the NumberField contains a valid numeric value. */ + public boolean isNumeric() { + final String Digits = "(\\p{Digit}+)"; + final String HexDigits = "(\\p{XDigit}+)"; + // an exponent is 'e' or 'E' followed by an optionally + // signed decimal integer. + final String Exp = "[eE][+-]?"+Digits; + final String fpRegex = + ("[\\x00-\\x20]*"+ // Optional leading "whitespace" + "[+-]?(" + // Optional sign character + "NaN|" + // "NaN" string + "Infinity|" + // "Infinity" string + + // A decimal floating-point string representing a finite positive + // number without a leading sign has at most five basic pieces: + // Digits . Digits ExponentPart FloatTypeSuffix + // + // Since this method allows integer-only strings as input + // in addition to strings of floating-point literals, the + // two sub-patterns below are simplifications of the grammar + // productions from the Java Language Specification, 2nd + // edition, section 3.10.2. + + // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt + "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+ + + // . Digits ExponentPart_opt FloatTypeSuffix_opt + "(\\.("+Digits+")("+Exp+")?)|"+ + + // Hexadecimal strings + "((" + + // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt + "(0[xX]" + HexDigits + "(\\.)?)|" + + + // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt + "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" + + + ")[pP][+-]?" + Digits + "))" + + "[fFdD]?))" + + "[\\x00-\\x20]*");// Optional trailing "whitespace" + + return java.util.regex.Pattern.matches(fpRegex, getText()); + } + + /** Sets a double-value into the NumberField. */ + public void setDouble(double d) { + setText(String.valueOf(d)); + } + + /** Sets a double-value with N digits into the NumberField. */ + public void setDouble(double d, int N) { + setText(String.format(Locale.ENGLISH, "%." + N + "f", d)); + } + + /** Sets a float-value into the NumberField. */ + public void setFloat(float f) { + setText(String.valueOf(f)); + } + + /** Sets a float-value with N digits into the NumberField. */ + public void setFloat(float f, int N) { + setText(String.format(Locale.ENGLISH, "%." + N + "f", f)); + } + + /** Sets an int-value into the NumberField. */ + public void setInt(int i) { + setText(String.valueOf(i)); + } + + /** Sets a long-value into the NumberField. */ + public void setLong(long l) { + setText(String.valueOf(l)); + } + + /** Clears the NumberField */ + public void clear() { + setText(""); + } + + protected void processKeyEvent(KeyEvent e) { + super.processKeyEvent(e); + if (isNumeric() || getText().equals("-") || + getText().equals("") || getText().equals(".")) + setBackground(Color.white); + else + setBackground(Color.red); + } + +} \ No newline at end of file diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/balkendiagramm.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/balkendiagramm.java new file mode 100644 index 0000000..ca4b7e8 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/balkendiagramm.java @@ -0,0 +1,85 @@ +import java.awt.*; +import java.awt.geom.Line2D; +import java.awt.geom.Rectangle2D; +import javax.swing.*; + +public class Balkendiagramm extends JPanel { + + private double gesamt; + private double haeufigkeit[]; + private double maxwert; + private int verschiebung = 0; + private double haeufigkeit_de[] = { 0.0651, 0.0189, 0.0306, 0.0508, 0.174, 0.0166, 0.0301, 0.0476,0.0755, 0.0027, 0.0121,0.0344,0.0252,0.0978,0.0251,0.0079,0.0002,0.07,0.0727,0.0615,0.0435,0.067,0.0189,0.0003,0.0004,0.0113}; + private boolean zeigeDeutsch = true; + + + public void setHaeufigkeit(double haeufigkeit[]) { + + this.haeufigkeit = haeufigkeit; + gesamt = 0; + for (int i = 0; i < 26; i++) { + gesamt += haeufigkeit[i]; + } + for (int i = 0; i < 26; i++) { + haeufigkeit[i] /= gesamt; + } + maxwert = 0.20; + + } + + public void setVerschiebung(int verschiebung) { + if (verschiebung >= 0 && verschiebung < 26) { + this.verschiebung = verschiebung; + + } else { + this.verschiebung = 0; + } // end of if-else + } + public void setZeigeDeutsch(boolean show) { + zeigeDeutsch = show; + } + + + public void paintComponent(Graphics g) { + Graphics2D g2 = (Graphics2D) g; + super.paintComponent(g2); + Dimension d = getSize(null); + //Skalieren auf 100x100 mit y-Invertierung + g2.scale(d.getWidth() / 120.0, -d.getHeight() / 100.0); + // Ursprung verschieben nach (15,-90) + g2.translate(5, -90); + // Linien etwas breiter + g2.setStroke(new BasicStroke(1.5f)); + // x-Achse zeichnen + g2.draw(new Line2D.Double(0, 0, 104, 0)); + // Linien etwas dünner + g2.setStroke(new BasicStroke(0.5f)); + // Deutsche Häufigkeit grau + g2.setColor(Color.darkGray); + if(zeigeDeutsch) { + + // Säulen zeichnen + for (int i = 0; i < 26; i++) { + g2.fill(new Rectangle2D.Double(4 * i+1, 0, 2, haeufigkeit_de[i] / maxwert *100)); + } + } + + // Texthäufigkeit blau + g2.setColor(Color.blue); + // Säulen zeichnen + for (int i = 0; i < 26; i++) { + g2.fill(new Rectangle2D.Double(4 * i, 0, 2, haeufigkeit[(i+verschiebung)%26] / maxwert *100)); + } + // y-Invertierung rückgängig machen + g2.scale(1, -1); + // Beschriftung x-Achse + String xAchse = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + // Fontgröße für Beschriftung ändern + g2.setColor(Color.black); + g2.setFont(g2.getFont().deriveFont(5f)); + for (int i = 0; i < 26; i++) { + g2.drawString(xAchse.substring(i, i + 1), + i * 4, +6); + } + } +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/baustein.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/baustein.java new file mode 100644 index 0000000..55954a1 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/baustein.java @@ -0,0 +1,42 @@ +public class Baustein implements Comparable{ + + // Anfang Attribute + private String zeichen; + private int anzahl; + // Ende Attribute + + public Baustein(String zeichen) { + super(); + anzahl = 1; + this.zeichen = zeichen; + } + // Anfang Methoden + + + + public int compareTo(Baustein c) { + + int anz = c.getAnzahl(); + + //ascending order + return anz - this.anzahl; + + //descending order + //return compareQuantity - this.quantity; + + } + public String getZeichen() { + return zeichen; + } + + public int getAnzahl() { + return anzahl; + } + + public void incAnzahl() { + anzahl++; + } + + + // Ende Methoden +} \ No newline at end of file diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/breakvigenere.jar b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/breakvigenere.jar new file mode 100644 index 0000000..cb3748e Binary files /dev/null and b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/breakvigenere.jar differ diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/breakvigenere.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/breakvigenere.java new file mode 100644 index 0000000..e186561 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/breakvigenere.java @@ -0,0 +1,360 @@ +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import javax.swing.event.*; +import javax.swing.table.*; +import java.util.ArrayList; +import java.util.Collections; +import javax.swing.text.DefaultHighlighter; +import javax.swing.text.Highlighter; +import javax.swing.text.Highlighter.HighlightPainter; +import javax.swing.text.BadLocationException; + + +/** + * + * Programm zur Kryptoanalyse + * von monoalphabetischen Substitutionen + * + * @version 1.0 from 23.11.2016 + * @author Thomas Schaller + */ + +public class BreakVigenere extends JFrame { + // Anfang Attribute + private JTextField[] buchstabe; + + private JButton jButton1 = new JButton(); + private JButton jBBuchstabe = new JButton(); + private Balkendiagramm bdText = new Balkendiagramm(); + private Balkendiagramm bdBruteForce = new Balkendiagramm(); + private Balkendiagramm bdDeutsch = new Balkendiagramm(); + + private JTextArea jTAKrypto = new JTextArea(""); + private JScrollPane jTAKryptoScrollPane = new JScrollPane(jTAKrypto); + private JTextArea jTAKlartext = new JTextArea(""); + private JScrollPane jTAKlartextScrollPane = new JScrollPane(jTAKlartext); + private JLabel jLKryptotext = new JLabel(); + private JLabel jLKlartext = new JLabel(); + private JLabel jLCopyright = new JLabel(); + private JTextArea jTAKorrelation = new JTextArea(""); + private JScrollPane jTAKorrelationScrollPane = new JScrollPane(jTAKorrelation); + private JSpinner jSpVerschiebung = new JSpinner(); + private SpinnerNumberModel jSpVerschiebungModel = new SpinnerNumberModel(0, 0, 99, 1); + private JNumberField jNFAnzahlTreffer = new JNumberField(); + private JLabel lVerschiebung = new JLabel(); + private JLabel lAnzahlgleicherBuchstaben = new JLabel(); + // Ende Attribute + + public BreakVigenere (String title) { + super (title); + setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); + int frameWidth = 702; + int frameHeight = 651; + setSize(frameWidth, frameHeight); + Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); + int x = (d.width - getSize().width) / 2; + int y = (d.height - getSize().height) / 2; + setLocation(x, y); + Container cp = getContentPane(); + cp.setLayout(null); + // Anfang Komponenten + + jTAKryptoScrollPane.setBounds(16, 24, 665, 105); + jTAKrypto.setLineWrap(true); + jTAKrypto.setFont(new Font("Courier New", Font.BOLD, 16)); + jTAKrypto.setText("JMLQMFNSKIOQNXGYZOQEBJLUKXHFCXGVLGBMREAJZCAEKVIFOWJLMFGJLPQZJIYHQQHYJLAFGFLRLQYESTPMHIAWQZFIPPMZMIZGPDOIIIVGTHIIQVKHLVHQOPLNMIKMSWCYKMUIVBREADEQOXLVVMILSMVWYZLVAONSIIVIKVKIVPOIZIEGXHLHCDILKIVPKYAWKTKRIIVQJMRXQZKVHFBVULHRVQYXYMBTKQPYAUSNHLZUSJUJBQTFHRLEKMUIAUTPHXMUTMZGPQXWWVIONINIAONVPIJQTIUWMONWIRLUMIUAMDQIZTWXEKYEXTOELPQNXMZIFEKGOWJONIYDCDVSSCODGTOMMMTKLKMNKRPRLQSRHGPEKMUIUFUHLZMDLJLRBXOGOXMZHYJLAONPBKMDBSYRIONNLHMYKMUDMXTIUOTMXXLBBNAGOWBMHIUDCYTGOWBQTESTPMHIAMVEKMUIZFGFBPINKVGYOQNIUYVPYSHPTQBIYJONGVLRIXVLHFMFKEBWHGTYADMZJETMBQXJHRLQXHPIXDUKYIAEOZLTWXEESTPMHIAMAONIJLQRLVPIZGTKHFMDKWDEZZUGOIQZLIZXMEBIYJITXIUSPZKWJLTEYISHQQYIYACDJIPQRMNVCSUUZESMMZOWJLMZQVFTBARSNIVSOSCEVNGXAMAFGFLPTMYSJEQZLSYQMUTIZZWYBIYWKTRWZPMDLVLMHGCLSIVPKRRIVZCSYXAAJIYIQZKWRIVZYEADMEBSYKMEILSEOQTKLVVQARKOZKVXVKZMVLPWKTYGOAIONHHPMUILADCQXVHXMZCYYHMZJETETEREAIQZOWJLMEORUWXDILLFMZAXGXEUKFLMABOISWEQOWLZQDZZAMWYTMHTIDKRAETXKWNIPAXGOXLQXXJLBUMOLMBPOIIYKTYXHFMZJIZOMZTWHXHQYFLWBUSQLRLUKVLMPQTJVPOQORKIZPOICIZEILPILQTIUETBNEIIBQGYZHMDZEIYTMGYZKMINPAA\nMDJIUQAEKRZMVPGPSIDQXFYECONXHPAAKMUQIXHIUYBLZAVVLQTLPIZZGGOOTMXXLBBNAGOWBMHIUWWNKKPRVFSEUAQQJIYZWZBSYRMENEUHMXZWPGPEUQPXCYKMUIXQXMVHQEILLTWXEESTPMHIAMAONIZYJEZMAYBUURTMBUSFLMABOISIQZKVWIZUUHLZWZCLYIVPZVPXPQSMBWAUILHYNHKVZGPAHIUIAFGRKEZPGPWLINKXLFMEILYRSFKESWWPOINIEANRAIIXVLHFMFOWJLMDKMOIVRUPNILQXFBGPEZEIIVHKVDIVPKXLFMXREZSJQXIPXAHKVDVNQRXLETBNEIIBQJMLIZMRPLVLUTKZMVHUPBXWDOWJLETRXLFMUJIUQWZUESTPMHIAMAONIUWCNYXPXCFOSUWUQZLVHMZCEYHQQYWJLWZYIPXTMTKLQJXOGOKZGTHZXHXOGOLIFZIKMMEHIYIQFYMTNITXESWWXGRNIDAXXYMBTKQPYAGTHIITXGWVHMDOXHPQQTMZGPQMISIPDZISIWZHEAXQEZEHPJQXXPIUBLSOPMZKVZGPXAKCSZPOIHPXTGFLXMZGGONMIKMSWLDKMVHMDBMLVEDZIYRHGCIJLAQRRHPAYKGOEVUYGOIATOPMWUUZXLPPMZXLIZTOIYDCPOIUEKTOLTFMZGRUXMMRFLVBUYGOIQNKIYJCZJIUIQZKGOMNRXMLVAONIPFMNKWAIPQTHGYUQOWAECEFALMZGTHLRUQZESPAONIPFMZJMLECRKMUIZSKQLMVEGQLRIONWLWQFFIUYVPYSCIZNARKIVEORKHIEYWPGPPOIRPMUTIYIIGLHLVODKVLRLDKLLRSMTRUEPQFYLMVVGLYLCZJIYXABZIYMUVGLYZMDGPSKMYKMUIZFKHLVVQGTVPQFGRPWKTKKLPMTXXLKQABEURQNGXAMAFGHLPTMVSYXIMRFLVBUYZLVEDLISXMMRTOEJQZIBRLNKPSEAAYOLRVIUVAYVPYGOYNPOI\nWSTKGPWLINKXPWKTKGOMNRXMLVCZMQPXDQXAYJMXZITETBNEIIBGTHZGPXYWLPPUKVGYEGXHLETNKVAMAEILLMJQHIUYBLZHPIVMILQILQSFBGPEZEIIVLAHYIPQTAHVQYPEOVODOJMHMDLVHRHAYIIPIUYIKIDUMIUVMBUVAEAUJILECRARKWKTRYNZWDYXHXBPKVHPJQXXPWKTKMIILUKXHFCXGVLGBMJIZXZUZLLQQGYDBZMDCIUHMZJEIIQMHIYEVPKVZETEOQVVQSORHPDQXAYJMXZIHPXTGFLXMQORGYBDGKLRLUKWLVSDETASODGTOMAONWAEZWKZVVAONPHKDUMIUVMEMIYMMFORKIVRUPNIVPKRQEPDNYUHMDZIUMVHKVNIAEKROIQFARKHQQAVZTZZMPPGPHURAVQFNITMCEBSYKMEILSEOQTITIBTUHLACDJIBRBQXHLQVMSIUZQSKRYIKTOJMVMNKOHRVF"); + jTAKrypto.addKeyListener(new KeyAdapter() { + public void keyTyped(KeyEvent evt) { + jTAKrypto_KeyTyped(evt); + } + }); + cp.add(jTAKryptoScrollPane); + + jTAKlartextScrollPane.setBounds(16, 480, 665, 105); + jTAKlartext.setLineWrap(true); + jTAKlartext.setFont(new Font("Courier New", Font.BOLD, 16)); + cp.add(jTAKlartextScrollPane); + jLKryptotext.setText("Kryptotext"); + jLKryptotext.setBounds(16, 3, 110, 20); + cp.add(jLKryptotext); + jLKlartext.setText("Klartext"); + jLKlartext.setBounds(16, 456, 110, 20); + cp.add(jLKlartext); + jLCopyright.setText("(cc) Schaller (ZPG Informatik) - V1.0 (2017)"); + jLCopyright.setBounds(128, 584, 419, 33); + jLCopyright.setHorizontalAlignment(SwingConstants.CENTER); + jLCopyright.setFont(new Font("Dialog", Font.PLAIN, 12)); + cp.add(jLCopyright); + + // -------------------------------- Tab 1 --------------------------------------------- + JPanel tab1 = new JPanel(); + tab1.setLayout(null); + + jSpVerschiebung.setBounds(192, 40, 41, 25); + jSpVerschiebung.setValue(1); + jSpVerschiebung.setModel(jSpVerschiebungModel); + jSpVerschiebung.addChangeListener(new ChangeListener() { + public void stateChanged(ChangeEvent evt) { + jSpVerschiebung_StateChanged(evt); + } + }); + tab1.add(jSpVerschiebung); + + lVerschiebung.setBounds(16, 10, 310, 20); + lVerschiebung.setText("Angriff auf die Schlüssellänge mit Brute Force"); + tab1.add(lVerschiebung); + JLabel l = new JLabel(); + l.setBounds(14, 80, 320, 100); + l.setText("Die meisten Schlüsselwörter sind nicht länger als 10 bis maximal 15 Buchstaben. Probiere daher systematisch die Schlüssellänge durch und schaue, ob die Häufigkeitsverteilungen des ersten Teiltextes in etwa dem deutschen Alphabet entspricht. Eine Verschiebung ist dabei erlaubt."); + tab1.add(l); + l = new JLabel(); + l.setBounds(16, 40, 162, 20); + l.setText("Vermutete Schlüssellänge:"); + tab1.add(l); + + double[] h2 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + bdBruteForce.setBounds(380,10,300,120); + bdBruteForce.setHaeufigkeit(h2); + bdBruteForce.setZeigeDeutsch(false); + tab1.add(bdBruteForce); + + double[] h3 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + bdDeutsch.setBounds(380,140,300,120); + bdDeutsch.setHaeufigkeit(h3); + tab1.add(bdDeutsch); + + + + // ---------------------------------- Tab 2 ------------------------------- + JPanel tab2 = new JPanel(); + tab2.setLayout(null); + + l = new JLabel(); + l.setBounds(16, 10, 310, 20); + l.setText("Angriff auf die Teiltexte mit Häufigkeitsanalyse"); + tab2.add(l); + + l = new JLabel(); + l.setBounds(380, 10, 300, 25); + l.setText("Buchstabenhäufigkeit Deutsch"); + tab2.add(l); + + l = new JLabel(); + l.setBounds(16, 40, 300, 25); + l.setText("Schlüssel"); + tab2.add(l); + + l = new JLabel(); + l.setBounds(16, 100, 300, 100); + l.setText("Versuche den Schlüssel zu knacken, indem du in jedem Teiltext die Häufigkeitsverteilung der Buchstaben mit der üblichen Häufigkeitsverteilung in deutschen Texten in Einklang bringst."); + tab2.add(l); + + double[] h = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + bdText.setBounds(380,40,300,180); + bdText.setHaeufigkeit(h); + tab2.add(bdText); + + jButton1.setBounds(16, 210, 193, 25); + jButton1.setText("Entschlüsselung versuchen"); + jButton1.setMargin(new Insets(2, 2, 2, 2)); + jButton1.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent evt) { + jButton1_ActionPerformed(evt); + } + }); + tab2.add(jButton1); + + + + // Ende Komponenten + buchstabe = new JTextField[26]; + for (int i = 0; i<26 ;i++ ) { + buchstabe[i] = new JTextField(); + buchstabe[i].setBounds(16+i*25, 65, 25, 25); + buchstabe[i].setText("A"); + buchstabe[i].setHorizontalAlignment(SwingConstants.CENTER); + buchstabe[i].setVisible(false); + buchstabe[i].addFocusListener(new FocusAdapter() { + public void focusGained(FocusEvent evt) { + highlightKryptotext(evt); + } + }); + buchstabe[i].addKeyListener(new KeyAdapter() { + public void keyReleased(KeyEvent e) { + e.consume(); + } + + public void keyTyped(KeyEvent e) { + e.consume(); + } + + public void keyPressed(KeyEvent e) { + JTextField textField = (JTextField) e.getSource(); + String text = textField.getText(); + if ((e.getKeyChar()>='a' && e.getKeyChar()<='z') ||(e.getKeyChar()>='A' && e.getKeyChar()<='Z')) { + text = ""+e.getKeyChar(); + } + if (e.getKeyChar()==' ' || e.getKeyChar() == KeyEvent.VK_BACK_SPACE || e.getKeyChar() == KeyEvent.VK_DELETE) { + text = ""; + + } // end of if + textField.setText(text.toUpperCase()); + e.consume(); + if (textField.getText().length()>0) { + bdText.setVerschiebung((int) textField.getText().charAt(0)-65); + + } else { + bdText.setVerschiebung(0); + } // end of if-else + bdText.repaint(); + + } // end of if + + }); + + tab2.add(buchstabe[i]); + } // end of for + + JTabbedPane tabpane = new JTabbedPane + (JTabbedPane.TOP,JTabbedPane.SCROLL_TAB_LAYOUT ); + tabpane.setBounds(10,140,672,290); + + tabpane.addTab("Schritt 1", tab1); + tabpane.addTab("Schritt 2", tab2); + cp.add(tabpane); + + setResizable(false); + setVisible(true); + } + + + + // Anfang Methoden + public void jButton1_ActionPerformed(ActionEvent evt) { + String krypttext = jTAKrypto.getText().toUpperCase(); + String klartext=""; + int c = 0; + int diff = (Integer) jSpVerschiebung.getValue(); + for (int i=0;i=65 && asc<=90) { + h[asc-65]++; + } + } // end of if + c++; + } // end of if + } + bdText.setHaeufigkeit(h); + if (buchstabe[start].getText().length()>0) { + bdText.setVerschiebung((int) buchstabe[start].getText().charAt(0)-65); + + } else { + bdText.setVerschiebung(0); + } // end of if-else + bdText.repaint(); + } catch(BadLocationException e) {} + } + + + public void jSpVerschiebung_StateChanged(ChangeEvent evt) { + String krypto = jTAKrypto.getText().toUpperCase(); + String krypto2=""; + int diff = (Integer) jSpVerschiebung.getValue(); + Highlighter h1 = jTAKrypto.getHighlighter(); + h1.removeAllHighlights(); + int c = 0; + double[] h = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + try{ + for (int i=0;i=65 && asc<=90) { + h[asc-65]++; + } + } // end of if + c++; + } // end of if + } + bdBruteForce.setHaeufigkeit(h); + bdBruteForce.repaint(); + } catch(BadLocationException e) {} + + for (int i=0; iRegula Hoefer-Isenegger
+@version 0.1 +*/ +public class LineRenderer { + private Point2D.Double point; + private Turtle turtle; + + LineRenderer(Turtle turtle) { + this.point = new Point2D.Double(); + this.turtle = turtle; + } + + /** Initialisation with coordinates x and + y. + */ + public void init(double x, double y) { + this.point.setLocation(x, y); + } + /** Same as init(double x, double y), but with a Point2D.Double + argument for convenience + */ + public void init(Point2D.Double p) { + this.init(p.x, p.y); + } + /** Get the current x-coordinate in screen coordinates.*/ + private double getX(){ + return (toScreenCoords(point)).getX(); + } + /** Get the current x-coordinate in screen coordinates.*/ + private double getY(){ + return toScreenCoords(point).getY(); + } + /** Calls the clipLineTo and wrapLineTo methods, according + to the turtle's edge behavior. + + Only overwrite this method when working with another + (one that you have defined) edge behaviour.
+ If you mean to change the manner of drawing lines, do this in + the methods clipLineTo() and wrapLineTo(). + @see #clipLineTo + @see #wrapLineTo + */ + protected void internalLineTo(double x, double y){ + Point2D.Double screenPos = toScreenCoords(x, y); + if(turtle.isClip()){ + clipLineTo(screenPos.getX(), screenPos.getY()); + } + if (turtle.isWrap()){ + wrapLineTo(screenPos.getX(), screenPos.getY()); + } + init(x,y); + } + /** Calls the internalLineTo(x,y), which does the actual painting. + */ + public void lineTo(double x, double y) { + internalLineTo(x, y); + } + /** Calls the internalLineTo(x,y), which does the actual painting. + + This method works the same way as lineTo(double x, double y), but + is added for convenience. + */ + public void lineTo(Point2D.Double p) { + internalLineTo(p.getX(), p.getY()); + } + /** Does the actual painting for clip mode. + + It works already with ScreenCoords! + For further comments cf. internalLineTo(double x, double y). + @see #internalLineTo + */ + protected void clipLineTo(double x, double y){ + turtle.getPlayground().lineTo(getX(), getY(), x, y, turtle.getPen()); + } + /** Does the actual painting for wrap mode. + + It works already with ScreenCoords! + For further comments cf. internalLineTo(double x, double y). + @see #internalLineTo + */ + protected void wrapLineTo(double x, double y){ + double dx = getX() - x; + double dy = getY() - y; + Point2D.Double start = new Point2D.Double(x, y); + Point2D.Double end = new Point2D.Double(start.x+dx, start.y+dy); + + intoPanel(start, end); + Point2D.Double tmp; + while ((tmp = calcIntersection(start.x, start.y, end.x, end.y)) != null){ + turtle.getPlayground().lineTo(start.x, start.y, tmp.getX(), tmp.getY(), turtle.getPen()); + start = tmp; + intoPanel(start, end); + dx = end.x - start.x; + dy = end.y - start.y; + } + + turtle.getPlayground().lineTo(start.x, start.y, end.x, end.y, turtle.getPen()); + } + /** Makes the coordinates fit into the Panel. + + Well, this is some sort of modulus calculation. + */ + private void intoPanel(Point2D.Double start, Point2D.Double end){ + int pWidth = turtle.getPlayground().getWidth(); + int pHeight = turtle.getPlayground().getHeight(); + while(start.x < 0){ + start.x += pWidth; + end.x += pWidth; + } + while (start.x > pWidth){ + start.x -= pWidth; + end.x -= pWidth; + } + if(start.x == 0 && end.x < start.x){ + start.x += pWidth; + end.x += pWidth; + } + if(start.x == pWidth && end.x > start.x){ + start.x -= pWidth; + end.x -= pWidth; + } + while(start.y < 0){ + start.y += pHeight; + end.y += pHeight; + } + while (start.y > pHeight){ + start.y -= pHeight; + end.y -= pHeight; + } + if(start.y == 0 && end.y < start.y){ + start.y += pHeight; + end.y += pHeight; + } + if(start.y == pHeight && end.y > start.y){ + start.y -= pHeight; + end.y -= pHeight; + } + + } + /** Intersection line with playground-edges + (startX / startY) MUST lie in the playground! + */ + private Point2D.Double calcIntersection(double startX, double startY, double endX, double endY){ + double dx = endX - startX; + double dy = endY - startY; + double W = turtle.getPlayground().getWidth(); + double H = turtle.getPlayground().getHeight(); + if(endX < 0){ + if((dy/dx <= startY/startX) && (dy/dx >= -(H-startY)/startX)){ // links + return new Point2D.Double(0, startY-startX*dy/dx); + } + } + else if(endX > W){ + if((dy/dx >= -startY/(W-startX)) && (dy/dx <= (H-startY)/(W-startX))){// rechts + return new Point2D.Double(W, startY+(W-startX)*dy/dx); + } + } + if(endY < 0){ // oben + return new Point2D.Double(startX-startY*dx/dy, 0); + } + else if(endY > H){ // unten + return new Point2D.Double(startX+(H-startY)*dx/dy, H); + } + else{ + return null; // Endpoint lies in the window + } + } + /** Calculates the screen coordinates of the turtle's actual + position according to the interpretation of the playground. + */ + private Point2D.Double toScreenCoords(double x, double y){ + return turtle.getPlayground().toScreenCoords(x, y); + } + /** Calculates the screen coordinates of the turtle's actual + position according to the interpretation of the playground. + + Added for convenience. + @see #toScreenCoords(double, double) + */ + private Point2D.Double toScreenCoords(Point2D.Double p){ + return turtle.getPlayground().toScreenCoords(p.x, p.y); + } +} + diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/pen.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/pen.java new file mode 100644 index 0000000..8fcc8b7 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/pen.java @@ -0,0 +1,268 @@ +// Pen.java + +// Copyright 2002 Regula Hoefer-Isenegger +// +// This file is part of The Java Turtle package (TJT) +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package ch.aplu.turtle; + +import ch.aplu.turtle.*; +import java.awt.Color; +import java.awt.BasicStroke; +import java.awt.Stroke; +import java.awt.Font; +import java.awt.GraphicsEnvironment; + +/** The Pen class provides anything used for drawing the lines, such as line width, + pen color, end caps, dashed lines, etc. + + @see java.awt.BasicStroke + + @author Regula Hoefer-Isenegger + @version 0.1.1 +*/ +public class Pen +{ + /* Attributes *********************************************/ + + /** The default font that is used when drawing Text. + + First argument must be one of "Serif", "SansSerif", "Monotyped", "Dialog" or "DialogInput" + to guarantee that this font exists on all systems. + + @see java.awt.Font for more information, e.g. on font styles. + + */ + public static Font DEFAULT_FONT = new Font("SansSerif", Font.PLAIN, 24); + private Color color; + private Color fillColor; + private BasicStroke stroke; + private Font font; + + /* Constructors *******************************************/ + /** Constructor with standard Color and standard Stroke. + + @see java.awt.BasicStroke + */ + public Pen(){ + color = Color.black; + setFillColor(Color.black); + stroke = new BasicStroke(); + font = DEFAULT_FONT; + } + /** Constructor with Color color and standard Stroke. + + @see java.awt.BasicStroke + */ + public Pen(Color color){ + this.color = color; + setFillColor(color); + stroke = new BasicStroke(); + font = DEFAULT_FONT; + } + /* Methods ************************************************/ + /** Query the Pens color.*/ + public Color getColor(){ + return color; + } + /** Set the Pens color.*/ + public void setColor(Color color){ + this.color = color; + } + /** Set the Pens fill color. + */ + public void setFillColor(Color color){ + this.fillColor = color; + } + /** Query the Pens fill color.*/ + public Color getFillColor(){ + return this.fillColor; + } + /** Get the Pens Stroke + + @see BasicStroke + @see Stroke + */ + public Stroke getStroke(){ + return stroke; + } + /** Query the Pens line width*/ + public float getLineWidth(){ + return stroke.getLineWidth(); + } + /** Query the Pens end cap style. + + @see java.awt.BasicStroke + */ + public int getEndCap(){ + return stroke.getEndCap(); + } + /** Query the Pens line join style. + + @see java.awt.BasicStroke + */ + public int getLineJoin(){ + return stroke.getLineJoin(); + } + /** Query the Pens miter limit style. + + @see java.awt.BasicStroke + */ + public float getMiterLimit(){ + return stroke.getMiterLimit(); + } + /** Query the Pens dash array. + + @see java.awt.BasicStroke + */ + public float[] getDashArray(){ + return stroke.getDashArray(); + } + /** Query the Pens dash phase. + + @see java.awt.BasicStroke + */ + public float getDashPhase(){ + return stroke.getDashPhase(); + } + + /** Set the Pens line width. */ + public void setLineWidth(float width){ + stroke = new BasicStroke((float)width, + stroke.getEndCap(), + stroke.getLineJoin(), + stroke.getMiterLimit(), + stroke.getDashArray(), + stroke.getDashPhase()); + } + /** Set the Pens end cap style. + + @see java.awt.BasicStroke + */ + public void setEndCap(int endCap){ + stroke = new BasicStroke(stroke.getLineWidth(), + endCap, + stroke.getLineJoin(), + stroke.getMiterLimit(), + stroke.getDashArray(), + stroke.getDashPhase()); + } + /** Set the Pens line join style. + + @see java.awt.BasicStroke + */ + public void setLineJoin(int join){ + stroke = new BasicStroke(stroke.getLineWidth(), + stroke.getEndCap(), + join, + stroke.getMiterLimit(), + stroke.getDashArray(), + stroke.getDashPhase()); + } + /** Set the Pens miter limit. + + @see java.awt.BasicStroke + */ + public void setMiterLimit(float miterlimit){ + stroke = new BasicStroke(stroke.getLineWidth(), + stroke.getEndCap(), + stroke.getLineJoin(), + miterlimit, + stroke.getDashArray(), + stroke.getDashPhase()); + } + /** Set the Pens dash array. + + @see java.awt.BasicStroke + */ + public void setDash(float[] dashArray){ + stroke = new BasicStroke(stroke.getLineWidth(), + stroke.getEndCap(), + stroke.getLineJoin(), + stroke.getMiterLimit(), + dashArray, + stroke.getDashPhase()); + } + /** Set the Pens dash phase. + + @see java.awt.BasicStroke + */ + public void setDashPhase(float dashPhase){ + stroke = new BasicStroke(stroke.getLineWidth(), + stroke.getEndCap(), + stroke.getLineJoin(), + stroke.getMiterLimit(), + stroke.getDashArray(), + dashPhase); + } + /** Provides information about the currently available font families (e.g. "Roman"). + Each font name is a string packed into a array of strings. + @see java.awt.Font for more information about font attributes etc. + */ + public static String[] getAvailableFontFamilies(){ + GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); + String s[] = ge.getAvailableFontFamilyNames(); + return s; + } + /** Change the font style. + + @see java.awt.Font for possible styles. + */ + public void setFontStyle(int style){ + font = font.deriveFont(style); + } + /** Change the font size (in points). + */ + public void setFontSize(int size){ + font = font.deriveFont((float)size); + } + /** Change the font size (in points). + You will probably only need the int version setFontSize(int). + */ + public void setFontSize(float size){ + font = font.deriveFont(size); + } + /** Query the size (in points, rounded to int) of the current font. + */ + public int getFontSize(){ + return font.getSize() ; + } + /** Change the font to the given one. + */ + public void setFont(Font f){ + font = f; + } + /** Query the current font. + */ + public Font getFont(){ + return font; + } +} + + + + + + + + + + + + + + diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/playground.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/playground.java new file mode 100644 index 0000000..6c0db5b --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/playground.java @@ -0,0 +1,789 @@ +// Playground.java + +// Copyright 2002 Regula Hoefer-Isenegger +// Major code modifications by Aegidius Pluess +// Adaption for the Java-Editor by Gerhard Röhner +// +// This file is part of The Java Turtle package (TJT) +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package ch.aplu.turtle; + +import ch.aplu.turtle.*; +import java.util.*; +import javax.swing.*; +import java.awt.*; +import java.awt.print.*; +import java.awt.geom.*; +import java.awt.image.*; +import java.awt.font.*; + +/** + A Playground is the Turtle's home, i.e. the Turtle lives + and moves in the Playground. + + ThePlayground is responsible for interpreting angle and position of the + Turtle and for choosing the correct turtle image and putting it on the right + spot of the Playground. This means: e.g. whenever you wish to switch the x- and y-axis, you + should do it in this class, and not in the Turtle class. + + @author Regula Hoefer-Isenegger + @version 0.1.1 +*/ + +public class Playground extends JPanel implements Printable { + + /** Hold the Turtles of this Playground. */ + private Vector turtles; + + /** Hold the offscreen buffer and graphics context + * where Turtle traces are drawn. + */ + private BufferedImage traceBuffer = null; + protected Graphics2D traceG2D = null; + private Dimension playgroundSize; + + /** Hold the offscreen buffer and graphics context + * of the Turtles images. + */ + private BufferedImage turtleBuffer = null; + protected Graphics2D turtleG2D = null; + + /** Flag to tell whether we have at least one Turtle shown. */ + private boolean isTurtleVisible = false; + + /** Flag to tell whether we use automatic repainting */ + private boolean isRepainting = true; + + /** The default background color. + */ + protected static Color DEFAULT_BACKGROUND_COLOR = Color.white; + + private double printerScale = 1; // Default printer scaling + private TPrintable traceCanvas; // Store ref to user class + private Graphics2D printerG2D = null; + private boolean isPrintScreen = false; // Indicate we are printing the playground + private double printerScaleFactor = 1.1; // Magnification factor for printer + + /** + * originX is the x-position of the cartesian coodinate system within the playground. + */ + public int originX; + + /** + * originY is the y-position of the cartesian coodinate system within the playground. + */ + public int originY; + + private static Color[] ColorArray = {Color.cyan, Color.red, Color.green, Color.blue, Color.yellow, + Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.black, Color.gray + }; + + /** + * Create a Playground with default background color. + * e.g. creates a new vector (which holds the + * Turtles), + */ + public Playground() { + turtles = new Vector(); + setDoubleBuffered(false); + setBackground(DEFAULT_BACKGROUND_COLOR); + initBuffers(new Dimension(100, 100)); + } + + /** + * Initializes the offscreen buffers and sets the size. + */ + protected void initBuffers(Dimension size) { + Color bkColor = getBackground(); + playgroundSize = size; + traceBuffer = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB); + traceG2D = traceBuffer.createGraphics(); + traceG2D.setColor(bkColor); + traceG2D.fillRect(0, 0, size.width, size.height); + traceG2D.setBackground(bkColor); + + turtleBuffer = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB); + turtleG2D = turtleBuffer.createGraphics(); + turtleG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + originX = size.width/2; + originY = size.height/2; + } + + /** Add a new Turtle to the Playground. + */ + public void add(Turtle turtle) { + turtles.add(turtle); + turtle.setPlayground(this); + int i = turtles.size(); + while (i > 10) { + i = i - 10; + } // end of while + turtle.init(ColorArray[i-1]); + toTop(turtle); + } + + public void setBounds(int x, int y, int width, int height) { + super.setBounds(x, y, width, height); + initBuffers(new Dimension(width, height)); + } + + /** Remove a Turtle from the Playground. + */ + public void remove(Turtle turtle) { + turtles.remove(turtle); + } + + /** Tell current number of Turtles in this Playground. + */ + public int countTurtles() { + return turtles.size(); + } + + /** Return the Turtle at index index. + */ + public Turtle getTurtle(int index) { + return turtles.elementAt(index); + } + + /** Move the given Turtle above all the others, then + paints all turtles. + @see #toTop + */ + public void paintTurtles(Turtle turtle) { + toTop(turtle); + paintTurtles(); + } + + /** Paint all turtles (calling paintComponent()) + */ + public void paintTurtles() { + isTurtleVisible = false; + Graphics2D g2D = getTurtleG2D(); + for (int i = 0; i < countTurtles(); i++) { + Turtle aTurtle = getTurtle(i); + if (!aTurtle.isHidden()) { + paintTurtle(aTurtle); + } + } + + // This is the main repaint call, when the turtle is + // moving (even when all turtles are hidden). + // Strange behaviour an slow Mac machines (pre J2SE 1.4 version): + // It happens that some turtle images are not completely redrawn. + // This is probably due to an improper handling of fast multiple repaint requests. + // Workaround: we wait a small amount of time (and give the thread away) + // (No visible slow down on new machines.) + + paintPlayground(); + if (printerG2D == null) { + //if (isRepainting) + //repaint(); + //paintPlayground(); + } + + if (isTurtleVisible) { + try { + Thread.currentThread().sleep(10); + } + catch (Exception e) {} + } + } + + + /** Paint the given Turtle. + * ( no repaint() ) + */ + public void paintTurtle(Turtle turtle) { + if (turtleBuffer == null){ + turtleBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); + turtleG2D = turtleBuffer.createGraphics(); + } + Graphics2D turtleGraphics = getTurtleG2D(); + turtle.getTurtleRenderer().paint(turtle._getX(), turtle._getY(), turtleGraphics); + isTurtleVisible = true; + } + + /** Put an image of the given Turtle in turtle buffer. + */ + protected void stampTurtle(Turtle turtle) { + turtle.clone(); + isTurtleVisible = true; + if (printerG2D == null) + repaint(); + } + + /** Draw a line from the point (x0, y0) to (x1, y1) + with the color of the given Pen. + */ + protected void lineTo(double x0, double y0, double x1, double y1, Pen pen) { + int ix0 = (int)Math.round(x0); + int iy0 = (int)Math.round(y0); + int ix1 = (int)Math.round(x1); + int iy1 = (int)Math.round(y1); + Color color = pen.getColor(); + + if (traceBuffer == null) { + traceBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); + traceG2D = traceBuffer.createGraphics(); + } + Graphics2D traceG2D = getTraceG2D(); + traceG2D.setColor(color); + traceG2D.setStroke(pen.getStroke()); + traceG2D.drawLine(ix0, iy0, ix1, iy1); + if (printerG2D != null) + printerG2D.drawLine(ix0, iy0, ix1, iy1); + } + + /** A class for convenience. */ + protected class Point extends java.awt.Point { + Point(int x, int y) { + super(x, y); + } + Point() { + super(); + } + Point(Point p) { + super(p.x, p.y); + } + + /** Get a new Point with coordinates (this.x+p.x, this.y+p.y). + */ + protected Point add(Point p) { + return new Point(this.x+p.x, this.y+p.y); + } + + /** Translate by the amounts dx = p.x, dy = p.y. */ + protected void translate(Point p) { + translate(p.x, p.y); + } + + public String toString() { + return "(" + x + "," + y + ")"; + } + } + + /** Fill a region. + The region is defined by the Turtles actual position and + is bounded by any other color than the give background color. + */ + public void fill(Turtle t, Color bgColor) { + final Point[] diff = { new Point(0,-1), new Point(-1,0), new Point(1,0), new Point(0,1)}; + final int N=0; + final int W=1; + final int E=2; + final int S=3; + + int bgcolor = bgColor.getRGB(); + int fillColor = t.getPen().getFillColor().getRGB(); + Vector list = new Vector(); + Point2D.Double p1 = toScreenCoords(t.getPos()); + int startX = (int)Math.round(p1.getX()); + int startY = (int)Math.round(p1.getY()); + Point p = new Point(startX, startY); + if (traceBuffer.getRGB(startX, startY) == bgcolor) { + traceBuffer.setRGB(startX, startY, fillColor); + list.addElement(new Point(startX, startY)); + int d = N; + int back; + while (list.size() > 0) { + while (d <= S) { // forward + Point tmp = p.add(diff[d]); + try { + if (traceBuffer.getRGB(tmp.x, tmp.y) == bgcolor) { + p.translate(diff[d]); + traceBuffer.setRGB(p.x, p.y, fillColor); + if (printerG2D != null) + { + printerG2D.setColor(t.getPen().getFillColor()); + // printerG2D.drawLine(p.x,p.y, p.x, p.y); + BasicStroke stroke = new BasicStroke(2); + printerG2D.setStroke(stroke); + Line2D line = new Line2D.Double(p.x, p.y, p.x, p.y); + printerG2D.draw(line); + } + list.addElement(new Integer(d)); + d=N; + } + else { + d++; + } + } + catch (ArrayIndexOutOfBoundsException e) { + d++; + } + } + Object obj = list.remove(list.size()-1); + try { + d=((Integer)obj).intValue(); // last element + back = S - d; + p.translate(diff[back]); + } + catch (ClassCastException e) { + // the first (zeroest) element in list is the start-point + // just do nothing with it + } + } + } + traceG2D.drawLine(0, 0, 0, 0); // Workaround because on Mac the trace buffer is not drawn without this + if (printerG2D == null) + repaint(); + } + + /** + * Clear the playground with given color. + */ + public void clear(Color color) { + traceG2D.setColor(color); + traceG2D.fillRect(0, 0, getWidth(), getHeight()); + turtleG2D.setColor(color); + turtleG2D.fillRect(0, 0, getWidth(), getHeight()); + isTurtleVisible = true; + if (printerG2D == null) + repaint(); + } + + /** + * Clear playground. + */ + public void clear() { + clear(getBackground()); + } + + /** Paint the Playground. + just a method for convenience. + */ + public void paintComponent() { + paintComponent(getGraphics()); + } + + /** Draw the trace and turtle buffers. + */ + public void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2D = (Graphics2D)g; + g2D.drawImage(traceBuffer, 0, 0, this); + if (isTurtleVisible) + g2D.drawImage(turtleBuffer, 0, 0, this); + } + + public void paintPlayground() { + Graphics g = getGraphics(); + if (g != null) { + super.paintComponent(g); + Graphics2D g2D = (Graphics2D)g; + g2D.drawImage(traceBuffer, 0, 0, this); + if (isTurtleVisible) + g2D.drawImage(turtleBuffer, 0, 0, this); + + } // end of if + } + + /** Remove all turtles from the turtle buffer. + */ + public void clearTurtles() { + for (int i = 0; i < countTurtles(); i++) { + Turtle turtle = getTurtle(i); + clearTurtle(turtle); + } + } + + /** Remove the given turtle from the turtle buffer. + Override this method if you have added a new behaviour (like + wrap or clip) to the turtle. + */ + public void clearTurtle(Turtle turtle) { + if(turtle != null){ + if (!turtle.isHidden()) { + if(turtle.isClip()){ + clearClipTurtle(turtle); + } + else if(turtle.isWrap()){ + clearWrapTurtle(turtle); + } + } + } + } + + /** This method is called when the given Turtle is in wrap mode. + + @see ch.aplu.turtle.Turtle#wrap + */ + protected void clearWrapTurtle(Turtle turtle){ + clearWrapTurtle(turtle, turtleBuffer); + } + + /** Here the actual clearing of a Turtle in wrap mode from the + given image is performed. + */ + protected void clearWrapTurtle(Turtle turtle, Image im){ + Rectangle bounds = getBounds(turtle); + int pWidth = getWidth(); + int pHeight = getHeight(); + int x = bounds.x; + int y = bounds.y; + while (x > pWidth){ + x -= pWidth; + } + while (x < 0){ + x += pWidth; + } + while (y > pHeight){ + y -= pHeight; + } + while (y < 0){ + y += pHeight; + } + x = x % pWidth; + y = y % pHeight; + toAlphaNull(im, new Rectangle(x, y, bounds.width, bounds.height)); // OK + boolean right = (x + bounds.width > getWidth()); + boolean bottom = (y + bounds.height > getHeight()); + if (right) { + toAlphaNull(im, new Rectangle(x-pWidth, y, bounds.width, bounds.height)); + } + if (bottom) { + toAlphaNull(im, new Rectangle(x, y-pHeight, bounds.width, bounds.height)); + } + if (right && bottom) { + toAlphaNull(im, new Rectangle(x-pWidth, y-pHeight, bounds.width, bounds.height)); + } + } + + /** Copy and translate a given Rectangle. + */ + private Rectangle copyAndTranslate(Rectangle rect, int dx, int dy) { + return new Rectangle(rect.x+dx, rect.y+dy, + rect.width, rect.height); + } + + /** This method is called when the given Turtle is in clip mode. + + @see ch.aplu.turtle.Turtle#clip + */ + protected void clearClipTurtle(Turtle turtle) { + clearClipTurtle(turtle, turtleBuffer); + } + + /** Here the actual clearing of a Turtle in clip mode from the + given image is performed. + */ + protected void clearClipTurtle(Turtle turtle, Image im) { + Rectangle bounds = getBounds(turtle); + toAlphaNull(im, bounds); + } + + /** Set the alpha channel of all pixels in the given image + in the given Rectangle to zero (i.e. totally transparent). + + This method is used byte the clearXXXTurtle methods. + */ + private void toAlphaNull(Image im, Rectangle rect) { + Rectangle rim = new Rectangle(0, 0, im.getWidth(this), im.getHeight(this)); + Rectangle r = new Rectangle(); + if (rect.intersects(rim)) { + r=rect.intersection(rim); + } + int size = r.width*r.height; + float[] alphachannel = new float[r.width*r.height]; + ((BufferedImage)im).getAlphaRaster().setPixels(r.x, r.y, r.width, r.height, alphachannel); + } + + /** Puts a Turtle above all others. + */ + public Turtle toTop(Turtle turtle) { + if (turtles.removeElement(turtle)) { + turtles.add(turtle); + } + return turtle; + } + /** Put a Turtle below all others. + */ + public Turtle toBottom(Turtle turtle) { + if (turtles.removeElement(turtle)) { + turtles.add(0,turtle); + } + return turtle; + } + + /** Calculate the screen coordinates of the given point. + */ + public Point2D.Double toScreenCoords(Point2D.Double p) { + return internalToScreenCoords(p.x, p.y); + } + + /** Calculate the screen coordinates of the given point coordinates. + */ + public Point2D.Double toScreenCoords(double x, double y) { + return internalToScreenCoords(x, y); + } + + protected Point2D.Double internalToScreenCoords(double x, double y) { + // reflect at x-axis, then translate to center of Playground + // pixel coordinates coorespond to turtle coordinates, only translation needed + double newX = originX + x; + double newY = originY - y; + return new Point2D.Double(newX, newY); + } + + /** Calculate the turtle coordinates of the given screen coordinates. + */ + public Point2D.Double toTurtleCoords(double x, double y) { + // pixel coordinates coorespond to turtle coordinates, only translation needed + double newX = x - originX; + double newY = originY - y; + return new Point2D.Double(newX, newY); + } + + /** Calculate the turtle coordinates of the given screen point. + */ + public Point2D.Double toTurtleCoords(Point2D.Double p) { + return toTurtleCoords(p.x, p.y); + } + + /** Calculate the screen angle. + I.e. the interpretation of angle. + @param radians The angle in radians. + */ + double toScreenAngle(double radians) { + double sa = radians; + if (sa < Math.PI/2){ + sa += 2*Math.PI; + } + sa -= Math.PI/2; + if (sa != 0) { + sa = Math.PI*2 - sa; + } + return sa; + } + + /** Calculate the bounds of the Turtles picture on the screen. + */ + protected Rectangle getBounds(Turtle turtle) { + Rectangle bounds = turtle.getBounds(); + Point2D.Double tmp = toScreenCoords(new Point2D.Double(bounds.getX(), bounds.getY())); + bounds.setRect(tmp.x-2, tmp.y-2, bounds.width+4, bounds.height+4); + return bounds; + } + + /** Return the graphics context of the turtle buffer. + */ + public Graphics2D getTurtleG2D() { + return turtleG2D; + } + + /** Return the image of the turtle buffer. + */ + public BufferedImage getTurtleBuffer() { + return turtleBuffer; + } + + /** Return the graphics context of the trace buffer. + */ + public Graphics2D getTraceG2D() { + return traceG2D; + } + + /** Return the graphics context of the printer. + */ + public Graphics2D getPrinterG2D() { + return printerG2D; + } + + /** Return the image of the trace buffer. + */ + public BufferedImage getTraceBuffer() { + return traceBuffer; + } + + /** Clean the traces. + All turtles stay how and where they are, only lines, text and stamps will be removed. + */ + void clean() { + Graphics2D g = getTraceG2D(); + g.setColor(getBackground()); + g.fillRect(0,0,getWidth(), getHeight()); + if (printerG2D == null) + repaint(); + } + + /** Draw the text at the current position of the Turtle t. + Drawing a text at some coordinates (x,y) we mean that the bottom left corner of + the text will be at these coordinates. + Font and colour are specified by the Turtle's Pen. + */ + public void label(String text, Turtle t) { + Point2D.Double sc = toScreenCoords(t.getPos()); + int x = (int)Math.round(sc.x); + int y = (int)Math.round(sc.y); + Graphics2D traceG2D = getTraceG2D(); + FontRenderContext frc = traceG2D.getFontRenderContext(); + Font f = t.getFont(); + TextLayout tl = new TextLayout(text, f, frc); + traceG2D.setColor(t.getPen().getColor()); + tl.draw(traceG2D, x, y); + if (printerG2D != null) + { + printerG2D.setColor(t.getPen().getColor()); + tl.draw(printerG2D, x, y); + } + + if (printerG2D == null) + repaint(); + } + + /** Set antialiasing on or off for the turtle trace buffer + * This may result in an better trace quality. + */ + public void setAntiAliasing(boolean on) { + if (on) + traceG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + else + traceG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_OFF); + } + + + /** + * Set the given TPrintable (implementing draw()), + * open a printer dialog and start printing with given scale. + * Return false, if printer dialog is aborted, + * otherwise return true.
+ * If tp == null, the current playground is printed. + * + */ + protected boolean print(TPrintable tp, double scale) { + if (tp == null) + isPrintScreen = true; + else + isPrintScreen = false; + printerScale = scale; + PrinterJob pj = PrinterJob.getPrinterJob(); + pj.setPrintable(this); + traceCanvas = tp; + if (pj.printDialog()) { + try { + pj.print(); + } + catch (PrinterException ex) { + System.out.println(ex); + } + return true; + } + else + return false; + } + + /** + * For internal use only. Implementation of Printable. + * (Callback method called by printing system.) + */ + public int print(Graphics g, PageFormat pf, int pageIndex) { + if (pageIndex != 0) + return NO_SUCH_PAGE; + Graphics2D g2D = (Graphics2D)g; + double printerWidth = pf.getImageableWidth(); + double printerHeight = pf.getImageableHeight(); + double printerSize = printerWidth > printerHeight ? printerWidth : + printerHeight; + double xZero = pf.getImageableX(); + double yZero = pf.getImageableY(); + + printerG2D = g2D; // Indicate also, we are printing now + + // Needed for fill operations: the trace canvas must be empty in order to + // perform the fill algoritm (searching for outline of figure) + if (!isPrintScreen) + clean(); + + g2D.scale(printerScaleFactor * printerScale, printerScaleFactor * printerScale); + g2D.translate(xZero/printerScale, yZero/printerScale); + + if (isPrintScreen) + { + print(g); + } + else // Printing the traceCanvas + { + // Hide all turtles + boolean[] turtleState = new boolean[countTurtles()]; + for (int i = 0; i < countTurtles(); i++) { + Turtle aTurtle = getTurtle(i); + turtleState[i] = aTurtle.isHidden(); + aTurtle.ht(); + } + traceCanvas.draw(); + + // Restore old context + for (int i = 0; i < countTurtles(); i++) { + Turtle aTurtle = getTurtle(i); + if (!turtleState[i]) + aTurtle.st(); + } + } + + printerG2D = null; + return PAGE_EXISTS; + } + + /** Return the color of the pixel at the current turtle position. + */ + public Color getPixelColor(Turtle t) { + Point2D.Double p1 = toScreenCoords(t.getPos()); + int x = (int)Math.round(p1.getX()); + int y = (int)Math.round(p1.getY()); + return new Color(traceBuffer.getRGB(x, y)); + } + + public void enableRepaint(boolean b) { + isRepainting = b; + } + + /** set the background color of the playground + */ + public void setBackground(Color color) { + super.setBackground(color); + if (traceG2D != null) { + clear(color); + } // end of if + } + + /** + * Sets the origin of the cartesian coordinate system within the playground + */ + public void setOrigin(int x, int y) { + for (int i = 0; i < countTurtles(); i++) { + Turtle turtle = getTurtle(i); + Point2D.Double p1 = toScreenCoords(turtle.getPos()); + double newX = p1.getX() - x; + double newY = y - p1.getY(); + turtle.internalSetPos(newX, newY); + } + originX = x; + originY = y; + } + + public int getOriginX() { + return originX; + } + + public int getOriginY() { + return originY; + } + +} + diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/res/bong.wav b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/res/bong.wav new file mode 100644 index 0000000..0d2fef0 Binary files /dev/null and b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/res/bong.wav differ diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/sharedconstants.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/sharedconstants.java new file mode 100644 index 0000000..1bd3955 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/sharedconstants.java @@ -0,0 +1,84 @@ +// SharedConstants.java + +// Copyright 2002 Regula Hoefer-Isenegger +// +// This file is part of The Java Turtle (TJT) package +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +/* History; +V1.36 - Sep 2004: First official release +V1.37 - Nov 2004: Unchanged, modifications in ch.aplu.util +V1.38 - Dec 2004: Unchanged, modifications in ch.aplu.util +V1.39 - Jan 2005: Unchanged, modifications in ch.aplu.util +V1.40 - Mar 2005: Add background color, TurtleKeyAdapter, TurtleArea +V1.41 - May 2005: User defined turtle shape, minor changes in doc and code style +V1.42 - Dec 2005: Unchanged, modifications in ch.aplu.util +V1.43 - Feb 2006: Bug removed: Turtle.turtleFrame was not initialized in all ctors of class Turtle +V1.44 - Mar 2007: Bug removed: stampTurtle did not work properly in wrap mode +V1.45 - Aug 2007: TurtleKeyAdapter: use wait/notify to reduce CPU time (from 100% to 2%) +V1.46 - Aug 2007: synchronize(playground) for forward and rotate animation, + new method bong() using StreamingPlayer +V1.47 - Sept 2007: Unchanged, modifications in ch.aplu.util +V1.48 - Sept 2007: Unchanged, modifications in ch.aplu.util +V1.49 - Oct 2007: Unchanged, modifications in ch.aplu.util +V1.50 - Oct 2007: Unchanged, modifications in ch.aplu.util +V1.51 - Nov 2007: Fixed: correct position of label, when wrapping is on + Fixed: getPos() returns now the wrapped coordinates + Added: _getPos() returns unwrapped coordinates +V1.52 - Nov 2007: Added bean classes in order to use turtles with a Gui builder +V1.53 - Nov 2007: Added TurtlePane visual information when used in Gui builder design mode +V1.54 - Nov 2007: Minor changes to documentation +V1.55 - Dec 2007: Added property enableFocus to GPane, default: setFocusable(false) +V1.56 - Mar 2008: Unchanged, modifications in ch.aplu.util +V1.57 - Mar 2008: Unchanged, modifications in ch.aplu.util +V1.58 - Mar 2008: Modification to fill() (fill(x, y)): + region is defined with pixel color at current position + as opposed to background color +V1.59 - Oct 2008: Added ctors TurtleFrame with window position (ulx, uly) + Added Turtle.getPixelColor() +V2.00 - Nov 2008: Unchanged, modifications in ch.aplu.util + J2SE V1.4 no longer supported +V2.01 - Jan 2009: Unchanged, modifications in ch.aplu.util +V2.02 - Feb 2009 Turtle constructors run in EDT now +V2.03 - Feb 2009 Unchanged, modifications in ch.aplu.util +V2.04 - Feb 2009 Unchanged, modifications in ch.aplu.util +V2.05 - Feb 2009 Unchanged, modifications in ch.aplu.util +V2.06 - Mar 2009 Unchanged, modifications in ch.aplu.util +V2.07 - Mar 2009 All except print methods synchronized, so Turtle package is + now thread-safe +V2.08 - Apr 2009 Unchanged, modifications in ch.aplu.util +V2.09 - Jun 2009 Unchanged, modifications in ch.aplu.util +V2.10 - Jun 2010 Version for the Java-Editor +*/ + +package ch.aplu.turtle; + +interface SharedConstants +{ + int DEBUG_LEVEL_OFF = 0; + int DEBUG_LEVEL_LOW = 1; + int DEBUG_LEVEL_MEDIUM = 2; + int DEBUG_LEVEL_HIGH = 3; + + int DEBUG = DEBUG_LEVEL_OFF; + + String ABOUT = + "Copyright © 2002-2009\nRegula Hoefer-Isenegger, Aegidius Pluess\n, Gerhard Roehner\n" + + "under GNU General Public License\n" + + "http://www.aplu.ch\n" + + "All rights reserved"; + String VERSION = "2.10 - June 2015"; +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/streamingplayer.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/streamingplayer.java new file mode 100644 index 0000000..8a3d5d5 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/streamingplayer.java @@ -0,0 +1,84 @@ +// StreamingPlayer.java + +package ch.aplu.turtle; + + +import javax.sound.sampled.*; +import java.io.*; + +class StreamingPlayer +{ + private class PlayerThread extends Thread + { + public void run() + { + byte buf[] = new byte[20000]; + try + { + int cnt; + while ((cnt = audioInputStream.read(buf, 0, buf.length)) != -1) + { + if (cnt > 0) + sourceDataLine.write(buf, 0, cnt); + } + sourceDataLine.drain(); + sourceDataLine.close(); + } + catch (IOException ex) + { + System.out.println(ex); + System.exit(1); + } + } + } + + private AudioFormat audioFormat; + private AudioInputStream audioInputStream; + private SourceDataLine sourceDataLine; + private PlayerThread playerThread; + + StreamingPlayer(InputStream is) + { + try + { + audioInputStream = + AudioSystem.getAudioInputStream(is); + audioFormat = audioInputStream.getFormat(); + } + catch (Exception ex) + {} + } + + void start(boolean doFinish) + throws LineUnavailableException + { + DataLine.Info dataLineInfo = + new DataLine.Info(SourceDataLine.class, audioFormat); + sourceDataLine = + (SourceDataLine)AudioSystem.getLine(dataLineInfo); + sourceDataLine.open(audioFormat); + sourceDataLine.start(); + playerThread = new PlayerThread(); + playerThread.start(); + if (doFinish) + waitToFinish(); + } + + boolean isPlaying() + { + if (playerThread == null) + return false; + return (playerThread.isAlive()); + } + + void waitToFinish() + { + if (playerThread == null) + return; + try + { + playerThread.join(); + } + catch (InterruptedException ex) {} + } +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/tprintable.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/tprintable.java new file mode 100644 index 0000000..767d22a --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/tprintable.java @@ -0,0 +1,43 @@ +// TPrintable.java + +// Copyright 2002 Regula Hoefer-Isenegger + +// This file is part of The Java Turtle (TJT) package +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +// Added by Aegidius Pluess + +package ch.aplu.turtle; + +/** + * Interface for printing on an attached printer. + * Normally an application uses a Turtle and implements this interface. + * draw() should contain all drawing operations into the + * Turtle's Playground. The printing occures, when Turtle's print() is + * called.

+ */ + + +public interface TPrintable +{ + /** + * This method must perform all drawing operations. + * Be aware that for some undocumented reason + * draw() is called twice. So be sure you initialize it correctly. + */ + + public void draw(); +} \ No newline at end of file diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/turtle.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/turtle.java new file mode 100644 index 0000000..d30ff52 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/turtle.java @@ -0,0 +1,1585 @@ +// Turtle.java +// Modifications by Aegidius Pluess +// Adaption to the Java-Editor by Gerhard Röhner + +// Copyright 2002-2003 Regula Hoefer-Isenegger (Version 1.0) +// +// This file is part of The Java Turtle package (TJT) +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +/* +Screen coordinate system: 0..400 pixels in both directions corresponds to turtle +turtle coordinate system -200..200, so only translation is necessary (no scaling). + +This avoids rounding errors. +*/ + +package ch.aplu.turtle; + +import ch.aplu.turtle.*; +import java.awt.*; +import java.awt.event.*; +import java.awt.geom.*; +import java.awt.image.*; +import java.awt.print.*; +import java.io.*; +import javax.sound.sampled.*; +import javax.swing.*; + +/** + The core class for turtles. + + For a simple example on how to use Turtle, cf. the + Java Turtle Package description. + @author Regula Hoefer-Isenegger + @version 0.1.1 +*/ +public class Turtle implements Cloneable +{ + private double angle; + private Point2D.Double position; + private Playground playground; + private int framesPerSecond; + private double speed; // Pixel/sec + private double angleSpeed; // Radian/sec + private TurtleRenderer turtleRenderer; + private int angleResolution; + private LineRenderer lineRenderer; + private static TurtleFactory turtleFactory; + private boolean penUp; + private boolean showTurtle; + private boolean initialVisibility = true; + private Pen pen; + private Color color; + private int edgeBehaviour; + + + /** Convert from screen coordinates to turtle coordinates + */ + public Point2D.Double toTurtlePos(Point p) { + return playground.toTurtleCoords((double)p.x, (double)p.y); + } + + /** Convert from screen coordinates to turtle coordinates + */ + public Point2D.Double toTurtlePos(int x, int y) { + return playground.toTurtleCoords((double)x, (double)y); + } + + /** Convert from screen coordinates to turtle coordinates + */ + public double toTurtleX(int x) { + return (playground.toTurtleCoords((double)x, 0)).x; + } + + /** Convert from screen coordinates to turtle coordinates + */ + public double toTurtleY(int y) { + return (playground.toTurtleCoords(0, (double)y)).y; + } + + /** Add the specified mouse listener to receive mouse events. + */ + public void addMouseListener(MouseListener l) { + playground.addMouseListener(l); + } + + /** Add the specified mouse motion listener to receive mouse motion events. + */ + public void addMouseMotionListener(MouseMotionListener l) { + playground.addMouseMotionListener(l); + } + + /** + * Emit a beep. Fails if no standard speaker available. Try to use bong() instead. + * @see #bong() + */ + public void beep() { + Toolkit.getDefaultToolkit().beep(); + } + + /** + * Emit a 'bong' via the sound card. Useful on computers without standard + * speaker. + * @see #beep() + */ + public void bong() { + InputStream is = getClass().getResourceAsStream("res/bong.wav"); + StreamingPlayer player = new StreamingPlayer(is); + try + { + player.start(false); + } + catch (LineUnavailableException ex) + { + System.out.println("Error in bong(). Sound card unavailable"); + } + } + + + /** Represent clip mode. + @see #WRAP + @see #clip() + */ + protected final static int CLIP = 0; + /** Represent wrap mode. + + @see #CLIP + @see #wrap() + */ + protected final static int WRAP = 1; + /** Represent the default edge behaviour (i.e. CLIP or WRAP). + + @see #CLIP + @see #WRAP + @see #clip() + @see #wrap() + */ + protected static int DEFAULT_EDGE_BEHAVIOUR = CLIP; + /** Represent the default speed (velocity). + + @see #setSpeed(double) + */ + protected static double DEFAULT_SPEED = 200; + + /** Represent the default angle resolution. + + It specifies how many different turtle pictures + are generated. + */ + protected static int DEFAULT_ANGLE_RESOLUTION = 72; + + /** Specify how many frames per second are used for + turtle animation. + */ + protected static int DEFAULT_FRAMES_PER_SECOND = 10; + + /** Specify the default turtle color. + + @see #setColor(java.awt.Color) + */ + protected static Color DEFAULT_TURTLE_COLOR = Color.cyan; + + /** Specify the default pen color. + + @see #setPenColor(java.awt.Color) + */ + protected static Color DEFAULT_PEN_COLOR = Color.blue; + + /** Create a new Turtle + */ + public Turtle() { + } + + /** init a new Turtle + */ + protected void init(Color color) { + angle = 0; + position = new Point2D.Double(0, 0); + framesPerSecond = DEFAULT_FRAMES_PER_SECOND; + if (initialVisibility) + setSpeed(DEFAULT_SPEED); + else + setSpeed(-1); + showTurtle = initialVisibility; + setAngleResolution(DEFAULT_ANGLE_RESOLUTION); + angleSpeed = getSpeed() * Math.PI * 2 / DEFAULT_SPEED; + pen = new Pen(DEFAULT_PEN_COLOR); + if (getTurtleFactory() == null){ + turtleFactory = createTurtleFactory(); + } + setColor(color); + lineRenderer = createLineRenderer(); + getTurtleRenderer().setAngle(getAngle()); + } + + /** Set the angle resolution for the Turtle's pictures. + + It specifies how many pictures are used. e.g. an angle resolution + of 90 means that you get one picture for every 4 degrees + (= 360/90 degrees). + */ + public void setAngleResolution(int newResolution) { + synchronized(playground) { + angleResolution = newResolution; + } + } + + /** Return the TurtleFactory of this turtle. + + @see ch.aplu.turtle.TurtleFactory + */ + public TurtleFactory getTurtleFactory() { + return this.turtleFactory; + } + + /** Create a LineRenderer which is responsible + for the correct drawing of the lines. + + @return the new LineRenderer + */ + + protected LineRenderer createLineRenderer() { + return new LineRenderer(this); + } + + /** Create a TurtleRenderer which is responsible + for the correct drawing of the Turtle. + + @return the new TurtleRenderer + */ + protected TurtleRenderer createTurtleRenderer() { + return new TurtleRenderer(this); + } + + /** Create a TurtleFactory which provides for + the Turtle pictures. + + @return the new TurtleFactory + */ + + protected TurtleFactory createTurtleFactory() { + return new TurtleFactory(); + } + /** Get the angle resolution. + + @see #setAngleResolution(int) + */ + + protected int getAngleResolution() { + return angleResolution; + } + + /** Get the TurtleRenderer. + */ + TurtleRenderer getTurtleRenderer() { + return turtleRenderer; + } + + /** Get the LineRenderer. + */ + private LineRenderer getLineRenderer() { + return lineRenderer; + } + + /** Get the Playground. + */ + public Playground getPlayground() { + return playground; + } + + /** Set the Playground to the specified + playground. + + The Turtle is removed from the old + Playground and set to the new one. + */ + public void setPlayground(Playground playground) { + Playground pg = getPlayground(); + if(pg != null){ + pg.clearTurtle(this); + pg.remove(this); + pg.paintTurtles(); + } + this.playground = playground; + playground.paintTurtles(this); + } + + /** Get the angle speed. + + (I.e. how fast the Turtle rotation + animation is performed.) + */ + private double getAngleSpeed() { + return angleSpeed; + } + + /** Set the angle speed. + + @see #getAngleSpeed() + */ + private void setAngleSpeed(double newAngleSpeed) { + this.angleSpeed = newAngleSpeed; + } + + /** Get the current angle (heading) of the + Turtle. + */ + private double getAngle() { + return angle; // in radians + } + + /** Get the current speed. + */ + public double getSpeed() { + return speed; + } + + /** Query the Turtle's x-position without wrapping. + * Coordinate are not bound to playground even when + * wrapping is on. + */ + public double _getX() { + synchronized (playground) { + return position.getX(); + } + } + + /** Query the Turtle's y-position without wrapping. + * Coordinate are not bound to playground even when + * wrapping is on. + */ + public double _getY() { + synchronized (playground) { + return position.getY(); + } + } + + /** Query the Turtle's x-position. + * If turtle is outside playground and wrapping is on + * return the coordinate in range -200..200. + * */ + + public double getX() { + synchronized (playground) { + double xPos = _getX(); + return xPos; + } + } + + /** Query the Turtle's x-position. + * If turtle is outside playground and wrapping is on + * return the coordinate in range -200..200. + */ + public double getY() { + synchronized (playground) { + double yPos = _getY(); + return yPos; + } + } + + + /** Query the Turtle's position without wrapping. + * Coordinate are not bound to playground even when + * wrapping is on. + */ + public Point2D.Double _getPos() { + return position; + } + + /** Query the Turtle's position */ + public Point2D.Double getPos() { + return new Point2D.Double(getX(), getY()); + } + + /** Put the Turtle to a new position with the given x-coordinates. + + */ + public void setX(double x) { + synchronized (playground) { + getPlayground().clearTurtle(this); + internalSetX(x); + getPlayground().paintTurtles(this); + } + } + + /** Put the Turtle to a new position with the given screen x-coordinates. + */ + public void setScreenX(int x) { + synchronized (playground) { + setX(toTurtleX(x)); + } + } + + /** Put the Turtle to a new position with the given y-coordinates. + */ + public void setY(double y) { + synchronized (playground) { + getPlayground().clearTurtle(this); + internalSetY(y); + getPlayground().paintTurtles(this); + } + } + + /** Put the Turtle to a new position with the given screen y-coordinates. + */ + public void setScreenY(int y) { + synchronized (playground) { + setY(toTurtleY(y)); + } + } + + /** Set the Turtle's x-Coordinate. + */ + protected void internalSetX(double x) { + synchronized (playground) { + position.setLocation(x, _getY()); + } + } + + /** Set the Turtle's y-Coordinate. + */ + protected void internalSetY(double y) { + position.setLocation(_getX(), y); + } + + /** Set the Turtle's Position. + */ + protected void internalSetPos(double x, double y) { + position.setLocation(x, y); + } + + /** Hide the Turtle. + + @see #st() + If there is only one turte, the speed is set to -1 + so there is no Turtle animation at all. + Hiding the Turtle speeds up the graphics enormously. + */ + public void ht() { + synchronized (playground) { + this.internalHide(); + if (getPlayground().countTurtles() == 1) + setSpeed(-1); + } + } + + /** Hide the Turtle. + + This is the same as ht(). + @see #st() + */ + public void hideTurtle() { + ht(); + } + + /** This is the method called by the public methods ht() and hideTurtle(). + + Here the actual hiding takes place. + @see #ht() + @see #hideTurtle() + */ + protected void internalHide() { + getPlayground().clearTurtle(this); + showTurtle = false; + if (getPlayground().getPrinterG2D() == null) + getPlayground().repaint(); + } + + /** Set the Turtle to show mode. + + That means that the Turtle will be drawn. + @see #ht() + */ + public void st() { + synchronized (playground) { + if (getPlayground().getPrinterG2D() == null) { + getPlayground().paintTurtle(this); + showTurtle = true; + getPlayground().repaint(); + } + } + } + + /** The same as st(). + + @see #st() + */ + public void showTurtle() { + st(); + } + + /** Tell whether the Turtle is hidden or not. + + @return true if the Turtle is hidden, + false otherwise. + */ + public boolean isHidden() { + return !showTurtle; + } + + private int getFramesPerSecond() { + return this.framesPerSecond; + } + + /** Only set the angle attribute. This method does not + invoke any re-painting. + */ + private void setAngle(double radians) { + this.angle = radians; + } + + /** This is the same as setH(double degrees). + + @see #setH(double) + */ + public void setHeading(double degrees) { + synchronized (playground) { + setAngle(Math.toRadians(degrees)); + getTurtleRenderer().setAngle(Math.toRadians(degrees)); + getPlayground().clearTurtle(this); + getPlayground().paintTurtles(this); + } + } + + /** Set the Turtle's heading. + + 0 means facing NORTH.
+ the angles are measured clockwise. + @see #setHeading(double) + */ + public void setH(double degrees) { + setHeading(degrees); + } + /** Query the Turtle's heading. + + @see #setH(double) + */ + public double heading() { + synchronized (playground) { + return Math.toDegrees(getAngle()); + } + } + /** Set the Turtle's heading to the new value. + + @return the old (previous) value. + @see #setH(double) + */ + public double heading(double degrees) { + synchronized (playground) { + double tmp = Math.toDegrees(getAngle()); + setHeading(degrees); + return tmp; + } + } + + /** Set the Turtle's speed. + + If you try to set the speed to 0, it will be set to 1 (very slow). + A negative speed means that moving the Turtle (fd, bk) + will not be animated.
+ The unit is pixels per second (up to certain bounds depending on the CPU etc.).
+ Remark: Dashed lines will only be painted as you expect it with speed set + to -1. + + @see #fd(double) + @see #bk(double) + */ + public void setSpeed(double speed) { + if (speed == 0) + this.speed = 1; + else + this.speed = speed; + } + + + /** This method is responsible for the rotation animation. + */ + private void internalRotate(double angle) { + if(isHidden()){ + synchronized(playground) + { + setAngle(getAngle()+angle); + if (getTurtleRenderer().imageChanged(getAngle())) { + getTurtleRenderer().setAngle(getAngle()); + } + } + return; + } + if (angle != 0) { + int iterations = getAngleIterations(angle); + + double sign = angle/Math.abs(angle); + double increment = sign*getAngleSpeed()/(double)getFramesPerSecond(); + double startAngle = getAngle(); + + for (int index = 0; index < iterations; index++) { + long timeStamp = System.currentTimeMillis(); + + synchronized(playground) { + getPlayground().clearTurtle(this); + + if (index < iterations-1) { + setAngle(getAngle()+increment); + } + else { + setAngle(startAngle+angle); + } + + if (getTurtleRenderer().imageChanged(getAngle())) { + getTurtleRenderer().setAngle(getAngle()); + getPlayground().paintTurtles(this); + } + } + + long newTimeStamp = System.currentTimeMillis(); + Double secs = new Double(1000./getFramesPerSecond()); + long requiredTime = secs.longValue()-newTimeStamp+timeStamp; + + if (requiredTime > 0) { + try { + Thread.sleep(requiredTime); + } + catch (InterruptedException e) { + } + } + } + } + getPlayground().paintTurtles(this); + } + + /** This method is responsible for the moving animation. + */ + private void internalMove(double length) { + if (getSpeed()>0){ + if (length != 0) { + int iterations = getPathIterations(length); + // an angle of 0 means: facing NORTH + double startX = _getX(); + double startY = _getY(); + getLineRenderer().init(startX, startY); + double dx = length * Math.sin(getAngle()); + double dy = length * Math.cos(getAngle()); + double incrementX = dx / iterations; + double incrementY = dy / iterations; + for (int index = 0; index < iterations; index++) { + long timeStamp = System.currentTimeMillis(); + int nX = (int)_getX(); + int nY = (int)_getY(); + synchronized(playground) { + playground.clearTurtle(this); + if (index < iterations-1) { + internalSetX(_getX()+incrementX); + internalSetY(_getY()+incrementY); + } + else { // last step: Calc the "exact" value + internalSetX(startX + dx); + internalSetY(startY + dy); + } + if (nX != (int)_getX() || nY != - (int)_getY() || index == iterations-1){ + if (!isPenUp()) { + getLineRenderer().lineTo(_getX(), _getY()); + } + getPlayground().paintTurtles(this); + } + } + Double frames = new Double(1000./getFramesPerSecond()); + long newTimeStamp = System.currentTimeMillis(); + long requiredTime = frames.longValue()-newTimeStamp+timeStamp; + if (requiredTime > 0) { + try { + Thread.sleep(requiredTime); + } + catch (InterruptedException e) { + } + } + } + } + } + else { // Speed < 0, i.e. no animation + double startX = _getX(); + double startY = _getY(); + getLineRenderer().init(startX, startY); + double dx = length * Math.sin(getAngle()); + double dy = length * Math.cos(getAngle()); + getPlayground().clearTurtle(this); + internalSetX(startX + dx); + internalSetY(startY + dy); + if (!isPenUp()) { + getLineRenderer().lineTo(_getX(), _getY()); + } + getPlayground().paintTurtles(this); + } + } + + /** Turn the Turtle the given angle (in degrees) to the left + + @see #rt(double) + */ + public void lt(double degrees) { + left(degrees); + } + + /** Same as lt(double degrees) + + @see #lt(double) + */ + public void left(double degrees) { + internalRotate(Math.toRadians(-degrees)); + } + + /** Turn the Turtle the given angle (in degrees) to the right. + + @see #rt(double) + */ + public void rt(double degrees) { + right(degrees); + } + + /** Same as rt(double degrees). + + @see #rt(double) + */ + public void right(double degrees) { + internalRotate(Math.toRadians(degrees)); + } + + /** Same as fd(double distance) + + @see #fd(double) + */ + public void forward(double distance) { + internalMove(distance); + } + + /** Move the Turtle forwards. + + Negative values for distance are + allowed. In that case, the Turtle + will move backwards. + + @see #bk(double) + */ + public void fd(double distance) { + forward(distance); + } + + /** Same as bk(double distance). + + @see #bk(double) + */ + public void back(double distance) { + forward(-distance); + } + + /** Move the Turtle backwards. + + Negative values for distance are + allowed. In that case, the Turtle + will move forwards. + + @see #fd(double) + */ + public void bk(double distance) { + back(distance); + } + + /** Query the distance from the current location + to the given one. + */ + public double distance(double x, double y) { + synchronized (playground) { + return this.getPos().distance(x,y); + } + } + + /** Query the distance from the current location + to the given one. + * Actually a polygon with 36 sides is drawn. + */ + public double distance(Point2D.Double p) { + synchronized (playground) { + return this.getPos().distance(p); + } + } + + /** Draw a circle to the left from the current position with + * turtle's heading tangent direction and given radius. + * Actually a polygon with 36 sides is drawn. + */ + public void leftCircle(double radius) { + circle(radius, true); + } + + /** Draw a circle to the right from the current position with + * turtle's heading tangent direction and given radius. + * Actually a polygon with 36 sides is drawn. + */ + public void rightCircle(double radius) { + circle(radius, false); + } + + private void circle(double radius, boolean left) { + double nbSteps = 36; // 36 side polygon + double angle = 2 * Math.PI / nbSteps; + double step = 2 * radius * Math.sin(angle/2); + for (int i = 0; i < 36; i++) { + if (left) + if (i == 0) + lt(180.0 / nbSteps); + else + lt(360.0 / nbSteps); + else + if (i == 0) + rt(180.0 / nbSteps); + else + rt(360.0 / nbSteps); + fd(step); + } + } + + /** Calculate the number of iterations when animating left or right (rotation). + */ + private int getAngleIterations(double dAngle) { + if(getAngleSpeed()<0){ + return 1; + } + if(getAngleSpeed()==0){ + setAngleSpeed(1); + } + double dAbsAngle = Math.abs(dAngle); + Double dValue = new Double(Math.ceil(dAbsAngle/getAngleSpeed()*getFramesPerSecond())); + return dValue.intValue(); + } + + /** Calculate the number of iterations when animating forwards or backwards. + */ + private int getPathIterations(double length) { + if(speed < 0) + return 1; + + if (speed == 0) + setSpeed(1); + + double dAbsLength = Math.abs(length); + Double dValue = new Double(Math.ceil(dAbsLength/getSpeed()*getFramesPerSecond())); + return dValue.intValue(); + } + + /** Lift the Turtle's pen up so it + won't draw a line anymore. + + This is the same as pu(). + + @see #pu() + @see #penDown() + @see #pd() + */ + public void penUp() { + synchronized (playground) { + this.penUp = true; + } + } + + /** Lift the Turtle's pen up so it + won't draw a line anymore. + + This is the same as penUp(). + + @see #penUp() + @see #penDown() + @see #pd() + */ + public void pu() { + this.penUp(); + } + + /** Lower the Turtle's Pen down so it + will draw a line when moving. + + This is the same as pd(). + + @see #pd() + @see #penUp() + @see #pu() + */ + public void penDown() { + synchronized (playground) { + this.penUp = false; + } + } + + /** Lowers the Turtle's pen down so it + will draw a line when moving. + + This is the same as penDown(). + + @see #penDown() + @see #penUp() + @see #pu() + */ + public void pd() { + this.penDown(); + } + + /** Query the Pen's state (up or down). + + @return true if the Pen is + up, false otherwise. + @see #pu() + @see #pd() + */ + public boolean isPenUp() { + return this.penUp; + } + + /** Return the bounds of this Turtle. This is required + by the methods that (return-)paint the Turtles. + */ + Rectangle getBounds() { + Rectangle rect = new Rectangle(); + + Image img = getTurtleRenderer().currentImage(); + int nWidth = img.getWidth(getTurtleRenderer()); + int nHeight = img.getHeight(getTurtleRenderer()); + double x = (_getX()<0)?Math.floor(_getX()):Math.ceil(_getX()); + double y = (_getY()<0)?Math.floor(_getY()):Math.ceil(_getY()); + rect.setBounds((int)x-nWidth/2, (int)y + nHeight/2, nWidth, nHeight); + return rect; + } + + /** Get the Turtle's Pen. + + You need it if you want to change end caps etc. + @see Pen + */ + public Pen getPen() { + return pen; + } + /** Set the line thickness. + + This works only neatly in clip mode (yet). + @see #clip() + @see #wrap() + */ + public void setLineWidth(double lineWidth){ + synchronized (playground) { + setLineWidth((float)lineWidth); + } + } + + /** Set the line thickness. + + This works only neatly in clip mode (yet). + @see #clip() + @see #wrap() + */ + public void setLineWidth(float lineWidth) { + synchronized (playground) { + getPen().setLineWidth(lineWidth); + } + } + + /** Set the Turtle's color to the specified one. + + */ + public void setColor(Color color) { + synchronized (playground) { + internalSetColor(color); + getPlayground().paintTurtles(); + } + } + + private void internalSetColor(Color color) { + this.color = color; + if (getTurtleRenderer()==null) { + turtleRenderer = createTurtleRenderer(); + getTurtleRenderer().init(getTurtleFactory(), getAngleResolution()); + } + else{ + getTurtleRenderer().init(new TurtleFactory(), + this.angleResolution); + } + } + + /** Set the color to the specified one. + + @see #fill() + */ + public void setFillColor(Color color) { + synchronized (playground) { + getPen().setFillColor(color); + } + } + + /** Query the Turtle's current color. + */ + public Color getColor(){ + return color; + } + + /**Set the Turtle's pen color. + + */ + public void setPenColor(Color color) { + synchronized (playground) { + getPen().setColor(color); + } + } + + /** Move the Turtle back "home", i.e. set its position + to the origin, facing NORTH. + + Color, PenColor etc. remain the same. + */ + public void home() { + // first : clean the Turtle! + synchronized (playground) { + getPlayground().clearTurtle(this); + position = new Point2D.Double(0,0); + setHeading(0); + } + } + + /** The Turtle's Pen is + changed to an eraser (which is in fact a pen with + background color). + + This is the same as pe() + @see #pe() + */ + public void penErase() { + synchronized (playground) { + this.internalPenErase(); + } + } + + /** The Turtle's Pen is + changed to an eraser (which is in fact a pen with + background color). + + This is the same as penErase() + @see #penErase() + */ + public void pe() { + penErase(); + } + + protected void internalPenErase(){ + this.setPenColor(getPlayground().getBackground()); + } + + /** Put the Turtle to a new position with specified + x- and y-coordinates. + */ + public void setPos(double x, double y) { + synchronized (playground) { + getPlayground().clearTurtle(this); + internalSetPos(x, y); + getPlayground().paintTurtles(); + } + } + + /** Put the Turtle to a new position. + */ + public void setPos(Point2D.Double p) { + setPos(p.x, p.y); + } + + /** Put the Turtle to a new screen position. + + */ + public void setScreenPos(Point p) { + setPos(toTurtlePos(p)); + } + + + /** Put a Turtle image at the current position. + + */ + public void stampTurtle() { + synchronized (playground) { + this.getPlayground().stampTurtle(this); + } + } + + /** Calculate the direction to a given point. + + @return the direction from the current turtle position + towards the given point, measured in degrees and clockwise + from the vertical upwards position. + */ + public double towards(double x, double y) { + synchronized (playground) { + double dx = x - getX(); + double dy = y - getY(); + double result = Math.toDegrees(Math.atan2(dx, dy)); + return (result < 0) ? result+360 : result; + } + } + + /** Calculate the direction to a given point. + + @return the direction from the current turtle position + towards the given point, measured in degrees and clockwise + from the vertical upwards position. + */ + public double towards(Point2D.Double p) { + return towards(p.getX(), p.getY()); + } + + /** Put the Turtle to the top (i.e. above any other + turtle). + + Just invokes the toTop-Method in Playground. + @see Playground#toTop + @see #internalToBottom + */ + void internalToTop() { + this.getPlayground().toTop(this); + } + /** Put the Turtle to the bottom (i.e. under any other + turtle). + + Just invokes the toBottom-Method in Playground. + @see Playground#toBottom + @see #internalToTop + */ + void internalToBottom() { + this.getPlayground().toBottom(this); + } + + /** Put this turtle to the bottom. + + So any other turtle in the same Playground will be drawn over it. + */ + public void toBottom() { + // this.getPlayground().toBottom(this); + synchronized (playground) { + internalToBottom(); + getPlayground().paintTurtles(); + } + } + + /** Put this turtle to the top. + + So it will be drawn over any other turtle in the same Playground. + */ + public void toTop() { + // this.getPlayground().toTop(this); + synchronized (playground) { + this.getPlayground().paintTurtles(this); + } + } + + /** Set the pen width. + */ + public void penWidth(int newWidth) { + synchronized (playground) { + penWidth((float)newWidth); + } + } + + /** Internal Method for setting the penWidth. + */ + private void penWidth(float newWidth) { + setLineWidth(newWidth); + } + + /** Query the pen width. + */ + public int penWidth(){ + return (int)this.getPen().getLineWidth(); + } + + /** Returns the current edge behaviour. + + @see #CLIP + @see #WRAP + */ + protected int getEdgeBehaviour() { + return edgeBehaviour; + } + + /** Sets the edge behaviour to the specified value; + + @see #CLIP + @see #WRAP + */ + protected void setEdgeBehaviour(int edgeBehaviour) { + synchronized (playground) { + this.edgeBehaviour = edgeBehaviour; + } + } + + /** Set the Turtle to clip-mode. + @see #wrap() + @see #clip() + */ + public void clip() { + synchronized (playground) { + setEdgeBehaviour(CLIP); + } + } + + /** Cause the Turtle to wrap around the playground borders. + + e.g when as the Turtle leaves the Window on one side, + it reappears on the opposite side. + + @see #clip() + */ + public void wrap() { + synchronized (playground) { + setEdgeBehaviour(WRAP); + } + } + + /** Tell whether the Turtle is in clip mode. + + @return true if in clip mode, false otherwise. + @see #clip() + @see #wrap() + */ + public boolean isClip() { + return (getEdgeBehaviour() == CLIP); + } + /** Tell wheter the Turtle is in wrap mode. + + @return true if in wrap mode, false otherwise. + @see #wrap() + @see #clip() + */ + public boolean isWrap() { + return (getEdgeBehaviour() == WRAP); + } + + /** Fill the region the Turtle is in.
+ + A region is bounded by lines + of any color different to the pixel color at the current turtle position + and by the border of the window.
+ (The pen of the Turtle must be down.) + */ + public void fill() { + synchronized (playground) { + getPlayground().fill(this, getPlayground().getPixelColor(this)); + } + } + + /** Fill the region as if the Turtle where at coordinates x and y.
+ A region is bounded by lines + of any color different to the pixel color at the given position and by the border of + the window.
+ (The pen of the Turtle must be down.) + */ + public void fill(double x, double y) { + synchronized (playground) { + double oldX = getX(); + double oldY = getY(); + boolean hidden = isHidden(); + ht(); + setPos(x,y); + getPlayground().fill(this, getPlayground().getPixelColor(this)); + setPos(oldX, oldY); + if(!hidden){ + st(); + } + } + } + + /** Erase all traces and text painted by the turtles, but let all + * turtles where they are. + + */ + public void clean() { + synchronized (playground) { + getPlayground().clean(); + } + } + + /** Paint the specified text at the current turtle position. + + */ + public void label(String text) { + synchronized (playground) { + if (text != null && text.length() > 0) + getPlayground().label(text, this); + } + } + + /** Set the current font as specified. + + @see java.awt.Font + */ + public void setFont(Font font) { + synchronized (playground) { + getPen().setFont(font); + } + } + + /** Change the current font to the specified one. If you want to know what fonts are available on your system, + call #getAvailableFontFamilies() . + + @see #getAvailableFontFamilies + @see java.awt.Font more information about fontName, style and size. + */ + public void setFont(String fontName, int style, int size) { + synchronized (playground) { + getPen().setFont(new Font(fontName, style, size)); + } + } + + /** Set the font size. + + @see #setFontStyle changing the font style + @see #setFont changing the whole font. + */ + public void setFontSize(int size) { + synchronized (playground) { + getPen().setFontSize(size); + } + } + + /** Set the font style. + + This is either java.awt.Font.PLAIN, java.awt.Font.BOLD, java.awt.Font.ITALIC or java.awt.Font.BOLD+java.awt.Font.ITALIC + @see #setFontSize changing the font size + @see #setFont changing the whole font. + */ + public void setFontStyle(int style) { + synchronized (playground) { + getPen().setFontStyle(style); + } + } + + /** Provide information about all font families (e.g. roman) currently available on your system. + Each font name is a string packed into a array of strings. + (This might be useful when you want to change the font.) + @see #setFont(java.awt.Font) + @see #setFont(java.lang.String, int, int) + */ + public static String[] getAvailableFontFamilies() { + return Pen.getAvailableFontFamilies(); + } + + /** Return the current font. + + @see #setFontSize changing the font size + @see #setFontStyle changing the font style + @see #setFont changing the whole font. + */ + public Font getFont() { + return getPen().getFont(); + } + + /** Creates a clone of the Turtle in it's Playground. + Color, position and heading are the same. + (It's visible and pen is down.) + Return the cloned object reference as a Object type, because + it overrides Object.clone(). + (You may cast it to a Turtle.) + */ + public Object clone() + { + synchronized (playground) { + Turtle t = new Turtle(); + t.setColor(getColor()); + t.setPos(getX(), getY()); + t.heading(heading()); + t.showTurtle(); + return t; + } + } + + /** Set antialiasing on or off for the turtle trace buffer + * This may result in an better image quality, especially + * for filling operations (platform dependant). + */ + public void antiAliasing(boolean on) { + synchronized (playground) { + getPlayground().setAntiAliasing(on); + } + } + + + /** + * Print the graphics context to an attached printer with + * the given magnification scale factor. + * scale = 1 will print on standard A4 format paper.
+ * + * The given tp must implement the GPrintable interface, + * e.g. the single method void draw(), where all the + * drawing into the GPanel must occur. + * + * Be aware the turtle(s) state (position, direction, etc.) + * must be reinitialized, because draw() is called several + * times by the printing system. + * + * A standard printer dialog is shown before printing is + * started. Only turte traces are printed. + *
+ * Example:
+ * + import ch.aplu.turtle.*;
+
+ public class PrintTest implements TPrintable
+ {
+ private Turtle t = new Turtle();
+
+ public PrintTest()
+ {
+ draw(); // Draw on screen
+ t.print(this); // Draw on printer
+ }
+
+ public void draw()
+ {
+ t.home(); // Needed for initialization
+ for (int i = 0; i < 5; i++)
+ t.fd(20).rt(90).fd(20).lt(90);
+ }
+
+ public static void main(String[] args)
+ {
+ new PrintTest();
+ }
+ }
+
+ */ + public boolean print(TPrintable tp, double scale) + { + return getPlayground().print(tp, scale); + } + + /** + * Same as print(tp, scale) with scale = 1. + */ + public boolean print(TPrintable tp) + { + return print(tp, 1); + } + + /** + * Print the Turtle's current playground with given scale. + */ + public boolean printScreen(double scale) + { + return print(null, scale); + } + + /** + * Same as printScreen(scale) with scale = 1. + */ + public boolean printScreen() + { + return printScreen(1); + } + + /** + * Clear the Turtle's playground. All turtle images and traces are erased, + * but the turtles remain (invisible) at their positions. + */ + public void clear() + { + synchronized (playground) { + getPlayground().clear(); + } + } + + /** + * Delay execution for the given amount of time ( in ms ). + */ + public static void sleep(int time) + { + try + { + Thread.currentThread().sleep(time); + } + catch (Exception e) {} + } + + /** + * Return version information + */ + public String version() + { + return SharedConstants.VERSION; + } + + /** + * Enable/disable automatic repainting. + * If disabled, call repaint() to perform repainting. + * Disabling automatic repainting and hiding the + * Turtle speeds up the graphics enormously. + */ + public void enableRepaint(boolean b) + { + getPlayground().enableRepaint(b); + } + + /** + * Perform manual repainting when automatic repainting is + * disabled. + */ + public void repaint() { + getPlayground().repaint(); + } + + /** + * Return the color of the pixel at the current turtle position. + */ + public Color getPixelColor() { + synchronized (playground) { + return getPlayground().getPixelColor(this); + } + } + + protected Graphics getGraphics() { + return playground.getGraphics(); + } + + /** + * Transforms the gui-position to mouse-coordinates. + */ + public void setBounds(int x, int y, int b, int h) { + int ox = playground.getOriginX(); + int oy = playground.getOriginY(); + x = -ox + x + 10; + y = oy - y - 13; + setPos(x, y); + } + + /** + * Shows or hides the turtle. + */ + public void setVisible(boolean show) { + if (show) { + showTurtle(); + } else { + hideTurtle(); + } // end of if-else + } + + /** + * Sets clipping-mode on/off. + */ + public void setClip(boolean clip) { + if (clip) { + clip(); + } else { + wrap(); + } // end of if-else + } + + /** + * Sets pen-position up/down. + */ + public void setPenUp(boolean penup) { + if (penup) { + penUp(); + } else { + penDown(); + } // end of if-else + } + + /** + * Sets angle of the turtle. + */ + public void setAngle(int angle) { + setHeading(angle); + } +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/turtlefactory.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/turtlefactory.java new file mode 100644 index 0000000..e9d700e --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/turtlefactory.java @@ -0,0 +1,66 @@ +// TurtleFactory.java + +// Copyright 2002 Regula Hoefer-Isenegger +// +// This file is part of The Java Turtle (TJT) package +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package ch.aplu.turtle; + +import java.awt.image.*; +import java.awt.*; + +class TurtleFactory +{ + /** Generates the shape of the turtle with color, + * angle angle, width w + * and height h. + */ + protected Image turtleImage(Color color, double angle, int w, int h) + { + BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR); + Graphics2D g = (Graphics2D)bi.getGraphics(); + // Origin in center + g.translate(w/2, h/2); + // angle = 0 is direction east (as usual in mathematics) + g.rotate(Math.PI/2 - angle); + g.setColor(color); + + // Body + g.fillOval((int)( -0.35 * w), (int)( -0.35 * h), + (int)(0.7 * w), (int)(0.7 * h)); + + // Head + g.fillOval((int)( -0.1 * w), (int)( -0.5 * h), + (int)(0.2 * w), (int)(0.2 * h)); + + // Tail + int[] xcoords = + {(int)( -0.05 * w), 0, (int)(0.05 * w)}; + int[] ycoords = + {(int)(0.35 * h), (int)(0.45 * h), (int)(0.35 * h)}; + g.fillPolygon(xcoords, ycoords, 3); + + // Feet + for (int i = 0; i < 4; i++) + { + g.rotate(Math.PI / 2); + g.fillOval((int)( -0.35 * w), (int)( -0.35 * h), + (int)(0.125 * w), (int)(0.125 * h)); + } + return (Image)bi; + } +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/turtlerenderer.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/turtlerenderer.java new file mode 100644 index 0000000..9489e78 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/ch/aplu/turtle/turtlerenderer.java @@ -0,0 +1,312 @@ +// TurtleRenderer.java + +// Copyright 2002 Regula Hoefer-Isenegger +// Some modifications by Aegidius Pluess + +// This file is part of The Java Turtle (TJT) package +// +// TJT is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// TJT is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with The Java Turtle Package; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package ch.aplu.turtle; + +import ch.aplu.turtle.*; +import java.awt.image.*; +import java.awt.*; +import javax.swing.*; +import java.awt.geom.*; +import java.util.*; + + +/** This class is responsible for creating and choosing the correct Turtle picture. + @author Regula Hoefer-Isenegger + @version 0.1 +*/ +public class TurtleRenderer implements ImageObserver { + /** Holds the current image */ + private Image currentImage; + /** Holds all images */ + private Vector images; + /** Tells how many pictures are needed*/ + private int resolution; + /** A reference to the Turtle */ + private Turtle turtle; + /** Holds the current Angle */ + private double currentAngle; + + private final int turtleSize = 29; + + /***/ + public TurtleRenderer(Turtle turtle) { + this.currentImage = null; + this.images = new Vector(); + this.turtle = turtle; + currentAngle = 0; + } + + /** As an image stays unchanged, there's no need to ever update it. So this method returns always false. + + For further information cf. java.awt.image.ImageObserver. + @see java.awt.image.ImageObserver#imageUpdate(java.awt.Image, int, int, int, int, int) + */ + public boolean imageUpdate(Image img, int infoflags, + int x, int y, int width, int height) { + return false; + } + + /** Return the current image. + */ + public Image currentImage() { + return this.currentImage; + } + + /** Tell whether the image has changed. + */ + public boolean imageChanged(double angle) { + return (this.currentImage != getImage(angle)); + } + + /** Set the current image to the specified one. */ + private void setCurrentImage(Image image) { + currentImage = image; + } + + /** Choose the image for the angle angle. + */ + private Image getImage(double angle) { + while (angle < 0) { + angle += 2*Math.PI; + } + while (angle >= 2*Math.PI) { + angle -= 2*Math.PI; + } + double res = 2*Math.PI/(double)this.resolution; + int index = (int)(angle/res); + return image(index); + } + + /** Set the current image to the one corresponding to the angle angle. + */ + public void setAngle(double angle) { + currentAngle = angle; + setCurrentImage(getImage(angle)); + } + + /** @return the current angle. */ + protected double getAngle(){ + return currentAngle; + } + + /** Create the images. There are resolution images (i.e. two subsequent + images contain an angle of 2π/ or 360/resolution degrees). + */ + public void init(TurtleFactory factory, int resolution) { + this.resolution = resolution; + Integer res = new Integer(resolution); + double incRes = Math.PI*2/res.doubleValue(); + double angle = 0; + images = new Vector(); + for (int i = 0; i < resolution; i++) { + images.add(factory.turtleImage(turtle.getColor(), + turtle.getPlayground().toScreenAngle(angle), + turtleSize, turtleSize)); + angle += incRes; + } + setCurrentImage(getImage(currentAngle)); + } + + /** Tell how many images this TurtleRenderer holds */ + private int countImages() { + return this.images.size(); + } + + /** Get the image at index */ + private Image image(int index) { + return (Image)this.images.elementAt(index); + } + + /** This method is responsible for painting the turtle onto the + playground at (x, y). + */ + public final void paint(double x, double y) { + internalPaint(x, y, turtle.getPlayground().getGraphics()); + } + + /** This method is responsible for painting the turtle onto the + playground at p. + */ + public final void paint(Point2D.Double p) { + internalPaint(p.x, p.y, turtle.getPlayground().getGraphics()); + } + + /** This method is responsible for painting the Turtle + at (x, y).
+ The Graphics argument tells where to paint. + */ + public final void paint(double x, double y, Graphics g) { + internalPaint(x, y, g); + } + + /** This method is responsible for painting the Turtle + at p.
+ The Graphics argument tells where to paint. + */ + public void paint(Point2D.Double p, Graphics g){ + internalPaint(p.x, p.y, g); + } + + /** Call clipPaint and wrapPaint(). + + You should override this method only, if you add a new (edge) + behaviour to the turtle. I recommend to you then to first call this + method from the overriding one. +
+ If you want to change anything about the real painting, override + clipPaint or wrapPaint. + + @see #clipPaint(int, int, Graphics2D) + @see #wrapPaint(int, int, Graphics2D) + */ + protected void internalPaint(double x, double y, Graphics g) { + if(turtle.isClip()){ + Point2D.Double p = + calcTopLeftCorner(turtle.getPlayground().toScreenCoords(x, y)); + clipPaint((int)p.x, (int)p.y, (Graphics2D)g); + } + else if(turtle.isWrap()){ + Point2D.Double p = + calcTopLeftCorner(turtle.getPlayground().toScreenCoords(x, y)); + wrapPaint((int)p.x, (int)p.y, (Graphics2D)g); + } + } + + /** Define how to paint in clip mode and do it */ + protected void clipPaint(int x, int y, Graphics2D g2D) { + g2D.drawImage(currentImage, x, y, this); + } + + /** Define how to paint in wrap mode and do it */ + protected void wrapPaint(int x, int y, Graphics2D g2D) { + int pWidth = turtle.getPlayground().getWidth(); + int pHeight = turtle.getPlayground().getHeight(); + int paintX = x; + while (paintX > pWidth) { + paintX -= pWidth; + } + while (paintX < 0) { + paintX += pWidth; + } + int paintY = y; + while (paintY > pHeight) { + paintY -= pHeight; + } + while (paintY < 0) { + paintY += pHeight; + } + g2D.drawImage(currentImage, paintX, paintY, this); + int nWidth = currentImage.getWidth(this); + int nHeight = currentImage.getHeight(this); + boolean right = (paintX+nWidth > pWidth); + boolean bottom = (paintY+nHeight > pHeight); + if(right) { + g2D.drawImage(currentImage, + paintX-pWidth, + paintY, + this); + } + if(bottom) { + g2D.drawImage(currentImage, + paintX, + paintY-pHeight, + this); + } + if(right && bottom) { + g2D.drawImage(currentImage, + paintX-pWidth, + paintY-pHeight, + this); + } + } + + /** Compute the x-coordinate of the top left corner of the Turtle image + (it depends on the specified x-coordinate and the image width). + */ + protected int calcTopLeftCornerX(double x) { + int intX = (int)x; + int nWidth; + if(currentImage == null) { + setCurrentImage(new TurtleFactory(). + turtleImage(turtle.getColor(), getAngle(), turtleSize, turtleSize)); + } + nWidth = this.currentImage.getWidth(this); + // the center of the turtle lies on the turtle's location: + intX -= nWidth/2; + return intX; // top left corner of the Turtle's image + } + + /** Compute the y-coordinate of the top left corner of the Turtle image + (it depends on the specified y-coordinate and the image height). + */ + protected int calcTopLeftCornerY(double y) { + int intY = (int)y; + if(currentImage == null) { + setCurrentImage(new TurtleFactory(). + turtleImage(turtle.getColor(), getAngle(), turtleSize, turtleSize)); + } + int nHeight = currentImage.getHeight(this); + // the center of the turtle lies on the turtle's location: + intY -= nHeight/2; + + return intY; // top left corner of the Turtle's image + } + /** Compute the top left corner of the Turtle image + (dependent on the specified x- and y-coordinate and the image + width and height. + */ + protected Point2D.Double calcTopLeftCorner(double x, double y) { + if(currentImage == null) { + setCurrentImage(new TurtleFactory(). + turtleImage(turtle.getColor(), getAngle(), turtleSize, turtleSize)); + } + int w = currentImage.getWidth(this); + int h = currentImage.getHeight(this); + return new Point2D.Double(x-w/2, y-h/2); + } + + /** Compute the top left corner of the Turtle image + (dependent on the specified point p and the image + width and height. + */ + protected Point2D.Double calcTopLeftCorner(Point2D.Double p) { + return calcTopLeftCorner(p.x, p.y); + } +} + + + + + + + + + + + + + + + + + + diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/eval.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/eval.java new file mode 100644 index 0000000..97c8ff0 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/eval.java @@ -0,0 +1,50 @@ +public class Eval { + + public static Wrap getOb(Object o) { + return new Wrap(o); + } + + protected static Object getOb(final String s) { + return new Object() {public String result = s;}; + } + + protected static Object getOb(final boolean b) { + return new Object() {public boolean result = b;}; + } + + protected static Object getOb(final byte b) { + return new Object() {public byte result = b;}; + } + + protected static Object getOb(final char c) { + return new Object() {public char result = c;}; + } + + protected static Object getOb(final double d) { + return new Object() {public double result = d;}; + } + + protected static Object getOb(final float f) { + return new Object() {public float result = f;}; + } + + protected static Object getOb(final int i) { + return new Object() {public int result = i;}; + } + + protected static Object getOb(final long l) { + return new Object() {public long result = l;}; + } + protected static Object getOb(final short s) { + return new Object() {public short result = s;}; + } +} + + +class Wrap { + public Object result; + + Wrap(Object result) { + this.result = result; + } +} \ No newline at end of file diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/inout.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/inout.java new file mode 100644 index 0000000..d702d54 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/inout.java @@ -0,0 +1,98 @@ +import java.io.*; + +/** +Support class for console input and output of numeric values.

+ +Example for input:
+ int age = InOut.readInt("Your age: ");

+ +Example for output:
+ System.out.println("price: " + InOut.format2(prize) + "Euro");
+ System.out.println("percent: " + InOut.formatN(percent, 1) + " %"); +*/ + + +public class InOut { + + /** Formats a double-value with 2 decimal digits. */ + public static String format2(double d) { + return String.format("%.2f", d); + } + + /** Formats a double-value with N decimal digits. */ + public static String formatN(double d, int N) { + return String.format("%." + N + "f", d); + } + + /** Reads a boolean-value from console. */ + public static boolean readBoolean(String prompt) { + final String[] trueValues = + { "1", "y", "t", "j", "w", "yes", "true", "ja", "wahr", "ok" }; + System.out.print(prompt); + String input = readln().toLowerCase(); + for (int i = 0; i < trueValues.length; ++i) + if (trueValues[i].equals(input)) + return true; + return false; + } + + /** Reads a char-value from console. */ + public static char readChar(String prompt) { + System.out.print(prompt); + return readln().charAt(0); + } + + /** Reads a double-value from console. */ + public static double readDouble(String prompt) { + System.out.print(prompt); + return Double.parseDouble(readln()); + } + + /** Reads a float-value from console. */ + public static float readFloat(String prompt) { + System.out.print(prompt); + return Float.parseFloat(readln()); + } + + /** Reads an int-value from console. */ + public static int readInt(String prompt) { + System.out.print(prompt); + return Integer.parseInt(readln()); + } + + /** Reads a long-value from console. */ + public static long readLong(String prompt) { + System.out.print(prompt); + return Long.parseLong(readln()); + } + + /** Reads a string-value from console. */ + public static String readString(String prompt) { + System.out.print(prompt); + return readln(); + } + + /** Reads a string-value from console without prompt. + For use at the end of a console program. */ + public static String readln() { + try { + return Input.readLine(); + } catch(Exception e) { + return ""; + } + } + + private static BufferedReader Input; + + static { + try { + Input = new BufferedReader(new InputStreamReader(System.in)); + } + catch (Exception e) { + System.out.println("console input not possible."); + } + } + + + +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/je/util/turtle.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/je/util/turtle.java new file mode 100644 index 0000000..4a704ad --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/je/util/turtle.java @@ -0,0 +1,311 @@ +package je.util; +/* + * Class : Turtle + * Copyright: (c) Gerhard Röhner + * + * History + * -------- + * 3.00 2000.10.10 first version as java class + * 3.01 2002.10.22 DrawDynamic + * 3.02 2010.10.11 sleepTime for paint delay + * 3.03 2012.03.20 cartesian coordinate system, used with originX, originY, setOrigin + * 3.04 2015.01.24 english version, integrated component + */ + + +import java.awt.*; +import java.awt.image.*; +import java.awt.event.*; + +/** + * This class is a turtle component for simple graphic programming. + * + * @see + * @author Gerhard Röhner + * @version 3.03 20/03/2012 + */ + + +public class Turtle extends Canvas { + + // -- private Attribute ------------------------------------------------------ + + private BufferedImage myBufferedImage; + private Graphics myBufferedGraphics; + private Color foreground; + private Color background; + private static final double piDurch180 = Math.PI / 180; + + // -- public attributes ----------------------------------------------------- + + /** + * turtleX is the x-coordinate of the turtle + *

+ * Example:

myTurtle.turtleX = 100;
+ */ + public double turtleX; + + /** + * turtleY is the y-coordinate of the turtle. + *

+ * Example:

myTurtle.turtleY = 200;
+ */ + public double turtleY; + + /** + * turtleW is the current angle of the turtle in the range form 0 to 360 degrees. + *

+ * Example:

myTurtle.turtleW = 180;
+ */ + public double turtleW; + + /** + * originX is the x-position of the cartesian coodinate system within the turtle canvas. + *

+ * Example:

myTurtle.originX = 0;
+ */ + public double originX; + + /** + * originY is the y-position of the cartesian coodinate system within the turtle canvas. + *

+ * Example:

myTurtle.originY = 100;
+ */ + public double originY; + + /** + * If drawDynamic is true you can watch the drawing of the turtle. + *

+ * Example:

myTurtle.drawDynamic = true;
+ */ + public boolean drawDynamic; + + /** + * For drawDynamic = true you set the delay in milliseconds for drawing. + *

+ * Example:

myTurtle.sleepTime = 500;
+ */ + public int sleepTime = 0; + + // --- constructor ----------------------------------------------------------- + + /** + * Creates a Turtle with a canvas. + * At the beginning the Turtle is placed in the middle of it's canvas. + * The start angle is 0 degree, which means the Turtle looks to the right. + * The background color is white, the drawing color black. + *

+ * The turtle position can easily be changed by clicking into the canvas. + *

+ * The size of the canvas can easily be changed with the mouse. + *

+ * Example:

Turtle myTurtle = new Turtle();
+ */ + + public Turtle() { + myBufferedImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); + myBufferedGraphics = myBufferedImage.getGraphics(); + + setForeground(Color.black); + setBackground(Color.white); + drawDynamic = true; + + addMouseListener(new MouseAdapter() { + public void mouseClicked(MouseEvent evt) { + turtleMouseClicked(evt);}}); + addComponentListener(new ComponentAdapter() { + public void componentResized(ComponentEvent evt) { + turtleResized(evt);}}); + setOrigin(50, 50); + } + + private void turtleMouseClicked(MouseEvent evt) { + turtleX = evt.getX() - originX; + turtleY = originY - evt.getY(); + turtleW = 0; + } + + private void turtleResized(ComponentEvent evt) { + int width = getWidth(); + int height = getHeight(); + + BufferedImage newBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics newGraphics = newBufferedImage.getGraphics(); + newGraphics.setColor(background); + newGraphics.fillRect(0, 0, width, height); + newGraphics.setColor(foreground); + newGraphics.drawImage(myBufferedImage, 0, 0, this); + + turtleX = 0; + turtleY = 0; + turtleW = 0; + setOrigin(width / 2, height / 2); + + myBufferedImage = newBufferedImage; + myBufferedGraphics = newGraphics; + } + + public boolean isDoubleBuffered() { + return true; + } + + private void wTurtleMod360() { + while (turtleW >= 360) + turtleW = turtleW - 360; + while (turtleW < 0) + turtleW = turtleW + 360; + } + + // --- angle and turns ------------------------------------------------------- + + /** + * Turns the angle of the turtle relativ by the parameter angle. + * Positive values means a turn right, negative a turn left. + *

+ * Example:

myTurtle.turn(-90);
+ */ + public void turn(double angle) { + turtleW = turtleW + angle; + wTurtleMod360(); + } + + /** + * Sets the angle of the turtle absolute by the parameter angle. + * The angle increases counterclockwise, therefore + * right = 0, top = 90, left = 180, bottom = 270. + *

+ * Example:

myTurtle.turnto(270);
+ */ + public void turnto(double angle) { + turtleW = angle; + wTurtleMod360(); + } + + // --- Drawing --------------------------------------------------------------- + + /** + * The Turtle draws a line of the length specified in the current direction. + *

+ * Example:

myTurtle.draw(100);
+ */ + public void draw(double length) { + drawto(turtleX + length * Math.cos(turtleW * piDurch180), + turtleY + length * Math.sin(turtleW * piDurch180)); + } + + /** + * The Turtle draws a line form the current position (turtleX, turtleY) to the + * position (x, y) relativ to the cartesian coordinate system. + *

+ * Example:

myTurtle.drawto(200, 300);
+ */ + public void drawto(double x, double y) { + int x1 = (int) (originX + turtleX); + int x2 = (int) (originX + x); + int y1 = (int) (originY - turtleY); + int y2 = (int) (originY - y); + + myBufferedGraphics.drawLine(x1, y1, x2, y2); + if (drawDynamic){ + getGraphics().drawLine(x1, y1, x2, y2); + try { + Thread.currentThread().sleep(sleepTime); + } catch(InterruptedException e) { + } + } else + repaint(); + + turtleX = x; + turtleY = y; + } + + // --- Moving ---------------------------------------------------------------- + + /** + * The Turtle moves without drawing the length specified in the current + * direction. + *

+ * Example:

myTurtle.move(100);
+ */ + public void move(double length) { + turtleX = turtleX + length * Math.cos (turtleW * piDurch180); + turtleY = turtleY + length * Math.sin (turtleW * piDurch180); + } + + /** + * The Turtle moves without drawing to position (x, y) relativ to the + * cartesian coordinate system. + *

+ * Example:

myTurtle.moveto(100, 200);
+ */ + public void moveto(double x, double y) { + turtleX = x; + turtleY = y; + } + + // --- set origin ----------------------------------------------------------- + + /** + * Sets the origin of the cartesian coordinate system within the turtle's canvas. + *

+ * Example:

myTurtle.setOrigin(100, 200);
+ */ + public void setOrigin(double x, double y) { + originX = x; + originY = y; + } + + // --- fore- and background color -------------------------------------------- + + /** + * Sets the drawing color of the Turtle to color c. + *

+ * Example:

myTurtle.setForeground(Color.red);
+ */ + public void setForeground(Color c) { + foreground = c; + myBufferedGraphics.setColor(foreground); + super.setForeground(foreground); + } + + /** + * Sets the canvas color of the Turtle to color c. + *

+ * Example:

myTurtle.setBackground(Color.blue); 
+ */ + public void setBackground(Color c) { + background = c; + myBufferedGraphics.setColor(background); + myBufferedGraphics.fillRect(0, 0, getWidth(), getHeight()); + myBufferedGraphics.setColor(foreground); + repaint(); + } + + /** + * Clear the Turtle's canvas with the background color. + *

+ * Example:

myTurtle.clear();
+ */ + public void clear() { + setBackground(background); + getGraphics().drawImage(myBufferedImage, 0, 0, this); + repaint(); + } + + // --- Showing -------------------------------------------------------------- + + /** + * Shows the Turtle's canvas. + */ + public void paint(Graphics g) { + g.drawImage(myBufferedImage, 0, 0, this); + } + + /** + * Updates the Turtle's canvas. + */ + public void update(Graphics g) { + paint(g); + } + +} diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/jeclasses.jar b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/jeclasses.jar new file mode 100644 index 0000000..7fa4892 Binary files /dev/null and b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/jeclasses.jar differ diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/jnumberfield.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/jnumberfield.java new file mode 100644 index 0000000..fd41a78 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/jnumberfield.java @@ -0,0 +1,129 @@ +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import java.util.*; + +/** +Swing-component for input and output of numeric values. +*/ + +public class JNumberField extends JTextField { + + /** constructor for a JNumberField */ + public JNumberField() { + enableEvents(AWTEvent.KEY_EVENT_MASK); + } + + /** Gets a double-value from the JNumberField. */ + public double getDouble() { + Double d = new Double(getText()); + return d.doubleValue(); + } + + /** Gets a float-value from the JNumberField. */ + public float getFloat() { + Double d = new Double(getText()); + return d.floatValue(); + } + + /** Gets an int-value from the JNumberField. */ + public int getInt() { + Double d = new Double(getText()); + return d.intValue(); + } + + /** Gets a long-value from the JNumberField. */ + public long getLong() { + Double d = new Double(getText()); + return d.longValue(); + } + + /** Checks wether the JNumberField contains a valid numeric value. */ + public boolean isNumeric() { + final String Digits = "(\\p{Digit}+)"; + final String HexDigits = "(\\p{XDigit}+)"; + // an exponent is 'e' or 'E' followed by an optionally + // signed decimal integer. + final String Exp = "[eE][+-]?"+Digits; + final String fpRegex = + ("[\\x00-\\x20]*"+ // Optional leading "whitespace" + "[+-]?(" + // Optional sign character + "NaN|" + // "NaN" string + "Infinity|" + // "Infinity" string + + // A decimal floating-point string representing a finite positive + // number without a leading sign has at most five basic pieces: + // Digits . Digits ExponentPart FloatTypeSuffix + // + // Since this method allows integer-only strings as input + // in addition to strings of floating-point literals, the + // two sub-patterns below are simplifications of the grammar + // productions from the Java Language Specification, 2nd + // edition, section 3.10.2. + + // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt + "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+ + + // . Digits ExponentPart_opt FloatTypeSuffix_opt + "(\\.("+Digits+")("+Exp+")?)|"+ + + // Hexadecimal strings + "((" + + // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt + "(0[xX]" + HexDigits + "(\\.)?)|" + + + // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt + "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" + + + ")[pP][+-]?" + Digits + "))" + + "[fFdD]?))" + + "[\\x00-\\x20]*");// Optional trailing "whitespace" + + return java.util.regex.Pattern.matches(fpRegex, getText()); + } + + /** Sets a double-value into the JNumberField. */ + public void setDouble(double d) { + setText(String.valueOf(d)); + } + + /** Sets a double-value with N digits into the JNumberField. */ + public void setDouble(double d, int N) { + setText(String.format(Locale.ENGLISH, "%." + N + "f", d)); + } + + /** Sets a float-value into the JNumberField. */ + public void setFloat(float f) { + setText(String.valueOf(f)); + } + + /** Sets a float-value with N digits into the JNumberField. */ + public void setFloat(float f, int N) { + setText(String.format(Locale.ENGLISH, "%." + N + "f", f)); + } + + /** Sets an int-value into the JNumberField. */ + public void setInt(int i) { + setText(String.valueOf(i)); + } + + /** Sets a long-value into the JNumberField. */ + public void setLong(long l) { + setText(String.valueOf(l)); + } + + /** Clears the JNumberField */ + public void clear() { + setText(""); + } + + protected void processKeyEvent(KeyEvent e) { + super.processKeyEvent(e); + if (isNumeric() || getText().equals("-") || + getText().equals("") || getText().equals(".")) + setBackground(Color.white); + else + setBackground(Color.red); + } + +} \ No newline at end of file diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/meta-inf/manifest.mf b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/meta-inf/manifest.mf new file mode 100644 index 0000000..4e2b00e --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/meta-inf/manifest.mf @@ -0,0 +1,5 @@ +Manifest-Version: 1.0 +Class-Path: JEClasses.jar +Created-By: 1.8.0_161 (Oracle Corporation) +Main-Class: BreakVigenere + diff --git a/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/numberfield.java b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/numberfield.java new file mode 100644 index 0000000..ca7ef01 --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/angriffvigenere_partiellebruteforce/numberfield.java @@ -0,0 +1,128 @@ +import java.awt.*; +import java.awt.event.*; +import java.util.*; + +/** +AWT-component for input and output of numeric values. +*/ + +public class NumberField extends TextField { + + /** constructor for a NumberField */ + public NumberField() { + enableEvents(AWTEvent.KEY_EVENT_MASK); + } + + /** Gets a double-value from the NumberField. */ + public double getDouble() { + Double d = new Double(getText()); + return d.doubleValue(); + } + + /** Gets a float-value from the NumberField. */ + public float getFloat() { + Double d = new Double(getText()); + return d.floatValue(); + } + + /** Gets an int-value from the NumberField. */ + public int getInt() { + Double d = new Double(getText()); + return d.intValue(); + } + + /** Gets a long-value from the NumberField. */ + public long getLong() { + Double d = new Double(getText()); + return d.longValue(); + } + + /** Checks wether the NumberField contains a valid numeric value. */ + public boolean isNumeric() { + final String Digits = "(\\p{Digit}+)"; + final String HexDigits = "(\\p{XDigit}+)"; + // an exponent is 'e' or 'E' followed by an optionally + // signed decimal integer. + final String Exp = "[eE][+-]?"+Digits; + final String fpRegex = + ("[\\x00-\\x20]*"+ // Optional leading "whitespace" + "[+-]?(" + // Optional sign character + "NaN|" + // "NaN" string + "Infinity|" + // "Infinity" string + + // A decimal floating-point string representing a finite positive + // number without a leading sign has at most five basic pieces: + // Digits . Digits ExponentPart FloatTypeSuffix + // + // Since this method allows integer-only strings as input + // in addition to strings of floating-point literals, the + // two sub-patterns below are simplifications of the grammar + // productions from the Java Language Specification, 2nd + // edition, section 3.10.2. + + // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt + "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+ + + // . Digits ExponentPart_opt FloatTypeSuffix_opt + "(\\.("+Digits+")("+Exp+")?)|"+ + + // Hexadecimal strings + "((" + + // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt + "(0[xX]" + HexDigits + "(\\.)?)|" + + + // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt + "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" + + + ")[pP][+-]?" + Digits + "))" + + "[fFdD]?))" + + "[\\x00-\\x20]*");// Optional trailing "whitespace" + + return java.util.regex.Pattern.matches(fpRegex, getText()); + } + + /** Sets a double-value into the NumberField. */ + public void setDouble(double d) { + setText(String.valueOf(d)); + } + + /** Sets a double-value with N digits into the NumberField. */ + public void setDouble(double d, int N) { + setText(String.format(Locale.ENGLISH, "%." + N + "f", d)); + } + + /** Sets a float-value into the NumberField. */ + public void setFloat(float f) { + setText(String.valueOf(f)); + } + + /** Sets a float-value with N digits into the NumberField. */ + public void setFloat(float f, int N) { + setText(String.format(Locale.ENGLISH, "%." + N + "f", f)); + } + + /** Sets an int-value into the NumberField. */ + public void setInt(int i) { + setText(String.valueOf(i)); + } + + /** Sets a long-value into the NumberField. */ + public void setLong(long l) { + setText(String.valueOf(l)); + } + + /** Clears the NumberField */ + public void clear() { + setText(""); + } + + protected void processKeyEvent(KeyEvent e) { + super.processKeyEvent(e); + if (isNumeric() || getText().equals("-") || + getText().equals("") || getText().equals(".")) + setBackground(Color.white); + else + setBackground(Color.red); + } + +} \ No newline at end of file diff --git a/Software/iud_key_angriffe_vigenere/readme.adoc b/Software/iud_key_angriffe_vigenere/readme.adoc new file mode 100644 index 0000000..01321fa --- /dev/null +++ b/Software/iud_key_angriffe_vigenere/readme.adoc @@ -0,0 +1,12 @@ += Material der ZPG IMP 8: + +|=== +|Zuordnung| Rechner und Netze +|Klassenstufe| IMP 8 +|Bildungsplanbezug | +|Werkzeug| +|Autoren| +|=== + +== Inhalt +Programme für die Kryptoanalyse der Vigenèreverschlüsselung \ No newline at end of file