Advertisement
Guest User

Kvadratiki

a guest
Nov 20th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. package kvadratiki;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.*;
  5. import java.awt.geom.*;
  6. import javax.swing.*;
  7.  
  8. public class Kvadratiki {
  9.     public static void main(String[] args)
  10.     {
  11.         MouseFrame frame = new MouseFrame();
  12.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13.         frame.setVisible(true);
  14.     }
  15. }
  16.     class MouseFrame extends JFrame
  17.     {
  18.         public MouseFrame(){
  19.            
  20.         }
  21.         {
  22.             setTitle("Квадратики");
  23.             setSize(500, 500);
  24.             MousePanel panel = new MousePanel();
  25.             add(panel);
  26.         }
  27.         //
  28.         //
  29.     }
  30.     class MousePanel extends JPanel
  31.     {
  32.         public MousePanel()
  33.         {
  34.             squares = new ArrayList <Rectangle2D>();
  35.             current = null;
  36.            
  37.             addMouseListener(new MouseHandler());
  38.             addMouseMotionListener(new MouseMotionHandler());
  39.         }
  40.         public void paintComponent(Graphics g)
  41.         {
  42.             super.paintComponent(g);
  43.             Graphics2D g2 = (Graphics2D) g;
  44.             for (Rectangle2D r : squares)
  45.             {
  46.                 g2.draw(r);
  47.                 }
  48.             }
  49.        
  50.        
  51.             public Rectangle2D find(Point2D p)
  52.             {
  53.                 for (Rectangle2D r : squares)
  54.                 {
  55.                     if (r.contains(p)) return r;
  56.                 }
  57.                 return null;
  58.             }
  59.            
  60.            
  61.             public void add(Point2D p)
  62.             {
  63.                 double x = p.getX();
  64.                 double y = p.getY();
  65.                
  66.                 current = new Rectangle2D.Double(
  67.                         x - SIDELENGTH / 2,
  68.                         y - SIDELENGTH / 2,
  69.                         SIDELENGTH,
  70.                         SIDELENGTH);
  71.                 squares.add(current);
  72.                 repaint();
  73.             }
  74.            
  75.            
  76.             public void remove(Rectangle2D s)
  77.             {
  78.                 if (s==null) return;
  79.                 if (s == current) current = null;
  80.                 repaint();             
  81.             }
  82.            
  83.             private static final int SIDELENGTH = 10;
  84.             private ArrayList<Rectangle2D> squares;
  85.             private Rectangle2D current;
  86.             private class MouseHandler extends MouseAdapter
  87.             {
  88.                 public void mousePressed(MouseEvent event) {
  89.                     current = find(event.getPoint());
  90.                             if (current == null)
  91.                                 add(event.getPoint());
  92.                             }
  93.                 public void mouseClicked(MouseEvent event)
  94.                 {
  95.                     current = find(event.getPoint());
  96.                     if (current != null && event.getClickCount() >= 2)
  97.                         remove (current);
  98.                 }
  99.                
  100.             }
  101.            
  102.             private class MouseMotionHandler
  103.                 implements MouseMotionListener
  104.                 {
  105.                 public void mouseMoved(MouseEvent event)
  106.                 {
  107.                     if (find(event.getPoint()) == null) {
  108.                         setCursor(Cursor.getDefaultCursor());
  109.                     for (Rectangle2D s: squares) {
  110.                         int x = (int) s.getCenterX(), y = (int) s.getCenterY();
  111.                         s.setFrame(x - SIDELENGTH / 2, y - SIDELENGTH / 2, SIDELENGTH ,SIDELENGTH);
  112.                     }
  113.                     repaint();
  114.                     }
  115.                     else {
  116.                         setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  117.                         int x = (int) find(event.getPoint()).getCenterX(), y = (int) find(event.getPoint()).getCenterY();
  118.                         find(event.getPoint()).setFrame (x - SIDELENGTH, y - SIDELENGTH, SIDELENGTH*2, SIDELENGTH*2 );
  119.                         repaint();
  120.                     }
  121.                 }
  122.                
  123.                 public void mouseDragged(MouseEvent event)
  124.                 {
  125.                     if (current == null)
  126.                         return;
  127.                    
  128.                         int x = event.getX();
  129.                         int y = event.getY();
  130.                        
  131.                         current.setFrame(
  132.                                 x - SIDELENGTH / 2,
  133.                                 y - SIDELENGTH / 2,
  134.                                 SIDELENGTH,
  135.                                 SIDELENGTH);
  136.                         repaint();
  137.                    
  138.                 }
  139.         }
  140.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement