Advertisement
Guest User

Untitled

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