ERLEDIGT
NEIN
NEIN
ANTWORTEN
0
0
ZUGRIFFE
359
359
EMPFEHLEN
-
21.07.09 23:35 #1
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.885
- Blog-Einträge
- 29
Hallo,
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
package de.tutorials; import static java.awt.RenderingHints.KEY_ANTIALIASING; import static java.awt.RenderingHints.VALUE_ANTIALIAS_ON; import static java.lang.Math.cos; import static java.lang.Math.sin; import static java.lang.Math.toRadians; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.GeneralPath; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Stack; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class TurtleUI extends JFrame { GeneralPath path; Point displaySize; Point startPosition; int stepWidth; double angleStep; String code; int iterations; Map<String, List<Rule>> ruleClassToRulesMap = new LinkedHashMap<String, List<Rule>>(); JTextArea txtRule; JButton btnDraw; JScrollPane scrollPane; double totalRuleProbability; JPanel panel; public TurtleUI() { super("Turtle"); setDefaultCloseOperation(EXIT_ON_CLOSE); initComponents(); pack(); } private void initComponents() { path = new GeneralPath(); panel = new JPanel() { protected void paintComponent(Graphics g) { Graphics2D gr = (Graphics2D) g; gr.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON); gr.clearRect(0, 0, getWidth(), getHeight()); gr.draw(path); } }; scrollPane = new JScrollPane(panel); scrollPane.setPreferredSize(new Dimension(640, 480)); add(scrollPane, BorderLayout.NORTH); txtRule = new JTextArea(10, 20); add(txtRule, BorderLayout.CENTER); btnDraw = new JButton("draw"); btnDraw.addActionListener(createDrawButtonActionListener()); add(btnDraw, BorderLayout.SOUTH); } private ActionListener createDrawButtonActionListener() { return new ActionListener() { public void actionPerformed(ActionEvent e) { parseInstructions(txtRule.getText()); panel.setPreferredSize(new Dimension(displaySize.x, displaySize.y)); pack(); expandCode(); drawTurtle(); panel.updateUI(); } }; } private void drawTurtle() { double angle = 0; double x = startPosition.x; double y = displaySize.y - startPosition.y; path.reset(); path.moveTo(x, y); Stack<double[]> markedPositions = new Stack<double[]>(); for (char c : code.toCharArray()) { switch (c) { case 'F': double angleAsRadians = toRadians(angle); x += cos(angleAsRadians) * stepWidth; y -= sin(angleAsRadians) * stepWidth; path.lineTo(x, y); break; case '+': angle += angleStep; break; case '-': angle -= angleStep; break; case '[': markedPositions.push(new double[] { x, y, angle }); break; case ']': double[] xyAngle = markedPositions.pop(); x = xyAngle[0]; y = xyAngle[1]; angle = xyAngle[2]; path.moveTo(x, y); } } } private void expandCode() { for (int i = 0; i < iterations; i++) { StringBuilder builder = new StringBuilder(); for (char c : code.toCharArray()) { String ruleKey = String.valueOf(c); if (ruleClassToRulesMap.containsKey(ruleKey)) { for (Rule rule : ruleClassToRulesMap.get(ruleKey)) { if (Math.random() <= rule.probability) { builder.append(rule.code); } } } else { builder.append(c); } } code = builder.toString(); } } /** * @param args */ public static void main(String[] args) { new TurtleUI().setVisible(true); } private void parseInstructions(String input) { Scanner scanner = new Scanner(input); displaySize = asPoint(scanner.nextLine()); startPosition = asPoint(scanner.nextLine()); stepWidth = Integer.parseInt(scanner.nextLine()); angleStep = Double.parseDouble(scanner.nextLine()); code = scanner.nextLine(); resetTurtle(); if (scanner.hasNextLine()) { iterations = Integer.parseInt(scanner.nextLine()); while (scanner.hasNextLine()) { String ruleText = scanner.nextLine(); Rule rule = parseRule(ruleText); register(rule); } normalizeRuleProbabilities(); System.out.println(ruleClassToRulesMap); } scanner.close(); } private Rule parseRule(String ruleText) { String ruleName = "F"; String ruleCode = ruleText; double ruleProbability = 1.0; if (ruleText.contains(" ")) { String[] tokens = ruleText.split(" "); ruleName = tokens[0]; if (tokens.length == 3) { ruleProbability = Double.parseDouble(tokens[1]); totalRuleProbability += ruleProbability; ruleCode = tokens[2]; } else { ruleCode = tokens[1]; } } return new Rule(ruleName, ruleCode, ruleProbability); } private void resetTurtle() { for(String ruleClass : ruleClassToRulesMap.keySet()){ if (ruleClassToRulesMap.containsKey(ruleClass)){ ruleClassToRulesMap.get(ruleClass).clear(); } } ruleClassToRulesMap.clear(); ruleClassToRulesMap = new LinkedHashMap<String, List<Rule>>(); totalRuleProbability = 0.0; } private void register(Rule rule) { List<Rule> rules = ruleClassToRulesMap.get(rule.name); if (rules == null) { ruleClassToRulesMap.put(rule.name, rules = new ArrayList<Rule>()); } rules.add(rule); } private void normalizeRuleProbabilities() { for (String ruleClass : ruleClassToRulesMap.keySet()) { for (Rule rule : ruleClassToRulesMap.get(ruleClass)) { if (totalRuleProbability > 0.0) { rule.probability /= totalRuleProbability; } } } } private Point asPoint(String displaySize) { String[] xy = displaySize.split(" "); return new Point(Integer.parseInt(xy[0]), Integer.parseInt(xy[1])); } static class Rule { String name; String code; double probability; public Rule(String name, String code, double probability) { this.name = name; this.code = code; this.probability = probability; } } }
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
Ähnliche Themen
-
[Quiz#15] Thomas Darimont (Java Genetischer Algorithmus)
Von Thomas Darimont im Forum ArchivAntworten: 1Letzter Beitrag: 11.04.10, 18:58 -
[Quiz #13] Thomas Darimont (Java)
Von Thomas Darimont im Forum ArchivAntworten: 0Letzter Beitrag: 10.01.10, 23:28 -
[QUIZ#7] Thomas Darimont (Java)
Von Thomas Darimont im Forum ArchivAntworten: 1Letzter Beitrag: 08.12.08, 09:45 -
[QUIZ #2] Thomas Darimont (Java)
Von Thomas Darimont im Forum ArchivAntworten: 0Letzter Beitrag: 28.09.08, 14:08 -
[QUIZ#1] Thomas Darimont (Java)
Von Thomas Darimont im Forum ArchivAntworten: 2Letzter Beitrag: 22.09.08, 14:13






Login





