在图像Java上显示图像

时间:2018-11-22 04:26:04

标签: java image jframe applet

如何在applet或jframe上的另一个图像上显示图像?

我知道小程序中的一张图片是这样的:

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

对于jframe:

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

那我该如何为另一个图像添加图像呢?

2 个答案:

答案 0 :(得分:1)

如评论中所述,(Java)Applet确实已经死了,不值得追求。此答案中的ImagePanel扩展了JPanel,从理论上讲可以显示在任何基于Java Swing的顶级容器中(例如JFrameJWindowJOptionPane。 )或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);
    }
}

答案 1 :(得分:-1)

正如人们在评论中所说,Applet已经死了,您实际上不需要它们来学习GUI程序。

但是,如果您愿意,可以使用一些方法,例如使用drawImage()方法。也许是这样的:

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