gpt4 book ai didi

java - 如何使用图像作为背景并在其前面放置图像?

转载 作者:行者123 更新时间:2023-11-30 03:42:24 24 4
gpt4 key购买 nike

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

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

}

}

那么如何在不使背景消失的情况下获得背景前面的图像呢?

最佳答案

首先,避免覆盖 paint顶级容器的方法,如 JFrame ,它们不是双缓冲的,并且它们具有复杂的组件层次结构,您不想参与其中

相反,从 JPanel 扩展开始,Swing 组件默认情况下是双缓冲的,因此您无需担心自己全部实现并覆盖它的 paintComponent方法并在其中执行您的自定义绘画。

看看Performing Custom PaintingPainting in AWT and Swing了解更多详情。

Swing 中的绘制遵循“画家 Canvas ”范例,也就是说,首先绘制的内容将被接下来绘制的内容覆盖,因此为此,请确保先绘制背景,然后绘制每个图层您希望它出现的顺序。

Example

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

}

}

关于java - 如何使用图像作为背景并在其前面放置图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26545536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com