TP11 & TP12

This commit is contained in:
2022-04-12 17:16:22 +02:00
parent 39ef600e6d
commit 619b3ac7b6
32 changed files with 408 additions and 29 deletions

View File

@@ -0,0 +1,26 @@
public class Observer implements MouseMotionListener, MouseListener {
public Observer() {}
void mouseClicked(MouseEvent evenement) {}
void mouseEntered(MouseEvent evenement) {}
void mouseExited(MouseEvent evenement) {}
void mouseMoved(MouseEvent evenement) {}
void mousePressed(MouseEvent evenement) {
Rectangle rect = (Rectangle)evenement.getSource();
rect.SetX(evenement.getX());
rect.SetY(evenement.getY());
rect.ShouldDraw(true);
}
void mouseReleased(MouseEvent evenement) {
Rectangle rect = (Rectangle)evenement.getSource();
rect.ShouldDraw(false);
}
void mouseDragged(MouseEvent evenement) {
Rectangle rect = (Rectangle)evenement.getSource();
rect.SetSX(evenement.getX());
rect.SetSY(evenement.getY());
}
}

View File

@@ -0,0 +1,62 @@
public class Rectangle extends JComponent {
private int X;
private int Y;
private int SX;
private int SY;
private boolean shouldDraw;
public Rectangle() {
shouldDraw = false;
X = 0;
Y = 0;
SX = 0;
SY = 0;
}
public void SetX(int X) {
this.X = X;
}
public void SetY(int Y) {
this.Y = Y;
}
public void SetSX(int SX) {
this.SX = SX;
}
public void SetSY(int SY) {
this.SY = SY;
}
public void ShouldDraw(boolean shouldDraw) {
this.shouldDraw = shouldDraw;
}
@Override
protected void paintComponent(Graphics brush) {
Graphics newBrush = brush.create();
if (this.isOpaque()) {
newBrush.setColor(this.getBackground());
newBrush.fillRect(0, 0, this.getWidth(), this.getHeight());
}
if (this.shouldDraw) {
newBrush.setColor(new Color(0, 220, 255));
if (X < SX) {
int buff = X;
X = SX;
SX = buff;
}
if (Y < SY) {
int buff = Y;
Y = SY;
SY = buff;
}
}
}
}

View File

@@ -0,0 +1,12 @@
public class TestRectangle {
public static void main(String[] args) {
JFrame f = new JFrame("Fond");
f.setSize(700, 200);
f.setLocation(100, 100);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Rectangle r = new Rectangle();
r.addMouseMotionListener(new Observer());
f.setVisible(true);
}
}