Unable to figure out how to overlap images in java

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); } }//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 

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!

+4
source share
1 answer

This is mainly a question of layouts, and I suggest you read a tutorial on them: Layout Tutorials

In your situation, JLayeredPane may work well. It uses the default zero layout, so you will need to specify the sizes and positions of your component if you use this, but you can also give it the z-order of your components and thus be able to place the slot machine in a low position and things, which prevail over the top positions. Again, the tutorial will help with the details: JLayeredPane Tutorial

In addition, you really should not read any files using the paintComponent method, especially a file with an image that does not change - the fact that the feeling of re-reading the image slows down the picture every time you redraw the image, when the image does not change, and you can you just save it in a variable in the class? So read it once in the class constructor, and then save it in a variable.

+7
source

All Articles