Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html
- //Nécessaire pour dessiner les planètes
- import java.awt.Graphics2D.*;
- import java.awt.Graphics;
- //Nécessaire pour MouseInputListener
- import java.util.*;
- import java.awt.*;
- import java.io.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- import javax.swing.JPanel;
- import javax.swing.JFrame;
- //Lock des threads
- import java.util.concurrent.locks.Condition;
- import java.util.concurrent.locks.Lock;
- //Importation image de fond
- import java.io.IOException;
- import java.io.InputStream;
- import javax.imageio.ImageIO;
- import java.awt.image.BufferedImage;
- import java.util.Random;
- import java.lang.Math.*;
- public class Panel extends JPanel implements MouseInputListener,MouseWheelListener,KeyListener{
- private ArrayList<Planete> pla;
- private ArrayList<Planete> actualiser;
- private ArrayList<GazInterstellaire> gaz;
- private double c = 0.10, xClick, yClick, dx, dy, tmpCam, angleX = 0, angleY = 0, valX, valY;
- private int masseNouvellePlanete = 50, rayonNouvellePlanete = 50;
- private Modele univers;
- private Lock verrou;
- private float zoom = 1f;
- public BufferedImage img, deathStar;
- private double[] camera = new double[3], cameraTranslation = new double[3];
- private static final int INDETERMINATE = -1;
- private int previousKeyPressed = INDETERMINATE, buttonDown = 0, keyTmp = 0, clicPlanete, x, y, tailleTmp;
- private boolean lance = false, rekt;
- private String[] historique;
- private double[][] rotationX = new double[3][3];
- private double[][] rotationY = new double[3][3];
- private double[][] InvRotationX = new double[3][3];
- private double[][] InvRotationY = new double[3][3];
- private String directionPrecedente = "rieng";
- private BufferedImage image;
- private int compteurScreen = 0, compteurImages;
- private JFrame fen;
- public Panel(Modele univers, Lock verrou, InputStream is, int hauteur, int largeur, InputStream deathStar, JFrame fen){
- this.fen = fen;
- pla = univers.getPlanetes();
- System.out.println("Panel : "+pla.size());
- tailleTmp = pla.size();
- univers.setTailleJeu2(tailleTmp);
- this.univers = univers;
- this.verrou = verrou;
- this.camera[0] = largeur/2;
- this.camera[1] = hauteur/2;
- this.camera[2] = 0;
- this.cameraTranslation[0] = 0;
- this.cameraTranslation[1] = 0;
- this.cameraTranslation[2] = 0;
- this.historique = univers.getHistorique();
- try {
- img = ImageIO.read(is);
- this.deathStar = ImageIO.read(deathStar);
- }catch(IOException e){
- e.printStackTrace();
- }
- this.addMouseListener(this);
- this.addMouseMotionListener(this);
- this.addMouseWheelListener(this);
- this.addKeyListener(this);
- this.setFocusable(true);
- this.InitialisationMatrices(angleX, angleY);
- }
- /**
- * @void Ajout d'une planete
- *
- * Ajoute la planete donnee en parametre a la liste
- * si la planete ne vaut pas nulle, elle sera dans
- * dessinee plus tard
- *
- * @param oui Planete qui sera ajoutee
- *
- */
- public void ajouterDraw(Planete p){
- pla.add(p);
- }
- public void setTaillePlanete(int ray){
- this.rayonNouvellePlanete = ray;
- }
- public void InitialisationMatrices(double angleX, double angleY){
- this.rotationX[0][0] = 1;
- this.rotationX[0][1] = 0;
- this.rotationX[0][2] = 0;
- this.rotationX[1][0] = 0;
- this.rotationX[1][1] = (float)Math.cos(angleX*Math.PI/180);
- this.rotationX[1][2] = (float)(-(Math.sin(angleX*Math.PI/180)));
- this.rotationX[2][0] = 0;
- this.rotationX[2][1] = (float)Math.sin(angleX*Math.PI/180);
- this.rotationX[2][2] = (float)Math.cos(angleX*Math.PI/180);
- this.InvRotationX[0][0] = 1;
- this.InvRotationX[0][1] = 0;
- this.InvRotationX[0][2] = 0;
- this.InvRotationX[1][0] = 0;
- this.InvRotationX[1][1] = (float)Math.cos((-angleX)*Math.PI/180);
- this.InvRotationX[1][2] = (float)(-(Math.sin((-angleX)*Math.PI/180)));
- this.InvRotationX[2][0] = 0;
- this.InvRotationX[2][1] = (float)Math.sin((-angleX)*Math.PI/180);
- this.InvRotationX[2][2] = (float)Math.cos((-angleX)*Math.PI/180);
- this.rotationY[0][0] = (float)Math.cos(angleY*Math.PI/180);
- this.rotationY[0][1] = 0;
- this.rotationY[0][2] = (float)Math.sin(angleY*Math.PI/180);
- this.rotationY[1][0] = 0;
- this.rotationY[1][1] = 1;
- this.rotationY[1][2] = 0;
- this.rotationY[2][0] = (float)(-(Math.sin(angleY*Math.PI/180)));
- this.rotationY[2][1] = 0;
- this.rotationY[2][2] = (float)Math.cos(angleY*Math.PI/180);
- this.InvRotationY[0][0] = (float)Math.cos((-angleY)*Math.PI/180);
- this.InvRotationY[0][1] = 0;
- this.InvRotationY[0][2] = (float)Math.sin((-angleY)*Math.PI/180);
- this.InvRotationY[1][0] = 0;
- this.InvRotationY[1][1] = 1;
- this.InvRotationY[1][2] = 0;
- this.InvRotationY[2][0] = (float)(-(Math.sin((-angleY)*Math.PI/180)));
- this.InvRotationY[2][1] = 0;
- this.InvRotationY[2][2] = (float)Math.cos((-angleY)*Math.PI/180);
- }
- public void setRotationX(double angle){
- this.rotationX[1][1] = (float)Math.cos(angleX*Math.PI/180);
- this.rotationX[1][2] = (float)(-(Math.sin(angleX*Math.PI/180)));
- this.rotationX[2][1] = (float)Math.sin(angleX*Math.PI/180);
- this.rotationX[2][2] = (float)Math.cos(angleX*Math.PI/180);
- this.InvRotationX[1][1] = (float)Math.cos((-angleX)*Math.PI/180);
- this.InvRotationX[1][2] = (float)(-(Math.sin((-angleX)*Math.PI/180)));
- this.InvRotationX[2][1] = (float)Math.sin((-angleX)*Math.PI/180);
- this.InvRotationX[2][2] = (float)Math.cos((-angleX)*Math.PI/180);
- }
- public void setRotationY(double angleY){
- this.rotationY[0][0] = (float)Math.cos(angleY*Math.PI/180);
- this.rotationY[0][2] = (float)Math.sin(angleY*Math.PI/180);
- this.rotationY[2][0] = (float)(-(Math.sin(angleY*Math.PI/180)));
- this.rotationY[2][2] = (float)Math.cos(angleY*Math.PI/180);
- this.InvRotationY[0][0] = (float)Math.cos((-angleY)*Math.PI/180);
- this.InvRotationY[0][2] = (float)Math.sin((-angleY)*Math.PI/180);
- this.InvRotationY[2][0] = (float)(-(Math.sin((-angleY)*Math.PI/180)));
- this.InvRotationY[2][2] = (float)Math.cos((-angleY)*Math.PI/180);
- }
- /**
- * @ void Surcharge des méthodes de l'interface MouseInputListener
- */
- public void mouseClicked(MouseEvent e){
- xClick = (double)(e.getX());
- yClick = (double)(e.getY());
- }
- public void mouseExited(MouseEvent e){
- }
- public void mouseEntered(MouseEvent e){
- }
- public void mouseReleased(MouseEvent e){
- double []tmp = {xClick,yClick,0};
- tmp = InvCoordGraph(tmp);
- double xTmp = tmp[0], yTmp = tmp[1], zTmp = tmp[2];
- if(univers.getMode() && buttonDown == MouseEvent.BUTTON1 && !univers.getJeuA()){
- pla.add(new Planete(masseNouvellePlanete,new Position(xTmp,yTmp,zTmp),new Position(0,0,0),new Position(dx/100,dy/100,0),rayonNouvellePlanete,(int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256)));
- univers.ajouterHistorique("Nouvelle Planète !");
- }
- else if(univers.getMode() && buttonDown == MouseEvent.BUTTON1 && univers.getJeuA()){
- univers.setTailleJeu(1);
- univers.ajouterHistorique("Nouvelle Planète !");
- pla.add(new Planete(masseNouvellePlanete,new Position((xClick-this.camera[0])/zoom,(yClick-this.camera[1])/zoom,0),new Position(0,0,0),new Position(dx/25,dy/25,0),rayonNouvellePlanete,(int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256)));
- }
- if(!univers.getMode() && buttonDown == MouseEvent.BUTTON1 && lance && !univers.getJeuA()){
- pla.get(clicPlanete).getVitesse().setX(dx/50);
- pla.get(clicPlanete).getVitesse().setY(dy/50);
- lance = false;
- univers.ajouterHistorique("Modification de la vitesse de la planètes "+clicPlanete+" !");
- }
- this.camera[0] += this.cameraTranslation[0];
- this.camera[1] += this.cameraTranslation[1];
- this.cameraTranslation[0] = 0;
- this.cameraTranslation[1] = 0;
- dx = 0;
- dy = 0;
- }
- /**
- * @ void récupération des coordonnées de l'endroit cliqué sur le JPanel
- */
- public void mousePressed(MouseEvent e){
- buttonDown = e.getButton();
- xClick = (double)(e.getX());
- yClick = (double)(e.getY());
- double []tmp = {xClick,yClick,0};
- tmp = InvCoordGraph(tmp);
- int test1 = (int)((tmp[0]-this.camera[0])/zoom);
- int test2 = (int)((tmp[1]-this.camera[1])/zoom);
- if(buttonDown == MouseEvent.BUTTON1 && !univers.getMode() && !univers.getJeuA()){
- for(int j = 0; j < pla.size(); j++){
- Planete actuel = pla.get(j);
- double [] coord = {actuel.getPos().getX(),actuel.getPos().getY(),actuel.getPos().getZ()};
- coord = CoordGraph(coord);
- double xTmp = coord[0], yTmp = coord[1];
- if(test1 > xTmp-(pla.get(j).getRayon2())*2 && test1 < xTmp+(pla.get(j).getRayon2()) && test2 > yTmp-(pla.get(j).getRayon2())*2 && test2 < yTmp+(pla.get(j).getRayon2())){
- lance = true;
- clicPlanete = j;
- }
- }
- }
- }
- public void mouseMoved(MouseEvent e){
- valX = e.getX()-this.camera[0];
- valY = e.getY()-this.camera[1];
- double x = e.getX()-camera[0], y = e.getY()-camera[1];
- int test1 = (int)((x-this.camera[0])/zoom);
- int test2 = (int)((y-this.camera[1])/zoom);
- for(int j = 0; j < pla.size(); j++){
- if(test1 > pla.get(j).getPos().getX()-(pla.get(j).getRayon2())*2 && test1 < pla.get(j).getPos().getX()+(pla.get(j).getRayon2()) && test2 > pla.get(j).getPos().getY()-(pla.get(j).getRayon2())*2 && test2 < pla.get(j).getPos().getY()+(pla.get(j).getRayon2())){
- //System.out.println("Planète "+j+" : masse = "+pla.get(j).getMasse()+"x10^24Kg, rayon = "+pla.get(j).getRayon2()*100+"Km");
- }
- }
- }
- /**
- * @ void récupération du vecteur de coordonnées fait avec un Drag'n'Drop à la souris
- */
- public void mouseDragged(MouseEvent e){
- dx = (e.getX() - xClick);
- dy = (e.getY() - yClick);
- if(buttonDown == MouseEvent.BUTTON3){
- this.cameraTranslation[0] = dx;
- this.cameraTranslation[1] = dy;
- }
- }
- public void mouseWheelMoved(MouseWheelEvent e){
- if(e.getWheelRotation() < 0){
- this.setZoom(1.05f);
- this.camera[2] += 1;
- }
- else{
- this.setZoom(0.95f);
- this.camera[2] -= 1;
- }
- }
- public void keyTyped(KeyEvent evt){
- keyTmp = evt.getKeyCode();
- }
- public void keyPressed(KeyEvent evt){
- if(previousKeyPressed == INDETERMINATE){
- previousKeyPressed = evt.getKeyCode();
- }
- //else if(previousKeyPressed != evt.getKeyCode() && previousKeyPressed == KeyEvent.VK_CONTROL && !univers.getJeuA()){
- else if(!univers.getJeuA()){
- switch(evt.getKeyCode()){
- case KeyEvent.VK_LEFT :
- angleY -= 1;
- setRotationY(angleY);
- break;
- case KeyEvent.VK_RIGHT :
- angleY += 1;
- setRotationY(angleY);
- break;
- case KeyEvent.VK_DOWN :
- angleX -= 1;
- setRotationX(angleX);
- break;
- case KeyEvent.VK_UP :
- angleX += 1;
- setRotationX(angleX);
- break;
- default : break;
- }
- }
- }
- public void keyReleased(KeyEvent evt){
- keyTmp = 0;
- if(previousKeyPressed == evt.getKeyCode()){
- previousKeyPressed = INDETERMINATE;
- }
- }
- /**
- * @ void réglage du zoom
- * cette méthode set le zoom en le multipliant
- * par la valeur envoyée en argument
- * par le actionListener de "Zoom Avant/Arrière"
- */
- public void setZoom(float z){
- this.zoom *= z;
- }
- public void setZoomDefault(){
- this.zoom = 1f;
- }
- /**
- * @ArrayList<Planete> tri de liste de planetes
- *
- * cette methode trie l'array list de toutes les planetes
- * en ordre de sa valeur de Z pour l'orbite
- *
- * @param pla un ArrayList, avec des objets de type Planete, qui sera trie
- *
- */
- private void triList(){
- Collections.sort(pla, new PlaneteComparator(this));
- }
- /**
- * @void Affichage des planetes
- *
- * cette methode lit l'array list de toutes les planetes
- * deja creees et les dessine grace a un graphics et
- * avec valeurs donnees par l'utilisateur
- *
- * @param g ----text----
- *
- */
- public void paintComponent(Graphics g){
- System.out.println("Panel paint : "+pla.size());
- g.drawImage(img, 0, 0, null);
- g.setColor(new Color(0,200,200));
- triList();
- g.fillOval((int)(camera[0]+cameraTranslation[0]),(int)(camera[1]+cameraTranslation[1]),8,8);
- g.drawImage(deathStar,(int)(this.rotationX[0][0]*200*zoom+camera[0]+cameraTranslation[0]),(int)(this.rotationY[1][1]*200*zoom+camera[1]+cameraTranslation[1]), null);
- g.setColor(new Color(128, 128, 128, 200));
- for(int j = 0; j < gaz.size(); j++){
- GazInterstellaire actuel = gaz.get(j);
- double [] coord = {actuel.getPos().getX(),actuel.getPos().getY(),actuel.getPos().getZ()};
- coord = CoordGraph(coord);
- double xTmp = coord[0], yTmp = coord[1];
- //double xTmp = (actuel.getPos().getX())*zoom+camera[0]+cameraTranslation[0], yTmp = (actuel.getPos().getY())*zoom+camera[1]+cameraTranslation[1];
- g.fillOval((int)(xTmp+cameraTranslation[0]),
- (int)(yTmp+cameraTranslation[1]),
- (int)(actuel.getRayon2()*zoom),
- (int)(actuel.getRayon2()*zoom));
- }
- for(int i = 0; i < pla.size(); i++){
- Planete actuel = pla.get(i);
- //double xTmp = (actuel.getPos().getX())*zoom+camera[0]+cameraTranslation[0], yTmp = (actuel.getPos().getY())*zoom+camera[1]+cameraTranslation[1];
- double [] coord = {actuel.getPos().getX(),actuel.getPos().getY(),actuel.getPos().getZ()};
- coord = CoordGraph(coord);
- double xTmp = coord[0], yTmp = coord[1];
- g.setColor(new Color((int)(actuel.getRouge()),(int)(actuel.getVert()),(int)(actuel.getBleu())));
- g.fillOval((int)(xTmp+cameraTranslation[0]),
- (int)(yTmp+cameraTranslation[1]),
- (int)((actuel.getRayon2()+(actuel.getPos().getZ())/10)*zoom),
- (int)((actuel.getRayon2()+(actuel.getPos().getZ())/10)*zoom));
- }
- Dimension tailleEcran = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
- int hauteur = (int)tailleEcran.getHeight();
- int largeur = (int)tailleEcran.getWidth();
- String textScore = "Score : " + univers.getPoints();
- String textSouris = "X : " + valX + ", Y : " + valY;
- g.setColor(Color.WHITE);
- if(univers.getJeuA()){
- g.drawString(textScore + " " + textSouris, largeur/2-100, 20);
- }else{
- g.drawString(textSouris, largeur/2-50, 20);
- g.drawString("Zoom : "+this.zoom,largeur/2-250, 20);
- }
- g.setColor(Color.WHITE);
- int decalage = 30;
- //if(univers.getJeuA()){
- for(int i=0; i<5; i++){
- String s = this.historique[i];
- if(s.equals("Nouvelle Planète !") || s.equals("Votre vidéo est enregistrée !") || s.equals("Lecture") || s.equals("Lecture de la musique")){
- g.setColor(Color.GREEN); //vert
- }else if(s.equals("La vidéo n'a pas pu être sauvée") || s.equals("En pause") || s.equals("Musique en Pause")){
- g.setColor(Color.RED); //rouge
- }else if(s.equals("Fichier ouvert!")){
- g.setColor(Color.ORANGE); //orange
- }else if(s.equals("Vous jouez à Système Stable !")){
- g.setColor(Color.CYAN);
- }else if(s.equals("Fin du jeu vous avez marqué : ")){
- g.setColor(Color.YELLOW);
- }
- if(!s.equals("Fin du jeu vous avez marqué : ")){
- g.drawString(s, largeur-300, 20 + decalage);
- }else{
- g.drawString(s+univers.getPoints(), largeur-300, 20 + decalage);
- }
- g.setColor(Color.WHITE);
- decalage += 20;
- }
- //}
- g.dispose();
- }
- public double[] sommeCoord(double []coord, double [] cam){
- coord[0] += cam[0];
- coord[1] += cam[1];
- coord[2] += cam[2];
- return coord;
- }
- public double[] sommeCoord2(double []coord, double [] cam){
- coord[0] += -(cam[0]);
- coord[1] += -(cam[1]);
- coord[2] += -(cam[2]);
- return coord;
- }
- public double[] produitCoord(double []coordActu, double [][] coordMatrice){
- coordActu[0] = coordActu[0]*coordMatrice[0][0] + coordActu[1]*coordMatrice[1][0] + coordActu[2]*coordMatrice[2][0];
- coordActu[1] = coordActu[0]*coordMatrice[0][1] + coordActu[1]*coordMatrice[1][1] + coordActu[2]*coordMatrice[2][1];
- coordActu[2] = coordActu[0]*coordMatrice[0][2] + coordActu[1]*coordMatrice[1][2] + coordActu[2]*coordMatrice[2][2];
- return coordActu;
- }
- /*public double[] produitZoom(double coord[], double zoom, boolean inverse){
- if(inverse){
- coord[0] *= zoom;
- coord[1] *= zoom;
- coord[2] *= zoom;
- }else{
- coord[0] /= zoom;
- coord[1] /= zoom;
- coord[2] /= zoom;
- }return coord;
- }*/
- public double[] CoordGraph(double coordM[]){
- double []coordMatrice = produitCoord(coordM,this.rotationX);
- coordMatrice = produitCoord(coordMatrice,this.rotationY);
- coordMatrice = sommeCoord(coordMatrice,this.camera);
- //coordMatrice = produitZoom(coordMatrice,this.zoom,true);
- return coordMatrice;
- }
- public double[] InvCoordGraph(double coordM[]){
- //double[] coordMatrice2 = produitZoom(coordM,this.zoom,false);
- double[] coordMatrice2 = sommeCoord2(coordM,this.camera);
- coordMatrice2 = produitCoord(coordMatrice2,this.InvRotationY);
- coordMatrice2 = produitCoord(coordMatrice2,this.InvRotationX);
- return coordMatrice2;
- }
- /**
- * @void Actualisation
- *
- * actualise la fenetre
- *
- */
- public void actualiserPlanete(){
- repaint();
- }
- /**
- * @void Affichage des planetes
- *
- * cette methode lit l'array list des planetes et
- * les ajoute a la fenetre pour pouvoir etre
- * dessinees plus tard
- *
- * Prend un screenshot de la fenêtre pour en faire un film après
- *
- */
- public void afficher(){
- verrou.lock();
- if(univers.isThereACollision()){
- pla = univers.getPlanetes();
- univers.isCollisionne();
- }
- if(univers.getGaz() != null){
- gaz = univers.getGaz();
- }
- if(univers.getRekt() && compteurScreen == 0){
- image = new BufferedImage(this.getWidth(), this.getHeight(),BufferedImage.TYPE_INT_RGB);
- }
- if(univers.getRekt()){
- compteurScreen++;
- }
- if(compteurScreen == 25 && univers.getRekt()){
- try
- {
- this.paint(image.createGraphics());
- ImageIO.write(image, "BMP",new File("./Capture/"+compteurImages+".bmp"));
- }
- catch(Exception ex)
- {
- ex.printStackTrace();
- }
- compteurImages++;
- compteurScreen = 0;
- }
- verrou.unlock();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement