无法弄清楚如何在java中重叠图像

发布于 2024-12-06 08:10:19 字数 2602 浏览 0 评论 0原文

因此,我决定将编程作为一种爱好,现在正在教程的帮助下创建老虎机。然而,我遇到了图像重叠的问题。我使用照片编辑器创建了一个 .png 文件,其中包含我想要的背景以及三个用于插槽动画师的透明框。

绘制背景的代码:

  public class SlotMachineBackground extends JPanel
        {
          private ImageIcon image;

      public void paintComponent (Graphics g)
      {
        super.paintComponent (g);
        image = new ImageIcon ("/Users/Documents/slotmachine.png");
        image.paintIcon (this, g, 0,0);
      }
    }//end class

然后我制作了插槽动画师:

public class SlotAnimator extends JPanel implements ActionListener
{
  private Timer animator;
  private ImageIcon imageArray []= new ImageIcon [22];
  int currentFrame = 0;
  int slotNumber = 1;
  int box = 1;
  SlotMachine m = new SlotMachine ();
  String [] mP = m.returnTurn();

  public SlotAnimator (int delay)
  {
    for (int i = 1; i < 22; i += 2)
      imageArray [i] = new ImageIcon ("/Users/Documents/blank.gif");
    for (int i = 0; i < 21; i ++)
    {
      if (i == 0 || i == 8 || i== 12)
        imageArray [i] = new ImageIcon ("/Users/Documents/cherry.gif");
      if ( i == 2 || i == 6 || i == 16)
        imageArray[i] = new ImageIcon ("/Users/Documents/1bar.gif");
      if (i == 4)
        imageArray [i] = new ImageIcon ("/Users/Documents/seven.gif");
      if (i== 10 || i == 14)
        imageArray[i] = new ImageIcon ("/Users/Documents/2bar.gif");
      if (i == 18)
        imageArray[i] = new ImageIcon ("/Users/Documents/3bar.gif");
      if (i==20)
        imageArray [i] = new ImageIcon ("/Users/Documents/jackpot.gif");
    }

    animator = new Timer (delay, this);
    animator.start();

    }

  public void paintComponent (Graphics g)
  {  
    super.paintComponent (g);

    if (currentFrame >= imageArray.length)
    {
      animator.stop();
      ImageIcon im = m.findPicture (mP[box]);
      box++;
      im.paintIcon (this, g, 0, 0);
    }
    imageArray [currentFrame].paintIcon (this, g, 0, 0);
  }

  public void actionPerformed (ActionEvent e)
  {
    repaint();

     currentFrame ++;
  }
}//end class

接下来,我尝试将两者组合成一个 JFrame:

public class SlotAnimatorTest 
{
  public static void main (String [] args)
  {
    JFrame frame = new JFrame (); 
    SlotMachineBackground b = new SlotMachineBackground ();


    SlotAnimator a0 = new SlotAnimator (45);
    SlotAnimator a1 = new SlotAnimator (90);
    SlotAnimator a2 = new SlotAnimator (180);


    frame.add (b);
    frame.add(a0);
    frame.setSize (1500,1500);
    frame.setVisible (true);

  }
}

但是,只有动画师出现。我很困惑,你可以看出我仍然是一个业余爱好者。无论如何,提前感谢您的任何建议!

So I decided to pick up programming as a hobby, and am now working on creating a slot machine with help from tutorials. However, I ran into problems with overlapping images. I used a photo editor to create a .png file of what I want to be the background with three transparent boxes for the slot animators.

The code to paint the background:

  public class SlotMachineBackground extends JPanel
        {
          private ImageIcon image;

      public void paintComponent (Graphics g)
      {
        super.paintComponent (g);
        image = new ImageIcon ("/Users/Documents/slotmachine.png");
        image.paintIcon (this, g, 0,0);
      }
    }//end class

then I made slot animators:

public class SlotAnimator extends JPanel implements ActionListener
{
  private Timer animator;
  private ImageIcon imageArray []= new ImageIcon [22];
  int currentFrame = 0;
  int slotNumber = 1;
  int box = 1;
  SlotMachine m = new SlotMachine ();
  String [] mP = m.returnTurn();

  public SlotAnimator (int delay)
  {
    for (int i = 1; i < 22; i += 2)
      imageArray [i] = new ImageIcon ("/Users/Documents/blank.gif");
    for (int i = 0; i < 21; i ++)
    {
      if (i == 0 || i == 8 || i== 12)
        imageArray [i] = new ImageIcon ("/Users/Documents/cherry.gif");
      if ( i == 2 || i == 6 || i == 16)
        imageArray[i] = new ImageIcon ("/Users/Documents/1bar.gif");
      if (i == 4)
        imageArray [i] = new ImageIcon ("/Users/Documents/seven.gif");
      if (i== 10 || i == 14)
        imageArray[i] = new ImageIcon ("/Users/Documents/2bar.gif");
      if (i == 18)
        imageArray[i] = new ImageIcon ("/Users/Documents/3bar.gif");
      if (i==20)
        imageArray [i] = new ImageIcon ("/Users/Documents/jackpot.gif");
    }

    animator = new Timer (delay, this);
    animator.start();

    }

  public void paintComponent (Graphics g)
  {  
    super.paintComponent (g);

    if (currentFrame >= imageArray.length)
    {
      animator.stop();
      ImageIcon im = m.findPicture (mP[box]);
      box++;
      im.paintIcon (this, g, 0, 0);
    }
    imageArray [currentFrame].paintIcon (this, g, 0, 0);
  }

  public void actionPerformed (ActionEvent e)
  {
    repaint();

     currentFrame ++;
  }
}//end class

Next, I tried to combine the two into a JFrame:

public class SlotAnimatorTest 
{
  public static void main (String [] args)
  {
    JFrame frame = new JFrame (); 
    SlotMachineBackground b = new SlotMachineBackground ();


    SlotAnimator a0 = new SlotAnimator (45);
    SlotAnimator a1 = new SlotAnimator (90);
    SlotAnimator a2 = new SlotAnimator (180);


    frame.add (b);
    frame.add(a0);
    frame.setSize (1500,1500);
    frame.setVisible (true);

  }
}

However, only the animator comes up. I'm stumped, as you can tell I am still an amateur. Anyways, thank you in advance for any advice!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩套路吗 2024-12-13 08:10:19

这主要是布局问题,我建议您阅读有关它们的教程: 布局教程

在您的情况下,JLayeredPane 可能效果很好。它使用默认的空布局,因此如果要使用它,您需要指定组件的大小和位置,但您也可以告诉它组件的 z 顺序,从而能够将老虎机放置在最低位置和最高位置的事物。同样,本教程将帮助您了解详细信息: JLayeredPane 教程

另外,您确实不应该从 PaintComponent 方法中读取任何文件,尤其是未更改的图像文件 - 每次重新绘制图像时重新读取图像会减慢绘图速度,这有什么意义图像不会改变,您可以将其存储在类中的变量中吗?因此,在类的构造函数中读取一次,然后将其保存在变量中。

It's mostly a matter of layouts, and I suggest that you read the tutorial on them: The Layout Tutorials

In your situation a JLayeredPane might work well. It uses a default null layout, and so you would need to specify your component's sizes and positions if you were to use this, but you can also tell it the z-order of your components and thus be able to place the slot machine in the lowest position and things that go on top in higher positions. Again, the tutorial will help with the details: JLayeredPane Tutorial

Also, you really shouldn't be reading in any files from within a paintComponent method, especially a file of an image that doesn't change -- what's the sense of re-reading an image slowing down the drawing each time you repaint the image when the image doesn't change and you can just store it in a variable in the class? So read it once in the class's constructor, and then save it in a variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文