So, I decided to raise programming as a hobby, and now I'm working on creating a slot machine using tutorials. However, I am having problems with overlapping images. I used the photo editor to create a .png file of what I want to be a background with three transparent fields for slot animators.
Code for painting 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); } }
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 ++; } }
Then I tried to combine these two in 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 appears. I'm at a standstill, as you can tell, I'm still an amateur. Anyway, thanks in advance for any advice!
source share