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

📄 cannonworld.java

📁 一些JAVA的小程序
💻 JAVA
字号:
////	CannonWorld application, second version of Cannon Game//	Described in Chapter 6 of//	Understanding Object-Oriented Programming with Java//	by Timothy A Budd//	Published by Addison-Wesley////	see ftp://ftp.cs.orst.edu/pub/budd/java/ReadMe.html//	for further information////import java.awt.*;import java.awt.event.*;class CannonWorld extends Frame {	public static void main (String [ ] args)	{		CannonWorld world = new CannonWorld ();		world.show ();	}	public static final int FrameWidth = 600;	public static final int FrameHeight = 400;	private int angle = 45;	private String message = "Angle: " + angle;	private CannonBall cannonBall = null;	private Scrollbar slider;	private class FireButtonListener implements ActionListener {		public void actionPerformed (ActionEvent e) {			double radianAngle = angle * Math.PI / 180.0;			double sinAngle = Math.sin(radianAngle);			double cosAngle = Math.cos(radianAngle);			// create the cannon ball			cannonBall = new CannonBall (				20 + (int) (30 * cosAngle), dy(5+(int) (30 * sinAngle)),				5, 12 * cosAngle, -12 * sinAngle);			repaint();			}	}	private class ScrollBarListener implements AdjustmentListener {		public void adjustmentValueChanged (AdjustmentEvent e) {			angle = slider.getValue();			message = "Angle: " + angle;			repaint();			}	}	public CannonWorld () {		setSize (FrameWidth, FrameHeight);		setTitle ("Cannon Game");			// add the scroll bar		Button fire = new Button("fire");		fire.addActionListener(new FireButtonListener());		add ("North", fire);		slider = new Scrollbar(Scrollbar.VERTICAL, angle, 5, 0, 90);		slider.addAdjustmentListener(new ScrollBarListener());		add ("East", slider);	}	public static int dy (int y) {	return FrameHeight - y; }	public void paint (Graphics g)	{		int x = 20;	// location of cannon		int y = 15;		double radianAngle = angle * Math.PI / 180.0;		int lv = (int) (30 * Math.sin(radianAngle));		int lh = (int) (30 * Math.cos(radianAngle));		int sv = (int) (10 * Math.sin(radianAngle + Math.PI/2));		int sh = (int) (10 * Math.cos(radianAngle + Math.PI/2));			// draw cannon		g.setColor(Color.green);		g.drawLine(x, dy(y), x+lh, dy(y+lv));		g.drawLine(x+lh, dy(y+lv), x+lh+sh, dy(y+lv+sv));		g.drawLine(x+lh+sh, dy(y+lv+sv), x+sh, dy(y+sv));		g.drawLine(x+sh, dy(y+sv), x, dy(y));		g.drawOval(x-8, dy(y+10), 12, 12);			// draw target		g.setColor(Color.red);		g.fillRoundRect(FrameWidth-100, dy(12), 50, 10, 6, 6);			// draw cannonball		if (cannonBall != null) { 			cannonBall.move();			cannonBall.paint(g);			//try {				//Thread.sleep(20);				//} catch(InterruptedException e) { }			if (dy(cannonBall.y()) > 0) 				repaint();			else {				int targetX = FrameWidth - 100;				if ((cannonBall.x() > targetX) && (cannonBall.x() < (targetX + 50)))					message = "You Hit It!";				else					message = "Missed!";				cannonBall = null;				}			}		g.drawString(message, FrameWidth/2, FrameHeight/2);	}}

⌨️ 快捷键说明

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