Zadanie – refaktoryzacja kodu

Kody źródłowe z odcinka

package pl.am.swing.animtions4;

import javax.swing.*;
import java.awt.*;

public class MyFrame extends JFrame {

    public MyFrame() {
        setTitle("Zdarzenia myszy");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1000, 800);

        add(new MyComponent());
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            MyFrame myFrame = new MyFrame();
            myFrame.setVisible(true);
        });

    }
}
package pl.am.swing.animtions4;

import javax.swing.*;
import java.awt.*;

public class MyComponent extends JComponent {

    MyRectangle rectangle1 = new MyRectangle();
    MyRectangle rectangle2 = new MyRectangle(0, 200, 20, 20, 15, 2);
    MyRectangle rectangle3 = new MyRectangle(200, 200, 100, 20, 2, 15);

    public MyComponent() {
        Timer timer = new Timer(40, e -> {
            rectangle1.updatePosition();
            rectangle2.updatePosition();
            rectangle3.updatePosition();

            repaint();
        });

        timer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        Toolkit.getDefaultToolkit().sync();
        Graphics2D graphics2D = (Graphics2D) g;



        Rectangle limit = new Rectangle(0, 0, Utils.maxWidth, Utils.maxHeight);
        graphics2D.setPaint(Color.LIGHT_GRAY);
        graphics2D.fill(limit);


        graphics2D.setPaint(Color.BLUE);
        graphics2D.fill(rectangle1.getShape());
        graphics2D.fill(rectangle2.getShape());
        graphics2D.fill(rectangle3.getShape());


    }
}
package pl.am.swing.animtions4;

import java.awt.*;

public class MyRectangle {
    private int rectangleX = 500;
    private int rectangleY = 0;
    private int rectangleWidth = 50;
    private int rectangleHeight = 50;
    private int speedX = 5;
    private int speedY = 10;

    public MyRectangle() {
    }

    public MyRectangle(int rectangleX, int rectangleY, int rectangleWidth, int rectangleHeight, int speedX, int speedY) {
        this.rectangleX = rectangleX;
        this.rectangleY = rectangleY;
        this.rectangleWidth = rectangleWidth;
        this.rectangleHeight = rectangleHeight;
        this.speedX = speedX;
        this.speedY = speedY;
    }

    public void updatePosition() {
        rectangleX += speedX;
        rectangleY += speedY;

        if (rectangleX + rectangleWidth > Utils.maxWidth) {
            rectangleX = Utils.maxWidth - rectangleWidth;
            speedX = -speedX;
        }

        if (rectangleY + rectangleHeight > Utils.maxHeight) {
            rectangleY = Utils.maxHeight - rectangleHeight;
            speedY = -speedY;
        }

        if (rectangleX < 0) {
            rectangleX = 0;
            speedX = -speedX;
        }

        if (rectangleY < 0) {
            rectangleY = 0;
            speedY = -speedY;
        }
    }

    public Shape getShape() {
        Point point = new Point(rectangleX, rectangleY);
        Dimension dimension = new Dimension(rectangleWidth, rectangleHeight);

        Rectangle rectangle = new Rectangle(point, dimension);

        return rectangle;
    }
}
package pl.am.swing.animtions4;

public class Utils {
    public static int maxWidth = 800;
    public static int maxHeight = 600;
}

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *