本文介绍了Java2D的移动图像后拆除旧像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我朝着Java2D的图像,它反弹以及。出于某种原因,它总是留下的老照片的线索。我怎么能解决这个问题?

So I'm moving an image in Java2D and it bounces aswell. For some reason, it always leaves behind a trail of old images. How could I fix this?

主要类:

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) {
                        }
                }
        }
}

什么是错的例子:

Example of what's wrong:

推荐答案

不要直接搽的JFrame 。相反,使用的JP​​anel 或延长的JComponent 的。对于绘画覆盖的paintComponent(),而不是油漆()。不要忘了叫 super.paintComponent方法(G); ,否则你会遇到相同的行为在你的榜样 - <$ C $的previous结果C>的drawImage()不清除的踪迹依然存在。

Don't paint directly on JFrame. Instead, use JPanel or extension of JComponent. For painting override paintComponent() rather than paint(). Don't forget to call super.paintComponent(g);, otherwise you will encounter the same behavior as in your example - the previous result of drawImage() is not cleared and the trail remains.

看看执行风俗画教程,和仔细看一看特别是油漆机制更多的细节部分。

Take a look at Performing Custom Painting tutorial, and Closer Look at the Paint Mechanism section in particular, for more details.

考虑下面的例子使用Swing定时器,动画形象的JP​​anel

Consider the following example that animates image in JPanel using Swing timer:

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();
            }
        });
    }
}

这篇关于Java2D的移动图像后拆除旧像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 14:13