嘿家伙我正在制作一个java游戏,其中黄色块随机出现在游戏屏幕上,你必须收集它们。这些对象是从一个类创建的,我想知道是否有办法绘制所有这些对象?
这是产生它们的代码:
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;
}
}
如果你能提供帮助,那会很棒。
答案 0 :(得分:3)
为什么不创建Coin的ArrayList(类名应该大写)或ArrayList。然后,如果需要在显示中添加或删除硬币,则可以在ArrayList中添加或删除它。然后paintComponent方法可以在for循环中遍历数组列表,在循环中绘制每个硬币。
此外,显示Coin的最简单方法是将其放入ImageIcon,然后使用它来设置JLabel的图标。
e.g。使用图像缩放使硬币变小并使用图像过滤器将白色背景更改为透明:
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;
}
}
答案 1 :(得分:1)
您有两种选择:
1)保存所有硬币的列表并覆盖JPanel的paintComponent()方法,并在添加coint后调用rePaint()。
2)Coin可以扩展一些提供paintComponent方法的JComponent;然后你可以在JPanel中调用this.add(...);和this.rePaint()。
有些人注意到:
1)你的形象应该是静态的;否则每枚硬币都有一张图像;虽然它是相同的图像。
示例代码:
package OurGame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
public class coins extends JPanel implements ActionListener {
private Timer t;
private ArrayList<Coin> coins = new ArrayList<Coin>();
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();
this.coins.add(new Coin(rx.nextInt(640),ry.nextInt(480)));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(Coin coin : this.coins) {
g.drawImage(coint.getImage(), coin.getX(), coint.getY(), observer);
}
}
}