Advertisement
Guest User

Untitled

a guest
May 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.75 KB | None | 0 0
  1. import jdk.jfr.Percentage;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.MouseAdapter;
  6. import java.awt.event.MouseEvent;
  7.  
  8. public class Main {
  9.  
  10.     public static void main(String[] args) {
  11.         SwingUtilities.invokeLater(() -> {
  12.             Okno okno = new Okno();
  13.         });
  14.     }
  15. }
  16.  
  17. class Okno extends JFrame {
  18.  
  19.     private JRadioButton linia, prostokat, elipsa, zielony, czerwony, niebieski;
  20.     private ButtonGroup ksztalty, kolory;
  21.     private JLabel l, p, e, z, c, n;
  22.     private int x, y;
  23.     private JPanel panel, temp, temp2;
  24.  
  25.     public Okno() {
  26.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  27.         this.setVisible(true);
  28.         this.setSize(500, 500);
  29.         this.setLayout(null);
  30.  
  31.         init();
  32.  
  33.     }
  34.  
  35.     private void init() {
  36.         JPanel ks = ksztalty();
  37.         ks.setBounds(0, 0, 400, 44);
  38.         this.add(ks);
  39.  
  40.         JPanel ko = kolory();
  41.         ko.setBounds(0, 50, 400, 44);
  42.         this.add(ko);
  43.  
  44.         temp2 = new JPanel(null);
  45.         temp2.setBounds(0, 100, 400, 450);
  46.         this.add(temp2);
  47.  
  48.         temp2.addMouseListener(new MouseAdapter() {
  49.             @Override
  50.             public void mousePressed(MouseEvent e) {
  51.                 pobierzWsp(e);
  52.             }
  53.  
  54.             @Override
  55.             public void mouseReleased(MouseEvent e) {
  56.                 rysuj(e);
  57.             }
  58.         });
  59.     }
  60.  
  61.     private JPanel ksztalty() {
  62.         JPanel temp = new JPanel(new FlowLayout());
  63.         this.l = new JLabel("Linia");
  64.         this.linia = new JRadioButton();
  65.         this.p = new JLabel("Prostokat");
  66.         this.prostokat = new JRadioButton();
  67.         this.e = new JLabel("Elipsa");
  68.         this.elipsa = new JRadioButton();
  69.         this.ksztalty = new ButtonGroup();
  70.         this.ksztalty.add(linia);
  71.         this.ksztalty.add(prostokat);
  72.         this.ksztalty.add(elipsa);
  73.  
  74.         temp.add(l);
  75.         temp.add(linia);
  76.         temp.add(p);
  77.         temp.add(prostokat);
  78.         temp.add(e);
  79.         temp.add(elipsa);
  80.  
  81.         return temp;
  82.     }
  83.  
  84.     private JPanel kolory() {
  85.         JPanel temp = new JPanel(new FlowLayout());
  86.         this.z = new JLabel("Zielony");
  87.         this.zielony = new JRadioButton();
  88.         this.c = new JLabel("Czerwony");
  89.         this.czerwony = new JRadioButton();
  90.         this.n = new JLabel("Niebieski");
  91.         this.niebieski = new JRadioButton();
  92.         this.kolory = new ButtonGroup();
  93.  
  94.         this.kolory.add(zielony);
  95.         this.kolory.add(czerwony);
  96.         this.kolory.add(niebieski);
  97.  
  98.         temp.add(z);
  99.         temp.add(zielony);
  100.         temp.add(c);
  101.         temp.add(czerwony);
  102.         temp.add(n);
  103.         temp.add(niebieski);
  104.  
  105.         return temp;
  106.     }
  107.  
  108.     private void pobierzWsp(MouseEvent e) {
  109.         this.x = e.getX();
  110.         this.y = e.getY();
  111.     }
  112.  
  113.     private void rysuj(MouseEvent e) {
  114.         int xKonc = e.getX();
  115.         int yKonc = e.getY();
  116.  
  117.         String typ = null;
  118.         if (linia.isSelected())
  119.             typ = "1";
  120.         else if (prostokat.isSelected())
  121.             typ = "2";
  122.         else if (elipsa.isSelected())
  123.             typ = "3";
  124.  
  125.         Color color = null;
  126.         if (zielony.isSelected())
  127.             color = Color.green;
  128.         else if (niebieski.isSelected())
  129.             color = Color.blue;
  130.         else if (czerwony.isSelected())
  131.             color = Color.red;
  132.  
  133.         if (panel != null)
  134.             panel.setOpaque(true);
  135.        
  136.         panel = new Rysowanie(color, typ, x, y, xKonc, yKonc);
  137.  
  138.         panel.setBounds(x, y, xKonc - x, yKonc - y);
  139.         temp2.add(panel);
  140.  
  141.         temp2.revalidate();
  142.         temp2.repaint();
  143.     }
  144. }
  145.  
  146. class Rysowanie extends JPanel {
  147.  
  148.     private Color color;
  149.     private String type;
  150.     private int x, y, xKonc, yKonc;
  151.  
  152.     public Rysowanie(Color color, String type, int x, int y, int xKonc, int yKonc) {
  153.         this.color = color;
  154.         this.setLayout(null);
  155.         this.type = type;
  156.         this.x = x;
  157.         this.y = y;
  158.         this.xKonc = xKonc;
  159.         this.yKonc = yKonc;
  160.     }
  161.  
  162.     @Override
  163.     public Dimension getPreferredSize() {
  164.         return new Dimension(500, 500);
  165.     }
  166.  
  167.     @Override
  168.     public void paintComponent(Graphics g) {
  169.         super.paintComponent(g);
  170.         Graphics2D g2d = (Graphics2D) g.create();
  171.         g2d.setColor(this.color);
  172.         switch (type) {
  173.             case "1":
  174.                 g2d.drawLine(0, 0, xKonc - x, yKonc - y);
  175.                 break;
  176.             case "2":
  177.                 g2d.drawRect(0, 0, xKonc - x - 3, yKonc - y - 3);
  178.                 break;
  179.             case "3":
  180.                 g2d.drawOval(0, 0, xKonc - x - 3, yKonc - y - 3);
  181.                 break;
  182.         }
  183.         g2d.dispose();
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement