Graphics 绘图类: 提供两个方法。Paint (绘图,被系统自动调用) repaint(重绘)
Paint 调用原理(1.窗口最大化,再最小化
package com.sunzhiyan; import java.awt.*; import javax.swing.*; public class Demo_5 extends JFrame{ /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Demo_5 demo5 = new Demo_5(); } public Demo_5() { //将面板实例化加载到窗体里面 Mypanel panel = new Mypanel(); this.add(panel); this.setTitle("draw"); this.setLocation(400, 200); this.setSize(400, 400); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); //super(); // TODO Auto-generated constructor stub } } //定义一个面板,用来画图 class Mypanel extends JPanel{ //定义一个画笔工具 public void paint (Graphics g){
//兼容写法
super.paint(g); g.drawRect(40, 40, 40, 40);
//填充矩形
g.fillRect(20, 20, 40, 40);
//写文字
g.setFont(new Font("华文彩云",Font.BOLD,30)); g.drawString("你好", 60, 60);
} }
Java SE (2)之 Graphics 画图工具,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/sunxun/p/3833621.html