我正在使用JFrame,並且在我的框架上保留了背景圖片。現在的問題是,圖像的大小小於框架的大小,所以我必須再次在窗口的空白部分保留相同的圖像。如果用戶點擊最大化按鈕,我可能不得不在運行時將圖像放在框架的空白區域。任何人都可以告訴我如何做到這一點?java swing背景圖片
回答
你想要類似Windows桌面的背景圖像,當多次使用背景圖像而不是調整大小或僅顯示它居中?
您只需保留一次圖像並在paintComponent方法中多次繪製它。
你能給我一個這樣的例子代碼。如果你有。這對我很有幫助。請? – Nilesh 2010-02-09 08:45:47
查看finnw的示例 – 2010-02-09 19:29:24
聽起來好像你在談論平鋪與拉伸,儘管目前還不清楚你想要哪種行爲。
這個方案有兩個例子:
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
public static void main(String[] args) throws IOException {
final Image image = ImageIO.read(new URL("http://sstatic.net/so/img/logo.png"));
final JFrame frame = new JFrame();
frame.add(new ImagePanel(image));
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
@SuppressWarnings("serial")
class ImagePanel extends JPanel {
private Image image;
private boolean tile;
ImagePanel(Image image) {
this.image = image;
this.tile = false;
final JCheckBox checkBox = new JCheckBox();
checkBox.setAction(new AbstractAction("Tile") {
public void actionPerformed(ActionEvent e) {
tile = checkBox.isSelected();
repaint();
}
});
add(checkBox, BorderLayout.SOUTH);
};
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (tile) {
int iw = image.getWidth(this);
int ih = image.getHeight(this);
if (iw > 0 && ih > 0) {
for (int x = 0; x < getWidth(); x += iw) {
for (int y = 0; y < getHeight(); y += ih) {
g.drawImage(image, x, y, iw, ih, this);
}
}
}
} else {
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
}
}
非常感謝您的迴應其工作... – Nilesh 2010-02-11 07:51:46
嗨,你有任何建議如何平鋪圖像,而不是拉伸它,以保持圖像的質量? – NumenorForLife 2014-05-01 17:44:29
另一種方式來平鋪圖像是TexturePaint
:
public class TexturePanel extends JPanel {
private TexturePaint paint;
public TexturePanel(BufferedImage bi) {
super();
this.paint = new TexturePaint(bi, new Rectangle(0, 0, bi.getWidth(), bi.getHeight()));
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(paint);
g2.fill(new Rectangle(0, 0, getWidth(), getHeight()));
}
}
- 1. 如何更新Java swing背景圖紙?
- 2. Java swing自定義背景
- 3. Java Swing淡出背景
- 4. 背景圖片
- 5. 背景圖片
- 6. 背景圖片
- 7. 背景圖片上的背景圖像
- 8. Jumbotron背景圖片
- 9. QLineEdit背景圖片
- 10. JOptionPane背景圖片
- 11. UIProgressView背景圖片
- 12. 多背景圖片
- 13. JPanel背景圖片
- 14. Bootstrap背景圖片
- 15. UITableViewController背景圖片
- 16. AndEngine背景圖片
- 17. Joomla背景圖片?
- 18. 背景CSS圖片
- 19. Android背景圖片
- 20. Android:背景圖片
- 21. CSS背景圖片
- 22. Outlook背景圖片
- 23. Div背景圖片
- 24. ASP.NET背景圖片
- 25. ImageMagick背景圖片?
- 26. iframe背景圖片
- 27. UITableView背景圖片
- 28. createElement背景圖片
- 29. AQGridView背景圖片
- 30. NSBox背景圖片
「我必須在空白部分再次保持相同的圖像」沒有按對我來說沒有意義。你可以用你有什麼樣的東西和你想要的東西來製作一張圖片嗎? – 2010-02-09 07:36:23