1

だから私はJava2Dで画像を動かしていますが、それも跳ねます。どういうわけか、常に古いイメージの跡が残ります。どうすればこれを修正できますか?

メインクラス:

package org.main.graphics;

import java.awt.Graphics;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

import org.main.entity.Entity;
import org.main.entity.Loael;

@SuppressWarnings("serial")
public class GameWindow extends JFrame implements Runnable {

        private List<Entity> entities = new ArrayList<Entity>();
        private Thread animator;

        public GameWindow() throws IOException {
                super("Game");
                setSize(640, 480);
                setVisible(true);
                setResizable(false);
                setLocationRelativeTo(null);
                revalidate();

                entities.add(new Loael(500, 400));

                animator = new Thread(this);
                animator.start();
        }

        public void paint(Graphics g) {
                for (Entity entity : entities) {
                        try {
                                g.drawImage(entity.getImage(), entity.getX(), entity.getY(),
                                                this);
                        } catch (IOException e) {
                                e.printStackTrace();
                        }
                }
        }

        @Override
        public void run() {
                while (true) {
                        for (Entity entity : entities) {
                                entity.animate(getBounds());
                                repaint();
                        }
                        try {
                                Thread.sleep(1);
                        } catch (InterruptedException e) {
                        }
                }
        }
}

問題の例:

ここに画像の説明を入力

4

1 に答える 1

3

直接塗らないでくださいJFrame。代わりに、JPanelまたは拡張子を使用しますJComponentpaintComponent()ではなくオーバーライドをペイントする場合paint()。を呼び出すことを忘れないでくださいsuper.paintComponent(g);。そうしないと、例と同じ動作が発生します - の前の結果drawImage()はクリアされず、トレイルが残ります。

詳細については、Performing Custom Paintチュートリアルと、特にPaint Mechanism セクションをよく見てください。

JPanelSwing タイマーを使用して画像をアニメーション化する次の例を考えてみましょう。

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

public class AnimateDemo {
    private static void createAndShowUI() {
        try {
            Image image = ImageIO.read(new URL(
                    "http://duke.kenai.com/iconSized/duke.gif"));
            final MyPanel panel = new MyPanel(image);
            JFrame frame = new JFrame("AnimateDemo");
            frame.getContentPane().add(panel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);

            ActionListener timerAction = new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    panel.animate();
                }
            };
            Timer timer = new Timer(10, timerAction);
            timer.setRepeats(true);
            timer.start();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static class MyPanel extends JPanel {
        private Image image;
        private int coordinateX = 0;
        private int coordinateY = 0;

        private boolean incrementX = true;
        private boolean incrementY = true;

        public MyPanel(Image image) {
            this.image = image;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            if (image != null) {
                g.drawImage(image, coordinateX, coordinateY, this);
            }
        }

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

        public void animate() {
            if (image != null) {

                if (image.getWidth(this) + coordinateX > getWidth()) {
                    incrementX = false;
                }
                if (coordinateX < 0) {
                    incrementX = true;
                }

                if (incrementX)
                    coordinateX++;
                else
                    coordinateX--;

                if (image.getHeight(null) + coordinateY > getHeight()) {
                    incrementY = false;
                }
                if (coordinateY < 0) {
                    incrementY = true;
                }

                if (incrementY)
                    coordinateY++;
                else
                    coordinateY--;

                repaint();
            }
        }
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}
于 2012-09-09T04:16:51.393 に答える