Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6607451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:30:17+00:00 2026-05-25T19:30:17+00:00

So I decided to pick up programming as a hobby, and am now working

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:30:18+00:00Added an answer on May 25, 2026 at 7:30 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We decided to use the minimumRequiredVersion in our clickOnce application manifest, and now when
I decided to change a utility I'm working on to use a tabpage. When
So I'm trying to pick C++, and to do so I decided to write
I've been programming for a while, but just recently decided to start developing for
I am new to web programming, coming from a video game development background (c++),
According to this article from Herb Sutter, one should always pick Class Specializing over
I need to implement vector clocks in a small programming exercise and I decided
I've been reading up on functional programming a lot recently, and I finally decided
I decided to learn some OpenGL ES 2.0 and I am trying to pick
Decided to use Apache's Common Configuration package to parse an XML File. I decided

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.