如何在Java游戏中重绘图像 [英] How to repaint over images in Java game

查看:408
本文介绍了如何在Java游戏中重绘图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我在绘制与用户相交的硬币时遇到了麻烦.我要这段代码要做的是,当用户精灵与它在硬币上绘制的十个硬币图像中的任何一个相交时,它不再出现在屏幕上(还需要添加某种计数器,以便当用户收集时全部十个硬币将停止线程,并说"You Win!").我的问题是我应该怎么做,因为我已经尝试过在if语句中使用repaint(),并且它现在无法正确编译.非常感谢您提供有关如何在硬币上绘画的帮助,甚至可能添加某种计数器(认为for循环会派上用场)!这是代码:

Hello everyone I am having trouble with painting over coins that the user intersects with. What I want this code to do is when the user sprite intersects with any of the ten coin images it paints over the coin so that it no longer appears on the screen (also need to add in some kind of counter so that when the user collects all ten coins it will stop the thread, and say "You Win!"). My question is how would I go about doing this because I have tried using repaint() in the if statements, and it is not compiling correctly anymore. Any help on how to paint over the coins, and possibly even add some kind of counter (thinking a for loop would come in handy) would be greatly appreciated! Here is the code:

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
    }
}

推荐答案

在我的脑海中,将while(true)放在您的Run()方法中(您说过您正在使用线程),然后在循环,在循环结束时(在try/catch内)放置类似//thread//.sleep(allotted time)的内容,这将使线程停止设置的毫秒数.在repaint()的Javadoc中,您会看到它调用了最近的paint(Graphics g)方法.这样,您可以从程序中的任何位置访问paint()方法.

Off the top of my head, put while(true) within your Run() method (you said you were using a thread) and call repaint() within the loop, at the end of the loop (within a try/catch) put something like //thread//.sleep(allotted time) This will stop the thread for the set amount of Milliseconds. Within the Javadoc of repaint() you'll see that it calls the nearest paint(Graphics g) method. By doing this you can access your paint() method from wherever you are in your program.

我对硬币的建议是使其成为自己的硬币.该类将类似于以下内容:

My suggestion to the coin's is make it it's own class. The class would look similar to this:

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(是的,这很痛苦)(必须将它们涂在绘画中以传递Graphics变量)

After that, in your paint method you could make 10 Coin objects and use g which you instantiate in the parameters. You could then make an array of Coin objects and add 10 Coins that you make in your paint method manually (yeah, it's a pain) (you have to make them in paint to pass the Graphics variable)

Coin[] coins = new Coin[10];

顺便说一句,这是您制作数组的方式.制作硬币阵列后,在paint方法中创建一个for循环.它应该看起来像这样:

Is how you make the array, by the way. After you make the array of coin's, then make a for loop in your paint method. It should look something like this:

        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,这将在硬币所在的位置绘制一个清晰的矩形.

This will make a clear rectangle be drawn over wherever the Coin is at if the user's X and Y is equal to the Coin's X and Y.

这篇关于如何在Java游戏中重绘图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆