0

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

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

排除了每个新图像的 Graphics 中的普通 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);

    }

}

非常感谢您的帮助!

4

2 回答 2

1

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

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

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

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

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

于 2013-04-19T15:22:55.213 回答
0

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

于 2013-04-19T15:22:47.540 回答