Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Classes;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.util.ArrayList;
- import java.util.List;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.Timer;
- public class Test {
- public static Rectangle A = new Rectangle(200, 300, 10, 10), B = new Rectangle(800, 300, 10, 10),
- C = new Rectangle(800, 600, 10, 10), D = new Rectangle(200, 600, 10, 10);
- public static Rectangle M;
- public static void main(String[] args) {
- frame();
- }
- private static JPanel panel;
- private static void frame() {
- M = new Rectangle(500, 450, 10, 10);
- JFrame frame = new JFrame();
- panel = new JPanel() {
- private static final long serialVersionUID = 1L;
- @Override
- public void paintComponent(Graphics g) {
- Test.paint(g);
- }
- };
- Timer t = new Timer(50, al);
- t.start();
- frame.add(panel);
- frame.setVisible(true);
- frame.setSize(1000, 1000);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- private static double x1, x2, x3, x4, y1, y2, y3, y4;
- protected static void paint(Graphics g) {
- g.setColor(Color.WHITE);
- g.fillRect(0, 0, 1000, 1000);
- g.setColor(Color.BLACK);
- g.fillRect((int) (A.getX() + x1), (int) (A.getY() + y1), (int) A.getWidth(), (int) A.getHeight());
- g.fillRect((int) (B.getX() + x2), (int) (B.getY() + y2), (int) B.getWidth(), (int) B.getHeight());
- g.fillRect((int) (C.getX() + x3), (int) (C.getY() + y3), (int) C.getWidth(), (int) C.getHeight());
- g.fillRect((int) (D.getX() + x4), (int) (D.getY() + y4), (int) D.getWidth(), (int) D.getHeight());
- g.setColor(Color.RED);
- g.fillRect((int) M.getX(), (int) M.getY(), (int) M.getWidth(), (int) M.getHeight());
- }
- public static ActionListener al = new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent arg0) {
- double a = toRadians(3);
- List<Rectangle> recs = new ArrayList<>();
- recs.add(A);
- // recs.add(B);
- // recs.add(C);
- // recs.add(D);
- for (Rectangle R : recs) {
- double x = (M.getX() + Math.cos(a) * (R.getX() - M.getX()) - Math.sin(a) * (R.getY() - M.getY()));
- double y = (M.getY() + Math.sin(a) * (R.getX() - M.getX()) + Math.cos(a) * (R.getY() - M.getY()));
- R.setLocation((int) x,(int) y);
- }
- panel.repaint();
- }
- };
- public static double toRadians(double deg) {
- return (deg * Math.PI) / 180;
- }
- public static double toDegrees(double rad) {
- return (rad / Math.PI) * 180;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement