Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import javax.swing.*;
- import java.awt.event.MouseListener;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class Image extends JPanel{
- private ArrayList<Forme> listForme;
- private ArrayList<Image> listImage;
- private int _perimetre;
- private int _aire;
- private boolean _drawCircle = false;
- private boolean _drawLine = false;
- private boolean _drawEllipse = false;
- private boolean _axiale = false;
- private boolean _centrale = false;
- private int _drawPolygon = 0;
- private int _rotation=0;
- private float _homotetie=0;
- private int clickcount = 0;
- private int cotePoly=0;
- private int rotAngle=0;
- private int numHomotetie=0;
- private ArrayList<Integer> listClickX;
- private ArrayList<Integer> listClickY;
- public Image(){
- super();
- listClickX = new ArrayList<Integer>();
- listClickY = new ArrayList<Integer>();
- listForme = new ArrayList<Forme>();
- listImage = new ArrayList<Image>();
- this.addMouseListener(new MouseAdapter() {
- public void mouseClicked(MouseEvent e) {
- if(_drawCircle == true){
- clickcount++;
- if(clickcount == 1){
- listClickX.add(e.getX());
- listClickY.add(e.getY());
- int coordX=e.getX();
- int coordY=e.getY();
- System.out.println("Le centre est situé à ("+coordX+";"+coordY+")");
- }
- else if(clickcount == 2){
- listClickX.add(e.getX());
- listClickY.add(e.getY());
- double rayon; // considere comme l'hypotenuse
- int a = listClickX.get(1) - listClickX.get(0);
- int b = listClickY.get(1) - listClickY.get(0);
- rayon = Math.sqrt(a*a + b*b);
- listForme.add(new Cercle((int)rayon, listClickX.get(0), listClickY.get(0)));
- System.out.println("Rayon: " + ((Cercle)(listForme.get(listForme.size() - 1))).getR());
- System.out.println("Taille de list Forme: " + listForme.size());
- System.out.println("Draw Circle");
- update(getGraphics());
- }
- }
- else if(_drawLine == true){
- clickcount++;
- System.out.println("Vous avez cliqué " + clickcount + " fois");
- if(clickcount == 1){
- listClickX.add(e.getX());
- listClickY.add(e.getY());
- System.out.println("Premier point défini");
- }
- else if(clickcount == 2){
- listClickX.add(e.getX());
- listClickY.add(e.getY());
- System.out.println("Second point défini");
- listForme.add(new Ligne(listClickX.get(0), listClickY.get(0),listClickX.get(1), listClickY.get(1)));
- System.out.println("Taille de list Forme: " + listForme.size());
- System.out.println("Draw Line");
- update(getGraphics());
- }
- }
- else if(_drawEllipse == true){
- clickcount++;
- if(clickcount == 1){
- listClickX.add(e.getX());
- listClickY.add(e.getY());
- int coordX=e.getX();
- int coordY=e.getY();
- System.out.println("Le centre est situé à ("+coordX+";"+coordY+")");
- }
- else if(clickcount == 2){
- listClickX.add(e.getX());
- listClickY.add(e.getY());
- System.out.println("Premier axe défini");
- }
- else if(clickcount == 3)
- {
- listClickX.add(e.getX());
- listClickY.add(e.getY());
- System.out.println("Second axe défini");
- 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))));
- System.out.println("Axe a: " + ((Ellipse)(listForme.get(listForme.size() - 1))).getA());
- System.out.println("Axe b: " + ((Ellipse)(listForme.get(listForme.size() - 1))).getB());
- System.out.println("Taille de list Forme: " + listForme.size());
- update(getGraphics());
- }
- }
- else if(_drawPolygon != 0)
- {
- clickcount++;
- System.out.println("Vous avez cliqué " + clickcount + " fois");
- listClickX.add(e.getX());
- listClickY.add(e.getY());
- update(getGraphics());
- }
- else{
- System.out.println("Ready to draw ?" + _drawEllipse);
- }
- }
- });
- }
- public void readytoDrawCircle(){
- _drawCircle = true;
- _drawLine = false;
- _drawEllipse= false;
- _drawPolygon = 0;
- }
- public void readytoDrawLine(){
- _drawLine = true;
- _drawCircle = false;
- _drawEllipse= false;
- _drawPolygon = 0;
- }
- public void readytoDrawEllipse(){
- _drawLine = false;
- _drawCircle = false;
- _drawEllipse= true;
- _drawPolygon = 0;
- }
- public void readytoDrawPolygon(){
- _drawLine = false;
- _drawCircle = false;
- _drawEllipse= false;
- try {
- cotePoly = Integer.parseInt(JOptionPane.showInputDialog(null, "Combien de sommets voulez-vous pour votre polygone ?","Polygone : Nombre de sommets/côtés", JOptionPane.QUESTION_MESSAGE));;
- }
- catch (NumberFormatException e){
- cotePoly = 0;
- }
- _drawPolygon = cotePoly;
- }
- public void readyAxiale()
- {
- _axiale=true;
- update(getGraphics());
- }
- public void readyCentrale()
- {
- _centrale=true;
- update(getGraphics());
- }
- public void readyRotation()
- {
- try {
- rotAngle = Integer.parseInt(JOptionPane.showInputDialog(null, "Quel angle voulez-vous pour cette rotation ?","Rotation : Angle", JOptionPane.QUESTION_MESSAGE));;
- }
- catch (NumberFormatException e) {
- rotAngle = 0;
- }
- _rotation = rotAngle;
- update(getGraphics());
- }
- public void readyHomotetie()
- {
- try {
- numHomotetie = Integer.parseInt(JOptionPane.showInputDialog(null, "De combien voulez-vous changer la taille de la forme (en %) ?","Homotetie : %", JOptionPane.QUESTION_MESSAGE));;
- }
- catch (NumberFormatException e){
- numHomotetie = 0;
- }
- _homotetie = numHomotetie/100;
- update(getGraphics());
- }
- public void paint(Graphics g){
- super.paint(g);
- Graphics2D g2d = (Graphics2D) g;
- setSize(600,600);
- g.drawLine(0, 300, 600, 300);
- g.drawLine(300, 0, 300, 600);
- if(_drawCircle == true && clickcount == 2){
- 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
- System.out.println("Rayon du cercle: " + ((Cercle)(listForme.get(listForme.size()-1))) .getR());
- clickcount = 0;
- listClickX.clear();
- listClickY.clear();
- _drawCircle = false;
- }
- else if(_drawLine == true && clickcount == 2){
- g.drawLine(listClickX.get(0),listClickY.get(0),listClickX.get(1),listClickY.get(1));
- clickcount = 0;
- listClickX.clear();
- listClickY.clear();
- _drawLine = false;
- }
- else if( _drawEllipse== true && clickcount ==3)
- {
- int ra=Math.abs(listClickX.get(0)-listClickX.get(1));
- int rb= Math.abs(listClickY.get(0)- listClickY.get(2));
- g.drawOval(listClickX.get(0)- ra,listClickY.get(0) - rb,ra*2,rb*2);
- clickcount = 0;
- listClickX.clear();
- listClickY.clear();
- _drawEllipse = false;
- }
- else if( _drawPolygon!=0 && clickcount == _drawPolygon)
- {
- int[] listX= new int[listClickX.size()];
- int[] listY= new int[listClickX.size()];
- for(int i=0;i<listClickX.size();i++)
- {
- listX[i]=listClickX.get(i);
- listY[i]=listClickY.get(i);
- }
- listForme.add(new Polygone(listClickX.get(0),listClickY.get(0),listClickX,listClickY));
- g.drawPolygon(listX,listY, _drawPolygon);
- clickcount = 0;
- listClickX.clear();
- listClickY.clear();
- _drawPolygon = 0;
- }
- if(_axiale == true)
- {
- if(listForme.size()==0)
- {
- System.out.println("Creez une forme avant");
- }
- else if(listForme.get(listForme.size()-1).getClass() == Cercle.class)
- {
- listForme.get(listForme.size()-1).setX(600-listForme.get(listForme.size()-1).getX());
- 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);
- }
- else if(listForme.get(listForme.size()-1).getClass() == Ligne.class)
- {
- listForme.get(listForme.size()-1).setX(600-listForme.get(listForme.size()-1).getX());
- ((Ligne)(listForme.get(listForme.size()-1))).setX2(600-((Ligne)(listForme.get(listForme.size()-1))).getX2());
- 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());
- }
- else if(listForme.get(listForme.size()-1).getClass() == Ellipse.class)
- {
- listForme.get(listForme.size()-1).setX(600-listForme.get(listForme.size()-1).getX());
- 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);
- }
- else if(listForme.get(listForme.size()-1).getClass() == Polygone.class)
- {
- ArrayList<Integer> listIntX=(((Polygone)(listForme.get(listForme.size()-1))).getpX());
- ArrayList<Integer> listIntY=(((Polygone)(listForme.get(listForme.size()-1))).getpY());
- int[] listX= new int[listIntX.size()];
- int[] listY= new int[listIntX.size()];
- for(int i=0;i<listIntX.size();i++)
- {
- System.out.println("X:" + listIntX.get(i));
- listIntX.set(i, 600-listIntX.get(i));
- System.out.println("X=" + listIntX.get(i));
- listX[i]=listIntX.get(i);
- listY[i]=listIntY.get(i);
- }
- ((Polygone)(listForme.get(listForme.size()-1))).setpX(listIntX);
- g.drawPolygon(listX,listY, listIntX.size());
- }
- _axiale=false;
- }
- else if(_centrale== true)
- {
- if(listForme.size()==0)
- {
- System.out.println("Creez une forme avant");
- }
- else if(listForme.get(listForme.size()-1).getClass() == Cercle.class)
- {
- listForme.get(listForme.size()-1).setX(600-listForme.get(listForme.size()-1).getX());
- listForme.get(listForme.size()-1).setY(600-listForme.get(listForme.size()-1).getY());
- 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);
- }
- else if(listForme.get(listForme.size()-1).getClass() == Ligne.class)
- {
- listForme.get(listForme.size()-1).setX(600-listForme.get(listForme.size()-1).getX());
- ((Ligne)(listForme.get(listForme.size()-1))).setX2(600-((Ligne)(listForme.get(listForme.size()-1))).getX2());
- listForme.get(listForme.size()-1).setY(600-listForme.get(listForme.size()-1).getY());
- ((Ligne)(listForme.get(listForme.size()-1))).setY2(600-((Ligne)(listForme.get(listForme.size()-1))).getY2());
- 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());
- }
- else if(listForme.get(listForme.size()-1).getClass() == Ellipse.class)
- {
- listForme.get(listForme.size()-1).setX(600-listForme.get(listForme.size()-1).getX());
- listForme.get(listForme.size()-1).setY(600-listForme.get(listForme.size()-1).getY());
- 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);
- }
- else if(listForme.get(listForme.size()-1).getClass() == Polygone.class)
- {
- ArrayList<Integer> listIntX=(((Polygone)(listForme.get(listForme.size()-1))).getpX());
- ArrayList<Integer> listIntY=(((Polygone)(listForme.get(listForme.size()-1))).getpY());
- int[] listX= new int[listIntX.size()];
- int[] listY= new int[listIntX.size()];
- for(int i=0;i<listIntX.size();i++)
- {
- System.out.println("X:" + listIntX.get(i));
- listIntX.set(i, 600-listIntX.get(i));
- listIntY.set(i, 600-listIntY.get(i));
- System.out.println("X=" + listIntX.get(i));
- listX[i]=listIntX.get(i);
- listY[i]=listIntY.get(i);
- }
- ((Polygone)(listForme.get(listForme.size()-1))).setpX(listIntX);
- ((Polygone)(listForme.get(listForme.size()-1))).setpY(listIntY);
- g.drawPolygon(listX,listY, listIntX.size());
- }
- _centrale=false;
- }
- else if(_rotation!=0)
- {
- }
- else if(_homotetie!=0)
- { g.setColor(Color.yellow);
- if(listForme.size()==0)
- {
- System.out.println("Creez une forme avant");
- }
- else if(listForme.get(listForme.size()-1).getClass() == Cercle.class)
- {
- ((Cercle)(listForme.get(listForme.size()-1))).setR((int)(_homotetie*((Cercle)(listForme.get(listForme.size()-1))).getR()));
- 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);
- }
- else if(listForme.get(listForme.size()-1).getClass() == Ellipse.class)
- {
- ((Ellipse)(listForme.get(listForme.size()-1))).setA((int)(_homotetie*((Ellipse)(listForme.get(listForme.size()-1))).getA()));
- ((Ellipse)(listForme.get(listForme.size()-1))).setB((int)(_homotetie*((Ellipse)(listForme.get(listForme.size()-1))).getB()));
- 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);
- }
- _homotetie=0;
- }
- }
- public void addImage(Image image){
- //Verification de l'aire de l'image
- if (image.getAire() < this.getAire()){
- this.listImage.add(image);
- }
- }
- public void addForme(Forme forme){
- boolean ctrlDoublon = true;
- for(int i = 0; i < this.listForme.size(); i++){
- if(this.listForme.get(i).equals(forme)){ //L'unicité se fait par rapport aux dimensions de la forme
- if(this.listForme.get(i).getAire() == forme.getAire() && this.listForme.get(i).getPerimetre() == forme.getPerimetre()){
- ctrlDoublon = false; //Doublon existe
- }
- }
- }
- //Verification de l'aire de l'image
- if (ctrlDoublon == true){
- this.listForme.add(forme);
- }
- }
- public void calculAire(){
- //Somme des aires de ses composants
- int aire = 0;
- for(int i = 0; i < listForme.size(); i++){
- aire += listForme.get(i).getAire();
- }
- this.setAire(aire);
- }
- public void calculPerimetre(){
- //Somme des perimetres de ses composants
- int perimetre = 0;
- for(int i = 0; i < listForme.size(); i++){
- perimetre += listForme.get(i).getPerimetre();
- }
- this.setPerimetre(perimetre);
- }
- public void setAire(int aire){
- this._aire = aire;
- }
- public void setPerimetre(int perimetre){
- this._perimetre = perimetre;
- }
- public int getAire(){
- return this._aire;
- }
- public int getPerimetre(){
- return this._perimetre;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement