例7.1小应用程序用6种字型显示字符串,显示内容说明本身的字型。
import java.applet.*; import java.awt.*; public class Example7_1 extends Applet { Font f1 = new Font("Helvetica", Font.PLAIN, 18); Font f2 = new Font("Helvetica", Font.BOLD, 10); Font f3 = new Font("Helvetica", Font.ITALIC, 12); Font f4 = new Font("Courier", Font.PLAIN, 12); Font f5 = new Font("TimesRoman", Font.BOLD + Font.ITALIC, 14); Font f6 = new Font("Dialog", Font.ITALIC, 14); public void paint(Graphics g) { setSize(250, 200); g.setFont(f1); g.drawString("18pt plain Helvetica", 5, 20); g.setFont(f2); g.drawString("10pt bold helvetica", 5, 43); g.setFont(f3); g.drawString("12pt italic Helvetica", 5, 58); g.setFont(f4); g.drawString("12pt plain courier", 5, 75); g.setFont(f5); g.drawString("14pt bold & italic Times Roman", 5, 92); g.setFont(f6); g.drawString("14pt italic Dialog", 5, 111); } }
例7.2小应用程序设置颜色并涂方块,其中绘制方块的方法参见7.2节。
import java.applet.*; import java.awt.*; public class Example7_2 extends Applet { public void paint(Graphics g) { setSize(380, 200); for (int i = 0; i <= 10; i++) { Color myredcolor = new Color(i * 25 + 5, 0, 0); g.setColor(myredcolor); g.fillRect(i * 32 + 5, 2, 28, 28);// 用指定的颜色填充方块 } for (int i = 0; i <= 10; i++) { Color mygreencolor = new Color(0, i * 25 + 5, 0); g.setColor(mygreencolor); g.fillRect(i * 32 + 5, 32, 28, 28);// 用指定的颜色填充方块 } for (int i = 0; i <= 10; i++) { Color mybluecolor = new Color(0, 0, i * 25 + 5); g.setColor(mybluecolor); g.fillRect(i * 32 + 5, 62, 28, 28);// 用指定的颜色填充方块 } } }
例7.3小应用程序重写update()方法,只清除圆块,不清除文字。窗口显示一个不断移动的红色方块。
import java.applet.*; import java.awt.*; public class Example7_3 extends Applet { int i = 1; public void init() { setBackground(Color.yellow); } public void paint(Graphics g) { i = i + 8; if (i > 160) { i = 1; } g.setColor(Color.red); g.fillRect(i, 10, 20, 20); g.drawString("我正在学习update()方法!", 100, 100); try { Thread.sleep(100);// 程序暂停0.1秒 } catch (InterruptedException e) { } repaint(); } public void update(Graphics g) { g.clearRect(i, 10, 200, 100);// "我正在学习update()方法!" paint(g); } }
例7.4使用XOR绘图模式的应用程序。
import java.awt.*; import javax.swing.*; public class Example7_4 extends JFrame { public static void main(String args[]) { GraphicsDemo myGraphicsFrame = new GraphicsDemo(); } } class ShapesPanel extends JPanel { ShapesPanel() { setBackground(Color.white); } public void paintComponet(Graphics g) { super.paintComponent(g); setBackground(Color.yellow);// 背景色为黄色 g.setXORMode(Color.red);// 设置XOR绘图模式,颜色为红色 g.setColor(Color.green); g.fillRect(20, 20, 80, 40);// 实际颜色是green+yellow的混合色=灰色。 g.setColor(Color.yellow); g.fillRect(60, 20, 80, 40);/* 后一半是yellow+yellow=red,前一半是yellow+灰色 */ g.setColor(Color.green); g.fillRect(20, 70, 80, 40);// 实际颜色是green+yellow的混合色=灰色。 g.fillRect(60, 70, 80, 40);/* * 前一半是(green+yellow)+灰色=背景色 后一半是green+yellow=灰色 */ g.setColor(Color.green); g.fillRect(80, 100, 180, 200);// 该直线式green+yellow=灰色。 g.fillRect(100, 100, 200, 200);// 该直线式green+yellow=灰色。 /* * 再绘制部分重叠的直线。原直线中间段是灰色+灰色=背景色, 延长部分是green+yellow=灰色。 */ g.drawLine(140, 140, 220, 220); g.setColor(Color.yellow);// 分析下列直线颜色变化,与早先的图有重叠 g.fillRect(20, 30, 160, 30); g.fillRect(20, 75, 160, 75); } } class GraphicsDemo extends JFrame { public GraphicsDemo() { this.getContentPane().add(new ShapesPanel()); setTitle("基本绘图方法演示"); setSize(300, 300); setVisible(true); } }
例7.5小应用程序用init()或start()方法下载(获取)图像,用paint()方法显示得到的图像。
import java.applet.*; import java.awt.*; public class Example7_5 extends Applet { Image myImg; public void start() { myImg = getImage(getCodeBase(), "myPic.jpg"); } public void print(Graphics g) { g.drawImage(myImg, 2, 2, this); } }
例7.6小应用程序演示图像缓冲显示技术。
import java.applet.*; import java.awt.*; import java.awt.image.*; import javax.swing.*; import java.awt.event.*; public class Example7_6 extends Applet { Image myPicture; /* * init()方法中,先定义一个Image对象,并赋予createImage()方法的返回值,接着创建 * Graphics对象并赋予其图形环境。最后,让Graphics对象调用drawImage()方法显示图像。 * 由于这里的Graphics对象offScreenGc是非屏幕对象,小程序窗口不会有图像显示 */ public void init() { myPicture = getImage(getCodeBase(), "myPic.JPG"); Image offScreenImage = createImage(size().width, size().height); Graphics offScreenGc = offScreenImage.getGraphics(); offScreenGc.drawImage(myPicture, 0, 0, this); new BufferedDemo(myPicture); } /* * drawImage()方法的第四个参数是实现imageObserver接口,在init()方法中,调用 * drawImage()方法的参数是this,所以小程序要定义imageUpdate()方法 */ public boolean imageUpdate(Image img, int infoFlg, int x, int y, int w, int h) { if (infoFlg == ALLBITS) { repaint(); return false; } else { return true; } } } /* * 程序的执行过程是,当小程序调用drawImage()方法时,drawImage()方法将创建一个 * 调用imageUpdate()方法的线程,在imageUpdate()方法中,测定图像是否已经已大部分调用内 * 存。创建的线程不断调用imageUpdate()方法,直到该方法返回false为止。参数infoFlg使 * 小程序能知道图像装入内容的情况。当infoFlg等于ALLBITS时,表示图像已全部装入内存。 * 当该方法发现图像已全部装入后,置imageLoaded为真,并调用repaint()方法重画小程序 * 窗口。方法返回false防止线程再次调用imageUpdate()方法。 */ class BufferedDemo extends JFrame { public BufferedDemo(Image img) { this.getContentPane().add(new PicPanel(img)); setTitle("双缓冲技术演示"); setSize(300, 300); setVisible(true); } } class PicPanel extends JPanel implements MouseListener, MouseMotionListener { int x = 0, y = 0, dx = 0, dy = 0; BufferedImage bimg1, bimg2; boolean upState = true; public PicPanel(Image img) { this.setBackground(Color.white); this.addMouseListener(this); this.addMouseMotionListener(this); bimg1 = new BufferedImage(img.getWidth(this), img.getHeight(this), BufferedImage.TYPE_INT_ARGB); bimg2 = new BufferedImage(img.getWidth(this), img.getHeight(this), BufferedImage.TYPE_INT_ARGB); Graphics2D g2D1 = bimg1.createGraphics(); Graphics2D g2D2 = bimg2.createGraphics(); g2D1.drawImage(img, 0, 0, this); g2D2.drawImage(img, 0, 0, this); g2D2.drawRect(1, 1, img.getWidth(this) - 3, img.getHeight(this) - 3); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2D = (Graphics2D) g; if (upState) { g2D.drawImage(bimg1, x, y, this); } else { g2D.drawImage(bimg2, x, y, this); } } public void mousePressed(MouseEvent e) { if (e.getX() >= x && e.getX() < x + bimg1.getWidth(this) && e.getY() >= y && e.getY() < y + bimg1.getHeight(this)) { upState = false; setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); dx = e.getX() - x; dy = e.getY() - y; repaint(); } } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseReleased(MouseEvent e) { this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); upState = true; repaint(); } public void mouseMoved(MouseEvent e) { } public void mouseDragged(MouseEvent e) { if (!upState) { x = e.getX() - dx; y = e.getY() - dy; repaint(); } } }
例7.7小应用程序先将幻灯片读入数组中存储,单击鼠标变换幻灯片,逐张显示。
import java.applet.*; import java.awt.*; import java.awt.event.*; public class Example7_7 extends Applet implements MouseListener { final int number = 50;// 假定幻灯片有50张 int count = 0; Image[] card = new Image[number]; public void init() { addMouseListener(this); for (int i = 0; i < number; i++) { card[i] = getImage(getCodeBase(), "DSC0033" + i + ".jpg"); } } public void paint(Graphics g) { if ((card[count]) != null) { g.drawImage(card[count], 10, 10, card[count].getWidth(this), card[count].getHeight(this), this); } } public void mousePressed(MouseEvent e) { count = (count + 1) % number;// 循环逐张显示 repaint(); } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } }
例7.8小应用程序说明播放动画的方法。
import java.applet.*; import java.awt.*; import java.awt.event.*; public class Example7_8 extends Applet implements Runnable { final int number = 50; int count = 0; Thread mythread; Image[] pic = new Image[number]; public void init() { setSize(300, 200); for (int i = 1; i <= number; i++) {// 载入动画图片 pic[i - 1] = getImage(getCodeBase(), "DSC0033" + i + ".jpg"); } } public void start() { mythread = new Thread(this);// 创建一个线程 mythread.start();// 启动线程执行 } public void stop()// 结束线程执行 { mythread = null; } public void run()// 线程的执行代码 { while (true) { repaint(); count = (count + 1) % number;// 改变显的图片号 try { mythread.sleep(200); } catch (InterruptedException e) { } } } public void paint(Graphics g) { if ((pic[count]) != null) { g.drawImage(pic[count], 10, 10, pic[count].getWidth(this), pic[count].getHeight(this), this); } } }
例7.9能播放声音的小应用程序。
import java.applet.*; import java.awt.*; import java.awt.event.*; public class Example7_9 extends Applet implements ActionListener { AudioClip clip;// 声明一个音频对象 Button buttonPlay, buttonLoop, buttonStop; public void init() { clip = getAudioClip(getCodeBase(), "2.wav"); // 根据程序所在的地址处的声音文件2.wav创建音频对象, // Applet类的getCodeBase()方法可以获得小程序所在的html页面的URL地址。 buttonPlay = new Button("开始播放"); buttonLoop = new Button("循环播放"); buttonStop = new Button("停止播放"); buttonPlay.addActionListener(this); buttonLoop.addActionListener(this); buttonStop.addActionListener(this); add(buttonPlay); add(buttonLoop); add(buttonStop); } public void stop() { clip.stop();// 当离开此页面时停止播放。 } public void actionPerformed(ActionEvent e) { if (e.getSource() == buttonPlay) { clip.play(); } else if (e.getSource() == buttonLoop) { clip.loop(); } if (e.getSource() == buttonStop) { clip.stop(); } } }