TP11 & TP12
This commit is contained in:
26
APL2.1/TP11/Rectangle/Observer.java
Normal file
26
APL2.1/TP11/Rectangle/Observer.java
Normal 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());
|
||||
}
|
||||
}
|
||||
62
APL2.1/TP11/Rectangle/Rectangle.java
Normal file
62
APL2.1/TP11/Rectangle/Rectangle.java
Normal 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
12
APL2.1/TP11/Rectangle/TestRectangle.java
Normal file
12
APL2.1/TP11/Rectangle/TestRectangle.java
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user