Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import jdk.jfr.Percentage;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- public class Main {
- public static void main(String[] args) {
- SwingUtilities.invokeLater(() -> {
- Okno okno = new Okno();
- });
- }
- }
- class Okno extends JFrame {
- private JRadioButton linia, prostokat, elipsa, zielony, czerwony, niebieski;
- private ButtonGroup ksztalty, kolory;
- private JLabel l, p, e, z, c, n;
- private int x, y;
- private JPanel panel, temp, temp2;
- public Okno() {
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- this.setVisible(true);
- this.setSize(500, 500);
- this.setLayout(null);
- init();
- }
- private void init() {
- JPanel ks = ksztalty();
- ks.setBounds(0, 0, 400, 44);
- this.add(ks);
- JPanel ko = kolory();
- ko.setBounds(0, 50, 400, 44);
- this.add(ko);
- temp2 = new JPanel(null);
- temp2.setBounds(0, 100, 400, 450);
- this.add(temp2);
- temp2.addMouseListener(new MouseAdapter() {
- @Override
- public void mousePressed(MouseEvent e) {
- pobierzWsp(e);
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- rysuj(e);
- }
- });
- }
- private JPanel ksztalty() {
- JPanel temp = new JPanel(new FlowLayout());
- this.l = new JLabel("Linia");
- this.linia = new JRadioButton();
- this.p = new JLabel("Prostokat");
- this.prostokat = new JRadioButton();
- this.e = new JLabel("Elipsa");
- this.elipsa = new JRadioButton();
- this.ksztalty = new ButtonGroup();
- this.ksztalty.add(linia);
- this.ksztalty.add(prostokat);
- this.ksztalty.add(elipsa);
- temp.add(l);
- temp.add(linia);
- temp.add(p);
- temp.add(prostokat);
- temp.add(e);
- temp.add(elipsa);
- return temp;
- }
- private JPanel kolory() {
- JPanel temp = new JPanel(new FlowLayout());
- this.z = new JLabel("Zielony");
- this.zielony = new JRadioButton();
- this.c = new JLabel("Czerwony");
- this.czerwony = new JRadioButton();
- this.n = new JLabel("Niebieski");
- this.niebieski = new JRadioButton();
- this.kolory = new ButtonGroup();
- this.kolory.add(zielony);
- this.kolory.add(czerwony);
- this.kolory.add(niebieski);
- temp.add(z);
- temp.add(zielony);
- temp.add(c);
- temp.add(czerwony);
- temp.add(n);
- temp.add(niebieski);
- return temp;
- }
- private void pobierzWsp(MouseEvent e) {
- this.x = e.getX();
- this.y = e.getY();
- }
- private void rysuj(MouseEvent e) {
- int xKonc = e.getX();
- int yKonc = e.getY();
- String typ = null;
- if (linia.isSelected())
- typ = "1";
- else if (prostokat.isSelected())
- typ = "2";
- else if (elipsa.isSelected())
- typ = "3";
- Color color = null;
- if (zielony.isSelected())
- color = Color.green;
- else if (niebieski.isSelected())
- color = Color.blue;
- else if (czerwony.isSelected())
- color = Color.red;
- if (panel != null)
- panel.setOpaque(true);
- panel = new Rysowanie(color, typ, x, y, xKonc, yKonc);
- panel.setBounds(x, y, xKonc - x, yKonc - y);
- temp2.add(panel);
- temp2.revalidate();
- temp2.repaint();
- }
- }
- class Rysowanie extends JPanel {
- private Color color;
- private String type;
- private int x, y, xKonc, yKonc;
- public Rysowanie(Color color, String type, int x, int y, int xKonc, int yKonc) {
- this.color = color;
- this.setLayout(null);
- this.type = type;
- this.x = x;
- this.y = y;
- this.xKonc = xKonc;
- this.yKonc = yKonc;
- }
- @Override
- public Dimension getPreferredSize() {
- return new Dimension(500, 500);
- }
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2d = (Graphics2D) g.create();
- g2d.setColor(this.color);
- switch (type) {
- case "1":
- g2d.drawLine(0, 0, xKonc - x, yKonc - y);
- break;
- case "2":
- g2d.drawRect(0, 0, xKonc - x - 3, yKonc - y - 3);
- break;
- case "3":
- g2d.drawOval(0, 0, xKonc - x - 3, yKonc - y - 3);
- break;
- }
- g2d.dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement