xtré
xtré

Reputation: 132

How do I get new images after a delay?

I'm trying to paint a new image of the same image to the screen after every 10 second delay interval. I've been trying to figure it out myself and I also looked things up but can't find anything that helps.

My specific problem is that I don't know how to paint a new image using the original image. I want to make it so every 10 seconds the new image should appear and start following it's ID array which then follows it's own x y coordinates (IDx[0]=x; IDx[0]=y;)

A plain g.drawImage in Graphics for every new image is ruled out because I need it to draw new images/bots until the condition is met for them to stop. But the aim is the same just in an automatic way.

My window:

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

    }

}

Help is very much appreciated!

Upvotes: 0

Views: 190

Answers (2)

camickr
camickr

Reputation: 324118

Always invoke super.XXX() of the method you override. You should be overriding paintComponent():

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

Don't use a Thread.sleep() for animation. Instead you should be using a Swing Timer.

A plain g.drawImage in Graphics for every new image is ruled out because I need it to draw new images/bots

You can use the same BufferedImage. When the Timer fires you update the image. Then the paintComponent() method just paints the updated image.

Upvotes: 1

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16142

You already have the structures necessary to draw in a separate thred, why don't you use them? This looks too much like homework to me...

Upvotes: 0

Related Questions