Animation continue à l'aide d'applets java


Je veux créer une animation continue d'un camion de déménagement dans des applets java. Dans ce le camion devrait réapparaître de l'autre côté dès qu'il disparaît. J'ai essayé d'utiliser un camion, mais il apparaît de l'autre côté seulement après la disparition d'un côté. Donc, j'ai utilisé deux.

Voici mon code:

public class prc1 extends Applet implements Runnable{
Thread t;
int x=0;
int y=0;
int i;
public void start(){
    t = new Thread(this);
    t.start();
}
public void paint(Graphics g){
    g.setColor(Color.gray);
    g.fillRect(x,10,70,45);
    g.setColor(Color.yellow);
    g.fillRoundRect(50+x,15,35,35,10,10);
    g.setColor(Color.black);
    g.fillArc(5+x,50,10,10,0,360);
    g.fillArc(55+x,50,10,10,0,360);

    g.setColor(Color.gray);
    g.fillRect(y,10,70,45);
    g.setColor(Color.yellow);
    g.fillRoundRect(50+y,15,35,35,10,10);
    g.setColor(Color.black);
    g.fillArc(5+y,50,10,10,0,360);
    g.fillArc(55+y,50,10,10,0,360);
}
public void run(){
    try{
        y=-90;
        x=0;    
        for(; ;){
            Thread.sleep(100);
            repaint();
            run();
            x=x+5;
            if(x==400){
                for(; ;){
                    y=y+5;
                    Thread.sleep(100);
                    repaint();
                    run();
                    x=x-5;
                    if(x==0){
                        y=-90;      
                        break;
                    }
                }
            }

        }
    }catch(InterruptedException e){ }
  }
}

Maintenant, le problème est que le camion ne bouge pas du tout.

Author: user3382203, 2016-04-13

1 answers

Donc, l'idée de base est, vous devez déterminer quand le camion commence à quitter la zone visible, puis calculer la quantité de "débordement", ce qui vous permettrait alors de peindre le camion une deuxième fois à une nouvelle position soustraite du débordement.

Je vous ai cassé le code, donc le Truck est sa propre classe, cela le rend plus facile à gérer et permet un peu plus de contrôle sur le processus de peinture.

Camion

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private Truck truck;

        public TestPane() {
            truck = new Truck();
            Timer timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    truck.update(getSize());
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            truck.paint(g2d);
            g2d.dispose();
        }

    }

    public class Truck {

        private int x, y;
        // I had to calculate these
        private int width = 85;
        private int height = 65;

        private int xDelta = 4;

        private int xOverFlow = 0;

        public void update(Dimension bounds) {
            x += xDelta;
            if (x > bounds.width) {
                x = 0;
            }

            if (x + width > bounds.width) {
                xOverFlow = (x + width) - bounds.width;
            } else {
                xOverFlow = -1;
            }
        }

        public void paint(Graphics2D g) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.translate(x, y);
            paintTruck(g2d);
            g2d.dispose();

            if (xOverFlow > 0) {
                g2d = (Graphics2D) g.create();
                g2d.translate(xOverFlow - width, y);
                paintTruck(g2d);
                g2d.dispose();
            }
        }

        protected void paintTruck(Graphics2D g2d) {
            g2d.setColor(Color.gray);
            g2d.fillRect(0, 10, 70, 45);
            g2d.setColor(Color.yellow);
            g2d.fillRoundRect(50, 15, 35, 35, 10, 10);
            g2d.setColor(Color.black);
            g2d.fillArc(5, 50, 10, 10, 0, 360);
            g2d.fillArc(55, 50, 10, 10, 0, 360);

            g2d.setColor(Color.gray);
            g2d.fillRect(0, 10, 70, 45);
            g2d.setColor(Color.yellow);
            g2d.fillRoundRect(50, 15, 35, 35, 10, 10);
            g2d.setColor(Color.black);
            g2d.fillArc(5, 50, 10, 10, 0, 360);
            g2d.fillArc(55, 50, 10, 10, 0, 360);
        }
    }

}

Une autre option pourrait être d'avoir plus qu'une instance de truck, alors que la seconde commence à quitter le visible, vous pouvez en créer une autre

Par exemple...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private List<Truck> trucks;

        public TestPane() {
            trucks = new ArrayList<>(2);
            trucks.add(new Truck());
            Timer timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Truck[] truckArray = trucks.toArray(new Truck[trucks.size()]);
                    for (Truck truck : truckArray) {
                        truck.update(getSize());
                        if (truck.getX() > getWidth()) {
                            trucks.remove(truck);
                        } else if (truck.getX() + truck.getWidth() > getWidth() && trucks.size() == 1) {
                            trucks.add(new Truck());
                        }
                    }
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            for (Truck truck : trucks) {
                truck.paint(g2d);
            }
            g2d.dispose();
        }

    }

    public class Truck {

        // I had to calculate these
        private int width = 85;
        private int height = 65;

        private int x = -width;
        private int y;

        private int xDelta = 4;

        public void update(Dimension bounds) {
            x += xDelta;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        public int getWidth() {
            return width;
        }

        public int getHeight() {
            return height;
        }

        public void paint(Graphics2D g) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.translate(x, y);
            paintTruck(g2d);
            g2d.dispose();
        }

        protected void paintTruck(Graphics2D g2d) {
            g2d.setColor(Color.gray);
            g2d.fillRect(0, 10, 70, 45);
            g2d.setColor(Color.yellow);
            g2d.fillRoundRect(50, 15, 35, 35, 10, 10);
            g2d.setColor(Color.black);
            g2d.fillArc(5, 50, 10, 10, 0, 360);
            g2d.fillArc(55, 50, 10, 10, 0, 360);

            g2d.setColor(Color.gray);
            g2d.fillRect(0, 10, 70, 45);
            g2d.setColor(Color.yellow);
            g2d.fillRoundRect(50, 15, 35, 35, 10, 10);
            g2d.setColor(Color.black);
            g2d.fillArc(5, 50, 10, 10, 0, 360);
            g2d.fillArc(55, 50, 10, 10, 0, 360);
        }
    }

}
 1
Author: MadProgrammer, 2016-04-13 08:41:36