📄 chapter16n1.java
字号:
/** * * demonstration of AWT - animated smiley face * * Written by: Roger Garside * * First Written: 11/July/96 * Last Rewritten: 30/May/97 * */import java.awt.* ;import java.awt.event.* ;public class Chapter16n1 extends Frame implements WindowListener, ActionListener { Canvas1 canvas ; Button change, quit ; String name ; boolean isSmiling ; /** * * constructor * */ public Chapter16n1(String n) { name = n ; isSmiling = true ; // set up basic window setTitle("Chapter16n1") ; setBackground(Color.green) ; setSize(500, 400) ; addWindowListener(this) ; // set up area for face canvas = new Canvas1(this) ; add("Center", canvas) ; // set up area with buttons Panel p = new Panel() ; p.setLayout(new FlowLayout()) ; change = new Button("Change") ; p.add(change) ; change.addActionListener(this) ; quit = new Button("Quit") ; p.add(quit) ; quit.addActionListener(this) ; add("South", p) ; } // end of constructor method /** * * main * */ public static void main(String[] args) { Chapter16n1 f ; if (args.length == 0) f = new Chapter16n1("World") ; else f = new Chapter16n1(args[0]) ; f.setVisible(true) ; } // end of main method /** * * actionPerformed * */ public void actionPerformed(ActionEvent event) { // deal with "Quit" button if (event.getSource() == quit) { dispose(); System.exit(0); } // deal with "Change" button else if (event.getSource() == change) { isSmiling = !isSmiling ; canvas.repaint() ; } } // end of method actionPerformed /** * * windowClosing * */ public void windowClosing(WindowEvent event) { dispose(); System.exit(0); } // end of method windowClosing public void windowOpened(WindowEvent event) {} public void windowIconified(WindowEvent event) {} public void windowDeiconified(WindowEvent event) {} public void windowClosed(WindowEvent event) {} public void windowActivated(WindowEvent event) {} public void windowDeactivated(WindowEvent event) {} } // end of class Chapter16n1class Canvas1 extends Canvas { Chapter16n1 parent ; /** * * constructor * */ public Canvas1(Chapter16n1 f) { parent = f ; } // end of constructor method /** * * paint * */ public void paint(Graphics g) { // set up some dimensions for the drawing Dimension d = getSize() ; int cx = d.width / 2, cy = d.height /2, faceRadius = 50, noseLength = 20, mouthRadius = 30, mouthAngle = 50, eyeRadius = 5 ; // draw the frame g.setColor(Color.black) ; g.drawRoundRect(2, 2, d.width - 5, d.height - 5, 20, 20) ; // draw the face g.setColor(Color.red) ; g.drawOval(cx - faceRadius, cy - faceRadius, faceRadius * 2, faceRadius * 2) ; g.setColor(Color.blue) ; g.fillOval(cx - 30 - eyeRadius, cy - 20, eyeRadius * 2, eyeRadius * 2) ; g.fillOval(cx + 30 - eyeRadius, cy - 20, eyeRadius * 2, eyeRadius * 2) ; g.setColor(Color.red) ; g.drawLine(cx, cy - (noseLength / 2), cx, cy + (noseLength / 2)) ; if (parent.isSmiling) g.drawArc(cx - mouthRadius, cy - mouthRadius, mouthRadius * 2, mouthRadius * 2, 270 - mouthAngle, mouthAngle * 2) ; else g.drawArc(cx - mouthRadius, cy - mouthRadius + 50, mouthRadius * 2, mouthRadius * 2, 90 - mouthAngle, mouthAngle * 2) ; // write the text Font f1 = new Font("TimesRoman", Font.PLAIN, 14) ; Font f2 = new Font("TimesRoman", Font.ITALIC, 14) ; FontMetrics fm1 = g.getFontMetrics(f1) ; FontMetrics fm2 = g.getFontMetrics(f2) ; String s1 = "Hello, " ; String s2 = parent.name ; int w1 = fm1.stringWidth(s1) ; int w2 = fm2.stringWidth(s2) ; g.setColor(Color.black) ; g.setFont(f1) ; int ctx = cx - ((w1 + w2) / 2) ; int cty = cy + faceRadius + 30 ; g.drawString(s1, ctx, cty) ; ctx += w1 ; g.setFont(f2) ; g.drawString(s2, ctx, cty) ; } // end of method paint } // end of class Canvas1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -