Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tree;
- import java.awt.EventQueue;
- import java.awt.Graphics;
- import javax.swing.JFrame;
- import java.awt.Toolkit;
- import javax.swing.JLabel;
- import java.awt.BorderLayout;
- import javax.swing.JButton;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.UIManager;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- public class Main {
- private JFrame frmTreePainter;
- private JTextField textField;
- MyPanel panel_tree;
- private JLabel lblStatus;
- class MyPanel extends JPanel {
- int levels = 5;
- int minDistance = 5;
- void drawTree(Graphics g, int x, int y, int currentLevel) {
- if (currentLevel <= levels) {
- int distance = minDistance * (1 << (levels - currentLevel));
- g.drawOval(x, y, 10, 10);
- if (currentLevel < levels) {
- g.drawLine(x, y, x - distance, y + 20);
- g.drawLine(x, y, x + distance, y + 20);
- }
- drawTree(g, x - distance, y + 20, currentLevel + 1);
- drawTree(g, x + distance, y + 20, currentLevel + 1);
- }
- }
- @Override
- public void paint(Graphics g) {
- // TODO Auto-generated method stub
- super.paint(g);
- drawTree(g, getWidth() / 2, 10, 1);
- }
- }
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- try {
- UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
- } catch (Throwable e) {
- e.printStackTrace();
- }
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Main window = new Main();
- window.frmTreePainter.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the application.
- */
- public Main() {
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- */
- void setLevel() {
- try {
- int depth = Integer.parseInt(textField.getText());
- panel_tree.levels = depth;
- panel_tree.repaint();
- lblStatus.setText("OK!");
- } catch (Exception e) {
- lblStatus.setText("Please enter an integer number!");
- }
- }
- private void initialize() {
- frmTreePainter = new JFrame();
- frmTreePainter.setIconImage(Toolkit.getDefaultToolkit()
- .getImage(Main.class.getResource("/com/sun/java/swing/plaf/windows/icons/ListView.gif")));
- frmTreePainter.setTitle("Tree painter GUI");
- frmTreePainter.setBounds(100, 100, 450, 300);
- frmTreePainter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JPanel panel = new JPanel();
- frmTreePainter.getContentPane().add(panel, BorderLayout.NORTH);
- JLabel lblDepth = new JLabel("Depth:");
- panel.add(lblDepth);
- textField = new JTextField();
- textField.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- setLevel();
- }
- });
- textField.setText("5");
- panel.add(textField);
- textField.setColumns(10);
- JButton btnPaint = new JButton("Paint!");
- btnPaint.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- setLevel();
- }
- });
- panel.add(btnPaint);
- panel_tree = new MyPanel();
- frmTreePainter.getContentPane().add(panel_tree, BorderLayout.CENTER);
- lblStatus = new JLabel(" ");
- frmTreePainter.getContentPane().add(lblStatus, BorderLayout.SOUTH);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment