IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Applets Java Discussion :

problem avec mon applet


Sujet :

Applets Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Futur Membre du Club
    Inscrit en
    Février 2008
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Février 2008
    Messages : 3
    Par défaut problem avec mon applet
    bonjour mon fichier se nomme AnimeBall.java

    et je n'arrive pas a le faire marcher , je ne l'ai meme pas encore implementer dans une page html. Je n'arrive pas a trouver la solution pouvez vous m'aider svp ?

    mon programme simule une balle rouge qui rebondi dans tous les sens et on peu choisir si l'on veut voir son cheminement ou pas.

    merci d'avance




    import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    import javax.swing.JApplet;

    public class AnimeBall extends JApplet {
    Motivator fullMover;
    Motivator fadeMover;

    public void init() {
    getContentPane().add(getContent());
    }

    public void start() {
    fullMover.start();
    fadeMover.start();
    }

    public void stop() {
    fullMover.stop();
    fadeMover.stop();
    }

    private JTabbedPane getContent() {
    FullTrail fullTrail = new FullTrail();
    fullMover = new Motivator(fullTrail);
    FadingTrail fader = new FadingTrail();
    fadeMover = new Motivator(fader);
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("full trail", fullTrail);
    tabbedPane.addTab("fading trail", fader);
    return tabbedPane;
    }
    }


    public class AnimeBall {
    Motivator fullMover;
    Motivator fadeMover;

    private void start() {
    fullMover.start();
    fadeMover.start();
    }

    private JTabbedPane getContent() {
    FullTrail fullTrail = new FullTrail();
    fullMover = new Motivator(fullTrail);
    FadingTrail fader = new FadingTrail();
    fadeMover = new Motivator(fader);
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("full trail", fullTrail);
    tabbedPane.addTab("fading trail", fader);
    return tabbedPane;
    }

    public static void main(String[] args) {
    AnimeBall trails = new AnimeBall();
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(trails.getContent());
    f.setSize(400,400);
    f.setLocation(200,200);
    f.setVisible(true);
    trails.start();
    }
    }
    class FullTrail extends JPanel implements Movable {
    BufferedImage offscreen;
    Point loc;
    int dx = 3;
    int dy = 2;
    final int D = 30;

    protected void paintComponent(Graphics g) {
    if(offscreen == null) {
    initOffscreen();
    }
    g.drawImage(offscreen, 0, 0, this);
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setPaint(Color.red);
    g2.fillOval(loc.x-D/2, loc.y-D/2, D, D);
    }

    public void moveBall() {
    updateOffscreen();
    checkBoundries();
    loc.x += dx;
    loc.y += dy;
    repaint();
    }

    private void checkBoundries() {
    if(loc.x - D/2 + dx < 0 || loc.x + D/2 + dx > getWidth())
    dx *= -1;
    if(loc.y - D/2 + dy < 0 || loc.y + D/2 + dy > getHeight())
    dy *= -1;
    }

    private void initOffscreen() {
    int w = getWidth();
    int h = getHeight();
    loc = new Point(w/2, h/2);
    offscreen = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = offscreen.createGraphics();
    g2.setColor(UIManager.getColor("Panel.background"));
    g2.fillRect(0,0,w,h);
    g2.dispose();
    }

    private void updateOffscreen() {
    Graphics g = offscreen.getGraphics();
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setPaint(new Color(240,200,240));
    g2.fillOval(loc.x-2, loc.y-2, 4, 4);
    g2.dispose();
    }
    }

    class FadingTrail extends JPanel implements Movable {
    Point[] trail;
    Point loc;
    int dx = 2;
    int dy = 3;
    final int D = 30;

    public FadingTrail() {
    int trailLength = 20;
    trail = new Point[trailLength];
    for(int j = 0; j < trail.length; j++) {
    trail[j] = new Point();
    }
    loc = new Point(200,100);
    }

    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);
    Graphics2D copy = (Graphics2D)g.create();
    copy.setPaint(new Color(240,200,240));
    float alpha = 1f;
    float alphaInc = 1f/20.5f;
    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
    for(int j = 0; j < trail.length; j++) {
    copy.setComposite(ac);
    copy.fillOval(trail[j].x-2, trail[j].y-2, 4, 4);
    alpha -= alphaInc;
    ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
    }
    copy.dispose();
    g2.setPaint(Color.red);
    g2.fillOval(loc.x-D/2, loc.y-D/2, D, D);
    }

    public void moveBall() {
    updateTrail();
    checkBoundries();
    loc.x += dx;
    loc.y += dy;
    repaint();
    }

    private void checkBoundries() {
    if(loc.x - D/2 + dx < 0 || loc.x + D/2 + dx > getWidth())
    dx *= -1;
    if(loc.y - D/2 + dy < 0 || loc.y + D/2 + dy > getHeight())
    dy *= -1;
    }

    private void updateTrail() {
    for(int j = trail.length-1; j > 0; j--) {
    trail[j].setLocation(trail[j-1].getLocation());
    }
    double theta = Math.atan2(dy,dx) + Math.PI;
    int x = loc.x + (int)((D/2)*Math.cos(theta));
    int y = loc.y + (int)((D/2)*Math.sin(theta));
    trail[0].setLocation(x, y);
    }
    }

    interface Movable {
    public void moveBall();
    }

    class Motivator implements Runnable {
    Movable movable;
    Thread thread;
    boolean keepMoving;

    public Motivator(Movable m) {
    movable = m;
    }

    public void run() {
    while(keepMoving) {
    try {
    Thread.sleep(50);
    } catch(InterruptedException e) {
    keepMoving = false;
    }
    movable.moveBall();
    }
    }

    public void start() {
    if(!keepMoving) {
    keepMoving = true;
    thread = new Thread(this);
    thread.setPriority(Thread.NORM_PRIORITY);
    thread.start();
    }
    }

    public void stop() {
    keepMoving = false;
    thread.interrupt();
    thread = null;
    }
    }

  2. #2
    Membre Expert
    Avatar de krachik
    Inscrit en
    Décembre 2004
    Messages
    1 964
    Détails du profil
    Informations forums :
    Inscription : Décembre 2004
    Messages : 1 964
    Par défaut
    Bonjour
    Utilises les balises CODE(bouton #) dans l'editeur pour encadrert ton code parce que c'est pas tres lisible comme ça
    bonjour mon fichier se nomme AnimeBall.java
    et je n'arrive pas a le faire marcher , je ne l'ai meme pas encore implementer dans une page html. Je n'arrive pas a trouver la solution pouvez vous m'aider svp ?
    mon programme simule une balle rouge qui rebondi dans tous les sens et on peu choisir si l'on veut voir son cheminement ou pas.
    merci d'avance
    Tu nous dis ce que fait ton programme mais tu ne nous dit pas ce que tu rencontres comme probleme . On ne va pas etre obligé de lire tout le code ou le compiler pour detecter le probleme.
    Alors qu'est ce qui ne marche pas ?
    @+

Discussions similaires

  1. Probleme avec mon algorithme de tri
    Par kaygee dans le forum Langage
    Réponses: 6
    Dernier message: 09/01/2006, 21h23
  2. Probleme avec mon professeur
    Par Hamza dans le forum Algorithmes et structures de données
    Réponses: 20
    Dernier message: 16/07/2005, 10h50
  3. [Classloader]Probleme avec une applet
    Par punx120 dans le forum Applets
    Réponses: 3
    Dernier message: 04/06/2005, 18h43
  4. [Thread] Probleme avec mon Timer
    Par Nico66 dans le forum EDT/SwingWorker
    Réponses: 10
    Dernier message: 02/06/2005, 17h10

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo