gpt4 book ai didi

java - 在java中绘制多个对象?

转载 作者:行者123 更新时间:2023-12-01 19:13:33 25 4
gpt4 key购买 nike

嘿,我正在制作一个 Java 游戏,其中黄色 block 随机出现在游戏屏幕上,你必须收集它们。这些对象是从一个类创建的,我想知道是否有一种方法可以绘制所有对象?

这是生成它们的代码:

package OurGame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.*;

public class coins extends JPanel implements ActionListener {
Timer t;
coin c;

public coins() {
t = new Timer(1000,this);
t.start();
}

public void actionPerformed(ActionEvent e) {
System.out.println("1 Second");

Random rx = new Random();
Random ry = new Random();

c = new coin(rx.nextInt(640),ry.nextInt(480));
}

}

这是硬币本身的代码。

package OurGame;

import java.awt.Image;

import javax.swing.ImageIcon;

public class coin {
Image coin;

int x,y;
public coin(int x1, int y1) {

ImageIcon i = new ImageIcon("C:/coin.png");
coin = i.getImage();

x = x1;
y = y1;
}

public Image getImage(){
return coin;
}

public int getX(){
return x;
}

public int getY() {
return y;
}
}

如果您能提供帮助,那就太好了。

最佳答案

为什么不创建Coin的ArrayList(类名要大写)或者ArrayList。然后,如果您需要在显示中添加或删除硬币,则可以从 ArrayList 中添加或删除它。然后,paintComponent 方法可以在 for 循环中迭代数组列表,绘制循环中的每个硬币。

此外,显示硬币的最简单方法是将其放入 ImageIcon 中,然后使用它来设置 JLabel 的图标。

例如使用图像缩放使硬币变小,并使用图像过滤器将白色背景更改为透明:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Coins extends JPanel implements ActionListener {
private static final String COIN_URL_PATH = "http://cdn.dailyclipart.net/wp-content/uploads/medium/clipart0273.jpg";
private static final String COIN_URL_PATH2 = "http://content.scholastic.com/content/media/products/71/0439510171_rgb15_xlg.jpg";
private static final String COIN_URL_PATH3 = "http://uscoinstoday.com/images/e/130580876887_0.jpg";
private static final int PAN_WIDTH = 900;
private static final int PAN_HT = 700;
protected static final int TRANSPARENT = new Color(255, 255, 255, 0)
.getRGB();
private Timer t;
private BufferedImage coinImage;
private ImageIcon coinIcon;
private Random random = new Random();

public Coins() {
setLayout(null);
try {
coinImage = ImageIO.read(new URL(COIN_URL_PATH));
double scaleFactor = 0.35;
BufferedImage destImg = new BufferedImage((int)(coinImage.getWidth() * scaleFactor),
(int) (coinImage.getHeight() * scaleFactor), BufferedImage.TYPE_INT_ARGB);
AffineTransform at = AffineTransform.getScaleInstance(scaleFactor, scaleFactor);
AffineTransformOp ato = new AffineTransformOp(at,
AffineTransformOp.TYPE_BICUBIC);
ato.filter(coinImage, destImg);

ImageFilter whiteToTranspFilter = new RGBImageFilter() {

@Override
public int filterRGB(int x, int y, int rgb) {
Color color = new Color(rgb);
int colorSum = color.getBlue() + color.getRed() + color.getGreen();
int maxColorSum = 600;
if (colorSum > maxColorSum ) {
return TRANSPARENT;
}
return rgb;
}
};
ImageProducer ip = new FilteredImageSource(destImg.getSource(), whiteToTranspFilter);
Image destImg2 = Toolkit.getDefaultToolkit().createImage(ip);


coinIcon = new ImageIcon(destImg2);
t = new Timer(1000, this);
t.start();

} catch (MalformedURLException e) {
e.printStackTrace();
System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}

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

public void actionPerformed(ActionEvent e) {
System.out.println("1 Second");

Coin c = new Coin(random.nextInt(640), random.nextInt(480), coinIcon);
add(c.getCoinLabel());
revalidate();
repaint();
}

public static void main(String[] args) {
Coins coins = new Coins();
JFrame frame = new JFrame("Coins");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(coins);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

}

class Coin {
JLabel coinLabel = new JLabel();

public Coin(int x1, int y1, ImageIcon coinIcon) {
coinLabel.setIcon(coinIcon);
coinLabel.setLocation(x1, y1);
coinLabel.setSize(coinLabel.getPreferredSize());
}

public JLabel getCoinLabel() {
return coinLabel;
}

}

关于java - 在java中绘制多个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7625215/

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