[英]How to repaint over images in Java game
大家好,我在绘制与用户相交的硬币时遇到麻烦。 我要这段代码要做的是,当用户精灵与它绘制在硬币上的十个硬币图像中的任何一个相交时,它不再出现在屏幕上(还需要添加某种计数器,以便当用户收集时所有十枚硬币将停止线程,并说“ You Win!”)。 我的问题是我将如何去做,因为我已经尝试过在if语句中使用repaint(),并且它现在无法正确编译。 非常感谢您提供有关如何在硬币上绘画的帮助,甚至可能添加某种计数器(认为for循环会派上用场)! 这是代码:
public void paint(Graphics g)
{
g.clearRect(0, 0, this.getWidth(), this.getHeight());
one.paintComponent(g);
two.paintComponent(g);
three.paintComponent(g);
four.paintComponent(g);
five.paintComponent(g);
six.paintComponent(g);
seven.paintComponent(g);
eight.paintComponent(g);
nine.paintComponent(g);
ten.paintComponent(g);
monster.setLocation(r.nextInt(10) - 5 + monster.x, r.nextInt(10 - 5 + monster.y));
monster.paintComponent(g);
user.paintComponent(g);
if(user.intersects(one))
{
}
if(user.intersects(two))
{
}
if(user.intersects(three))
{
}
if(user.intersects(four))
{
}
if(user.intersects(five))
{
}
if(user.intersects(six))
{
}
if(user.intersects(seven))
{
}
if(user.intersects(eight))
{
}
if(user.intersects(nine))
{
}
if(user.intersects(ten))
{
}
if(user.intersects(monster))
{
g.setFont(new Font("Serif", Font.BOLD, 35));
g.drawString("YOU HAVE DIED, YOU LOSE!", 100, 100); //Prints this when you lose
thread.interrupt(); //Stopping the thread if you die
}
}
首先,您应该将硬币放入列表中。 然后,如果有干扰,您只需删除列表中的项目(硬币)。 像这样:
private List<Coin> coins;
public ClassConstructor() {
coins = new ArrayList();
coins.add(new Coin(type value,...)); //this ten times if every Coin is different from other
//or this if every Coin are same:
for (int i = 0; i < 10; i++) {
coins.add(new Coin(type value,...));
}
}
public void update() {
//whatever
for (int i = coins.size(); i >= 0; i--) {
if (user.intersects(coins.get(i))) {
coins.remove(i);
//...
}
}
}
public void paint(Graphics g) {
for (int i = coins.size(); i >= 0; i--) {
coins.get(i).paintComponent(g);
}
}
现在,您不应该使用thread.interrupt()结束线程。 如果该线程是您的游戏循环,则应发送结束循环的标志。 您的游戏循环线程:
@Override
public void run() {
while (running) { //boolean running
update();
paint(g);
}}
最后,您的更新将如下所示:
public void update() {
//whatever
for (int i = coins.size(); i >= 0; i--) {
if (user.intersects(coins.get(i))) {
coins.remove(i);
//...
}
}
if (user.intersects(monster)) {
youThread.setRunning(false);
try {
gameLoopThread.join(); //"wait" until thread ends
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
在我的脑海中,将while(true)
放入Run()
方法(您说过您正在使用线程)中,并在循环内调用repaint(),在循环结束时(在try / catch中)放入类似于//thread//.sleep(allotted time)
这将使线程停止设置的毫秒数。 在repaint()
的Javadoc中,您会看到它调用了最近的paint(Graphics g)
方法。 这样,您可以从程序中的任何位置访问paint()
方法。
我对硬币的建议是使其成为自己的硬币。 该类将类似于以下内容:
public class Coin{
int x;
int y;
public void Coin(int x, int y, Graphics g){
Graphics2D g2 = (Graphics2D) g;
Image img1 = (//File Location);
g2.drawImage(img1, x, y, //this);
//If you have trouble with the ImageObserver (this part) try //class name of the applet.this
this.x = x;
this.y = y;
g2.finalize();
}
public int getX(){
return x;
}
public int getY(){
return y;
}
}
之后,在绘画方法中,您可以制作10个Coin
对象并使用在参数中实例化的g
。 然后,您可以制作一个Coin
对象数组,并手动添加您在paint
方法中创建的10个Coin
(是的,这很痛苦)(必须在paint中使它们通过以传递Graphics变量)
Coin[] coins = new Coin[10];
顺便说一句,是如何制作数组。 制作硬币阵列后,在paint
方法中创建一个for
循环。 它看起来应该像这样:
for(int i = 0; i < coins.length; i++){
if(coins[i].getX == //user X && coins[i].getY == //user Y){
g.clearRect(coins[i].getX, coins[i].getY, //Coin radius, //Coin radius);
}
}
如果用户的X和Y等于硬币的X和Y,则将在硬币所在的位置绘制一个清晰的矩形。
该代码取自我当前的项目(正在运行),我尝试将其调整为适合您的项目。
private void render () {
BufferStrategy bufferstrategy = getBufferStrategy ();
if (bufferstrategy == null) {
createBufferStrategy(3);
return;
}
Graphics g = bufferstrategy.getDrawGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
paint (g);
g.dispose();
bufferstrategy.show();
}
从Thread
所在的run()中调用render()
。
让我们分解一下,看看它是否可以帮助您。
BufferStrategy
为Graphics
。 paint (Graphics g)
而在我的情况下,它遍历所有可绘制对象。 希望您从中得到一些启示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.