import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import java.util.List; public class Rectangle extends JComponent implements MouseMotionListener, MouseListener, KeyListener{ private List xOrigine; private List yOrigine; private List yFinal; private List xFinal; private List longueurFenetre; private List hauteurFenetre; boolean isCtrlPressed; public Rectangle(){ this.xOrigine = new ArrayList(); this.yOrigine = new ArrayList(); this.xFinal = new ArrayList(); this.yFinal = new ArrayList(); this.longueurFenetre = new ArrayList(); this.hauteurFenetre = new ArrayList(); this.isCtrlPressed = false; } @Override protected void paintComponent(Graphics pinceau) { Graphics secondPinceau = pinceau.create(); if (this.isOpaque()){ secondPinceau.setColor(new Color(0,0,0)); secondPinceau.fillRect(0, 0, this.getWidth(), this.getHeight()); } secondPinceau.setColor(new Color(0,255,100)); for (int i=0; i= 0 && this.yOrigine(i) >= 0){ int newXOrigine; int newYOrigine; int newLongueur; int newHauteur; if (this.xFinal(i) >= this.xOrigine(i)){ newXOrigine = this.getWidth()*this.xOrigine(i)/this.longueurFenetre(i); newLongueur = this.getWidth()*(this.xFinal(i)-this.xOrigine(i))/this.longueurFenetre(i); } else{ newXOrigine = this.getWidth()*this.xFinal(i)/this.longueurFenetre(i); newLongueur = this.getWidth()*(this.xOrigine(i)-this.xFinal(i))/this.longueurFenetre(i); } if (this.yFinal(i) >= this.yOrigine(i)){ newYOrigine = this.getHeight()*this.yOrigine(i)/this.hauteurFenetre(i); newHauteur = this.getHeight()*(this.yFinal(i)-this.yOrigine(i))/this.hauteurFenetre(i); } else{ newYOrigine = this.getHeight()*this.yFinal(i)/this.hauteurFenetre(i); newHauteur = this.getHeight()*(this.yOrigine(i)-this.yFinal(i))/this.hauteurFenetre(i); } secondPinceau.fillRect(newXOrigine,newYOrigine,newLongueur,newHauteur); } } } @Override public void mouseDragged(MouseEvent e){ this.xFinal(xFinal.Count) = e.getX(); this.yFinal(yFinal.Count) = e.getY(); repaint(); } @Override public void mousePressed(MouseEvent e){ if (this.isCtrlPressed == false){ this.longueurFenetre.clear(); this.hauteurFenetre.clear(); this.xOrigine.clear(); this.yOrigine.clear(); this.xFinal.clear(); this.yFinal.clear(); } this.longueurFenetre.add(this.getWidth()); this.hauteurFenetre.add(this.getHeight()); this.xOrigine.add(e.getX()); this.yOrigine.add(e.getY()); this.xFinal.add(e.getX()); this.yFinal.add(e.getY()); repaint(); } @Override public void mouseMoved(MouseEvent e){ } @Override public void mouseClicked(MouseEvent e){ } @Override public void mouseEntered(MouseEvent e){ } @Override public void mouseExited(MouseEvent e){ } @Override public void mouseReleased(MouseEvent e){ } @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { int touche = e.getKeyCode(); if (touche == 17){ this.isCtrlPressed = true; } } @Override public void keyReleased(KeyEvent e) { int touche = e.getKeyCode(); if (touche == 17){ this.isCtrlPressed = false; } } }