Main2.java 1.62 KB
package tp2.ArdoiseMagique;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

public class Main2 extends JFrame {

    public Main2 () {
        super();
        this.setLocationRelativeTo(null);

        ArdoiseMagique ard = new ArdoiseMagique();
        ard.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                super.mouseDragged(e);
                ArdoiseMagique a = (ArdoiseMagique) e.getSource();
                if (SwingUtilities.isLeftMouseButton(e)) {
                    a.addPoint(
                        e.getX(),
                        e.getY()
                    );
                } else if (SwingUtilities.isRightMouseButton(e)) {
                    a.clear();
                }
            }
        });

        ard.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                ArdoiseMagique src = (ArdoiseMagique) e.getSource();
                if (SwingUtilities.isLeftMouseButton(e)) {
                    src.newCurve();
                } else if (SwingUtilities.isRightMouseButton(e)){
                    src.clear();
                }
            }
        });
        ard.setPreferredSize(new Dimension(1000,600));
        this.add(ard);

        this.setResizable(false);
        this.pack();

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }


    public static void main(String[] args) {
        new Main2();
    }
}