Java Graphics_9

Java Graphics_9_第1张图片

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,String,image,Class,button,Constructor)