我想用另一个面板替换面板(所有面板都有透明部分)。代码如下:
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PhotoFrame extends JFrame {
public class PhotosDisplayPanel extends JPanel {
private static final long serialVersionUID = 1L;
private String[] imgsLink = null;
private long delay = 1000;
private long period = 1000;
private int curIdx = 0;
private PhotoDisplayPanel currentPhoto = null;
public PhotosDisplayPanel(String[] imgsLink) {
super();
this.imgsLink = imgsLink;
setBackground(new Color(0, 0, 0, 0));
setLayout(new GridLayout(1, 1));
currentPhoto = new PhotoDisplayPanel(imgsLink[curIdx]);
add(currentPhoto);
}
public void start() {
if (imgsLink == null) {
return;
}
curIdx = -1;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
curIdx++;
if (curIdx >= imgsLink.length) {
curIdx = 0;
}
displayNextImage();
}
}, delay, period);
}
protected void displayNextImage() {
if (currentPhoto != null) {
remove(currentPhoto);
}
revalidate();
repaint();
currentPhoto = new PhotoDisplayPanel(imgsLink[curIdx]);
add(currentPhoto);
revalidate();
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
}
}
public class PhotoDisplayPanel extends JPanel {
private static final long serialVersionUID = 1L;
private String imgLink;
public PhotoDisplayPanel(String imgLink) {
super();
this.imgLink = imgLink;
}
@Override
protected void paintComponent(Graphics g) {
if (imgLink != null && !imgLink.equals("")) {
System.out.println("Draw image");
// get dimension of the panel
int pWidth = getWidth();
int pHeight = getHeight();
g.setColor(new Color(255, 0, 0, 50));
g.fillRect(0, 0, pWidth, pHeight);
Image img = new ImageIcon(imgLink).getImage();
// Calculate positions and dimensions
int imgWidth = img.getWidth(this);
int imgHeight = img.getHeight(this);
int iwidth = 0;
int iheight = 0;
if (imgWidth / imgHeight > pWidth / pHeight) {
iwidth = pWidth;
iheight = imgHeight * pWidth / imgWidth;
} else {
iheight = pHeight;
iwidth = imgWidth * pHeight / imgHeight;
}
int ix = (pWidth - iwidth) / 2;
int iy = (pHeight - iheight) / 2;
// Fill the picture to the panel
g.drawImage(img, ix, iy, iwidth, iheight, this);
} else {
super.paintComponent(g);
}
}
}
private static final long serialVersionUID = 1L;
private static final int defaultWidth = 650;
private static final int defaultHeight = 400;
private int width = defaultWidth;
private int height = defaultHeight;
private PhotosDisplayPanel photoPanel; // is view panel which display
// input and output
private String[] imgsLink;
public PhotoFrame() {
initialize();
imgsLink = new String[] { "background.png", "screenShot.jpg" };
createControls();
}
private void createControls() {
setLayout(new GridLayout(0, 1));
photoPanel = new PhotosDisplayPanel(imgsLink);
add(photoPanel);
photoPanel.start();
}
private void initialize() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(width, height);
setTitle("PhotosFrame");
setAlwaysOnTop(false);
setBackground(Color.CYAN);
setLayout(new GridLayout(1, 1));
setContentPane(new JLabel(new ImageIcon("blue_sunset.jpg")));
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new PhotoFrame().setVisible(true);
}
});
}
}
其中,currentPhoto是一个透明背景的面板。但是,旧面板的图像没有完全清理,我可以看到新面板下的旧图像。有没有办法全面清除旧面板的形象? 感谢
答案 0 :(得分:1)
但是,旧面板的图像并未完全清除,
查看Backgrounds With Transparency可能的原因和几个解决方案。
基本上在使用透明背景时,Swing不知道需要重新绘制背景,因此您需要强制重新绘制。