📄 chapter20n1a.java
字号:
/** * * a simple applet - with a parameter, etc * * Written by: Roger Garside * * First Written: 1/Feb/97 * Last Rewritten: 30/May/97 * */import java.awt.* ;import java.awt.event.* ;import java.applet.* ;public class Chapter20n1a extends Applet implements ActionListener { Canvas1a canvas ; Button change ; String welcomeName ; boolean isSmiling ; /** * init */ public void init() { System.err.println("call of 'init' method") ; setLayout(new BorderLayout()) ; welcomeName = getParameter("Who") ; if (welcomeName == null) welcomeName = "World" ; isSmiling = true ; setBackground(Color.green) ; // set up area for smiley face canvas = new Canvas1a(this) ; add("Center", canvas) ; // set up panel for button Panel p = new Panel() ; p.setLayout(new FlowLayout()) ; change = new Button("Change") ; p.add(change) ; change.addActionListener(this) ; add("South", p) ; } // end of method init /** * actionPerformed */ public void actionPerformed(ActionEvent event) { // deal with "Change" button isSmiling = !isSmiling ; canvas.repaint() ; } // end of method actionPerformed /** * start */ public void start() { System.err.println("call of 'start' method") ; } // end of method start /** * stop */ public void stop() { System.err.println("call of 'stop' method") ; } // end of method stop /** * destroy */ public void destroy() { System.err.println("call of 'destroy' method") ; } // end of method destroy } // end of class Chapter20n1aclass Canvas1a extends Canvas { Chapter20n1a parent ; /** * constructor */ public Canvas1a(Chapter20n1a 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.welcomeName ; 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 Canvas1a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -