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
| import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JApplet;
public class Test extends JApplet implements MouseListener{
int xDebut=0;
int yDebut=0;
int xFin=0;
int yFin=0;
BufferedImage monImage;
public void init() {
this.getContentPane().setSize(800, 600);
this.getContentPane().setBackground(Color.green);
this.getContentPane().addMouseListener(this);
Cursor monCurseur = new Cursor(1);
this.getContentPane().setCursor(monCurseur);
try {
monImage = ImageIO.read(new File("C:/essai.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
if (xDebut ==0 && yDebut == 0 && xFin == 0 && yFin ==0){
g.drawImage(monImage, 0, 0, monImage.getWidth(), monImage.getHeight(), this); // on dessine l'image
}
else{
BufferedImage imageB;
try {
imageB = ImageIO.read(new File("C:/essai.jpg")).getSubimage(xDebut, yDebut, (xFin-xDebut), (yFin-yDebut));
g.clearRect(0, 0, 800, 600);
g.drawImage(imageB, 0, 0, imageB.getWidth(), imageB.getHeight(), this); // on dessine l'image
} catch (IOException e) {
// TODO Bloc catch auto-généré
e.printStackTrace();
}
}
}
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
System.out.println("Pressé");
//On récupèrer X et Y de la souris
PointerInfo pointer = MouseInfo.getPointerInfo();
Point location = pointer.getLocation();
xDebut = (int)location.getX();
yDebut = (int) location.getY();
}
public void mouseReleased(MouseEvent arg0) {
System.out.println("Relaché");
//On récupèrer X et Y de la souris
PointerInfo pointer = MouseInfo.getPointerInfo();
Point location = pointer.getLocation();
xFin = (int)location.getX();
yFin = (int) location.getY();
repaint();
System.out.println("Xdebut "+xDebut+", YDebut:"+yDebut+",W, H:"+ (xFin-xDebut)+","+(yFin-yDebut));
}
} |
Partager