- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomPatternGenerator.java
More file actions
Latest commit
58 lines (50 loc) · 1.94 KB
/
Copy pathRandomPatternGenerator.java
File metadata and controls
58 lines (50 loc) · 1.94 KB
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
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.util.Random;
publicclassRandomPatternGeneratorextendsJFrame {
privateJTextAreapatternTextArea;
publicRandomPatternGenerator() {
setTitle("Random Pattern Generator");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create button
JButtongenerateButton = newJButton("Generate Pattern");
generateButton.addActionListener(newActionListener() {
@Override
publicvoidactionPerformed(ActionEvente) {
generatePattern();
}
});
// Create text area
patternTextArea = newJTextArea();
patternTextArea.setEditable(false);
// Add components to frame
getContentPane().setLayout(newBorderLayout());
getContentPane().add(generateButton, BorderLayout.NORTH);
getContentPane().add(newJScrollPane(patternTextArea), BorderLayout.CENTER);
setVisible(true);
}
privatevoidgeneratePattern() {
StringBuilderpattern = newStringBuilder();
Randomrandom = newRandom();
// Generate random pattern here, for example:
introws = random.nextInt(10) + 1; // Random number of rows (1 to 10)
for (inti = 0; i < rows; i++) {
intcols = random.nextInt(10) + 1; // Random number of columns (1 to 10)
for (intj = 0; j < cols; j++) {
pattern.append(random.nextBoolean() ? "* " : "- "); // Randomly choose between '*' and '-'
}
pattern.append("\n");
}
patternTextArea.setText(pattern.toString());
}
publicstaticvoidmain(String[] args) {
SwingUtilities.invokeLater(newRunnable() {
@Override
publicvoidrun() {
newRandomPatternGenerator();
}
});
}
}