首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在不断变化的画面上画图

在不断变化的画面上画图
EN

Stack Overflow用户
提问于 2012-09-08 04:28:41
回答 1查看 82关注 0票数 0

我在试着给老虎机编程。因此,我创建了一个JPanel,在其中我覆盖了paintComponent()方法。

代码语言:javascript
复制
public void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D)g;

    g2.drawImage(backImage, 0, 0, this.getWidth(), this.getHeight(),null); // Background
    if(slot1On) //Slot Machine
    {
        g2.drawImage(getSlot1(masterCount), 26, 50,slotSizeW,slotSizeH, null);
    }
    else
    {
        g2.drawImage(getSlot1(slot1Pos), 26, 50,slotSizeW,slotSizeH, null);
    }
    g2.setColor(Color.WHITE); // Text
    g2.setFont(new Font(lblAction.getFont().getName(),Font.BOLD,16));
    g2.drawString("Press Space to stop the wheel!", 32, 25);
    int i,j;
    for(i=0;i<3;i++) // Lines on the slot machine
    {
        g2.setColor(new Color(0,0,0,0.9f));
        g2.fillRect(27+ 12 * i + slotSizeW * i, 50 + slotSizeH/2-1, slotSizeW, 3);
        g2.fillRect(28+ 12 * i + slotSizeW * i, 50 +slotSizeH/2-4, 3,9);
        g2.fillRect(22+ 12 * i + slotSizeW * (i +1) , 50 +slotSizeH/2-4, 3, 9);
        for(j=0;j<8;j++)
        {
            if(j < 4)
            {
                g2.setColor(new Color(0,0,0,0.9f - j * 0.1f));
                g2.fillRect(26 + 12 * i + slotSizeW * i, 50 + j * 4, slotSizeW, 4); 
            }
            else
            {
                g2.setColor(new Color(0,0,0,0.9f - (8 - j) * 0.1f));
                g2.fillRect(26+ 12 * i + slotSizeW * i, 50 + slotSizeH - (8 - j) * 4,    slotSizeW, 4); 
            }
        }
    }  
}

然而,我试图在老虎机之后绘制的所有内容都不会在老虎机移动时显示出来,如下图所示:

那么,为什么它们不能正确显示呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-08 04:43:34

试试这个:

代码语言:javascript
复制
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//the rest of your painting here
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12324938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档