gpt4 book ai didi

java - 如何使用计时器在java中使图像每x秒随机出现一次?

转载 作者:行者123 更新时间:2023-12-02 06:17:53 24 4
gpt4 key购买 nike

我正在开发一款游戏,我需要“击中”一只老鼠,它就会消失,您将获得 1 分。我让它在每次启动应用程序时随机出现,但我希望使用 Timer() 或其他东西每 x 秒随机绘制图像。

我的游戏屏幕代码如下所示:

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Gamevenster extends JPanel implements Runnable {
public String Gamestatus = "active";
private Thread thread;
//public Main game;

public int random(int min, int max) {
int range = (max - min) + 1;
return (int)(Math.random() * range) + min;
}

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

g.drawImage(achtergrond, 0, 0, this.getWidth(), this.getHeight(), null);
//g.drawImage(muisje, 10, 10, null);
g.drawImage(muisje, random(0, this.getWidth()), random(0, this.getHeight()), null);
}

private static final long serialVersionUID = 1L;

Image achtergrond, muisje;
JTextField invoer;
JButton raden;
JButton menu;

Gamevenster() {
setLayout(null);

ImageIcon icon = new ImageIcon(this.getClass().getResource("assets/achtergrondspel.png"));
achtergrond = icon.getImage();

ImageIcon icon2 = new ImageIcon(this.getClass().getResource("assets/muisje.png"));
muisje = icon2.getImage();

//Get the default toolkit
Toolkit toolkit = Toolkit.getDefaultToolkit();

//Load an image for the cursor
Image image = toolkit.getImage("src/assets/hand.png");

//Create the hotspot for the cursor
Point hotSpot = new Point(0,0);

//Create the custom cursor
Cursor cursor = toolkit.createCustomCursor(image, hotSpot, "Hand");

//Use the custom cursor
setCursor(cursor);

// setLayout( null );

// Invoer feld
invoer = new JTextField(10);
invoer.setLayout(null);
invoer.setBounds(150, 474, 290, 60); // Verander positie onder aan scherm is int 1

// Button voor raden
raden = new JButton("Raden");
raden.setLayout(null);
raden.setBounds(10, 474, 130, 60);
raden.setFont(new Font("Dialog", 1, 20));
raden.setForeground(Color.white);
raden.setBackground(new Color(46, 204, 113));
raden.setPreferredSize(new Dimension(130, 60));

// Menu knop
menu = new JButton("Menu");
menu.setLayout(null);
menu.setBounds(450, 474, 130, 60);
menu.setFont(new Font("Dialog", 1, 20));
menu.setForeground(Color.white);
menu.setBackground(new Color(46, 204, 113));
menu.setPreferredSize(new Dimension(130, 60));

// Toevoegen aan screen
add(invoer);
//add(raden);
add(menu);

menu.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
String i = invoer.getText();
System.out.println("Er is gedrukt! " + i);
}
});
}

public void start(){
thread = new Thread(this,"spelloop");
thread.start();
}

public void run() {
// TODO Auto-generated method stub
while(Gamestatus=="active"){
System.out.println("Gameloop werkt");
}
}
}

如你所见,我正在使用 g.drawImage(muisje, random(0, this.getWidth()), random(0, this.getHeight()), null);

因此它会在启动时随机添加图像。

当应用程序打开时,如何使用计时器每 x 秒执行一次此操作?

最佳答案

"How can I use a timer to do this every x seconds when the app is openend?"

看看这个例子。我从互联网上收集了图像,但您可以使用图像文件执行相同的操作。我所做的是使用 URLBufferedImage 数组,每 500 毫秒获得一个随机索引,然后 repaint() 面板

注意如果您要使用图像文件,您可能需要查看 this answer还有。

enter image description here

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class TestImageRotate {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
JFrame frame = new JFrame("Image Timer");
frame.add(new ImagePanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

private static class ImagePanel extends JPanel {

URL[] urls;
BufferedImage[] images;
Random rand = new Random();

public ImagePanel() {
urls = new URL[5];
try {
urls[0] = new URL("http://www.atomicframework.com/assetsY/img/stackoverflow_chicklet.png");
urls[1] = new URL("http://www.iconsdb.com/icons/download/orange/stackoverflow-256.png");
urls[2] = new URL("http://img.1mobile.com/market/screenshot/50/com.dd.stackoverflow/0.png");
urls[3] = new URL("http://www.iconsdb.com/icons/download/orange/stackoverflow-4-512.png");
urls[4] = new URL("http://www.iconsdb.com/icons/preview/light-gray/stackoverflow-xxl.png");

images = new BufferedImage[5];
images[0] = ImageIO.read(urls[0]);
images[1] = ImageIO.read(urls[1]);
images[2] = ImageIO.read(urls[2]);
images[3] = ImageIO.read(urls[3]);
images[4] = ImageIO.read(urls[4]);

} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
setBackground(Color.BLACK);

Timer timer = new Timer(500, new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
});
timer.start();
}

private int random() {
int index = rand.nextInt(5);
return index;
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
BufferedImage img = images[random()];
g.drawImage(img, 0, 0, 400, 400, 0, 0,
img.getWidth(), img.getHeight(), this);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
}
}
<小时/>

注意Timer代码。这就是我所做的一切

Timer timer = new Timer(500, new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
repaint();
}
});
timer.start();

对于 .grawImage,我使用 BufferedImages 数组中的随机索引

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
BufferedImage img = images[random()];
g.drawImage(img, 0, 0, 400, 400, 0, 0,
img.getWidth(), img.getHeight(), this);
}
<小时/>

更新示例。晚上我关闭了 IDE。太懒了,所以我只是想边走边想出这个。如果你还是不明白,明天我起床时我会添加一个真实的例子。

基本上,您希望为鼠标图像的 x 和 y 位置提供全局变量

int x = 0;
int y = 0;

当你绘制图像时,你想要使用这些位置

g.drawImage(img, x, y, whatEverWidth, whatEverHeight, this);

在计时器中,您可以在绘画之前随机修改x和y。让我们使用一些逻辑。

假设您的屏幕宽度为 500,屏幕高度为 500,鼠标图像宽度为 100,鼠标图像高度为 100

  • 因此最大 x 位置将为 400 = 屏幕宽度 - 鼠标图像宽度
  • 最大 y 位置将为 400 = 屏幕高度 - 鼠标图像高度

现在我们有了我们的范围。我们知道最小 x 位置为 0,最小 y 位置为 0。因此,我们希望每个 x 和 y 都有一个 0 到 400 之间的随机数。所以在计时器中你可以这样做

Timer timer = new Timer(1000, new ActionListener(){
public void actionPerformed(ActionEvent e) {
x = rand.nextInt(400) + 1;
y = rand.nextInt(400) + 1;
repaint();
}
});

每次调用重新绘制时,这都会在随机位置重新绘制鼠标图像。

<小时/>

更新2

我不知道还有什么需要解释的。我只是做了我指出的那些事情(用我的原始代码),只是添加了xy并使用它们来绘制图像,并在计时器中获得了一个随机位置。它对我来说效果很好。我不知道你做错了什么。

enter image description here

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class TestImageRotate {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
JFrame frame = new JFrame("Image Timer");
frame.add(new ImagePanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

private static class ImagePanel extends JPanel {

URL[] urls;
BufferedImage[] images;
Random rand = new Random();
private int x = 0;
private int y = 0;

public ImagePanel() {
urls = new URL[5];
try {
urls[0] = new URL("http://www.atomicframework.com/assetsY/img/stackoverflow_chicklet.png");
urls[1] = new URL("http://www.iconsdb.com/icons/download/orange/stackoverflow-256.png");
urls[2] = new URL("http://img.1mobile.com/market/screenshot/50/com.dd.stackoverflow/0.png");
urls[3] = new URL("http://www.iconsdb.com/icons/download/orange/stackoverflow-4-512.png");
urls[4] = new URL("http://www.iconsdb.com/icons/preview/light-gray/stackoverflow-xxl.png");

images = new BufferedImage[5];
images[0] = ImageIO.read(urls[0]);
images[1] = ImageIO.read(urls[1]);
images[2] = ImageIO.read(urls[2]);
images[3] = ImageIO.read(urls[3]);
images[4] = ImageIO.read(urls[4]);

} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
setBackground(Color.BLACK);

Timer timer = new Timer(500, new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
x = rand.nextInt(325);
y = rand.nextInt(325);
repaint();
}
});
timer.start();
}

private int random() {
int index = rand.nextInt(5);
return index;
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
BufferedImage img = images[random()];
g.drawImage(img, x, y, 75, 75, this);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
}
}

关于java - 如何使用计时器在java中使图像每x秒随机出现一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21289140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com