Last active
July 10, 2019 14:58
-
-
Save ahmednasserpro/6eb8ad4709d37926c906845fd4995fd9 to your computer and use it in GitHub Desktop.
Java draw h tree fractal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
public class HTreeTextField extends JFrame { | |
private JTextField tf = new JTextField("0", 5); | |
private HTree htree = new HTree(); | |
public HTreeTextField() { | |
htree.setSize(600, 500); | |
htree.setLocation(150, 150); | |
htree.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
JPanel panel = new JPanel(); | |
panel.add(new JLabel("Enter an order: ")); | |
panel.add(tf); | |
add(panel); | |
tf.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
int order = Integer.parseInt(tf.getText()); | |
htree.setOrder(order); | |
htree.setVisible(true); | |
} | |
}); | |
} | |
public static void main(String[] args) { | |
JFrame f = new HTreeTextField(); | |
f.setSize(150, 150); | |
f.setLocationRelativeTo(null); | |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
f.setVisible(true); | |
} | |
} | |
class HTree extends JFrame { | |
private int order = 0; | |
public void setOrder(int order) { | |
this.order = order; | |
repaint(); | |
} | |
public void paint(Graphics g) { | |
super.paint(g); | |
Point p1 = new Point(200, 160); | |
Point p2 = new Point(200, 360); | |
Point p3 = new Point(400, 160); | |
Point p4 = new Point(400, 360); | |
g.setColor(Color.red); | |
displayHTree(g, order, 200, p1, p2, p3, p4); | |
} | |
private void displayHTree(Graphics g, int order, int size, | |
Point p1, Point p2, Point p3, Point p4) { | |
g.drawLine(p1.x, p1.y, p2.x, p2.y); | |
g.drawLine(p3.x, p3.y, p4.x, p4.y); | |
g.drawLine(p1.x, p1.y + size / 2, p3.x, p3.y + size / 2); | |
if (order > 0) { | |
Point[] p1Points = splitLine(p1, size); | |
Point[] p2Points = splitLine(p2, size); | |
Point[] p3Points = splitLine(p3, size); | |
Point[] p4Points = splitLine(p4, size); | |
displayHTree(g, order - 1, size / 2, | |
p1Points[0], p1Points[1], p1Points[2], p1Points[3]); | |
displayHTree(g, order - 1, size / 2, | |
p2Points[0], p2Points[1], p2Points[2], p2Points[3]); | |
displayHTree(g, order - 1, size / 2, | |
p3Points[0], p3Points[1], p3Points[2], p3Points[3]); | |
displayHTree(g, order - 1, size / 2, | |
p4Points[0], p4Points[1], p4Points[2], p4Points[3]); | |
} | |
} | |
private Point[] splitLine(Point p, int size) { | |
Point[] points = new Point[4]; | |
points[0] = new Point(p.x - size / 4, p.y - size / 4); | |
points[1] = new Point(p.x - size / 4, p.y + size / 4); | |
points[2] = new Point(p.x + size / 4, p.y - size / 4); | |
points[3] = new Point(p.x + size / 4, p.y + size / 4); | |
return points; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment