java - 在另一个图像上添加图像?

标签 java image

我正在尝试制作一款回合制游戏,到目前为止进展顺利,但是现在我来到了我希望能够移动单位的部分。我使用图像来确定不同类型的地形,并使用另一个图像来显示一个单位,但我无法让该单位出现在地形上方,它只在地形下方弹出。看来我是第一个遇到这个问题的人,所以我的处理方式是错误的吗?

带有地形图像的图 block :

    protected void paintComponent(Graphics paintIt) //paints this sexy-hex
{
    super.paintComponent(paintIt);  //not sure if we need this? lol
    paintIt.setColor(is);   //sets the color (later not even used, we need pics and shit)
    //paintIt.fillPolygon(hex); //fills the hexagon with the desired color, will not be used later on either
    if(isExplored)
        paintIt.drawImage(image, 0, 0, null);

    else
        paintIt.fillPolygon(hex); // I want to paint it black


    if (hoover) //are we pointing at this cell?
    {
        paintIt.setColor(Color.WHITE);  //we want the nice line around the cell to be white
        Graphics2D g2 = (Graphics2D) paintIt;   
        g2.setStroke(new BasicStroke(3));   //and 3 pixels wide (we are allowed to change it, supposedly in proportion to "rad"
        g2.drawPolygon(hex);    //draws the line around the hexagon
    }       
}

单位:

    protected void paintComponent(Graphics paintIt)
{
    if(!selected)
    {
        paintIt.drawImage(image, 0, 0, null);
        paintIt.drawImage(image2,0,0,null);
        /*
        super.paintComponent(paintIt);
        paintIt.setColor(Color.BLACK);
        paintIt.drawPolygon(shape); */  
    }
    else
    {
        super.paintComponent(paintIt);
        paintIt.setColor(Color.RED);
        paintIt.drawPolygon(shape); 
    }
}

对这些评论感到抱歉,对一些甚至没有使用的代码感到抱歉,只是认为如果我们需要返回某个地方,最好在项目完成之前保留它。 单位图像比地形小,所以很适合。我在没有绘制地形的情况下进行了测试,因此我知道该单位是在地形下方绘制的。 如果仍然有代码,您需要了解我在做什么,请告诉我,只是认为这是相关部分。 编辑#2 磁贴是从扩展 JFrame 的另一个类创建的类。单元类是从 JFrame 类创建的,但存储在图 block 中。 tile 和unit 都是JComponent。

最佳答案

已修复,我必须在地形类中绘制单位图像才能使其出现在顶部。因为我已经将单元存储在类中,所以我不知道为什么我不从一开始就这样做。 谢谢,无论如何<3

关于java - 在另一个图像上添加图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5591853/

相关文章:

java - Robovm 绑定(bind)导入问题

java - 使用 Math.random() 的表达式总是返回相同的值

java - 为什么Mysql字段不更新? - java

java - 我可以在 JAX-WS 中强制执行对 base64Binary 数据的严格验证吗?

html - 如何将 img 的宽度百分比包裹在特定的 div 宽度中?

css - div 内的图像与其他部分的高度不同

javascript - 如何确定 IE 中动态加载图像的 img 宽度/高度?

javascript - AngularJS 绑定(bind)在 <img> 标签的 'src' 属性中

java - 如何在Android中的ListView中获取副文本

javascript - 使用 jQuery 调整图像大小