简体   繁体   English

如何从卡片组图像中取出特定的卡?

[英]How do I take out a specific card from a deck image?

I want to take out a card from my deck image. 我想从卡座图像中取出一张卡片。 How can I do that without downloading every single card separately? 在不单独下载每张卡的情况下该怎么办? I don't want to initialize all 52 cards in my class. 我不想初始化班级中的所有52张卡。

Right now, I randomize colour and value and then it finds the picture in my source folder. 现在,我将颜色和值随机化,然后在源文件夹中找到图片。 But is it possible to only take one card out from my single deck image? 但是可以从我的单副牌图像中只取出一张卡吗?

bild = ImageIO.read(new File("source folder/" + colour + value + ".png"));      

You still have to load the entire image but when you're drawing, just use 您仍然必须加载整个图像,但是在绘制时,只需使用

g.drawImage(Image img, int dstx1, int dsty1, int dstx2, int dsty2, int srcx1, int srcy1, int srcx2, int srcy2, ImageObserver observer);

The src (=source) values will be the upper left und lower right coordinates within your image that define the part you want to draw while the dst (=destination) values will decide where it will be drawn. src(= source)值将是图像中的左上坐标和右下坐标,它们定义要绘制的零件,而dst(= destination)值将决定绘制对象的位置。

You should initialize them all but you don't have to do it by hands. 您应该全部初始化它们,而不必手动进行。

You should create one image that contains every single card and load it. 您应该创建一个包含每张卡的图像并加载它。 BufferedImage class provides you a getSubImage(int x, int y, int w, int h); BufferedImage类为您提供了一个getSubImage(int x, int y, int w, int h); and is returning a piece (a card) of your BufferedImage. 并返回一块(一张卡片)您的BufferedImage.

You can populate an array of card while looping through the image of your deck. 您可以在浏览卡片组的图像时填充一系列卡片。

    for(int i = 0; i<4; i++){
        for(int j = 0; j < 13; j++){
            deckArray[13 * i + j] == image.getSubImage(i * cardWidth,
                                                       j * cardHeight, 
                                                       cardWidth, 
                                                       cardHeight);
        }
    }

Something like this. 这样的事情。 Note that I'm assuming 4*13 images os sheet and no space between them. 请注意,我假设4 * 13张图像的工作表之间没有空间。 You will have to find a way to track you cards, which one is which in your array no matter how you store them, because what you definietly don't want is loading an image every time you want to use it. 您将必须找到一种跟踪卡的方法,无论存储方式如何,都可以在阵列中找到它,因为您绝对不希望的是每次使用图像时都加载图像。

It's not tested and not 100% sure I did all the math corretly, just a quick reference how could you achive something like this. 它未经测试,也不能100%确信我正确地完成了所有数学运算,只是快速参考,您如何才能达到这样的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM