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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
| ************************ElementG.java********************
******************************************************
import java.awt.*;
import java.awt.event.*;
public class ElementG extends Canvas
{
protected Image image;
protected Point position,posZ1,posZ2,posclick;
protected Dimension dim,dimZ1,dimZ2;
public boolean inmotion=false;
public ElementG(Point position, Dimension dim)
{
this.position = position;
this.dim = dim;
setPropZone();
java.awt.Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
//Attention les icones importées doivent être de dimension 120*120
image = toolkit.getImage("icone1.jpg");
}
protected void setPropZone()
{
posZ1 = new Point(0,40);
dimZ1 = new Dimension(40,40);
posZ2 = new Point(80,40);
dimZ2 = new Dimension(40,40);
}
public void setPosClick(Point posclick)
{
this.posclick=posclick;
}
public Point getPosClick()
{
return this.posclick;
}
public Point getPos()
{
return position;
}
public Dimension getDim()
{
return dim;
}
public boolean isInto(Point p)
{
if((position.x < p.x) && (position.x+dim.width > p.x) && (position.y < p.y) && (p.y < position.y+dim.height))
return true;
else
return false;
}
public boolean isIntoZ1(Point p)
{
if((position.x+posZ1.x < p.x) && (position.x+posZ1.x+dimZ1.width > p.x) && (position.y+posZ1.y < p.y) && (p.y < position.y+posZ1.y+dimZ1.height))
return true;
else
return false;
}
public boolean isIntoZ2(Point p)
{
if((position.x+posZ2.x < p.x && (position.x+posZ2.x+dimZ2.width > p.x) && (position.y+posZ2.y < p.y) && (p.y < position.y+posZ2.y+dimZ2.height)))
return true;
else
return false;
}
public void erase(Graphics g)
{
g.clearRect(position.x, position.y, dim.width, dim.height);
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(image,position.x,position.y,null);
}
//Cette fonction trouve elle-même la nouvelle coordonnée en prenant compte du point de click
public void move(Point move,Graphics g)
{
position.x = move.x-this.getPosClick().x;
position.y = move.y-this.getPosClick().y;
System.out.println("move "+position);
paint(g);
}
}
****************************************************************************************
****************************************************************************************
*****************Interface.java*************************************************************
import java.awt.*;
import java.math.*;
import java.awt.event.*;
import java.util.*;
public class Interface extends Frame implements MouseMotionListener, MouseListener
{
Vector<ElementG> Elements;
//ElementG Elem;
Image image;
java.awt.Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
public Interface()
{
image = toolkit.getImage("icone1.jpg");
Elements = new Vector<ElementG>();
for(int i =0;i<10;i++)
{
Elements.add(new ElementG( new Point((int)(Math.random()*800),(int)(Math.random()*600)), new Dimension(120,120)) );
this.add(Elements.get(i));
Elements.get(i).addMouseListener(this);
Elements.get(i).addMouseMotionListener(this);
}
//Elem = new ElementG(new Point(100,100),new Dimension(120,120));
//propriété de Interface
this.setSize(800,600);
this.setLocation(300, 50);
//this.add(Elem);
this.setVisible(true);
//ajout des écoutes
//Elem.addMouseListener(this);
//Elem.addMouseMotionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
show();
}
public void actionPerformed(ActionEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
//if(Elem.isInto(e.getPoint()) && (!Elem.isIntoZ1(e.getPoint())) && (!Elem.isIntoZ2(e.getPoint())))
//{
// Elem.setPosClick(new Point((int)(e.getX()-Elem.getPos().getX()),(int)(e.getY()-Elem.getPos().getY())));
// Elem.inmotion = true;
//}
for(int i=0;i<10;i++)
{
if(Elements.get(i).isInto(e.getPoint()) && (!Elements.get(i).isIntoZ1(e.getPoint())) && (!Elements.get(i).isIntoZ2(e.getPoint())))
{
Elements.get(i).setPosClick(new Point(e.getPoint().x - Elements.get(i).getPos().x, e.getPoint().y - Elements.get(i).getPos().y));
Elements.get(i).inmotion = true;
System.out.println("Element "+i);
}
}
}
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}
public void mouseReleased(MouseEvent e)
{
//if(Elem.inmotion)
//{
// Elem.erase(Elem.getGraphics());
// Elem.move(new Point((int)(e.getPoint().getX()),(int)(e.getPoint().getY())),Elem.getGraphics());
// Elem.inmotion = false;
//}
for(int i=0;i<10;i++)
{
if(Elements.get(i).inmotion)
{
Elements.get(i).erase(this.getGraphics());
Elements.get(i).move(new Point(e.getPoint().x,e.getPoint().y),this.getGraphics());
Elements.get(i).inmotion = false;
}
}
}
/*
public void mouseMoved(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
if(Elem.inmotion)
{
Elem.erase(this.getGraphics());
Elem.move(new Point((int)(e.getPoint().getX()),(int)(e.getPoint().getY())),Elem.getGraphics());
}
}
*/
public static void main(String[] args)
{
Interface A = new Interface();
}
}
************************************************************************************** |
Partager