你如何使用图像作为背景并将图像放在前面? [英] How do you use an image as background and place an image in front of that?

查看:167
本文介绍了你如何使用图像作为背景并将图像放在前面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了许多方法,但都没有成功。他们要么没有显示图像,要么他们让背景图像消失......你有什么建议吗?这是我的代码:

I have tried loads of ways, but none of them succeeded. they either didn't show the image, or they made the background image disappear... do you have any suggestions? Here is my code:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main extends JFrame{

int x, y;
Image Dak;
Image Levels;
private Image dbImage;
private Graphics dbg;

public Main(){

 setTitle("Help de Pieten");
 setSize(2000, 720);
 setResizable(true);
 setVisible(true);
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              try {
 this.setContentPane(
 new JLabel(new ImageIcon(ImageIO.read(new  File("Image1.gif")))));
} catch (IOException e) {}

validate(); 

ImageIcon i = new ImageIcon("Image2.gif");
    Levels = i.getImage();


    x = 100;
    y = 100;
}    

public void paint(Graphics g){
        dbImage = createImage(getWidth(), getHeight());
    dbg = dbImage.getGraphics();
    paintComponent(dbg);
    g.drawImage(dbImage, 0, 0, this);
}


public void paintComponent(Graphics g){

 g.drawImage(Levels, x, y, this);
   repaint();
}

public static void main(String[] args) {
    Main main = new Main();

}

}

那怎么做我在背景前面得到图像而没有使背景消失?

So how do I get images in front of the background without making the background dissapear?

推荐答案

首先,避免覆盖 paint 顶级容器的方法,如 JFrame ,它们不是双缓冲的,它们有一个复杂的组件层次结构,你不用它希望参与其中

To start with, avoid overriding the paint methods of top level containers like JFrame, they aren't double buffered and they have a complex component hierarchy with which you don't want to get involved with

相反,首先从 JPanel 扩展,Swing组件默认为双缓冲,所以你不必担心自己实现它,并覆盖它的 paintComponent 方法并在其中执行自定义绘画。

Instead, start by extending from something JPanel, Swing components are double buffered by default, so you don't need to worry about implementing it all yourself, and overriding it's paintComponent method and performing your custom painting within it.

查看执行自定义绘画疼痛详情请参阅AWT和Swing

Swing中的绘画遵循画家画布范例,即首先绘制的任何内容,将被覆盖通过下面绘制的任何内容,所以为此,请确保先绘制背景,然后按每个图层绘制,以便显示它。

Paint in Swing follows the "painters canvas" paradigm, that is, whatever is painted first, will be covered over by whatever is painted next, so to this end, make sure you paint your background first, followed by each layer in order you want it to appear.

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Images {

    public static void main(String[] args) {
        new Images();
    }

    public Images() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage background;
        private BufferedImage foreground;

        public TestPane() {
            try {
                background = ImageIO.read(new File("background image"));
                foreground = ImageIO.read(new File("foreground image"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return background == null ? new Dimension(200, 200) : new Dimension(background.getWidth(), background.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            if (background != null) {
                int x = (getWidth() - background.getWidth()) / 2;
                int y = (getHeight() - background.getHeight()) / 2;
                g2d.drawImage(background, x, y, this);

            }
            if (foreground != null) {
                int x = (getWidth() - foreground.getWidth()) / 2;
                int y = (getHeight() - foreground.getHeight()) / 2;
                g2d.drawImage(foreground, x, y, this);
            }
            g2d.dispose();
        }

    }

}

这篇关于你如何使用图像作为背景并将图像放在前面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆