tdulik

Tree Painter winter 2020

Nov 4th, 2020 (edited)
859
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package tree;
  2.  
  3. import java.awt.EventQueue;
  4. import java.awt.Graphics;
  5.  
  6. import javax.swing.JFrame;
  7. import java.awt.Toolkit;
  8. import javax.swing.JLabel;
  9. import java.awt.BorderLayout;
  10. import javax.swing.JButton;
  11. import javax.swing.JPanel;
  12. import javax.swing.JTextField;
  13. import javax.swing.UIManager;
  14. import java.awt.event.ActionListener;
  15. import java.awt.event.ActionEvent;
  16.  
  17. public class Main {
  18.  
  19.     private JFrame frmTreePainter;
  20.     private JTextField textField;
  21.     MyPanel panel_tree;
  22.     private JLabel lblStatus;
  23.  
  24.     class MyPanel extends JPanel {
  25.         int levels = 5;
  26.         int minDistance = 5;
  27.  
  28.         void drawTree(Graphics g, int x, int y, int currentLevel) {
  29.             if (currentLevel <= levels) {
  30.                 int distance = minDistance * (1 << (levels - currentLevel));
  31.                 g.drawOval(x, y, 10, 10);
  32.                 if (currentLevel < levels) {
  33.                     g.drawLine(x, y, x - distance, y + 20);
  34.                     g.drawLine(x, y, x + distance, y + 20);
  35.                 }
  36.                 drawTree(g, x - distance, y + 20, currentLevel + 1);
  37.                 drawTree(g, x + distance, y + 20, currentLevel + 1);
  38.             }
  39.  
  40.         }
  41.  
  42.         @Override
  43.         public void paint(Graphics g) {
  44.             // TODO Auto-generated method stub
  45.             super.paint(g);
  46.             drawTree(g, getWidth() / 2, 10, 1);
  47.         }
  48.  
  49.     }
  50.  
  51.     /**
  52.      * Launch the application.
  53.      */
  54.     public static void main(String[] args) {
  55.         try {
  56.             UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  57.         } catch (Throwable e) {
  58.             e.printStackTrace();
  59.         }
  60.         EventQueue.invokeLater(new Runnable() {
  61.             public void run() {
  62.                 try {
  63.                     Main window = new Main();
  64.                     window.frmTreePainter.setVisible(true);
  65.                 } catch (Exception e) {
  66.                     e.printStackTrace();
  67.                 }
  68.             }
  69.         });
  70.     }
  71.  
  72.     /**
  73.      * Create the application.
  74.      */
  75.     public Main() {
  76.         initialize();
  77.     }
  78.  
  79.     /**
  80.      * Initialize the contents of the frame.
  81.      */
  82.     void setLevel() {
  83.         try {
  84.             int depth = Integer.parseInt(textField.getText());
  85.             panel_tree.levels = depth;
  86.             panel_tree.repaint();
  87.             lblStatus.setText("OK!");
  88.  
  89.         } catch (Exception e) {
  90.             lblStatus.setText("Please enter an integer number!");
  91.         }
  92.     }
  93.     private void initialize() {
  94.         frmTreePainter = new JFrame();
  95.         frmTreePainter.setIconImage(Toolkit.getDefaultToolkit()
  96.                 .getImage(Main.class.getResource("/com/sun/java/swing/plaf/windows/icons/ListView.gif")));
  97.         frmTreePainter.setTitle("Tree painter GUI");
  98.         frmTreePainter.setBounds(100, 100, 450, 300);
  99.         frmTreePainter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  100.  
  101.         JPanel panel = new JPanel();
  102.         frmTreePainter.getContentPane().add(panel, BorderLayout.NORTH);
  103.  
  104.         JLabel lblDepth = new JLabel("Depth:");
  105.         panel.add(lblDepth);
  106.  
  107.         textField = new JTextField();
  108.         textField.addActionListener(new ActionListener() {
  109.             public void actionPerformed(ActionEvent e) {
  110.                 setLevel();
  111.             }
  112.         });
  113.         textField.setText("5");
  114.         panel.add(textField);
  115.         textField.setColumns(10);
  116.  
  117.         JButton btnPaint = new JButton("Paint!");
  118.         btnPaint.addActionListener(new ActionListener() {
  119.             public void actionPerformed(ActionEvent arg0) {
  120.                 setLevel();
  121.             }
  122.         });
  123.         panel.add(btnPaint);
  124.  
  125.         panel_tree = new MyPanel();
  126.         frmTreePainter.getContentPane().add(panel_tree, BorderLayout.CENTER);
  127.  
  128.         lblStatus = new JLabel(" ");
  129.         frmTreePainter.getContentPane().add(lblStatus, BorderLayout.SOUTH);
  130.     }
  131.  
  132. }
  133.  
RAW Paste Data