1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
package mesClasses;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class ImageOriginale extends JLabel implements MouseListener, MouseMotionListener{
Image img;
int xRect;
int yRect;
int largRect;
int hautRect;
boolean dessinerRectangleSelection=false;
public ImageOriginale(ImageIcon imgIcon){
this.setIcon(imgIcon);
this.img=imgIcon.getImage();
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void paint(Graphics g){
super.paint(g);
if(this.dessinerRectangleSelection){
int x=this.xRect;
int y=this.yRect;
int l=this.largRect;
int h=this.hautRect;
if(l<0){
x=x+l;
l=-l;
}
if(h<0){
y=y+h;
h=-h;
}
g.setColor(Color.RED);
g.drawRect(x, y, l, h);
}
}
public void mousePressed(MouseEvent me) {
this.xRect=me.getX();
this.yRect=me.getY();
this.largRect=0;
this.hautRect=0;
this.dessinerRectangleSelection=true;
}
public void mouseReleased(MouseEvent me) {
this.dessinerRectangleSelection=false;
if(this.largRect<0){
this.xRect=this.xRect+this.largRect;
this.largRect=-this.largRect;
}
if(this.hautRect<0){
this.yRect=this.yRect+this.hautRect;
this.hautRect=-this.hautRect;
}
int dx1=0;
int dy1=0;
int dx2=this.largRect;
int dy2=this.hautRect;
int sx1=this.xRect;
int sy1=this.yRect;
int sx2=this.xRect+this.largRect;
int sy2=this.yRect+this.hautRect;
BufferedImage bImg=new BufferedImage(this.largRect, this.hautRect, BufferedImage.TYPE_INT_RGB);
Graphics g=bImg.getGraphics();
g.drawImage(this.img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
g.dispose();
ImageIcon imgIcon=new ImageIcon(bImg);
JLabel lblImageSelectionnee=new JLabel(imgIcon);
new Fenetre(lblImageSelectionnee, false);
this.repaint();
}
public void mouseDragged(MouseEvent me) {
int x=me.getX();
int y=me.getY();
this.largRect=x-this.xRect;
this.hautRect=y-this.yRect;
this.repaint();
}
public void mouseMoved(MouseEvent me) {}
public void mouseClicked(MouseEvent me) {}
public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}
} |
Partager