2014-04-25 3 views
0

Mon animation gif scintille trop. J'ai entendu parler de la double mise en mémoire tampon qui peut aider, mais comment puis-je faire cela à mon animation gif? Ou y a-t-il un meilleur raccourci plus rapide. Ceci est juste une petite chose d'applet de test que je fais pour le plaisir, mais mettra en œuvre les leçons en classe.animation gif scintillant dans l'applet

Contexte:

import java.net.*; 
import java.applet.Applet; 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URL; 

import javax.imageio.ImageIO; 
import javax.swing.*; 
public class HiroshimaBlock extends Applet implements ActionListener { 

TextField distanceText = new TextField(10); 
TextField accelerationText = new TextField(10); 
Button security = new Button("Account Manager"); 
Button launch = new Button("LAUNCH!"); 
Button Reportl = new Button("Report Logs"); 
Image dancer; 
URL base; 
MediaTracker mt; 
Timer tm = new Timer(10, this); 

TextArea answers = new TextArea("I am ready for your first trip.", 4, 20, 
     TextArea.SCROLLBARS_NONE); 

Image image; 

@Override 
public void init() { 
    setSize(550, 500); 
    // Some messages for the top of the Applet: 
    addHorizontalLine(Color.orange); 

    addNewLine(); 
    // JOptionPane.showMessageDialog(null, "HiroshimaBlock", 
    // "Welcome to HiroshimaBlock", JOptionPane.PLAIN_MESSAGE); 
    // The two text fields and the launch button: 
    Frame c = (Frame) this.getParent().getParent(); 
    c.setTitle("HiroshimaBlock"); 

    mt = new MediaTracker(this); 
    try { 
     base = getDocumentBase(); 
    } catch (Exception e) { 
    } 
    dancer = getImage(base, "dancer1.gif"); 
    mt.addImage(dancer, 9); 
    try { 
     mt.waitForAll(); 
    } catch (InterruptedException e) { 
    } 

    loadImage(); 
    // add(distanceText); 
    // add(new Label("Distance of trip in light years")); 
    addNewLine(); 
    addNewLine(); 
    // add(accelerationText); 
    // add(new Label("Acceleration of rocket in g's")); 
    addNewLine(); 
    add(launch); 
    addNewLine(); 
    add(security); 
    addNewLine(); 
    add(Reportl); 

    // A text area for printing the answers: 
    // answers.setEditable(false); 
    // add(answers); 
    addNewLine(); 
    addNewLine(); 
    addHorizontalLine(Color.orange); 

} 

public void loadImage() { 

    URL url = getClass().getResource("hsblock.png"); 
    image = getToolkit().getImage(url); 
} 

public void paint(Graphics g) { 
    g.drawImage(image, 20, 20, this); 

    this.security.setLocation(25, 200); 
    addNewLine(); 
    this.launch.setLocation(25, 230); 
    this.Reportl.setLocation(25, 260); 
    this.security.setSize(100, 25); 
    this.launch.setSize(100, 25); 
    this.Reportl.setSize(100, 25); 

    g.drawImage(dancer, 150, 200, this); 
    tm.start(); 
} 

private void addHorizontalLine(Color c) { 
    // Add a Canvas 10000 pixels wide but only 1 pixel high, which acts as 
    // a horizontal line to separate one group of components from the next. 
    Canvas line = new Canvas(); 
    line.setSize(10000, 1); 
    line.setBackground(c); 

    add(line); 
} 

private void addNewLine() { 
    // Add a horizontal line in the background color. The line itself is 
    // invisible, but it serves to force the next Component onto a new line. 
    addHorizontalLine(getBackground()); 
} 

@Override 
public void actionPerformed(ActionEvent arg0) { 
    // TODO Auto-generated method stub 

} 

    } 
+0

1) Pourquoi le code d'un applet? Si cela est dû à spec. Par l'enseignant, veuillez les référer à [Pourquoi les enseignants CS devraient cesser d'enseigner des applets Java] (http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Pourquoi AWT plutôt que Swing? Voir ma réponse sur [Swing extras sur AWT] (http://stackoverflow.com/a/6255978/418556) pour de nombreuses bonnes raisons d'abandonner l'utilisation des composants AWT. –

+0

@Andrew bien im un débutant Je suppose que l'utilisation d'applets est amusant LOLOL Merci pour le lien btw intéressant lire. –

Répondre

0

Eh bien LOL je l'ai fait arrêter vacillante en utilisant la mise à jour LOLOLOL

import java.applet.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Test extends Applet { 

Image img; 

public void init() { 
    setSize(700, 700); 

    img = getImage(getDocumentBase(), "dancer1.gif"); 

} 

public void update(Graphics g) { 
    g.drawImage(img, 140, 200, this); 
} 

public void paint(Graphics g) { 
    update(g); 
} 

} 
+0

yall ne vous aide pas beaucoup à faire. doit être autosuffisant yooohooo! –

1

Vous voulez:

  • Créer une classe de dessin qui s'étend JPanel (ou JComponent)
  • substituez la méthode paintComponent(Graphics g) de votre classe.
  • Appelez d'abord la méthode du super dans cette méthode.
  • Puis faites votre dessin d'animation avec cette méthode. Cela vous donnera la double mise en mémoire tampon automatique de Swing qui devrait vous aider à lisser votre animation.
  • Affichez votre dessin JPanelez votre JApplet en l'ajoutant au contentPane de l'applet.

Par exemple:

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Image; 
import java.awt.RenderingHints; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import java.lang.reflect.InvocationTargetException; 
import java.nio.Buffer; 

import javax.swing.*; 

public class SimpleAnimation extends JApplet { 
    @Override 
    public void init() { 
     try { 
     SwingUtilities.invokeAndWait(new Runnable() { 
      public void run() { 
       DrawPanel drawPanel = new DrawPanel(); 
       getContentPane().add(drawPanel); 
      } 
     }); 
     } catch (InvocationTargetException | InterruptedException e) { 
     e.printStackTrace(); 
     } 
    } 
} 

class DrawPanel extends JPanel { 
    private static final int I_WIDTH = 20; 
    private static final int I_HEIGHT = 20; 
    private static final int TIMER_DELAY = 15; 
    private int x = 0; 
    private int y = 0; 
    private BufferedImage img = new BufferedImage(I_WIDTH, I_HEIGHT, BufferedImage.TYPE_INT_ARGB); 

    public DrawPanel() { 
     Graphics2D g2 = img.createGraphics(); 
     g2.setColor(Color.red); 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
      RenderingHints.VALUE_ANTIALIAS_ON); 
     g2.fillOval(1, 1, I_WIDTH - 2, I_HEIGHT - 2); 
     g2.dispose(); 

     new Timer(TIMER_DELAY, new TimerListener()).start(); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     if (img != null) { 
     g.drawImage(img, x, y, this); 
     } 
    } 

    private class TimerListener implements ActionListener { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
     x++; 
     y++; 
     repaint(); 
     } 
    } 
} 
+0

maintenant mes deux images n'apparaîtront pas –