Search code examples
javaswingjpanelpaintcomponent

How do I display multiple Images on JPanel?


I've been trying to figure this out forever, but I can't seem to figure it out.

I would like to display more than one image on the screen in the same JPanel but for some reason, it only displays the last image from the paint component

I'm trying to make a fruit ninja style game and would like to draw the fruits out of the frame before animations take place.

Does anyone have an idea of how to do this?Any help would be greatly appreciated...

import javax.swing.*;//imports JPanel class
import java.awt.*;//imports the Graphics class

public class FruitNinja extends JPanel {

   private Image dojo;
   private Image apple;
   private Image orange;
   private Image pineapple;
   private Image strawberry;
   private Image banana;

   private Timer timer;
   private int x, y;

   public FruitNinja() { // a constructor to set up graphics windo
      super();
      setBackground(Color.WHITE);
      loadImage();
      x = 25;
      y = 25;


   }



   private void loadImage() {
      ImageIcon ii = new ImageIcon("Dojo.jpg");
      dojo = ii.getImage();

      ImageIcon oo = new ImageIcon("Orange.ico");
      orange = oo.getImage();

      ImageIcon ss = new ImageIcon("Strawberry.png");
      strawberry = ss.getImage();

      ImageIcon bb = new ImageIcon("Banana.png");
      banana = bb.getImage();

      ImageIcon pp = new ImageIcon("Pineapple.png");
      pineapple = pp.getImage();

      ImageIcon aa = new ImageIcon("Apple.png");
      apple = aa.getImage();


   }

   public void paintComponent(Graphics g){ // draw graphics in the panel

      super.paintComponent(g);// to make panel display correctly
      g.drawImage(dojo, 0,0, this);
      //draws out dojo


      super.paintComponent(g);// to make panel display correctly
      g.drawImage(apple, 0,0, this);

      super.paintComponent(g);// to make panel display correctly
      g.drawImage(orange, 0,0, this);

      super.paintComponent(g);// to make panel display correctly
      g.drawImage(pineapple, 0,0, this);

      super.paintComponent(g);// to make panel display correctly
      g.drawImage(banana, 0,0, this);

      super.paintComponent(g);// to make panel display correctly
      g.drawImage(strawberry, 0,0, this);

      //draws out the fruits somewhere
   }

  /* 
   @Override
    public void actionPerformed(ActionEvent e) {

      x += 5;
      y += 5;

      if (y > getHeight()) {
         y = 25;
         x = 25;
      }
      repaint();
   }
*/

   public static void main(String[] args) { 

      FruitNinja panel = new FruitNinja(); // window for drawing  
      JFrame f = new JFrame(); // the program itself
      f.setTitle("Fruit Ninja");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//when the X button is clicked, the program quits
      f.setSize(1280,800);//size of the frame
      Container pane = f.getContentPane();//pane refers to the interior of the JFrame

      FruitNinja p1 = new FruitNinja();

      pane.add(p1);//add the FacePanel object to the interior of the frame
      f.setVisible(true);

   }
}

Also unrelated to this current question, since I'm trying to make a FruitNinja like game, how do I make it so the code registers that my mouse is there(so it slices the fruit when my mouse hovers over the fruit)? is it mouseListenter?


Solution

  • I would suggest having a look at Painting in AWT and Swing to understand why you're having the problem you're having

    One of the jobs that paintComponent does it prepares the Graphics context for painting of the component, it typically does this by filling it with the background color of the component

    So based on you code, that would suggest you only need to call paintComponent once, at the start of the method

    public void paintComponent(Graphics g){ // draw graphics in the panel
    
      super.paintComponent(g);// to make panel display correctly
      g.drawImage(dojo, 0,0, this);
      //draws out dojo
      g.drawImage(apple, 0,0, this);
      g.drawImage(orange, 0,0, this);
      g.drawImage(pineapple, 0,0, this);
      g.drawImage(banana, 0,0, this);
      g.drawImage(strawberry, 0,0, this);
      //draws out the fruits somewhere
    }
    

    So, now all the images should be painted, on top of each other

    Also unrelated to this current question, since I'm trying to make a FruitNinja game, how do I make it so the code registers that my mouse is there(so it slices the fruit when my mouse hovers over the fruit)? is it mouseListenter?

    You're probably looking for a MouseMotionListener, have a look at How to Write a Mouse Listener for more details