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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
| package model;
import java.awt.Image;
import java.util.Observable;
/***
* Cette classe stocke les valeurs x, y et z ainsi que l'Image d'un objet destiné à la 3D.
*/
public class Objet3D extends Observable implements Comparable<Objet3D> {
private int x;
private int y;
private int z;
private Image image;
/**
* taux correspond au ratio entre taille d'affichage et taille de l'image
* En dessous de 1, l'image à l'écran apparait plus petite que l'original
* En dessus de 1, l'image à l'écran apparait plus grosse que l'original
*/
private float taux = 1;
//définit et garde en mémoire le point d'ancrage de l'image
public static final int FIRST_LINE_START = 1;
public static final int FIRST_LINE_CENTER = 2;
public static final int FIRST_LINE_END = 3;
public static final int LINE_START = 4;
public static final int LINE_CENTER = 5;
public static final int LINE_END = 6;
public static final int LAST_LINE_START = 7;
public static final int LAST_LINE_CENTER = 8;
public static final int LAST_LINE_END = 9;
private int pointAncrage = FIRST_LINE_START;
public Objet3D (int x, int y, int z, Image im){
this.x = x;
this.y = y;
this.z = z;
image = im;
}
//setters
public void setX (int x){
this.x = x;
setChanged();
notifyObservers(x);
}
public void setY (int y){
this.y = y;
setChanged();
notifyObservers(y);
}
public void setZ (int z){
this.z = z;
setChanged();
notifyObservers(z);
}
public void setXY (int x, int y){
this.x = x;
this.y = y;
setChanged();
notifyObservers(x);
}
public void setXZ (int x, int z){
this.x = x;
this.z = z;
setChanged();
notifyObservers(x);
}
public void setYZ (int y, int z){
this.z = z;
this.y = y;
setChanged();
notifyObservers(y);
}
public void setXYZ (int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
setChanged();
notifyObservers(x);
}
public void setImage (Image im){
image = im;
notifyObservers(image);
}
public void setPointAncrage (int s){
pointAncrage = s;
notifyObservers(pointAncrage);
}
//getters
public int getLocX (){
switch (pointAncrage) {
case FIRST_LINE_START :
case LAST_LINE_START :
case LINE_START :
return x;
case FIRST_LINE_CENTER :
case LINE_CENTER :
case LAST_LINE_CENTER :
return x - (int)(image.getWidth(null)*taux/2);
case FIRST_LINE_END :
case LINE_END :
case LAST_LINE_END :
return x - (int)(image.getWidth(null)*taux);
default :
return x;
}
}
public int getLocY (){
switch (pointAncrage) {
case FIRST_LINE_CENTER :
case FIRST_LINE_END :
case FIRST_LINE_START :
return y;
case LINE_END :
case LINE_CENTER :
case LINE_START :
return y - (int)(image.getHeight(null)*taux/2);
case LAST_LINE_START :
case LAST_LINE_CENTER :
case LAST_LINE_END :
return y - (int)(image.getHeight(null)*taux);
default :
return y;
}
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public int getZ (){
return z;
}
public Image getImage(){
return image;
}
/*
*modifie la valeur x à partir d'une valeur de décalage reçue en parametres
*/
public void modifieX(int decalX){
setX(x+decalX);
}
public void modifieY(int decalY){
setY(y+decalY);
}
public void modifieZ(int decalZ){
setZ(z+decalZ);
}
public void modifieXY(int decalX, int decalY){
setXY(x+decalX,y+decalY);
}
public void modifieXZ(int decalX, int decalZ){
setXZ(x+decalX, z+decalZ);
}
public void modifieYZ(int decalY, int decalZ){
setYZ(y+decalY, z+decalZ);
}
public void modifieXYZ(int decalX, int decalY, int decalZ){
setXYZ(x+decalX, y+decalY, z+decalZ);
}
/**
* @return hauteur de l'image selon le taux choisi
*/
public int getHauteur() {
return (int)(image.getHeight(null)*taux);
}
/**
* @return largeur de l'image selon le taux choisi
*/
public int getLargeur() {
return (int)(image.getWidth(null)*taux);
}
/**
* @param taux taux à définir
*/
public void setTaux(float taux) {
this.taux = taux;
notifyObservers(taux);
}
/* (non-Javadoc)
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
public int compareTo(Objet3D c) {
if (c==this) return 0;
// si l'objet reçu en argument est plus 'profond' il est considéré comme inférieur à this
if (z<c.getZ()) return 1;
else if (z>c.getZ()) return -1;
else {
if (x<c.getX()) return 1;
else if (x>c.getX()) return -1;
else {
if (y<c.getY()) return 1;
else if (y>c.getY()) return -1;
else {
if (getLargeur()<c.getLargeur()) return 1;
else if (getLargeur()>c.getLargeur()) return -1;
else {
if (getHauteur()<c.getHauteur()) return 1;
else if (getHauteur()>c.getHauteur()) return -1;
}
}
}
}
return 0;
}
} |
Partager