TP Exceptions

This commit is contained in:
Simoes Lukas
2025-03-12 17:10:44 +01:00
parent 2a0aa37baa
commit cf33623a5d
45 changed files with 448 additions and 23 deletions

View File

@@ -2,33 +2,57 @@ import java.awt.*;
import javax.swing.*;
public class Fenetre extends JFrame {
private JLabel2 actif;
private boolean estActif;
public Fenetre() {
this.estActif = false;
this.setLocation(100, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(14, 1));
JLabel[] chromakopia = {
new JLabel("St. Chroma"),
new JLabel("Rah Tah Tah"),
new JLabel("Noid"),
new JLabel("Darling, I"),
new JLabel("Hey Jane"),
new JLabel("I Killed You"),
new JLabel("Judge Judy"),
new JLabel("Sticky"),
new JLabel("Take Your Mask Off"),
new JLabel("Tomorrow"),
new JLabel("Thought I Was Dead"),
new JLabel("Like Him"),
new JLabel("Balloon"),
new JLabel("I Hope You Find Your Way Home")
JLabel2[] chromakopia = {
new JLabel2("St. Chroma"),
new JLabel2("Rah Tah Tah"),
new JLabel2("Noid"),
new JLabel2("Darling, I"),
new JLabel2("Hey Jane"),
new JLabel2("I Killed You"),
new JLabel2("Judge Judy"),
new JLabel2("Sticky"),
new JLabel2("Take Your Mask Off"),
new JLabel2("Tomorrow"),
new JLabel2("Thought I Was Dead"),
new JLabel2("Like Him"),
new JLabel2("Balloon"),
new JLabel2("I Hope You Find Your Way Home")
};
for (JLabel titre : chromakopia) {
this.addMouseListener(new GestionSouris(titre));
for (JLabel2 titre : chromakopia) {
GestionSouris gestion = new GestionSouris(titre, this);
titre.addMouseListener(gestion);
titre.setGestionnaireSouris(gestion);
this.add(titre);
}
this.pack();
}
public boolean getEstActif() {
return this.estActif;
}
public void setEstActif(boolean n) {
this.estActif = n;
}
public void setActif(JLabel2 n) {
this.actif = n;
}
public JLabel2 getActif() {
return this.actif;
}
}

View File

@@ -4,10 +4,14 @@ import java.awt.*;
public class GestionSouris implements MouseListener {
private JLabel titre;
private JLabel2 titre;
private Fenetre fenetre;
private JLabel2 actif;
private boolean actuelEstActif;
public GestionSouris(JLabel titre) {
public GestionSouris(JLabel2 titre, Fenetre fenetre) {
this.titre = titre;
this.fenetre = fenetre;
}
public void mouseClicked(MouseEvent evenement) {
@@ -20,14 +24,35 @@ public class GestionSouris implements MouseListener {
}
public void mouseExited(MouseEvent evenement){
this.titre.setOpaque(true);
this.titre.setBackground(Color.WHITE);
this.titre.repaint();
if (!this.actuelEstActif) {
this.titre.setOpaque(true);
this.titre.setBackground(null);
this.titre.repaint();
}
else {
this.titre.setBackground(Color.LIGHT_GRAY);
this.titre.repaint();
}
}
public void mousePressed(MouseEvent evenement){
}
this.titre.setBackground(Color.LIGHT_GRAY);
this.actuelEstActif = true;
this.titre.repaint();
if (this.fenetre.getEstActif()) {
this.fenetre.getActif().setBackground(null);
this.fenetre.getActif().getGestionnaireSouris().setActuelEstActif(false);
this.fenetre.getActif().repaint();
}
this.fenetre.setEstActif(true);
this.fenetre.setActif(this.titre);
}
public void mouseReleased(MouseEvent evenement){
}
public void setActuelEstActif(boolean n) {
this.actuelEstActif = n;
}
}

Binary file not shown.

View File

@@ -0,0 +1,19 @@
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JLabel2 extends JLabel {
private GestionSouris gestionnaireSouris;
public JLabel2(String s) {
super(s);
}
public void setGestionnaireSouris(GestionSouris n) {
this.gestionnaireSouris = n;
}
public GestionSouris getGestionnaireSouris() {
return this.gestionnaireSouris;
}
}

Binary file not shown.

View File

@@ -0,0 +1,13 @@
import java.awt.*;
import javax.swing.*;
public class Fenetre extends JFrame {
public Fenetre() {
this.setSize(800, 500);
this.setLocation(100, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GestionSouris clicSouris = new GestionSouris(this);
this.addMouseListener(clicSouris);
this.addMouseMotionListener(new GestionMouvementSouris(this, clicSouris));
}
}

Binary file not shown.

View File

@@ -0,0 +1,24 @@
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GestionMouvementSouris implements MouseMotionListener {
private Fenetre fenetre;
private GestionSouris clicSouris;
public GestionMouvementSouris(Fenetre fenetre, GestionSouris clicSouris) {
this.fenetre = fenetre;
this.clicSouris = clicSouris;
}
public void mouseDragged(MouseEvent e) {
System.out.println("Appuyé");
this.clicSouris.setRect(e.getX(), e.getY());
this.fenetre.repaint();
}
public void mouseMoved(MouseEvent e) {
System.out.println("Relâché");
}
}

Binary file not shown.

View File

@@ -0,0 +1,57 @@
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class GestionSouris implements MouseListener {
private Fenetre fenetre;
private Rectangle rect;
private int debutX;
private int debutY;
private int finX;
private int finY;
public GestionSouris(Fenetre fenetre) {
this.fenetre = fenetre;
}
public void mouseClicked(MouseEvent evenement) {
}
public void mouseEntered(MouseEvent evenement){
}
public void mouseExited(MouseEvent evenement){
}
public void mousePressed(MouseEvent evenement){
System.out.println("Appui simple");
this.fenetre.add(new Rectangle(evenement.getX(), evenement.getY(), evenement.getX()+200, evenement.getY()+200));
this.debutX = evenement.getX();
this.debutY = evenement.getY();
this.rect = new Rectangle(this.debutX, this.debutY, evenement.getX(), evenement.getY());
//this.fenetre.add(rect);
this.fenetre.repaint();
}
public void mouseReleased(MouseEvent evenement){
this.finX = evenement.getX();
this.finY = evenement.getY();
}
public int getDebutX() {
return this.debutX;
}
public int getDebutY() {
return this.debutY;
}
public void setRect(int finX, int finY) {
this.rect.setBounds(this.debutX, this.debutY, finX, finY);
System.out.println("debut : [" + this.debutX + ", " + this.debutY + "]");
System.out.println("fin : [" + finX + ", " + finY + "]");
this.fenetre.repaint();
}
}

Binary file not shown.

View File

@@ -0,0 +1,6 @@
public class Main {
public static void main(String[] args) {
Fenetre fenetre = new Fenetre();
fenetre.setVisible(true);
}
}

Binary file not shown.

View File

@@ -0,0 +1,29 @@
import java.awt.*;
import javax.swing.*;
public class Rectangle extends JComponent {
private int departX;
private int departY;
private int finX;
private int finY;
public Rectangle(int departX, int departY, int finX, int finY) {
this.departX = departX;
this.departY = departY;
this.finX = finX;
this.finY = finY;
}
@Override
public void paintComponent(Graphics pinceau) {
Graphics secondPinceau = pinceau.create();
if (this.isOpaque()) {
secondPinceau.setColor(this.getBackground());
secondPinceau.fillRect(0, 0, this.getWidth(), this.getHeight());
}
secondPinceau.setColor(Color.BLUE);
secondPinceau.fillRect(this.departX, this.departY, this.finX-this.departX, this.finY-this.departY);
}
}