tdulik

Tree Painter assignment - step 1

Apr 16th, 2020
225
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package treePainter;
  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.JButton;
  9. import java.awt.BorderLayout;
  10. import javax.swing.JPanel;
  11. import javax.swing.JLabel;
  12. import javax.swing.JTextField;
  13. import javax.swing.UIManager;
  14.  
  15. public class Main {
  16.  
  17.     private JFrame frmTreePainter;
  18.     private JTextField textField;
  19.  
  20.     class MyPanel extends JPanel {
  21.         private static final long serialVersionUID = 1L;
  22.         int levels = 4;
  23.  
  24.         void paintTree(Graphics g, int x, int y, int level) {
  25.             if (level < levels) {
  26.                 g.drawOval(x, y, 10, 10);
  27.                 g.drawString(""+x, x, y);
  28.                 int distance=30;
  29.                 paintTree(g, x - distance, y + 20, level + 1);
  30.                 paintTree(g, x + distance, y + 20, level + 1);
  31.                 if (level < levels-1) {
  32.                     g.drawLine(x, y, x+distance, y+20);
  33.                     g.drawLine(x, y, x-distance, y+20);
  34.                 }
  35.  
  36.             }
  37.         }
  38.  
  39.         @Override
  40.         public void paint(Graphics g) {
  41.             // TODO Auto-generated method stub
  42.             super.paint(g);
  43.             paintTree(g, frmTreePainter.getWidth()/2, 10, 0);
  44.         }
  45.  
  46.     }
  47.  
  48.     /**
  49.      * Launch the application.
  50.      */
  51.     public static void main(String[] args) {
  52.         try {
  53.             UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
  54.         } catch (Throwable e) {
  55.             e.printStackTrace();
  56.         }
  57.         EventQueue.invokeLater(new Runnable() {
  58.             public void run() {
  59.                 try {
  60.                     Main window = new Main();
  61.                     window.frmTreePainter.setVisible(true);
  62.                 } catch (Exception e) {
  63.                     e.printStackTrace();
  64.                 }
  65.             }
  66.         });
  67.     }
  68.  
  69.     /**
  70.      * Create the application.
  71.      */
  72.     public Main() {
  73.         initialize();
  74.     }
  75.  
  76.     /**
  77.      * Initialize the contents of the frame.
  78.      */
  79.     private void initialize() {
  80.         frmTreePainter = new JFrame();
  81.         frmTreePainter.setTitle("Tree Painter");
  82.         frmTreePainter.setIconImage(Toolkit.getDefaultToolkit()
  83.                 .getImage(Main.class.getResource("/javax/swing/plaf/metal/icons/sortUp.png")));
  84.         frmTreePainter.setBounds(100, 100, 450, 300);
  85.         frmTreePainter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  86.  
  87.         JPanel panelForControls = new JPanel();
  88.         frmTreePainter.getContentPane().add(panelForControls, BorderLayout.NORTH);
  89.  
  90.         JLabel lblNewLabel = new JLabel("Tree levels:");
  91.         panelForControls.add(lblNewLabel);
  92.  
  93.         textField = new JTextField();
  94.         textField.setText("4");
  95.         panelForControls.add(textField);
  96.         textField.setColumns(10);
  97.  
  98.         JButton btnDrawIt = new JButton("Draw it!");
  99.         panelForControls.add(btnDrawIt);
  100.  
  101.         MyPanel panel = new MyPanel();
  102.         frmTreePainter.getContentPane().add(panel, BorderLayout.CENTER);
  103.     }
  104.  
  105. }
RAW Paste Data