Visualizzazione dei risultati da 1 a 10 su 12

Visualizzazione discussione

  1. #3
    Quote Originariamente inviata da andbin Visualizza il messaggio
    Devi precisare quale release di Java intendi usare. La Scripting API (JSR 223) è stata introdotta nel JDK 6, insieme al engine per Javascript chiamato Rhino, poi dal JDK 8 sostituito dal Nashorn. Il Nashorn è stato tenuto nel runtime Java per diverse versioni. Nel JDK 11 il Nashorn è stato marcato come "deprecato" (ma ancora usabile). Nel JDK 15 però il Nashorn è stato definitivamente rimosso (release note: Removal of Nashorn JavaScript Engine).

    In sostanza, basta guardare quali engine sono disponibili usando il getEngineFactories() di ScriptEngineManager.
    A partire dal JDK 15 NON ci sono engine predefiniti (ovvero getEngineFactories() dà 0 elementi).

    Quindi dipende da COSA devi fare e perché devi usare un engine Javascript.
    Ciao @andbin uso l' open-jdk-17
    Non ve ne sono di predefiniti.

    Mi serve per calcolare i valori di X, Y, Z per visualizzare grafici 3D.

    Condivido il codice:

    codice:
        
        import java.awt.*;    
        import java.awt.Graphics;
        import java.awt.Image;
        import javax.swing.*;
        import javax.imageio.ImageIO;
        import java.awt.image.BufferedImage;
        import java.io.File;
        import java.io.FileNotFoundException;
        import java.awt.event.*;
        import javax.script.ScriptEngineManager;
        import javax.script.ScriptEngine;    
        import javax.script.ScriptException;
        import javax.script.SimpleBindings;
        import static java.lang.Math.*;
          
        public class Draw extends JFrame {
        
        
           public static void main(String [] args) {                           
                    
                    SwingUtilities.invokeLater(() -> new Draw().setVisible(true));
                                     
    
           }  
          
           
           public Draw(){
           
            setSize(800, 800);                  
                                        
            setVisible(true); 
            
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                            
            add(new Pannello());
    
                    
           } 
           
     
       }
       
      
       //Classe Pedina è l'elemento che si sposta nelle 3 Dimensioni a scatti.    
       class Pedina{ 
           
           //Valori di riferimento relativi al poligono(vedere immagine assonometria-isometrica)
         private int LX = 52;
            private int LY = 52;                                   
            private int LZ = 60;
            
            private double X; private double Y; private double Z;
            
            private String Name = "";
           
           private Color Col;
           
           private Polygon p;
           
           private Point[] P = new Point[4];
           private Point[] A = new Point[4];
           
           String getName() {
               return Name;
        }
        
        double getX() {
               return X;
        }
        double getY() {
               return Y;
        }
        double getZ() {
               return Z;
        }
        Point[] getP() {
               return P;
        }
        //Serve a centrare il poligono
        Point getC() {
               return new Point((P[0].x+P[2].x)/2,(P[0].y+P[2].y)/2);
        }
            
        void setX(double X) {
               this.X = X;
        }
         void setY(double Y) {
               this.Y = Y;
        }
        void setZ(double Z) {
               this.Z = Z;
        }
        
        void setXYZ(double X, double Y, double Z) {
            this.X = X;
            this.Y = Y;
            this.Z = Z;
        }
    
    
        void Scala(int n){
        
                for(int k=0; k<n; k++){
            
                A[1].x=A[0].x/2+A[1].x/2;
                   A[1].y=A[0].y/2+A[1].y/2;               
                   
                   A[3].x=A[0].x/2+A[3].x/2;
                   A[3].y=A[0].y/2+A[3].y/2;
                   
                   A[2].x=A[0].x/2+A[2].x/2;
                   A[2].y=A[0].y/2+A[2].y/2;
               
                   
                   }
            
        }
           
           Pedina(String name, double X, double Y, double Z, Color col){
           
                         
                   //I punti A[] sono gli estremi del parallelogramma(poligono/pedina) 
                   //rappresentato
                       
                     A[0] = new Point(222,250);
                   A[1] = new Point(275,275);
                   A[2] = new Point(222,300);
                   A[3] = new Point(170,275);                                
                   
                   Scala(4); //Scala la pedina di n=4(in tal caso) fattori
                          
                   setX(X); setY(Y); setZ(Z);
    
                    
                P[0] = new Point((int)(A[0].x-LX*(X-1)+(Y-1)*LX),
                         (int)(A[0].y+(X-1)*LY/2+LY/2*(Y-1)-(Z-1)*LZ));  
                         
                    P[1] = new Point((int)(A[1].x-LX*(X-1)+(Y-1)*LX),
                         (int)(A[1].y+(X-1)*LY/2+LY/2*(Y-1)-(Z-1)*LZ));  
                             
                    P[2] = new Point((int)(A[2].x-LX*(X-1)+(Y-1)*LX),
                         (int)(A[2].y+(X-1)*LY/2+LY/2*(Y-1)-(Z-1)*LZ)); 
                             
                      P[3] = new Point((int)(A[3].x-LX*(X-1)+(Y-1)*LX),
                         (int)(A[3].y+(X-1)*LY/2+LY/2*(Y-1)-(Z-1)*LZ));  
                             
                    p = new Polygon();
                    
                    
                    p.addPoint(P[0].x,P[0].y);   
                    p.addPoint(P[1].x,P[1].y); 
                    p.addPoint(P[2].x,P[2].y); 
                    p.addPoint(P[3].x,P[3].y); 
                    
                    Col = col;
                    Name = name;
                    
        }      
        
        //Funzione che cambia la posizione della pedina:
        
        void changePosition(){
        
            
                P[0] = new Point((int)(A[0].x-LX*(X-1)+(Y-1)*LX),
                         (int)(A[0].y+(X-1)*LY/2+LY/2*(Y-1)-(Z-1)*LZ));  
                         
                    P[1] = new Point((int)(A[1].x-LX*(X-1)+(Y-1)*LX),
                         (int)(A[1].y+(X-1)*LY/2+LY/2*(Y-1)-(Z-1)*LZ));  
                             
                    P[2] = new Point((int)(A[2].x-LX*(X-1)+(Y-1)*LX),
                         (int)(A[2].y+(X-1)*LY/2+LY/2*(Y-1)-(Z-1)*LZ)); 
                             
                      P[3] = new Point((int)(A[3].x-LX*(X-1)+(Y-1)*LX),
                         (int)(A[3].y+(X-1)*LY/2+LY/2*(Y-1)-(Z-1)*LZ));  
                        
                    p = new Polygon();
                    
                    p.addPoint(P[0].x,P[0].y);   
                    p.addPoint(P[1].x,P[1].y); 
                    p.addPoint(P[2].x,P[2].y); 
                    p.addPoint(P[3].x,P[3].y);
        }
        
        //Funzioni per il recupero del poligono/colore
        public Polygon getPXYZ(){return p; }
        public Color getCol(){return Col; }
           
       }
       
       class Pannello extends JPanel {    
       
       
           String fileName;
           BufferedImage imageFile;
           BufferedImage image;
           File file;
           
        Pedina Pedina;
       
        JTextField TX;
        JTextField TY;
        JTextField TZ;
        
        JButton B;
           
        double X0 = 1.0; double Y0 = 1.0; double Z0 = 1.0;    
         
       
            Pannello(){
            
                JLabel     LX = new JLabel("X:");
                            LX.setVisible(true);
                            LX.setLocation(400,500);    
                            LX.setSize(100,25);
                            
                          TX = new JTextField("1.0");
                            TX.setVisible(true);
                            TX.setLocation(500,500);    
                            TX.setSize(100,25);
    
            JLabel     LY = new JLabel("Y:");   
                        LY.setVisible(true);
                            LY.setLocation(400,535);    
                            LY.setSize(100,25);    
                                            
                         TY = new JTextField("1.0");
                            TY.setVisible(true);
                            TY.setLocation(500,535);    
                            TY.setSize(100,25);
    
            JLabel     LZ = new JLabel("Z:");    
                        LZ.setVisible(true);
                            LZ.setLocation(400,570);    
                            LZ.setSize(100,25);
                                                
                         TZ = new JTextField("1.0");
                            TZ.setVisible(true);
                            TZ.setLocation(500,570);    
                            TZ.setSize(100,25);     
                            
                    B = new JButton("Imposta");
                            B.setVisible(true);
                            B.setLocation(500,590);    
                            B.setSize(100,25);           
                
                this.setLayout(null);       
                
                this.add(LX);
                this.add(LY);
                this.add(LZ);
                
                this.add(TX);
                this.add(TY);
                this.add(TZ); 
    
                                                  
                B.addActionListener(new load_XYZ());                       
                
                this.add(B);
                
                Pedina = new Pedina("Pedina", X0,Y0,Z0, new Color(0,0,255));
                
                //Percorso File immagine Isometrica(allegata)              
            fileName = "/home/cartella/img/ass-isometrica.png";
            
            imageFile = new BufferedImage(800,800,BufferedImage.TYPE_INT_RGB);
            
            file = new File(fileName);
            
            try {                         
            
                imageFile = ImageIO.read(file);                                
                
            
            } catch(Exception e) {
                System.out.println(e);
            }
                                       
                       
            
            }
            
            private class load_XYZ implements ActionListener {
                
            public void actionPerformed(ActionEvent e) { 
            
                X0 = Double.parseDouble(TX.getText());    
                Y0 = Double.parseDouble(TY.getText());
                Z0 = Double.parseDouble(TZ.getText());
                
                Pedina.setXYZ(X0,Y0,Z0);
                Pedina.changePosition();        
                    
                repaint();                    
                                                                                                            
            }
                
          }
          
          public double eval(String str) throws Exception{
              ScriptEngineManager sem = new ScriptEngineManager();
                ScriptEngine engine = sem.getEngineByName("JavaScript"); 
                System.out.println(sem.getEngineFactories()); 
                return (double)engine.eval("5+3");         
        }
     
       
            @Override
            protected void paintComponent(Graphics g)  {
            
                super.paintComponent(g);                                    
                                                                
            image = imageFile;                    
                        
            g.drawImage(image, 0, 0, null);            
            
            System.out.println("OK");
            
            double k=0.0628;
            double j=0.0628;
            int n=0; 
            int m=0;        
            
            double r=0.25; double R=1;
            
    
    
            Color colore = new Color(0,0,0);
            
            /*
            try {
            eval("4+2");
            }
            catch(Exception e){};*/
    
            
            for(double v=0; v<=6.28; v+=j){    
                                
                                            
                for(double u=0; u<=6.28; u+=k){
                
                                                                                                                                                            
                    //Coordinate parametriche per rappresentare il toro:                
                    X0 = (R+r*cos(v))*cos(u);
                    Y0 = (R+r*cos(v))*sin(u);
                    Z0 = r*sin(v);
                 
                                    
                                                                    
                        Pedina.setXYZ(X0,Y0,Z0);
                        
                        Pedina.changePosition();  
                                       
                        //*Esempio di variazione del colore(da migliorare):
                        colore = new Color((int)(n%200),50,0);
                                            
                        g.setColor(colore);    
                                                               
                        n++;  
                                          
                        g.fillPolygon(Pedina.getPXYZ()); 
                        
                        
                                                                         
                         
                    }                                   
                    
                    
                    m++;
                    
                  
                        
                }
                
                
                
            }
                    
    
            
        }
    Ultima modifica di jabjoint; 15-09-2022 a 11:55
    jabjoint

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.