www.pudn.com > Heart.rar > Heart.java


package mycode; 
 
import java.awt.*; 
import java.awt.event.*; 
 
public class Heart extends Frame implements WindowListener, ComponentListener, 
		ItemListener { 
	int width1 = 200, height1 = 200; // 窗口尺寸 
 
	Color color = Color.blue; // 画线的颜色 
 
	CheckboxGroup cg1; 
 
	Checkbox cb1, cb2, cb3; 
 
	public Heart() { 
		setTitle("Heart"); 
		setSize(width1, height1); 
		setBackground(Color.white); 
		setLayout(new FlowLayout()); 
		addWindowListener(this); 
		addComponentListener(this); 
		cg1 = new CheckboxGroup(); 
		cb1 = new Checkbox("blue", cg1, true); 
		cb2 = new Checkbox("red", cg1, false); 
		cb3 = new Checkbox("green", cg1, false); 
		cb1.addItemListener(this); 
		cb2.addItemListener(this); 
		cb3.addItemListener(this); 
		add(cb1); 
		add(cb2); 
		add(cb3); 
	} 
 
	public void paint(Graphics g) { 
		int x0, y0; // 原点坐标 
		x0 = width1 / 2; 
		y0 = height1 / 2; 
		g.setColor(color); 
		g.drawLine(x0, 0, x0, height1); 
		g.drawLine(0, y0, width1, y0); 
		int i, j = 40, x, y; 
		double pi = 3.14, angle, r; 
		while (j < 200) { 
			for (i = 0; i < 1023; i++) { 
				angle = i * pi / 512; 
				r = j * (1 - Math.cos(angle)); 
				x = (int) Math.round(r * Math.cos(angle) * 2); 
				y = (int) Math.round(r * Math.sin(angle)); 
				g.fillOval(x0 + x, y0 + y, 1, 1); // 画圆点 
			} 
			j = j + 20; 
		} 
	} 
 
	public void windowClosing(WindowEvent e) // 关闭窗口 
	{ 
		System.exit(0); 
	} 
 
	public void componentResized(ComponentEvent e) // 改变窗口大小时 
	{ 
		width1 = getWidth(); // 取得窗口的大小 
		height1 = getHeight(); 
	} 
 
	public void itemStateChanged(ItemEvent e) { // 选中单选按钮时 
		if (e.getSource() == cb1) // 判断产生事件对象e的组件是谁 
			color = Color.blue; 
		if (cb2.getState()) // 判断单选按钮cb2的状态 
			color = Color.red; 
		if (cg1.getSelectedCheckbox() == cb3) // 判断复选框组cg1选中的是谁 
			color = Color.green; 
		repaint(); // 重画 
	} 
 
	public static void main(String arg[]) { // 创建并显示Frame对象,Frame作为画布 
		new Heart().setVisible(true); 
	} 
 
	public void componentMoved(ComponentEvent e) { 
	} 
 
	public void componentHidden(ComponentEvent e) { 
	} 
 
	public void componentShown(ComponentEvent e) { 
	} 
 
	public void windowOpened(WindowEvent e) { 
	} 
 
	public void windowActivated(WindowEvent e) { 
	} 
 
	public void windowDeactivated(WindowEvent e) { 
	} 
 
	public void windowClosed(WindowEvent e) { 
	} 
 
	public void windowIconified(WindowEvent e) { 
	} 
 
	public void windowDeiconified(WindowEvent e) { 
	} 
}