**************CLASSE PRINCIPALE*************************
import javax.swing.*;
import java.awt.*;
public class disposition extends JPanel
{
public disposition()
{
setLayout(new GridLayout(1, 1));
ImageDepl im1 = new ImageDepl("c:\\00000021.gif");
ImageDepl im2 = new ImageDepl("c:\\00000022.gif");
this.add(im1);
this.add(im2);
setPreferredSize(new Dimension(450, 630));
}
public static void main(String[] args)
{
JFrame frame = new JFrame("bitre");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
disposition demo = new disposition();
demo.setOpaque(true); //content panes must be opaque
frame.setContentPane(demo);
frame.pack();
frame.setVisible(true);
}
}
**************CLASSE DE DESSIN*************************
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JComponent;
public class ImageDepl extends JComponent implements MouseMotionListener, MouseListener{
public Image image;
public Rectangle selection;
public int H;
public int L;
public ImageDepl(String str){
addMouseListener(this);
addMouseMotionListener(this);
this.image = Toolkit.getDefaultToolkit().getImage(str);
setSelectionBounds(new Rectangle(0, 0, 300, 400));
}
int donneLargeur(){
return L;
}
Image donneImage(){
return image;
}
int donneHauteur(){
return H;
}
public void paintComponent(Graphics graphics) {
Graphics g = graphics.create();
g.drawImage(this.image, this.selection.x, this.selection.y, this.selection.width, this.selection.height, this);
H=image.getHeight(this);
L=image.getWidth(this);
g.dispose();
}
public void setSelectionBounds(Rectangle rect)
{
this.selection=rect;
this.repaint();
}
protected int frameWidth=5;
protected int minSize=5;
protected int startDragX, startDragY;
protected Rectangle startDragRect;
protected boolean resizeLeft, resizeTop, resizeRight, resizeBottom, move;
public void mousePressed(MouseEvent e)
{
System.out.println(selection);
if (selection!=null)
{
int x=e.getX();
int y=e.getY();
int selX=selection.x;
int selY=selection.y;
int selW=selection.width;
int selH=selection.height;
Rectangle inside=new Rectangle(selX, selY, selW, selH);
Rectangle leftFrame=new Rectangle(selX, selY, frameWidth, selH);
Rectangle topFrame=new Rectangle(selX, selY, selW, frameWidth);
Rectangle rightFrame=new Rectangle(selX+selW-frameWidth, selY, frameWidth, selH);
Rectangle bottomFrame=new Rectangle(selX, selY+selH-frameWidth, selW, frameWidth);
boolean isInside=inside.contains(x, y);
boolean isLeft=leftFrame.contains(x, y);
boolean isTop=topFrame.contains(x, y);
boolean isRight=rightFrame.contains(x, y);
boolean isBottom=bottomFrame.contains(x, y);
if (isLeft && isTop)
{
resizeLeft=true;
resizeTop=true;
resizeRight=false;
resizeBottom=false;
move=false;
}
else if (isTop && isRight)
{
resizeLeft=false;
resizeTop=true;
resizeRight=true;
resizeBottom=false;
move=false;
}
else if (isRight && isBottom)
{
resizeLeft=false;
resizeTop=false;
resizeRight=true;
resizeBottom=true;
move=false;
}
else if (isBottom && isLeft)
{
resizeLeft=true;
resizeTop=false;
resizeRight=false;
resizeBottom=true;
move=false;
}
else if (isLeft)
{
resizeLeft=true;
resizeTop=false;
resizeRight=false;
resizeBottom=false;
move=false;
}
else if (isTop)
{
resizeLeft=false;
resizeTop=true;
resizeRight=false;
resizeBottom=false;
move=false;
}
else if (isRight)
{
resizeLeft=false;
resizeTop=false;
resizeRight=true;
resizeBottom=false;
move=false;
}
else if (isBottom)
{
resizeLeft=false;
resizeTop=false;
resizeRight=false;
resizeBottom=true;
move=false;
}
else if (isInside)
{
resizeLeft=false;
resizeTop=false;
resizeRight=false;
resizeBottom=false;
move=true;
}
else
{
resizeLeft=false;
resizeTop=false;
resizeRight=false;
resizeBottom=false;
move=false;
}
this.startDragX=x;
this.startDragY=y;
this.startDragRect=(Rectangle) selection.clone();
}
}
/* (non-Javadoc)
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
*/
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
*/
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
*/
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
*/
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
*/
public void mouseDragged(MouseEvent e)
{
System.out.println("deds");
if (startDragRect!=null)
{
int x=e.getX();
int y=e.getY();
int diffX=startDragX-x;
int diffY=startDragY-y;
int newX=startDragRect.x;
int newY=startDragRect.y;
int newW=startDragRect.width;
int newH=startDragRect.height;
if (resizeLeft)
{
newX=newX-diffX;
newW=newW+diffX;
}
if (resizeTop)
{
newY=newY-diffY;
newH=newH+diffY;
}
if (resizeRight)
{
newW=newW-diffX;
}
if (resizeBottom)
{
newH=newH-diffY;
}
if (move)
{
newX=newX-diffX;
newY=newY-diffY;
}
if (newW>minSize && newH>minSize)
{
setSelectionBounds(new Rectangle(newX, newY, newW, newH));
}
}
}
/* (non-Javadoc)
* @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
*/
public void mouseMoved(MouseEvent e)
{
System.out.println("la");
if (selection!=null)
{
int x=e.getX();
int y=e.getY();
int selX=selection.x;
int selY=selection.y;
int selW=selection.width;
int selH=selection.height;
Rectangle inside=new Rectangle(selX, selY, selW, selH);
Rectangle leftFrame=new Rectangle(selX, selY, frameWidth, selH);
Rectangle topFrame=new Rectangle(selX, selY, selW, frameWidth);
Rectangle rightFrame=new Rectangle(selX+selW-frameWidth, selY, frameWidth, selH);
Rectangle bottomFrame=new Rectangle(selX, selY+selH-frameWidth, selW, frameWidth);
boolean isInside=inside.contains(x, y);
boolean isLeft=leftFrame.contains(x, y);
boolean isTop=topFrame.contains(x, y);
boolean isRight=rightFrame.contains(x, y);
boolean isBottom=bottomFrame.contains(x, y);
if (isLeft && isTop)
{
setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
}
else if (isTop && isRight)
{
setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
}
else if (isRight && isBottom)
{
setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
}
else if (isBottom && isLeft)
{
setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
}
else if (isLeft)
{
setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
}
else if (isTop)
{
setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
}
else if (isRight)
{
setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
}
else if (isBottom)
{
setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
}
else if (isInside)
{
setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
}
else
{
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
else
{
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
}
Partager