package com.han;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* 相比(Graphics_7)而言:拖动窗口调整大小时,也没有白光闪烁。就显示出了
* Canvas与JPanel以及Container的区别。
* @author HAN
*
*/
@SuppressWarnings("serial")
public class Graphics_9 extends JFrame {
public Graphics_9() {
// TODO Auto-generated constructor stub
setContentPane(new MyContainer());
JButton button = new JButton("HAN Gaowen");
getContentPane().add(button);
}
class MyContainer extends Container {
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
URL imgURL = this.getClass().getResource("/images/Lighthouse.jpg");
Image image = Toolkit.getDefaultToolkit().getImage(imgURL); // ImageIO.read() is returned to BufferedImage.
g2.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Graphics_9 frame = new Graphics_9();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("绘制图片");
frame.setSize(440, 300);
frame.setVisible(true);
}
}
Java Graphics_9
最新推荐文章于 2025-09-12 18:08:49 发布