7 1 import java awt import java awt

  • Slides: 46
Download presentation

例7 -1 通过设置组件的字体属性来改变字体 import java. awt. *; import java. awt. event. *; public class

例7 -1 通过设置组件的字体属性来改变字体 import java. awt. *; import java. awt. event. *; public class Font. Test extends Frame{ Label label = new Label(“当前所使用字体:宋体,粗体, 24号字”, Label. CENTER); // Label. CENTER:中�� � Font font = new Font(“宋体”, Font. BOLD, 24)//� 置字体、 字型、字号 Font. Test(){ super("字体示例"); label. set. Font(font); //� 置� 象label的字体 Java程序设计案例教程

add(label); this. add. Window. Listener(new Window. Adapter(){ //匿名内部� 做� 听器 public void window. Closing(Window.

add(label); this. add. Window. Listener(new Window. Adapter(){ //匿名内部� 做� 听器 public void window. Closing(Window. Event e){ System. exit(0); }}); set. Size(500, 200); set. Location. Relative. To(null); //在屏幕中�� 示窗口 set. Visible(true); } public static void main(String [] args){ new Font. Test(); } } Java程序设计案例教程

7. 1. 1 字体 Font� 使用方法 Font font = new Font("宋体", Font. BOLD, 24);

7. 1. 1 字体 Font� 使用方法 Font font = new Font("宋体", Font. BOLD, 24); Label label = new Label("宋体, 加粗, 24号字"); label. set. Font(font); 或 Label label = new Label("宋体, 加粗, 24号字"); label. set. Font(new Font("宋体", Font. BOLD, 24)); Java程序设计案例教程

例7 -2 通过设置组件的前景色来设置显示颜色 import java. awt. *; import javax. swing. *; public class Color.

例7 -2 通过设置组件的前景色来设置显示颜色 import java. awt. *; import javax. swing. *; public class Color. Test extends JFrame { JLabel label = new JLabel(); Color. Test(){ Color color = new Color(255, 0, 0); label. set. Foreground(color); label. set. Font(new Font("宋体", Font. BOLD, 24)); label. set. Text("同一个世界,同一个梦想"); add(label); } Java程序设计案例教程

public static void main(String [] args){ Color. Test frame = new Color. Test(); frame.

public static void main(String [] args){ Color. Test frame = new Color. Test(); frame. set. Location. Relative. To(null); frame. set. Size(300, 150); frame. set. Default. Close. Operation(JFrame. EXIT_ON_ CLOSE); frame. set. Visible(true); } } Java程序设计案例教程

例7 -3 利用Graphics类绘制一个静止的风扇 import java. awt. *; import java. awt. event. *; import javax.

例7 -3 利用Graphics类绘制一个静止的风扇 import java. awt. *; import java. awt. event. *; import javax. swing. *; public class Still. Fan extends JFrame{ Still. Fan(){ set. Title("� 扇静� "); add(new Arcs. Panel()); } public static void main(String [] args){ Still. Fan frame = new Still. Fan(); frame. set. Location. Relative. To(null); frame. set. Default. Close. Operation(JFrame. EXIT_ON_ CLOSE); frame. set. Size(300, 300); frame. set. Visible(true); } } Java程序设计案例教程

class Arcs. Panel extends JPanel { protected void paint. Component(Graphics g){ super. paint. Component(g);

class Arcs. Panel extends JPanel { protected void paint. Component(Graphics g){ super. paint. Component(g); //� 用父� 方法 int x. Center = get. Width() / 2; int y. Center = get. Height() / 2; int radius = (int)((get. Width() < get. Height() ? get. Width() : get. Height()) * 0. 45); //确定扇形的半径 int x = x. Center - radius; //确定� 制扇形的x起始坐� int y = y. Center - radius; //确定� 制扇形的y起始坐� g. fill. Arc(x, y, 2*radius, 0, 30); //� 制第 1个扇形 g. fill. Arc(x, y, 2*radius, 90, 30); //� 制第 2个扇形 g. fill. Arc(x, y, 2*radius, 180, 30); //� 制第 3个扇形 g. fill. Arc(x, y, 2*radius, 270, 30); //� 制第 4个扇形 } Java程序设计案例教程 }

例7 -5 利用javax. swing. Image. Icon类创建和显示图像 import javax. swing. *; import java. awt. *;

例7 -5 利用javax. swing. Image. Icon类创建和显示图像 import javax. swing. *; import java. awt. *; public class Image_Icon extends JFrame { Image. Icon ch. Icon = new Image. Icon("china. gif"); //� 建� 像��� 象 Image. Icon us. Icon = new Image. Icon("us. gif"); Image. Icon ca. Icon = new Image. Icon("can. gif"); public Image_Icon() { set. Layout(new Grid. Layout(1, 3, 5, 5)); add(new JLabel(ch. Icon)); //� 建具有� 像的�� 并将�� 加到窗口上 add(new JLabel(us. Icon)); add(new JLabel(ca. Icon)); } Java程序设计案例教程

public static void main(String[] args) { Image_Icon frame = new Image_Icon(); frame. set. Title("利用javax.

public static void main(String[] args) { Image_Icon frame = new Image_Icon(); frame. set. Title("利用javax. swing. Image. Icon�� 建和 � 示� 像� 象"); frame. set. Default. Close. Operation(JFrame. EXIT_ON_ CLOSE); frame. set. Size(1000, 240); frame. set. Location. Relative. To(null); frame. set. Visible(true); } } Java程序设计案例教程

7. 3. 1 显示图像 Image. Icon� 的构造方法 public Image. Icon(String filename) 参数:filename—指定文件名或路径的String。 根据指定的文件� 建一个Image.

7. 3. 1 显示图像 Image. Icon� 的构造方法 public Image. Icon(String filename) 参数:filename—指定文件名或路径的String。 根据指定的文件� 建一个Image. Icon。指定String可以是一 个文件名或是一条文件路径。在指定一条路径� ,可使用 Internet� 准正斜杠(/)作� 分隔符。 Image. Icon� 的� 用 Image. Icon image. Icon = new Image. Icon("zhongguo. gif"); JLabel jlabel = new JLabel(image. Icon); 将� 像zhongguo. gif放到�� 上 Java程序设计案例教程

例7 -6 利用java. awt. Image类创建和显示图像 import javax. swing. *; import java. awt. *; public

例7 -6 利用java. awt. Image类创建和显示图像 import javax. swing. *; import java. awt. *; public class Image. Test extends JFrame { JPanel jpanel = new JPanel(); public Image. Test() { add(new img. Panel()); //� 建个性化面板 } public static void main(String[] args) { Image. Test frame = new Image. Test(); frame. set. Default. Close. Operation(JFrame. EXIT_ON_ CLOSE); frame. set. Title("利用Image�� 建和� 示� 像� 象"); frame. set. Size(380, 240); frame. set. Location. Relative. To(null); frame. set. Visible(true); } Java程序设计案例教程

class img. Panel extends JPanel { Image. Icon img. Icon = new Image. Icon("china.

class img. Panel extends JPanel { Image. Icon img. Icon = new Image. Icon("china. gif"); //� 建� 像��� 象 Image image = img. Icon. get. Image(); //通���� 象� 取� 像� 象 //Image image = get. Toolkit(). get. Image("china. gif"); //此行与上两行作用同 public void paint. Component(Graphics g) { super. paint. Component(g); //� 用父� 的方法,重画面板上的� 件 if(image != null) g. draw. Image(image, 0, 0, this); //在面板上� 制� 像 } } } Java程序设计案例教程

例7 -7 显示双缓冲图像 import java. awt. *; import java. applet. *; public class Double.

例7 -7 显示双缓冲图像 import java. awt. *; import java. applet. *; public class Double. Cach extends Applet{ Image image; public void init() { set. Size(350, 200); Dimension d = get. Size(); //� 得小程序窗口的尺寸 image = get. Toolkit(). get. Image("china. gif"); Image dbl. Image = create. Image(d. width, d. height); Graphics dbl. Graphics = dbl. Image. get. Graphics(); dbl. Graphics. draw. Image(image, 0, 0, this); } public void paint(Graphics g){ //Java Applet的�� 方法 g. draw. Image(image, 0, 0, null); //� 制� 像 } } Java程序设计案例教程

例7 -8 利用时间触发器制作动画 import java. awt. *; import java. awt. event. *; import javax.

例7 -8 利用时间触发器制作动画 import java. awt. *; import java. awt. event. *; import javax. swing. *; public class Timer. Moving. Ball extends JApplet implements Action. Listener{ private Timer timer = new Timer(10, this); //� 建定� 器� 象 private Image screen. Image = null; //定�� 像� 冲区� 象 private Graphics screen. Buffer = null; //定�� 像� 冲区的 � 像� 象 private int x = 5; //小球位置 private int move = 1; //小球移� 步� public void init( ){ set. Size(400, 200); screen. Image = create. Image(300, 150); //� 建� 像� 冲区� 象 screen. Buffer = screen. Image. get. Graphics( ); //� 取� 像� 冲 区的� 像� 象 Java程序设计案例教程 }

public void start( ){ timer. start(); //启� 定� 器 } public void action. Performed(Action.

public void start( ){ timer. start(); //启� 定� 器 } public void action. Performed(Action. Event e){ x += move; if((x > 205) || (x < 5)) move *= -1; //判断小球是否出界 repaint( ); //此操作将引� paint()方法自�� 行 } public void draw. Circle(Graphics g){ g. set. Color(Color. GREEN ); g. fill. Rect(0, 0, 300, 100); //画� 方形 g. set. Color (Color. RED ); g. fill. Oval(x, 5, 90); //画� } public void paint(Graphics g){ draw. Circle(screen. Buffer); //将� 像写入后台� 冲区 g. draw. Image(screen. Image, 50, this); //� 示� 像 Java程序设计案例教程 }

例7 -9 利用线程制作动画 import java. awt. *; import javax. swing. *; public class Thread.

例7 -9 利用线程制作动画 import java. awt. *; import javax. swing. *; public class Thread. Moving. Ball extends JApplet implements Runnable { Image screen. Image = null; Graphics screen. Buffer = null; private Thread thread; //定�� 程� 象 private int x = 5; private int move = 1; public void init() { set. Size(400, 200); screen. Image = create. Image(300, 150); screen. Buffer = screen. Image. get. Graphics(); } Java程序设计案例教程

public void start() { thread = new Thread(this); //� 建� 程� 象 thread. start();

public void start() { thread = new Thread(this); //� 建� 程� 象 thread. start(); //启�� 程 } public void run() { while (true) { //无限循� x += move; if ((x > 205) || (x < 5)) move *= -1; repaint(); try { Thread. sleep(20); //� 程休眠 20毫秒 } catch (Interrupted. Exception e) {/捕� 异常并� 理 } } } Java程序设计案例教程

public void draw. Circle(Graphics g) { g. set. Color(Color. GREEN); g. fill. Rect(0, 0,

public void draw. Circle(Graphics g) { g. set. Color(Color. GREEN); g. fill. Rect(0, 0, 300, 100); g. set. Color(Color. RED); g. fill. Oval(x, 5, 90); } public void paint(Graphics g) { draw. Circle(screen. Buffer); //将� 冲区的� 像复制到主� 冲区中 g. draw. Image(screen. Image, 50, this); } } Java程序设计案例教程

例7 -10 设计一个循环播放音乐的Applet程序 import java. awt. Graphics; import java. applet. *; public class Audio.

例7 -10 设计一个循环播放音乐的Applet程序 import java. awt. Graphics; import java. applet. *; public class Audio. Test extends Applet { Audio. Clip sound; public void init() { sound = get. Audio. Clip(get. Document. Base(), "china. wav"); this. set. Size(200, 100); } public void paint(Graphics g) { g. draw. String("Audio Test", 25); } public void start() { sound. loop(); } //循� 播放 public void stop() { sound. stop(); } //停止播放 } Java程序设计案例教程