Advertisement
pqpm

Untitled

Apr 20th, 2018
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. package tps.tp2.pack1Recursive;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JLabel;
  8. import javax.swing.SwingUtilities;
  9. import javax.swing.WindowConstants;
  10.  
  11. /**
  12.  * Print recursive squares painting
  13.  *
  14.  */
  15. public class P03Squares {
  16.  
  17.     public static Color BACKGROUNDCOLOR = new Color(0, 0, 150);
  18.     public static Color FOREGROUNDCOLOR = new Color(255, 180, 0);
  19.  
  20.     // Padrao = 5, alterado
  21.     public static int DEEP = 5;
  22.  
  23.     /**
  24.      * Build the frame and shows it
  25.      */
  26.     public P03Squares(int deep) {
  27.  
  28.         // the frame and title
  29.         JFrame frame = new JFrame();
  30.         frame.setTitle("...: Recursive Squares with deep " + deep + " :...");
  31.  
  32.         // Dispose frame on click on close button
  33.         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  34.  
  35.         // set size and center frame on screen
  36.         frame.setSize(400, 400);
  37.         frame.setLocationRelativeTo(null);
  38.  
  39.         // add print area occupying all the frame content area
  40.         frame.add(new PrintArea(deep));
  41.  
  42.         // put frame visible
  43.         frame.setVisible(true);
  44.     }
  45.  
  46.     /**
  47.      * Main method
  48.      */
  49.     public static void main(String[] args) {
  50.         SwingUtilities.invokeLater(new Runnable() {
  51.             public void run() {
  52.                 // launch for 1 to DEEP squares frames
  53.                 for (int i = DEEP; i >= 1; --i) {
  54.                     // build a new object each time: objects will run
  55.                     // independently
  56.                     new P03Squares(i);
  57.                 }
  58.  
  59.             }
  60.         });
  61.     }
  62. }
  63.  
  64. /**
  65.  * Our print area is, in fact, a label extended with the paint squares behavior
  66.  */
  67. class PrintArea extends JLabel {
  68.     private static final long serialVersionUID = 1L;
  69.  
  70.     // local deep variable, will keep the registered deep for this the print
  71.     // area
  72.     int deep;
  73.  
  74.     /**
  75.      * constructor
  76.      */
  77.     public PrintArea(int deep) {
  78.         // call super, that is JLabel, constructor
  79.         super();
  80.  
  81.         // set background color and set as well opaque to allow the background
  82.         // to be visible
  83.         setBackground(P03Squares.BACKGROUNDCOLOR);
  84.         setOpaque(true);
  85.  
  86.         // save the deep
  87.         this.deep = deep;
  88.     }
  89.  
  90.     /**
  91.      * paint method, called by JVM, when it is needed to update the PrintArea
  92.      */
  93.     public void paint(Graphics g) {
  94.         // call paint from the JLABEL, draws the background of the PrintArea
  95.         super.paint(g);
  96.  
  97.         // set drawing color
  98.         g.setColor(P03Squares.FOREGROUNDCOLOR);
  99.  
  100.         // call the amazing print square method
  101.         int n = printSquares(g, 0, 0, getWidth(), getHeight(), this.deep);
  102.  
  103.         // put to the world how much squares we printed
  104.         System.out.println("Deep = " + deep + ", squares painted: " + n);
  105.     }
  106.  
  107.     /**
  108.      * Auxiliary method that will to the work. It must print a square with 1/3
  109.      * of the length of the frame and at the center and if not the bottom level
  110.      * ask to do the same for each of the other 8 square with 1/3 of length but
  111.      * called with the new deep
  112.      */
  113.     private int printSquares(Graphics g, int xi, int yi, int width, int height, int currentDeep) {
  114.  
  115.         // TODO
  116.         // dummy rectangle
  117.         // initX, initY, width, height
  118.  
  119.         //Quadrado central
  120.         int newWidth = width / 3;
  121.         int newHeight = height / 3;
  122.         int x = (width / 3) + xi / 3;
  123.         int y = (height / 3) + yi / 3;
  124.         g.fillRect(x, y, newWidth, newHeight);
  125.  
  126.         int smallerWidth = newWidth / 3;
  127.         int smallerHeight = newHeight / 3;
  128.  
  129.         int sX = 0;
  130.         int sY = 0;
  131.         if (currentDeep > 1) {
  132.             for (int i = 0; i < 3; i++) {
  133.                 sX = width / 9 + (i * 3 * (width / 9));
  134.                 sY = height / 9;
  135.                 g.fillRect(sX, sY, smallerWidth, smallerHeight);
  136.                 for (int j = 0; j < 3; j++) {
  137.                     sY = height / 9 + (j * 3 * (height / 9));
  138.                     g.fillRect(sX, sY, smallerWidth, smallerHeight);
  139.                 }
  140.             }
  141.             return 1 + printSquares(g, sX, sY, newWidth, newHeight, currentDeep - 1);
  142.         } else
  143.             return 1;
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement