📄 chapter16n0.java
字号:
/** * * demonstration of AWT - simple 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 Chapter16n0 extends Frame implements WindowListener, ActionListener { /** * * constructor * */ public Chapter16n0() { // set up basic window setTitle("Chapter16n0") ; setBackground(Color.green) ; setSize(500, 400) ; addWindowListener(this) ; // set up area for face Canvas0 c = new Canvas0() ; add("Center", c) ; // set up area with buttons Panel p = new Panel() ; p.setLayout(new FlowLayout()) ; Button quit = new Button("Quit") ; p.add(quit) ; quit.addActionListener(this) ; add("South", p) ; } // end of constructor method /** * * main * */ public static void main(String[] args) { Chapter16n0 f = new Chapter16n0() ; f.setVisible(true) ; } // end of main method /** * * actionPerformed * */ public void actionPerformed(ActionEvent event) { // deal with "Quit" button dispose(); System.exit(0); } // end of method actionPerformed 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 Chapter16n0class Canvas0 extends Canvas { /** * * 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)) ; g.drawArc(cx - mouthRadius, cy - mouthRadius, mouthRadius * 2, mouthRadius * 2, 270 - 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 = "World" ; 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 Canvas0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -