我试图制作一个Swing程序,我目前在整个屏幕上有一个图像作为背景延伸,现在我只想在这个图像上绘制一个较小的图像
我的视频构造函数并绘制背景和前景图像:
public Video(BufferedImage foreground, BufferedImage background){
pane = this.getContentPane(); //get the content pane to place components
pane.setLayout(null); //use absolute positioning (using Insets)
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
this.setVisible(true);
this.setBounds(0,0,(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(),(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
DrawImage panel = new DrawImage(background);
add(panel);
this.image = foreground;
DrawForeground fg = new DrawForeground(image);
add(fg);
fg.setVisible(true);//don't make image visible until showImageAt() gives it a place to be
}
我的drawImage方法(绘制前景非常相似,但大小不同)
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
class DrawImage extends JPanel {
private BufferedImage img;
public DrawImage(BufferedImage img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0,this.getWidth(),this.getHeight(), null);
}
}