-1

How do I display an image over another image on either applet or jframe?

I know for one image in applet is this:

Image Bendy;
Bendy = getImage(getDocumentBase(), "Bendy Icon.PNG");
g.drawImage(Bendy, 20, 20, this);

And for jframe:

ImageIcon kh = new ImageIcon("KH3.gif");
JLabel lab = new JLabel(kh);
add(lab);

So how do I add an image over the other for either one?

DigiLei
  • 7
  • 5
  • 2
    [Why were applets deprecated in JDK 9?](https://stackoverflow.com/questions/45535112/why-were-applets-deprecated-in-jdk-9); [Oracle reveals Java Applet API deprecation plan](https://www.theregister.co.uk/2016/08/24/oracle_reveals_java_applet_api_deprecation_plan/); [Oracle Reminds Java Developers That Soon They Won’t Have a Browser to Run Applets](https://www.infoq.com/news/2017/02/oracle-java-browser-applet) – MadProgrammer Nov 22 '18 at 04:29
  • In short, Applet's are dead, long live HTML 5 ... or something – MadProgrammer Nov 22 '18 at 04:29
  • 2
    You do custom painting by overriding the `paintComponent()` method of a `JPanel`. Read the section from the Swing tutorial on [Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) for more information and working example. Then you can use the `Graphics.drawImage(...)` method. – camickr Nov 22 '18 at 04:29
  • Hey @DigiLei , did my answer help you solve your problem? – PradyumanDixit Nov 22 '18 at 16:50
  • Kinda. I figured out that by stacking them in the right order like this: pan.add(kh3); pan.add(kh2); It would print one image over the other if placed correctly. – DigiLei Nov 22 '18 at 17:50
  • @DigiLei consider marking the answer as correct by clicking the tick mark like V shaped button next to the answer, this helps future readers of the question and I'd appreciate that too, Cheers! :) – PradyumanDixit Nov 23 '18 at 05:34
  • `ImageIcon("KH3.gif");` Is that an animated GIF? – Andrew Thompson Nov 23 '18 at 07:32

2 Answers2

1

As mentioned in comments, (Java) Applets are indeed dead and not worth pursuing. The ImagePanel in this answer extends JPanel and could theoretically be displayed in any Java Swing based top-level container (e.g. JFrame, JWindow, JOptionPane..) or in another container like a JPanel.

enter image description here

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.Random;
import javax.imageio.ImageIO;
import java.net.URL;

public class StackedImages {

    private JComponent ui = null;
    String path1 = "https://i.stack.imgur.com/F0JHK.png";
    String path2 = "https://i.stack.imgur.com/gJmeJ.png";

    class ImagePanel extends JPanel {

        private final BufferedImage[] images;
        private final Random rand = new Random();
        private final Dimension prefeSize = new Dimension(400, 400);

        ImagePanel(BufferedImage[] images) {
            this.images = images;
            setBackground(Color.WHITE);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            for (int ii = 0; ii < 200; ii++) {
                int x = rand.nextInt(prefeSize.width);
                int y = rand.nextInt(prefeSize.height);
                int ind = rand.nextInt(images.length);
                g.drawImage(images[ind], x, y, this);
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return prefeSize;
        }
    }

    StackedImages() {
        try {
            initUI();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public final void initUI() throws Exception {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        BufferedImage[] bi = new BufferedImage[2];
        bi[0] = ImageIO.read(new URL(path1));
        bi[1] = ImageIO.read(new URL(path2));
        ui.add(new ImagePanel(bi));
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception useDefault) {
            }
            StackedImages o = new StackedImages();

            JFrame f = new JFrame(o.getClass().getSimpleName());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationByPlatform(true);

            f.setContentPane(o.getUI());
            f.pack();
            f.setMinimumSize(f.getSize());

            f.setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
-1

As said by people in comments, Applets are dead, you don't actually need them to learn GUI programs.

But still, if you want there are some ways you can do so, like using drawImage() method. Maybe something like this:

try
{
    BufferedImage background = ImageIO.read(new File("..."));
    BufferedImage logo = ImageIO.read(new File("..."));

    Graphics g = background.getGraphics();
    g.drawImage(logo, 0, 0, null);
}
catch (Exception e)
{
    e.printStackTrace();
}
PradyumanDixit
  • 2,372
  • 2
  • 12
  • 20
  • 1) `BufferedImage background = ImageIO.read(new File("..."));` Most images used in apps. are embedded resources and must be loaded by URL, but do it once when the class is constructed, as the usual paint mechanisms might be called at any time by the run-time. 2) `Graphics g = background.getGraphics();` This is not the way to approach custom painting. See camickr's comment for the correct way. 3) `g.drawImage(logo, 0, 0, null);` Every `JComponent` is an image observer. So that should be `g.drawImage(logo, 0, 0, this);` 4) *"Maybe something like this:"* Except .. completely different. – Andrew Thompson Nov 23 '18 at 07:30