学习笔记二十四:绘图技术

人的大脑和肢体一样,多用则灵,不用则废。——茅以升


本讲内容:绘图技术


一、绘图原理

Component类提供了两个和绘图相关最重要的方法:

1、paint(Graphics g)绘制组件的外观。

2、repaint()刷新组件的观。

当组件第一次在屏幕显示的时候,程度会自动调用paint()方法来绘制组件

在以下情况paint()将会被调用:

1、窗口最小化,再最大化。

2、窗口的大小发生变化。

3、repaint函数被调用。


public class Text extends JFrame{
	MyPanel mb=null;
	
	public static void main(String[] args) {
		Text t=new Text();
	}
	
	public Text() {
		mb=new MyPanel();
		this.add(mb);
		
		//设置窗体属性
		this.setTitle("绘图—小劲");
		this.setIconImage((new ImageIcon("images\\2.gif")).getImage());
		this.setLocation(300, 300);
		this.setSize(400,400);
		this.setResizable(false);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

//定义一个MyPanel  用于绘图和实现绘图的区域
class MyPanel extends JPanel{
	//这个方法是继承父类JPanel的,Graphics 绘图的重要类   画笔的意思
	public void paint(Graphics g) {
		super.paint(g);//一般要写,调用父类函数完成初始化任务
		g.drawOval(0, 0, 80, 90);//椭圆,左上角坐标为(0,0)横长80竖长90(<span style="font-family: Arial;">先画坐标)</span>
		g.drawLine(100, 0, 200, 100);//线,始点坐标(100,0)终点坐标(200,100)
		g.drawRect(220, 20, 60, 80);//矩形,左上角坐标为(220,20)横长60竖长80
		g.draw3DRect(300, 20, 60, 80, true);//3d矩形
		g.setColor(Color.blue);//如果不设置颜色,默认是黑色
		g.fillRect(10, 120, 60, 80);//填充颜色
		g.fill3DRect(10, 220, 60, 80, true);
		g.setColor(Color.red);//如果不设置会用上面的颜色
		g.fillOval(100, 120, 80, 80);
		
	}
}



二、画图片、字符串

public class Text extends JFrame{
	MyPanel mb=null;
	
	public static void main(String[] args) {
		Text t=new Text();
	}
	
	public Text() {
		mb=new MyPanel();
		this.add(mb);
		
		//设置窗体属性
		this.setTitle("绘图—小劲");
		this.setIconImage((new ImageIcon("images\\1.gif")).getImage());
		this.setLocation(300, 300);
		this.setSize(400,400);
		this.setResizable(false);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

class MyPanel extends JPanel{
	public void paint(Graphics g) {
		super.paint(g);
		//绘图用到的图片要拷贝在src目录下/表示当前目录
		Image p=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/p1.jpg"));
		g.drawImage(p,30,20,300,250,this);//画图片
		g.setColor(Color.red);//设置画笔的颜色
		g.setFont(new Font("华文彩云",Font.BOLD,50));//画字符串   BOLD,50粗体大小50
		g.drawString("阳西一中", 90, 330);
	}
}



三、综合案例

public class Text extends JFrame{
	JPanel mb1;
	JButton an1,an2,an3,an4,an5,an6,an7,an8;
	MyPanel mb=null;
	
	public static void main(String[] args) {
		Text t=new Text();
	}
	
	public Text() {
		mb1 = new JPanel(new GridLayout(6,3,200,2));
		an1=new JButton("黑客技术");
		an2=new JButton("软件破解");
		an3=new JButton("编程大牛");
		an4=new JButton("网页设计");
		an5=new JButton("PHP技术");
		an6=new JButton("C++技术");
		an7=new JButton("Java技术");
		an8=new JButton("linux技术");
		mb1.add(an1); mb1.add(an2); mb1.add(an3); mb1.add(an4);
		mb1.add(an5); mb1.add(an6); mb1.add(an7); mb1.add(an8);
		
		mb=new MyPanel();
		
		this.add(mb1,BorderLayout.SOUTH);
		this.add(mb);
		
		//设置窗体属性
		this.setTitle("技术大牛—小劲");
		this.setIconImage((new ImageIcon("images\\1.gif")).getImage());
		this.setLocation(300, 300);
		this.setSize(400,400);
		this.setResizable(false);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

class MyPanel extends JPanel{
	public void paint(Graphics g) {
		super.paint(g);
		Image p=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/p2.jpg"));
		g.drawImage(p, 20, 30, 85, 100, this);
		g.drawString("我是蛋疼的程序猿", 130, 50);
		g.drawString("我是90's 后", 130, 70);
		g.drawString("我是黑阔", 130, 90);
		g.drawString("我习惯一个人孤独的感觉", 130, 110);
	}
}



本讲就到这里,Take your time and enjoy it


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值