如何在延迟后获得新图像?

时间:2013-04-19 15:14:05

标签: java image swing

我试图在每10秒延迟间隔后将相同图像的新图像绘制到屏幕上。我一直试图弄清楚自己,我也查了一下但找不到任何有用的东西。

我的具体问题是我不知道如何使用原始图像绘制新图像。我想每隔10秒钟就出现一个新图像并开始跟随它的ID数组然后跟随它自己的x y坐标(IDx [0] = x; IDx [0] = y;)

每个新图像的图形中的普通g.drawImage被排除,因为我需要它来绘制新的图像/机器人,直到满足它们停止的条件。但目标只是自动化。

我的窗口:

package pack;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Window extends JPanel implements Runnable {

    public static final int GAME_WIDTH = 512;
    public static final int GAME_HEIGHT = GAME_WIDTH * 3 / 4;
    public static final int SCALE = 2;

    public static Image Player = Controls.Player;
    public static Image BB = ImageLoader.BB;
    public static Image Grass = ImageLoader.Grass;

    public static boolean painting = true;

    public static boolean Start = true;
    public static boolean Menu;

    public static boolean ONhs;
    public static boolean OFFhs;

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

        g.setColor(Color.GREEN);
        g.drawString("UPS: " + Integer.toString(UPS.ups), 100, 120);
        g.drawString("FPS: " + Integer.toString(UPS.fps), 100, 130);

        // // MENU ////
        if (Menu) {
            Image Grass = ImageLoader.Grass;
            Image onHoverStart = ImageLoader.onHoverStart;
            Image offHoverStart = ImageLoader.offHoverStart;

            g.drawImage(Grass, GAME_WIDTH * SCALE / 4, GAME_HEIGHT * SCALE / 6, null);

            if (ONhs) {
                g.drawImage(onHoverStart, 400, 250, null);
            } else {
                g.drawImage(offHoverStart, 400, 250, null);
            }
        }

        // // STARTED // //
        if (Start) {
            Image Player = Controls.Player;
            Image BB = ImageLoader.BB;

            g.drawImage(BB, PlayerData.BBDirectionx, PlayerData.BBDirectiony, null);

            g.drawImage(ImageLoader.Bot[0], Mob.BotxSELF - 32, Mob.BotySELF - 25, null);
            g.drawImage(ImageLoader.Bot[1], Mob.BotxSELF - 32, Mob.BotySELF - 25, null);

            g.drawImage(Player, Controls.Playerx - 45, Controls.Playery + 5, null);
        }
    }

    @Override
    public void run() {
        while (painting) {
            UPS.fpstick = UPS.fpstick + 1;
            repaint();
            try {
                Thread.sleep(12);
            } catch (InterruptedException e) {
            }
        }
    }

    public static void draw(Graphics g) {
        g.drawImage(ImageLoader.Bot[0], 32, 25, null);

    }

    public Window() {

        JFrame jf = new JFrame();
        JPanel panel = new JPanel(new BorderLayout());

        Controls c = new Controls("Window using Controls for Listener");

        panel.add(this);

        this.setPreferredSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE));
        this.setMinimumSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE));
        this.setMaximumSize(new Dimension(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE));
        this.setBackground(Color.DARK_GRAY);

        jf.setContentPane(panel);
        jf.pack();

            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE);
        jf.setVisible(true);
        jf.setResizable(false);

        jf.addKeyListener(c);
        jf.addMouseListener(c);
        jf.addMouseMotionListener(c);

    }

}

非常感谢帮助!

2 个答案:

答案 0 :(得分:1)

始终调用您覆盖的方法的super.XXX()。你应该覆盖paintComponent()

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

不要将Thread.sleep()用于动画。相反,您应该使用Swing Timer

  

每个新图像的图形中的普通g.drawImage被排除,因为我需要它来绘制新的图像/机器人

您可以使用相同的BufferedImage。当计时器触发时,您更新图像。然后paintComponent()方法只绘制更新的图像。

答案 1 :(得分:0)

你已经拥有了单独绘制所需的结构,为什么不使用它们?这看起来太像我的家庭作业......