学习笔记二十四:绘图技术
发表时间:2020-10-19
发布人:葵宇科技
浏览次数:46
人的大年夜脑和肢体一样,多用则灵,不消则废。——茅以升
本讲内容:画图技巧
一、画图道理
Component类供给了两个和画图相干最重要的办法:
1、paint(Graphics g)绘制组件的外不雅。
2、repaint()刷新组件的不雅。
当组件第一次在屏幕显示的时刻,程度换辉动调用paint()办法来绘制组件
在以下情况paint()将会被调用:
1、窗口最小化,再最大年夜化。
[img]http://img.blog.csdn.net/20150106155110218?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlndW9qaW4xMjMw/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
3、repaint函数被调用。
2、窗口的大年夜小产生变更。
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.setDefaultClo搜刮引擎优化peration(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.setDefaultClo搜刮引擎优化peration(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);
}
}[img]http://img.blog.csdn.net/20150106172243015?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlndW9qaW4xMjMw/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
三、综合案例
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.setDefaultClo搜刮引擎优化peration(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);
}
}[img]http://img.blog.csdn.net/20150106174422470?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlndW9qaW4xMjMw/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
本讲就到这里,Take your time and enjoy it








