Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.72 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.MouseAdapter;
  3. import java.awt.event.MouseEvent;
  4. import javax.swing.*;
  5. import java.awt.event.MouseListener;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Scanner;
  9.  
  10.  
  11.  
  12. public class Image extends JPanel{
  13.  
  14.  
  15. private ArrayList<Forme> listForme;
  16. private ArrayList<Image> listImage;
  17. private int _perimetre;
  18. private int _aire;
  19. private boolean _drawCircle = false;
  20. private boolean _drawLine = false;
  21. private boolean _drawEllipse = false;
  22. private boolean _axiale = false;
  23. private boolean _centrale = false;
  24. private int _drawPolygon = 0;
  25. private int _rotation=0;
  26. private float _homotetie=0;
  27. private int clickcount = 0;
  28. private int cotePoly=0;
  29. private int rotAngle=0;
  30. private int numHomotetie=0;
  31. private ArrayList<Integer> listClickX;
  32. private ArrayList<Integer> listClickY;
  33.  
  34. public Image(){
  35. super();
  36. listClickX = new ArrayList<Integer>();
  37. listClickY = new ArrayList<Integer>();
  38. listForme = new ArrayList<Forme>();
  39. listImage = new ArrayList<Image>();
  40.  
  41.  
  42. this.addMouseListener(new MouseAdapter() {
  43.  
  44. public void mouseClicked(MouseEvent e) {
  45.  
  46. if(_drawCircle == true){
  47. clickcount++;
  48. if(clickcount == 1){
  49. listClickX.add(e.getX());
  50. listClickY.add(e.getY());
  51. int coordX=e.getX();
  52. int coordY=e.getY();
  53. System.out.println("Le centre est situé à ("+coordX+";"+coordY+")");
  54. }
  55. else if(clickcount == 2){
  56. listClickX.add(e.getX());
  57. listClickY.add(e.getY());
  58. double rayon; // considere comme l'hypotenuse
  59. int a = listClickX.get(1) - listClickX.get(0);
  60. int b = listClickY.get(1) - listClickY.get(0);
  61. rayon = Math.sqrt(a*a + b*b);
  62.  
  63. listForme.add(new Cercle((int)rayon, listClickX.get(0), listClickY.get(0)));
  64. System.out.println("Rayon: " + ((Cercle)(listForme.get(listForme.size() - 1))).getR());
  65. System.out.println("Taille de list Forme: " + listForme.size());
  66. System.out.println("Draw Circle");
  67. update(getGraphics());
  68. }
  69. }
  70. else if(_drawLine == true){
  71. clickcount++;
  72. System.out.println("Vous avez cliqué " + clickcount + " fois");
  73. if(clickcount == 1){
  74. listClickX.add(e.getX());
  75. listClickY.add(e.getY());
  76. System.out.println("Premier point défini");
  77. }
  78. else if(clickcount == 2){
  79. listClickX.add(e.getX());
  80. listClickY.add(e.getY());
  81. System.out.println("Second point défini");
  82. listForme.add(new Ligne(listClickX.get(0), listClickY.get(0),listClickX.get(1), listClickY.get(1)));
  83. System.out.println("Taille de list Forme: " + listForme.size());
  84. System.out.println("Draw Line");
  85. update(getGraphics());
  86. }
  87. }
  88. else if(_drawEllipse == true){
  89. clickcount++;
  90. if(clickcount == 1){
  91. listClickX.add(e.getX());
  92. listClickY.add(e.getY());
  93. int coordX=e.getX();
  94. int coordY=e.getY();
  95. System.out.println("Le centre est situé à ("+coordX+";"+coordY+")");
  96. }
  97. else if(clickcount == 2){
  98. listClickX.add(e.getX());
  99. listClickY.add(e.getY());
  100. System.out.println("Premier axe défini");
  101. }
  102. else if(clickcount == 3)
  103. {
  104. listClickX.add(e.getX());
  105. listClickY.add(e.getY());
  106. System.out.println("Second axe défini");
  107. listForme.add(new Ellipse(listClickX.get(0), listClickY.get(0), Math.abs(listClickX.get(0)-listClickX.get(1)), Math.abs(listClickY.get(0)- listClickY.get(2))));
  108. System.out.println("Axe a: " + ((Ellipse)(listForme.get(listForme.size() - 1))).getA());
  109. System.out.println("Axe b: " + ((Ellipse)(listForme.get(listForme.size() - 1))).getB());
  110. System.out.println("Taille de list Forme: " + listForme.size());
  111. update(getGraphics());
  112. }
  113. }
  114. else if(_drawPolygon != 0)
  115. {
  116. clickcount++;
  117. System.out.println("Vous avez cliqué " + clickcount + " fois");
  118. listClickX.add(e.getX());
  119. listClickY.add(e.getY());
  120. update(getGraphics());
  121.  
  122. }
  123. else{
  124. System.out.println("Ready to draw ?" + _drawEllipse);
  125. }
  126.  
  127. }
  128.  
  129. });
  130. }
  131.  
  132. public void readytoDrawCircle(){
  133. _drawCircle = true;
  134. _drawLine = false;
  135. _drawEllipse= false;
  136. _drawPolygon = 0;
  137. }
  138. public void readytoDrawLine(){
  139. _drawLine = true;
  140. _drawCircle = false;
  141. _drawEllipse= false;
  142. _drawPolygon = 0;
  143. }
  144. public void readytoDrawEllipse(){
  145. _drawLine = false;
  146. _drawCircle = false;
  147. _drawEllipse= true;
  148. _drawPolygon = 0;
  149. }
  150.  
  151. public void readytoDrawPolygon(){
  152.  
  153. _drawLine = false;
  154. _drawCircle = false;
  155. _drawEllipse= false;
  156. try {
  157. cotePoly = Integer.parseInt(JOptionPane.showInputDialog(null, "Combien de sommets voulez-vous pour votre polygone ?","Polygone : Nombre de sommets/côtés", JOptionPane.QUESTION_MESSAGE));;
  158. }
  159. catch (NumberFormatException e){
  160. cotePoly = 0;
  161. }
  162.  
  163. _drawPolygon = cotePoly;
  164. }
  165.  
  166. public void readyAxiale()
  167. {
  168. _axiale=true;
  169. update(getGraphics());
  170. }
  171.  
  172. public void readyCentrale()
  173. {
  174. _centrale=true;
  175. update(getGraphics());
  176. }
  177.  
  178. public void readyRotation()
  179. {
  180. try {
  181. rotAngle = Integer.parseInt(JOptionPane.showInputDialog(null, "Quel angle voulez-vous pour cette rotation ?","Rotation : Angle", JOptionPane.QUESTION_MESSAGE));;
  182. }
  183. catch (NumberFormatException e) {
  184. rotAngle = 0;
  185. }
  186. _rotation = rotAngle;
  187. update(getGraphics());
  188. }
  189.  
  190. public void readyHomotetie()
  191. {
  192. try {
  193. numHomotetie = Integer.parseInt(JOptionPane.showInputDialog(null, "De combien voulez-vous changer la taille de la forme (en %) ?","Homotetie : %", JOptionPane.QUESTION_MESSAGE));;
  194. }
  195. catch (NumberFormatException e){
  196. numHomotetie = 0;
  197. }
  198. _homotetie = numHomotetie/100;
  199. update(getGraphics());
  200. }
  201.  
  202. public void paint(Graphics g){
  203.  
  204. super.paint(g);
  205. Graphics2D g2d = (Graphics2D) g;
  206. setSize(600,600);
  207.  
  208.  
  209. g.drawLine(0, 300, 600, 300);
  210. g.drawLine(300, 0, 300, 600);
  211. if(_drawCircle == true && clickcount == 2){
  212. g.drawOval(listClickX.get(0) - (((Cercle) (listForme.get(listForme.size()-1))).getR()) ,listClickY.get(0) - (((Cercle)(listForme.get(listForme.size()-1))).getR()),((Cercle)(listForme.get(listForme.size()-1))).getR()*2,((Cercle)(listForme.get(listForme.size()-1))).getR()*2); //w et d peuvent correspondre au diametre du coup x et y coordonne - rayon pour bien se trouver au niveau de la souris
  213. System.out.println("Rayon du cercle: " + ((Cercle)(listForme.get(listForme.size()-1))) .getR());
  214. clickcount = 0;
  215. listClickX.clear();
  216. listClickY.clear();
  217. _drawCircle = false;
  218. }
  219. else if(_drawLine == true && clickcount == 2){
  220. g.drawLine(listClickX.get(0),listClickY.get(0),listClickX.get(1),listClickY.get(1));
  221. clickcount = 0;
  222. listClickX.clear();
  223. listClickY.clear();
  224. _drawLine = false;
  225. }
  226. else if( _drawEllipse== true && clickcount ==3)
  227. {
  228. int ra=Math.abs(listClickX.get(0)-listClickX.get(1));
  229. int rb= Math.abs(listClickY.get(0)- listClickY.get(2));
  230. g.drawOval(listClickX.get(0)- ra,listClickY.get(0) - rb,ra*2,rb*2);
  231. clickcount = 0;
  232. listClickX.clear();
  233. listClickY.clear();
  234. _drawEllipse = false;
  235. }
  236. else if( _drawPolygon!=0 && clickcount == _drawPolygon)
  237. {
  238. int[] listX= new int[listClickX.size()];
  239. int[] listY= new int[listClickX.size()];
  240. for(int i=0;i<listClickX.size();i++)
  241. {
  242. listX[i]=listClickX.get(i);
  243. listY[i]=listClickY.get(i);
  244. }
  245. listForme.add(new Polygone(listClickX.get(0),listClickY.get(0),listClickX,listClickY));
  246. g.drawPolygon(listX,listY, _drawPolygon);
  247. clickcount = 0;
  248. listClickX.clear();
  249. listClickY.clear();
  250. _drawPolygon = 0;
  251. }
  252.  
  253. if(_axiale == true)
  254. {
  255. if(listForme.size()==0)
  256. {
  257. System.out.println("Creez une forme avant");
  258. }
  259. else if(listForme.get(listForme.size()-1).getClass() == Cercle.class)
  260. {
  261. listForme.get(listForme.size()-1).setX(600-listForme.get(listForme.size()-1).getX());
  262. g.drawOval(listForme.get(listForme.size()-1).getX()- (((Cercle) (listForme.get(listForme.size()-1))).getR()), listForme.get(listForme.size()-1).getY()- (((Cercle) (listForme.get(listForme.size()-1))).getR()), ((Cercle)(listForme.get(listForme.size()-1))).getR()*2,((Cercle)(listForme.get(listForme.size()-1))).getR()*2);
  263. }
  264. else if(listForme.get(listForme.size()-1).getClass() == Ligne.class)
  265. {
  266. listForme.get(listForme.size()-1).setX(600-listForme.get(listForme.size()-1).getX());
  267. ((Ligne)(listForme.get(listForme.size()-1))).setX2(600-((Ligne)(listForme.get(listForme.size()-1))).getX2());
  268. g.drawLine(listForme.get(listForme.size()-1).getX(), listForme.get(listForme.size()-1).getY(), ((Ligne)(listForme.get(listForme.size()-1))).getX2(), ((Ligne)(listForme.get(listForme.size()-1))).getY2());
  269. }
  270. else if(listForme.get(listForme.size()-1).getClass() == Ellipse.class)
  271. {
  272.  
  273. listForme.get(listForme.size()-1).setX(600-listForme.get(listForme.size()-1).getX());
  274. g.drawOval(listForme.get(listForme.size()-1).getX()-((Ellipse)(listForme.get(listForme.size()-1))).getA() ,listForme.get(listForme.size()-1).getY()-((Ellipse)(listForme.get(listForme.size()-1))).getB(),((Ellipse)(listForme.get(listForme.size()-1))).getA()*2,((Ellipse)(listForme.get(listForme.size()-1))).getB()*2);
  275. }
  276. else if(listForme.get(listForme.size()-1).getClass() == Polygone.class)
  277. {
  278. ArrayList<Integer> listIntX=(((Polygone)(listForme.get(listForme.size()-1))).getpX());
  279. ArrayList<Integer> listIntY=(((Polygone)(listForme.get(listForme.size()-1))).getpY());
  280. int[] listX= new int[listIntX.size()];
  281. int[] listY= new int[listIntX.size()];
  282. for(int i=0;i<listIntX.size();i++)
  283. {
  284. System.out.println("X:" + listIntX.get(i));
  285. listIntX.set(i, 600-listIntX.get(i));
  286. System.out.println("X=" + listIntX.get(i));
  287. listX[i]=listIntX.get(i);
  288. listY[i]=listIntY.get(i);
  289. }
  290. ((Polygone)(listForme.get(listForme.size()-1))).setpX(listIntX);
  291. g.drawPolygon(listX,listY, listIntX.size());
  292. }
  293. _axiale=false;
  294.  
  295. }
  296.  
  297. else if(_centrale== true)
  298. {
  299. if(listForme.size()==0)
  300. {
  301. System.out.println("Creez une forme avant");
  302. }
  303.  
  304. else if(listForme.get(listForme.size()-1).getClass() == Cercle.class)
  305. {
  306. listForme.get(listForme.size()-1).setX(600-listForme.get(listForme.size()-1).getX());
  307. listForme.get(listForme.size()-1).setY(600-listForme.get(listForme.size()-1).getY());
  308. g.drawOval(listForme.get(listForme.size()-1).getX()- (((Cercle) (listForme.get(listForme.size()-1))).getR()), listForme.get(listForme.size()-1).getY()- (((Cercle) (listForme.get(listForme.size()-1))).getR()), ((Cercle)(listForme.get(listForme.size()-1))).getR()*2,((Cercle)(listForme.get(listForme.size()-1))).getR()*2);
  309.  
  310. }
  311.  
  312. else if(listForme.get(listForme.size()-1).getClass() == Ligne.class)
  313. {
  314. listForme.get(listForme.size()-1).setX(600-listForme.get(listForme.size()-1).getX());
  315. ((Ligne)(listForme.get(listForme.size()-1))).setX2(600-((Ligne)(listForme.get(listForme.size()-1))).getX2());
  316. listForme.get(listForme.size()-1).setY(600-listForme.get(listForme.size()-1).getY());
  317. ((Ligne)(listForme.get(listForme.size()-1))).setY2(600-((Ligne)(listForme.get(listForme.size()-1))).getY2());
  318. g.drawLine(listForme.get(listForme.size()-1).getX(), listForme.get(listForme.size()-1).getY(), ((Ligne)(listForme.get(listForme.size()-1))).getX2(), ((Ligne)(listForme.get(listForme.size()-1))).getY2());
  319. }
  320.  
  321. else if(listForme.get(listForme.size()-1).getClass() == Ellipse.class)
  322. {
  323.  
  324. listForme.get(listForme.size()-1).setX(600-listForme.get(listForme.size()-1).getX());
  325. listForme.get(listForme.size()-1).setY(600-listForme.get(listForme.size()-1).getY());
  326. g.drawOval(listForme.get(listForme.size()-1).getX()-((Ellipse)(listForme.get(listForme.size()-1))).getA() ,listForme.get(listForme.size()-1).getY()-((Ellipse)(listForme.get(listForme.size()-1))).getB(),((Ellipse)(listForme.get(listForme.size()-1))).getA()*2,((Ellipse)(listForme.get(listForme.size()-1))).getB()*2);
  327. }
  328.  
  329. else if(listForme.get(listForme.size()-1).getClass() == Polygone.class)
  330. {
  331. ArrayList<Integer> listIntX=(((Polygone)(listForme.get(listForme.size()-1))).getpX());
  332. ArrayList<Integer> listIntY=(((Polygone)(listForme.get(listForme.size()-1))).getpY());
  333. int[] listX= new int[listIntX.size()];
  334. int[] listY= new int[listIntX.size()];
  335. for(int i=0;i<listIntX.size();i++)
  336. {
  337. System.out.println("X:" + listIntX.get(i));
  338. listIntX.set(i, 600-listIntX.get(i));
  339. listIntY.set(i, 600-listIntY.get(i));
  340. System.out.println("X=" + listIntX.get(i));
  341. listX[i]=listIntX.get(i);
  342. listY[i]=listIntY.get(i);
  343. }
  344. ((Polygone)(listForme.get(listForme.size()-1))).setpX(listIntX);
  345. ((Polygone)(listForme.get(listForme.size()-1))).setpY(listIntY);
  346. g.drawPolygon(listX,listY, listIntX.size());
  347. }
  348. _centrale=false;
  349. }
  350. else if(_rotation!=0)
  351. {
  352.  
  353. }
  354.  
  355. else if(_homotetie!=0)
  356. { g.setColor(Color.yellow);
  357. if(listForme.size()==0)
  358. {
  359. System.out.println("Creez une forme avant");
  360. }
  361.  
  362. else if(listForme.get(listForme.size()-1).getClass() == Cercle.class)
  363. {
  364. ((Cercle)(listForme.get(listForme.size()-1))).setR((int)(_homotetie*((Cercle)(listForme.get(listForme.size()-1))).getR()));
  365. g.fillOval(listForme.get(listForme.size()-1).getX()- (((Cercle) (listForme.get(listForme.size()-1))).getR()), listForme.get(listForme.size()-1).getY()- (((Cercle) (listForme.get(listForme.size()-1))).getR()), ((Cercle)(listForme.get(listForme.size()-1))).getR()*2,((Cercle)(listForme.get(listForme.size()-1))).getR()*2);
  366.  
  367. }
  368. else if(listForme.get(listForme.size()-1).getClass() == Ellipse.class)
  369. {
  370. ((Ellipse)(listForme.get(listForme.size()-1))).setA((int)(_homotetie*((Ellipse)(listForme.get(listForme.size()-1))).getA()));
  371. ((Ellipse)(listForme.get(listForme.size()-1))).setB((int)(_homotetie*((Ellipse)(listForme.get(listForme.size()-1))).getB()));
  372. g.fillOval(listForme.get(listForme.size()-1).getX()-((Ellipse)(listForme.get(listForme.size()-1))).getA() ,listForme.get(listForme.size()-1).getY()-((Ellipse)(listForme.get(listForme.size()-1))).getB(),((Ellipse)(listForme.get(listForme.size()-1))).getA()*2,((Ellipse)(listForme.get(listForme.size()-1))).getB()*2);
  373.  
  374. }
  375. _homotetie=0;
  376. }
  377.  
  378. }
  379.  
  380. public void addImage(Image image){
  381. //Verification de l'aire de l'image
  382. if (image.getAire() < this.getAire()){
  383. this.listImage.add(image);
  384. }
  385. }
  386.  
  387. public void addForme(Forme forme){
  388. boolean ctrlDoublon = true;
  389. for(int i = 0; i < this.listForme.size(); i++){
  390. if(this.listForme.get(i).equals(forme)){ //L'unicité se fait par rapport aux dimensions de la forme
  391. if(this.listForme.get(i).getAire() == forme.getAire() && this.listForme.get(i).getPerimetre() == forme.getPerimetre()){
  392. ctrlDoublon = false; //Doublon existe
  393. }
  394. }
  395. }
  396. //Verification de l'aire de l'image
  397. if (ctrlDoublon == true){
  398. this.listForme.add(forme);
  399. }
  400. }
  401.  
  402. public void calculAire(){
  403. //Somme des aires de ses composants
  404. int aire = 0;
  405. for(int i = 0; i < listForme.size(); i++){
  406. aire += listForme.get(i).getAire();
  407. }
  408. this.setAire(aire);
  409. }
  410.  
  411. public void calculPerimetre(){
  412. //Somme des perimetres de ses composants
  413. int perimetre = 0;
  414. for(int i = 0; i < listForme.size(); i++){
  415. perimetre += listForme.get(i).getPerimetre();
  416. }
  417. this.setPerimetre(perimetre);
  418. }
  419.  
  420. public void setAire(int aire){
  421. this._aire = aire;
  422. }
  423.  
  424. public void setPerimetre(int perimetre){
  425. this._perimetre = perimetre;
  426. }
  427.  
  428. public int getAire(){
  429. return this._aire;
  430. }
  431. public int getPerimetre(){
  432. return this._perimetre;
  433. }
  434.  
  435.  
  436.  
  437. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement