23 Container类 im port iava. awt public class My PanelDemo extends Frame t Panel pnl=new PanelO public My PanelDemo(String title) super( title);∥设置标题 this inito; public void init(t pnl setsize(100, 100); pnl setBackground(Color,yellow); this. setLayout(null) ∥不设置布局管理器 this setBackground(color blue) this setsize(200, 200); this, add(pn);∥把中间容器添加在顶级容器中 this setVisible(true); public static void main(String args My Panel Demo demo=new My PanelDemo("My PanelDeom! )
绪 论 Porgramming in Java 2008.3-2008.7 by Yanjun Zhang,Department of Computer HanDan Collage. 2.3 Container类 import java.awt.*; public class MyPanelDemo extends Frame { Panel pnl=new Panel(); public MyPanelDemo(String title){ super(title); //设置标题 this.init(); } public void init(){ pnl.setSize(100,100); pnl.setBackground(Color.yellow); this.setLayout(null); //不设置布局管理器 this.setBackground(Color.blue); this.setSize(200,200); this.add(pnl); //把中间容器添加在顶级容器中 this.setVisible(true); } public static void main(String args[]){ MyPanelDemo demo=new MyPanelDemo("MyPanelDeom!"); } }
24 Graphics类 ava 在 Windows中,当窗口被激活时,系统需要重画窗口上曾被覆盖部分 的图形图像(标准组件的图形)。Java允许在组件上绘制图形、打印文字、 显示图像等,为此在 java. awt. Componet类中声明了pant0和 repaint方 法用于显示和刷新图形。 public void paint( Graphics g:如果在程序中重写某组件的pant0方 系统将自动在该组件上绘制图形,程序中无须调用该方法。 public void repaint():程序中调用 repaint0方法时,系统将再次执 行 paint0方法,重新绘制图形。 Graphics类是所有图形上下文 Context的抽象基类,允许应用程序在 组件(已经在各种设备上实现)以及闭屏图像上进行绘制。 Graphics类 是一种特殊的抽象类,它必须依赖于某一个组件,但无需通过new实例化 而可以直接使用。 1声明的类是组件类 Component的子类,可直接重写pan(方法, 通过 Graphics对象g直接在组件上绘图 Por gr amming in Java 2008.3-2008 7 by Yan jun Zhang, Depar tment of Computer HanDan Col lage
绪 论 Porgramming in Java 2008.3-2008.7 by Yanjun Zhang,Department of Computer HanDan Collage. 2.4 Graphics类 在Windows中,当窗口被激活时,系统需要重画窗口上曾被覆盖部分 的图形图像(标准组件的图形)。Java允许在组件上绘制图形、打印文字、 显示图像等,为此在java.awt.Componet类中声明了paint()和repaint()方 法用于显示和刷新图形。 public void paint(Graphics g):如果在程序中重写某组件的paint()方 法,系统将自动在该组件上绘制图形,程序中无须调用该方法。 public void repaint():程序中调用repaint()方法时,系统将再次执 行paint()方法,重新绘制图形。 Graphics 类是所有图形上下文Context的抽象基类,允许应用程序在 组件(已经在各种设备上实现)以及闭屏图像上进行绘制。Graphics类 是一种特殊的抽象类,它必须依赖于某一个组件,但无需通过new实例化 而可以直接使用。 1.声明的类是组件类Component的子类,可直接重写paint()方法, 通过Graphics对象g直接在组件上绘图:
24 Graphics类 import java. awt. *r public class My Frame extends Framef My Frame(t super(" Java U窗口") setsize(200, 200); public void paint(Graphics g)t gsetcolor(Color. RED); g drawLine(10,20,140,140) g drawString("中华人民共和国",100,100) public static void main(String args[]i new My Frame( setVisible(true) Por gr amming in Java 2008.3-2008 7 by Yan jun Zhang, Depar tment of Computer HanDan Col lage
绪 论 Porgramming in Java 2008.3-2008.7 by Yanjun Zhang,Department of Computer HanDan Collage. 2.4 Graphics类 import java.awt.*; public class MyFrame extends Frame{ MyFrame(){ super("Java GUI窗口"); setSize(200,200); } public void paint(Graphics g){ g.setColor(Color.RED); g.drawLine(10,20,140,140); g.drawString("中华人民共和国",100,100); } public static void main(String args[]){ new MyFrame().setVisible(true);; } }
24 Graphics类 ava 2如果声明的类不是组件类 Component的子类: 则欲绘图的组件必须使用 getGraphics(方法获得一个包含有该组件 显示外观信息的 Graphics对象然后可以在这个组件上进行绘图操作。 public Graphics get Graphics import java. awt. public class Myt public static void main(String args])t Frame f=new frame 0: f setsize(200,200) f setVisible(true) f. Graphics. drawString( test,50, 50); Por gr amming in Java 2008.3-2008 7 by Yanjun Zhang, Depar tment of Computer HanDan Col lage
绪 论 Porgramming in Java 2008.3-2008.7 by Yanjun Zhang,Department of Computer HanDan Collage. 2.4 Graphics类 2.如果声明的类不是组件类Component的子类: 则欲绘图的组件必须使用getGraphics()方法获得一个包含有该组件 显示外观信息的Graphics对象然后可以在这个组件上进行绘图操作。 public Graphics getGraphics() import java.awt.*; public class My{ public static void main(String args[]){ Frame f=new Frame(); f.setSize(200,200); f.setVisible(true); f.getGraphics().drawString("test",50,50); } }
24 Graphics类 ava Graphics类还提供了一系列的方法来绘制几何图形、文字、图象以及 动画: ☆ Lines: drawLine0 ☆ Rectangles: drawRect(0、 filllRect0、 clearRect0 Raised or lowered rectangles: draw 3DRect( fill3DRecto Round-edged rectangles: draw RoundRect(, fillRoundRecto 令 Ovals: drawoval0和 filloval0 令Arcs: drawArc(和fArc0 令 Polygons: drawPolygon0和 fillPolygon(0) 令Text: drawString0 ☆mage: drawImage0 ☆ Animation:通过一个循环来完成 Por gr amming in Java 2008.3-2008 7 by Yanjun Zhang, Depar tment of Computer HanDan Col lage
绪 论 Porgramming in Java 2008.3-2008.7 by Yanjun Zhang,Department of Computer HanDan Collage. 2.4 Graphics类 Graphics类还提供了一系列的方法来绘制几何图形、文字、图象以及 动画: ❖ Lines:drawLine() ❖ Rectangles:drawRect()、fillRect()、clearRect() ❖ Raised or lowered rectangles:draw3DRect()、fill3DRect() ❖ Round-edged rectangles:drawRoundRect(),fillRoundRect() ❖ Ovals:drawOval() 和 fillOval() ❖ Arcs:drawArc() 和 fillArc() ❖ Polygons :drawPolygon() 和 fillPolygon() ❖ Text :drawString() ❖ Image :drawImage() ❖ Animation :通过一个循环来完成