【问题标题】:Java JApplet doesn't remove an old rectangle once it is movedJava JApplet 在移动旧矩形后不会删除它
【发布时间】:2014-07-11 21:26:38
【问题描述】:

我是图形和 japplet 的新手,我制作了一个穿过屏幕的矩形。但是由于某种原因,它只是画了一条线,而不是在移动后删除矩形的旧实例。

主要:

public class Main extends JApplet implements Runnable {
    private static final long serialVersionUID = 1L;
    private static int width = 900;
    private static int height = 600;
    public static int fps = 60;
    public Thread thread = new Thread(this);
    public static Ailoid ailoid = new Ailoid();

    public void init() {
        setSize(width, height);
        setBackground(Color.white);

        ailoid.setLocation(new Location(100, 100));
        AlienManager.registerAlien(ailoid);
    }

    public void paint(Graphics g) {
        g.setColor(Color.green);
        for (Alien alien : AlienManager.getAliens()) {
            Location loc = alien.getLocation();
            int x = loc.getX();
            int y = loc.getY();
            g.fillRect(x, y, 10, 20);
        }
    }

    // Thread start
    @Override
    public void start() {
        thread.start();
    }
    // Thread stop
    @SuppressWarnings("deprecation")
    @Override
    public void destroy() {
        thread.stop();
    }

    @Override
    public void run() {
        while (true) {
            Updater.run();
            repaint();
            try {
                // 1000 divided by fps to get frames per millisecond
                Thread.sleep(1000 / fps);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

更新者:

public class Updater {
    public static void run() {
        for (Alien alien : AlienManager.getAliens()) {
            Location loc = alien.getLocation();
            int x = loc.getX();
            int y = loc.getY();
            alien.setLocation(new Location(x, y));
        }
    }
}

为什么不删除旧图形?谢谢!

【问题讨论】:

    标签: java swing graphics japplet


    【解决方案1】:

    您的主要问题是您的paint(...) 方法没有调用超级方法,该方法允许组件重绘其内容:

    public void paint(Graphics g) {
        super.paint(g);
        //....
    

    话虽如此,您远最好不要在顶级窗口中绘制,而是在小程序显示的 JPanel 的 paintComponent 方法中绘制。如果你做了这个更正,那么做同样的事情——调用 super 方法。

    class MyPanel extends JPanel {
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            //....
    

    顺便说一句,这段代码:

    public class Updater {
        public static void run() {
            for (Alien alien : AlienManager.getAliens()) {
                Location loc = alien.getLocation();
                int x = loc.getX();
                int y = loc.getY();
                alien.setLocation(new Location(x, y));
            }
        }
    }
    

    看起来它并没有太多移动东西。事实上,根据这段代码,你的外星人应该完全静止。


    编辑
    此外,这是从不代码,你不应该在你的应用程序中拥有这些代码:

    @SuppressWarnings("deprecation")
    @Override
    public void destroy() {
        thread.stop();
    }
    

    线程的stop() 方法已被弃用并且永远不应该被调用是有原因的。如果您对原因感到好奇,请查看 API。

    【讨论】:

    • @MCMastery:关于,"This is a JApplet but thank you." -- 我知道这一点,这正是我要说的。 再次你不应该在顶层窗口的绘制方法中绘制,无论是 JApplet、JFrame 还是其他。
    • @MCMastery:关于,"Then how do I update it?"——你当前的代码复制了这些位置并重新使用它们。考虑稍微改变一下。如何做到这一点取决于您。
    • 哦,我必须在编辑时以某种方式删除它:P 我之前确实添加了一些动作。谢谢,它有效:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多