⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 driver.java

📁 Java程序设计(美) David D. Riley著 机械工业出版社 书籍配套 代码
💻 JAVA
字号:
import java.awt.*;/**	Author: David D. Riley *		Date: Jan, 2005 *   Coffee Cup Program (Figure 4.29) */	public class Driver extends ThreeButtons  {	private ThreeButtonFrame  window;	private Container redCup, blueCup;	/** post:	a window is created      *			and  redCup is repositioned at the top center of window     *			and  blueCup is repositioned at the bottom center of window 	 */	public Driver()   {		window = new ThreeButtonFrame( "Move the Cups" );        window.setVisible(true);		window.setLayout(null);        window.setBackground(Color.white);		redCup = newCup(Color.red);		window.add(redCup, 0);		blueCup = newCup(Color.blue);		window.add(blueCup, 0);		midAction();	}		/** post:	result is a new Container with a coffee cup colored c on top     *			and  result.getWidth() == 40   and   result.getHeight() == 20 	 */	private  Container  newCup(Color c)   {		Container background, cupBackground;		Oval cup, handle, handleCenter, top;		background = new Container();		background.setBounds(0, 0, 40, 20);		handle = new Oval(28, 1, 8, 13);		handle.setBackground(c);		handleCenter = new Oval(2, 2, 4, 9);		handleCenter.setBackground(Color.white);		handle.add(handleCenter, 0);        background.add(handle, 0);		cupBackground = new Container();        cupBackground.setBounds(0, 5, 30, 15);		cup = new Oval(0, -18, 30, 35);		cup.setBackground(c);        cupBackground.add(cup, 0);        background.add(cupBackground, 0);		top = new Oval(0, 1, 30, 6);        background.add(top, 0);		return background;	}				/**	pre:	redCup instantiated  and  blueCup instantiated		 *	post:	redCup.getY() == redCup.getY()@pre + 5     *          and  blueCup.getY() == blueCup.getY()@pre - 5 	 */	public  void  leftAction()   {		int newX, newY;		newX = redCup.getX();		newY = redCup.getY() + 5;		redCup.setLocation(newX, newY);		redCup.repaint();		newX = blueCup.getX();		newY = blueCup.getY() - 5;		blueCup.setLocation(newX, newY);		blueCup.repaint();	}	/**	pre:	redCup instantiated  and  blueCup instantiated		 *	post:	redCup is repositioned at the top center of window	 *			and  blueCup is repositioned at the bottom center of window 	 */	public  void  midAction()   {		redCup.setLocation(280, 30);		redCup.repaint();		blueCup.setLocation(280, 400);		blueCup.repaint();	}		/**	pre:	redCup instantiated  and  blueCup instantiated		 *	post:	redCup.getX() == redCup.getX()@pre - 10	 *			and  blueCup.getX() == blueCup.getX()@pre + 10 	 */	public  void  rightAction()   {		int newX, newY;		newX = redCup.getX() - 10;		newY = redCup.getY();		redCup.setLocation(newX, newY);		redCup.repaint();		newX = blueCup.getX() + 10;		newY = blueCup.getY();		blueCup.setLocation(newX, newY);		blueCup.repaint();	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -