www.pudn.com > Chapter16n0.rar > Chapter16n0.java


import java.awt.*; 
import java.awt.event.*; 
 
public class Chapter16n0 extends Frame 
                                  implements  WindowListener,ActionListener 
{ 
public Chapter16n0() 
{ 
super(); 
 
setTitle("Chapter16n0"); 
setBackground(Color.green); 
setSize(500,400); 
addWindowListener(this); 
 
Canvas0 canvas=new Canvas0(); 
add("Center",canvas); 
 
Panel p=new Panel(); 
p.setLayout(new FlowLayout()); 
Button quit=new Button("Quit"); 
p.add(quit); 
quit.addActionListener(this); 
add("South",p); 
} 
 
 
public static void main(String[] args) 
{ 
Chapter16n0 f=new Chapter16n0(); 
f.setVisible(true); 
} 
 
 
public void actionPerformed(ActionEvent event) 
{ 
dispose(); 
System.exit(0); 
} 
 
 
public void windowClosing(WindowEvent enent) 
{ 
dispose(); 
System.exit(0); 
} 
 
 
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){} 
} 
 
class Canvas0 extends Canvas 
{ 
public Canvas0() 
{ 
super(); 
} 
 
public void paint(Graphics g) 
{ 
Dimension d=getSize(); 
int cx=d.width/2, 
cy=d.height/2, 
faceRadius=50, 
noseLength=20, 
mouthRadius=30, 
mouthAngle=50, 
eyeRadius=5; 
 
g.setColor(Color.black); 
g.drawRoundRect(2,2,d.width-5,d.height-5,20,20); 
 
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); 
 
 
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); 
} 
}