Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. package Classes;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Rectangle;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13. import javax.swing.Timer;
  14.  
  15. public class Test {
  16.  
  17.     public static Rectangle A = new Rectangle(200, 300, 10, 10), B = new Rectangle(800, 300, 10, 10),
  18.             C = new Rectangle(800, 600, 10, 10), D = new Rectangle(200, 600, 10, 10);
  19.  
  20.     public static Rectangle M;
  21.  
  22.     public static void main(String[] args) {
  23.         frame();
  24.     }
  25.  
  26.     private static JPanel panel;
  27.  
  28.     private static void frame() {
  29.        
  30.         M = new Rectangle(500, 450, 10, 10);
  31.  
  32.         JFrame frame = new JFrame();
  33.         panel = new JPanel() {
  34.             private static final long serialVersionUID = 1L;
  35.  
  36.             @Override
  37.             public void paintComponent(Graphics g) {
  38.                 Test.paint(g);
  39.             }
  40.         };
  41.         Timer t = new Timer(50, al);
  42.         t.start();
  43.  
  44.         frame.add(panel);
  45.         frame.setVisible(true);
  46.         frame.setSize(1000, 1000);
  47.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  48.     }
  49.  
  50.     private static double x1, x2, x3, x4, y1, y2, y3, y4;
  51.    
  52.     protected static void paint(Graphics g) {
  53.         g.setColor(Color.WHITE);
  54.         g.fillRect(0, 0, 1000, 1000);
  55.         g.setColor(Color.BLACK);
  56.         g.fillRect((int) (A.getX() + x1), (int) (A.getY() + y1), (int) A.getWidth(), (int) A.getHeight());
  57.         g.fillRect((int) (B.getX() + x2), (int) (B.getY() + y2), (int) B.getWidth(), (int) B.getHeight());
  58.         g.fillRect((int) (C.getX() + x3), (int) (C.getY() + y3), (int) C.getWidth(), (int) C.getHeight());
  59.         g.fillRect((int) (D.getX() + x4), (int) (D.getY() + y4), (int) D.getWidth(), (int) D.getHeight());
  60.         g.setColor(Color.RED);
  61.         g.fillRect((int) M.getX(), (int) M.getY(), (int) M.getWidth(), (int) M.getHeight());
  62.     }
  63.  
  64.     public static ActionListener al = new ActionListener() {
  65.         @Override
  66.         public void actionPerformed(ActionEvent arg0) {
  67.            
  68.             double a = toRadians(3);
  69.            
  70.             List<Rectangle> recs = new ArrayList<>();
  71.             recs.add(A);
  72. //          recs.add(B);
  73. //          recs.add(C);
  74. //          recs.add(D);
  75.            
  76.             for (Rectangle R : recs) {
  77.                 double x = (M.getX() + Math.cos(a) * (R.getX() - M.getX()) - Math.sin(a) * (R.getY() - M.getY()));
  78.                 double y = (M.getY() + Math.sin(a) * (R.getX() - M.getX()) + Math.cos(a) * (R.getY() - M.getY()));
  79.                 R.setLocation((int) x,(int) y);
  80.             }
  81.            
  82.             panel.repaint();
  83.         }
  84.     };
  85.  
  86.     public static double toRadians(double deg) {
  87.         return (deg * Math.PI) / 180;
  88.     }
  89.  
  90.     public static double toDegrees(double rad) {
  91.         return (rad / Math.PI) * 180;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement