QwarkDev

Lab5 / java

Sep 2nd, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.40 KB | None | 0 0
  1. // Main.java
  2.  
  3. package com.company;
  4.  
  5. import java.awt.*;
  6. import javax.swing.JFrame;
  7. import javax.swing.JPanel;
  8. import javax.swing.SwingUtilities;
  9.  
  10. interface IDrawable
  11. {
  12.     Polygon getPolygone();
  13. }
  14.  
  15. class Quadrangle implements IDrawable
  16. {
  17.     int[] xCoords = new int[4];
  18.     int[] yCoords = new int[4];
  19.  
  20.     public Quadrangle(Point[] points)
  21.     {
  22.         for (int i = 0; i < 4; i++)
  23.         {
  24.             xCoords[i] = points[i].x;
  25.             yCoords[i] = points[i].y;
  26.         }
  27.     }
  28.  
  29.     public Quadrangle() { }
  30.  
  31.     @Override
  32.     public Polygon getPolygone() {
  33.         return new Polygon(xCoords, yCoords, 4);
  34.     }
  35. }
  36.  
  37. class Rect extends Quadrangle
  38. {
  39.     int[] xCoords = new int[4];
  40.     int[] yCoords = new int[4];
  41.  
  42.     public Rect(int x1, int y1, int x2, int y2)
  43.     {
  44.         xCoords[0] = x1;
  45.         yCoords[0] = y1;
  46.  
  47.         xCoords[1] = x1;
  48.         yCoords[1] = y2;
  49.  
  50.         xCoords[2] = x2;
  51.         yCoords[2] = y2;
  52.  
  53.         xCoords[3] = x2;
  54.         yCoords[3] = y1;
  55.     }
  56.  
  57.     @Override
  58.     public Polygon getPolygone() {
  59.         return new Polygon(xCoords, yCoords, 4);
  60.     }
  61. }
  62.  
  63. public class Main {
  64.  
  65.     private JFrame mainMap;
  66.  
  67.     public Main() {
  68.  
  69.         initComponents();
  70.  
  71.     }
  72.  
  73.     private void drawShape(Graphics graphics, Color color, IDrawable drawable)
  74.     {
  75.         var oldColor = graphics.getColor();
  76.         graphics.setColor(color);
  77.  
  78.         graphics.fillPolygon((drawable.getPolygone()));
  79.  
  80.         graphics.setColor(oldColor);
  81.     }
  82.  
  83.     public int getRandomNumber(int min, int max) {
  84.         return (int) ((Math.random() * (max - min)) + min);
  85.     }
  86.  
  87.     private Color getRandomColor()
  88.     {
  89.         int color = getRandomNumber(1, 5);
  90.  
  91.         switch (color)
  92.         {
  93.             case 1: return new Color(253, 79, 79);
  94.             case 2: return new Color(79, 253, 113);
  95.             case 3: return new Color(79, 166, 253);
  96.             case 4: return new Color(253, 195, 79);
  97.             case 5: return new Color(253, 79, 219);
  98.         }
  99.  
  100.         return new Color(0, 255, 0);
  101.     }
  102.  
  103.     private void drawShapes(Graphics graphics)
  104.     {
  105.         int row = 0, collumn = 0;
  106.         int size = 100;
  107.  
  108.         for (int i = 1; i < 21; i++)
  109.         {
  110.             Point startPos = new Point(row++ * size + 20, collumn * size +  20);
  111.             var rect = new Rect(startPos.x, startPos.y, startPos.x + size - 20 , startPos.y + size - 20);
  112.  
  113.             drawShape(graphics, getRandomColor(), rect);
  114.  
  115.             if (i % 5 == 0)
  116.             {
  117.                 collumn++;
  118.                 row = 0;
  119.             }
  120.         }
  121.     }
  122.  
  123.     private void initComponents() {
  124.  
  125.         mainMap = new JFrame("Ivan T. / Lab5 / 11-19");
  126.         mainMap.setResizable(false);
  127.  
  128.         mainMap.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  129.  
  130.         JPanel p = new JPanel() {
  131.             @Override
  132.             protected void paintComponent(Graphics g) {
  133.                 super.paintComponent(g);
  134.  
  135.                 drawShapes(g);
  136.             }
  137.  
  138.             @Override
  139.             public Dimension getPreferredSize() {
  140.                 return new Dimension(520, 420);
  141.             }
  142.         };
  143.  
  144.         mainMap.add(p);
  145.         mainMap.pack();
  146.         mainMap.setVisible(true);
  147.     }
  148.  
  149.     public static void main(String[] args) {
  150.         SwingUtilities.invokeLater(() -> new Main());
  151.     }
  152. }
  153.  
  154. // Result:
  155. https://i.imgur.com/sQ5CkRA.png
Add Comment
Please, Sign In to add comment