Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Java - gestione immagini: sovrapporle
Forum - Java - gestione immagini: sovrapporle

Avatar
Il_maca (Ex-Member)
Pro


Messaggi: 163
Iscritto: 28/01/2009

Segnala al moderatore
Postato alle 15:50
Domenica, 10/05/2009
CLASSE MAIN
Codice sorgente - presumibilmente Java

  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Container;
  4. import java.awt.Image;
  5. import java.awt.Toolkit;
  6. import javax.swing.Action;
  7. import java.awt.event.ActionEvent;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10. import javax.swing.AbstractAction;
  11. import javax.swing.JFrame;
  12. import javax.swing.Timer;
  13.  
  14. /*
  15.  * To change this template, choose Tools | Templates
  16.  * and open the template in the editor.
  17.  */
  18.  
  19. /**
  20.  *
  21.  * @author Mariano Campanella
  22.  */
  23. public class NewMain {
  24.     /**
  25.      * @param args the command line arguments
  26.      */
  27.     public static void main(String[] args) {
  28.      JFrame f = new JFrame();
  29.      Container cp = f.getContentPane();
  30.      cp.setBackground (new Color (255, 255, 0));
  31.      cp.setLayout(new BorderLayout());
  32.      Image min = Toolkit.getDefaultToolkit().getImage("minuti.gif");
  33.      Image sec = Toolkit.getDefaultToolkit().getImage("ore.gif");
  34.      final a rotatemin = new a(min);
  35.      final a rotatesec = new a(sec);
  36.  
  37.      Action LanciaEvento=new AbstractAction(){
  38.        public void actionPerformed(ActionEvent ae) {
  39.            SimpleDateFormat minuti = new SimpleDateFormat("mm");
  40.            int a=Integer.parseInt(minuti.format(new Date()));
  41.            SimpleDateFormat secondi = new SimpleDateFormat("ss");
  42.            int b=Integer.parseInt(secondi.format(new Date()));
  43.            rotatemin.rotate(a);
  44.            rotatesec.rotate(b);
  45.  
  46.        }
  47.      };
  48.      //});
  49.      Timer orologio=new Timer(100,LanciaEvento);
  50.      orologio.start();
  51.      cp.add(rotatemin, BorderLayout.CENTER);
  52.      cp.add(rotatesec, BorderLayout.CENTER);
  53.      f.pack();
  54.      f.setVisible(true);
  55.     }
  56.  
  57. }



CLASSE
Codice sorgente - presumibilmente Java

  1. import java.awt.Dimension;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.Image;
  5. import java.awt.MediaTracker;
  6. import java.awt.geom.AffineTransform;
  7. import javax.swing.JPanel;
  8.  
  9. public class a extends JPanel {
  10.    private Image image;
  11.    private double currentAngle;
  12.  
  13.  
  14.     @Override
  15.     public void setOpaque(boolean isOpaque) {
  16.         super.setOpaque(false);
  17.     }
  18.  
  19.  
  20.    public a(Image image) {
  21.      this.image = image;
  22.      MediaTracker mt = new MediaTracker(this);
  23.      mt.addImage(image, 0);
  24.      try {
  25.        mt.waitForID(0);
  26.      }
  27.      catch (Exception e) {
  28.        e.printStackTrace();
  29.      }
  30.    }
  31.  
  32.    public void rotate(int a) {
  33.      currentAngle=a*6.0;
  34.      if (currentAngle >= 360.0) {
  35.        currentAngle = 0;
  36.      }
  37.      repaint();
  38.    }
  39.     @Override
  40.    protected void paintComponent(Graphics g) {
  41.      super.paintComponent(g);
  42.      Graphics2D g2d = (Graphics2D)g;
  43.      AffineTransform origXform = g2d.getTransform();
  44.      AffineTransform newXform = (AffineTransform)(origXform.clone());
  45.      //center of rotation is center of the panel
  46.      int xRot = this.getWidth()/2;
  47.      int yRot = this.getHeight()/2;
  48.      newXform.rotate(Math.toRadians(currentAngle), xRot, yRot);
  49.      g2d.setTransform(newXform);
  50.      
  51.      int x = (getWidth() - image.getWidth(this))/2;
  52.      int y = (getHeight() - image.getHeight(this))/2-60;
  53.  
  54.      g2d.drawImage(image, x, y, this);
  55.      g2d.setTransform(origXform);
  56.    }
  57.  
  58.    public Dimension getPreferredSize() {
  59.      return new Dimension (image.getWidth(this), image.getHeight(this));
  60.    }
  61. }



Come si può ben capire, utilizzo una classe per gestire la rotazione di un'immagine! la rotazione è data moltiplicando l'angolazione per il coefficiente di tempo!
premetto col dire ke il programma non è totalmente mio, anzi, ho preso spunto da questa pagina:
http://www.rgagnon.com/javadetails/java-0248.html
ora il mio problema all'esecuzione è che non riesco a vedere entrambe le lancette, ma solo una! in particolare si vede solo quella dei secondi! ho paura ke il problema sia nell'uso dei panel, ke se non sbaglio non sono sovrapponibili!!!
kiedo se sapreste come agire o come correggere questo codice!!
please no-applet!! voglio provare a crearlo senza l'uso dell'applet!! grazie in anticipo

Ultima modifica effettuata da Il_maca il 10/05/2009 alle 15:52
PM Quote
Avatar
Il_maca (Ex-Member)
Pro


Messaggi: 163
Iscritto: 28/01/2009

Segnala al moderatore
Postato alle 19:30
Domenica, 10/05/2009
Codice sorgente - presumibilmente Java

  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Container;
  4. import java.awt.Image;
  5. import java.awt.Toolkit;
  6. import javax.swing.Action;
  7. import java.awt.event.ActionEvent;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10. import javax.swing.AbstractAction;
  11. import javax.swing.JFrame;
  12. import javax.swing.Timer;
  13.  
  14. /*
  15.  * To change this template, choose Tools | Templates
  16.  * and open the template in the editor.
  17.  */
  18.  
  19. /**
  20.  *
  21.  * @author Mariano Campanella
  22.  */
  23. public class NewMain {
  24.     /**
  25.      * @param args the command line arguments
  26.      */
  27.     public static void main(String[] args) {
  28.      JFrame f = new JFrame();
  29.      Container cp = f.getContentPane();
  30.      cp.setBackground (new Color (255, 255, 0));
  31.      cp.setLayout(new BorderLayout());
  32.      Image min = Toolkit.getDefaultToolkit().getImage("minuti.gif");
  33.      Image ore = Toolkit.getDefaultToolkit().getImage("ore.gif");
  34.      final a imma = new a(min,ore);
  35.  
  36.      Action LanciaEvento=new AbstractAction(){
  37.        public void actionPerformed(ActionEvent ae) {
  38.            SimpleDateFormat minuti = new SimpleDateFormat("mm");
  39.            int a=Integer.parseInt(minuti.format(new Date()));
  40.            SimpleDateFormat ore = new SimpleDateFormat("ss");
  41.            int b=Integer.parseInt(ore.format(new Date()));
  42.            imma.rotate(a);
  43.            imma.rotate2(b);
  44.        }
  45.      };
  46.      Timer orologio=new Timer(100,LanciaEvento);
  47.      orologio.start();
  48.      cp.add(imma, BorderLayout.CENTER);
  49.      
  50.      f.pack();
  51.      f.setVisible(true);
  52.     }
  53.  
  54. }



Codice sorgente - presumibilmente Java

  1. import java.awt.Dimension;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.Image;
  5. import java.awt.MediaTracker;
  6. import java.awt.geom.AffineTransform;
  7. import javax.swing.JPanel;
  8.  
  9. public class a extends JPanel {
  10.    private Image minuti;
  11.    private Image ore;
  12.    private double currentAngle;
  13.    private double currentAngle2;
  14.  
  15.     @Override
  16.     public void setOpaque(boolean isOpaque) {
  17.     super.setOpaque(false);
  18.     }
  19.  
  20.    public a(Image minuti,Image ore) {
  21.      this.minuti = minuti;
  22.      this.ore = ore;
  23.      MediaTracker mt = new MediaTracker(this);
  24.      mt.addImage(minuti, 0);
  25.      mt.addImage(ore, 0);
  26.      try {
  27.        mt.waitForID(0);
  28.      }
  29.      catch (Exception e) {
  30.        e.printStackTrace();
  31.      }
  32.  
  33.  
  34.    }
  35.  
  36.    public void rotate(int a) {
  37.      //rotate 5 degrees at a time
  38.      currentAngle=a*6.0;
  39.      if (currentAngle >= 360.0) {
  40.        currentAngle = 0;
  41.      }
  42.      repaint();
  43.    }
  44.    public void rotate2(int a) {
  45.      //rotate 5 degrees at a time
  46.      currentAngle2=a*6.0;
  47.      if (currentAngle2 >= 360.0) {
  48.        currentAngle2 = 0;
  49.      }
  50.      repaint();
  51.    }
  52.  
  53.     @Override
  54.    protected void paintComponent(Graphics g) {
  55.      super.paintComponent(g);
  56.      Graphics2D g2d = (Graphics2D)g;
  57.  
  58.      AffineTransform origXform = g2d.getTransform();
  59.      AffineTransform newXform = (AffineTransform)(origXform.clone());
  60.  
  61.      //Graphics2D g2d2 = (Graphics2D)g;
  62.      AffineTransform origYform = g2d.getTransform();
  63.      AffineTransform newYform = (AffineTransform)(origYform.clone());
  64.  
  65.  
  66.       //center of rotation is center of the panel
  67.      int xRot = this.getWidth()/2;
  68.      int yRot = this.getHeight()/2;
  69.  
  70.      newXform.rotate(Math.toRadians(currentAngle2), xRot, yRot);
  71.      g2d.setTransform(newXform);
  72.  
  73.      newYform.rotate(Math.toRadians(currentAngle2), xRot, yRot);
  74.      g2d.setTransform(newYform);
  75.  
  76.      //draw image centered in panel
  77.      int x = (getWidth() - minuti.getWidth(this))/2;
  78.      int y = (getHeight() - minuti.getHeight(this))/2-60;
  79.      g2d.drawImage(minuti, x, y, this);  
  80.      g2d.setTransform(origXform);
  81.  
  82.      int a = (getWidth() - ore.getWidth(this))/2;
  83.      int b = (getHeight() - ore.getHeight(this))/2 - 37;
  84.      g2d.drawImage(ore, a, b, this);
  85.      g2d.setTransform(origYform);
  86.    }
  87. }



Questa è la situazione attuale! mi scuso per aver cambiato i nomi di alcune variabili, ma ho notato ke urgeva ordine!!ok allora al momento il prog mi visualizza le due lancette, ma si muove solo quella dei minuti, che però funziona a secondi!!
perkè??sapete dirmi qualcosa??
non ci sto capendo + nulla

P.S. nel timer, all'immagine ore, è associato il simpledateformat per i secondi! non è un errore l'ho fatto per vedere istantaneamente se gira!!

Ultima modifica effettuata da Il_maca il 11/05/2009 alle 18:00
PM Quote