Java Graphics_9

来源:互联网 发布:淘宝网女式夏装冰丝裙 编辑:程序博客网 时间:2024/05/22 15:38

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 stubsetContentPane(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 stubGraphics_9 frame = new Graphics_9();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setTitle("绘制图片");frame.setSize(440, 300);frame.setVisible(true);}}


原创粉丝点击