《自考JAVA语言程序设计(一)课后习题答案与源代码(第八章)》由会员分享,可在线阅读,更多相关《自考JAVA语言程序设计(一)课后习题答案与源代码(第八章)(18页珍藏版)》请在金锄头文库上搜索。
1、第八章3.编写程序,一个画圆,一个画椭圆。程序运行结果:Applet的源文件:Work8_3.javaimport java.applet.Applet;import java.awt.Color;import java.awt.Graphics;/* * 8.3 用一个红色笔画圆,同时用一个蓝色笔画椭圆 */public class Work8_3 extends Applet implements Runnableprivate static final long serialVersionUID = 1L;/* 两个线程,红笔线程,和蓝笔线程 */private Thread red_t
2、hread, blue_thread;/* 红色画笔、蓝色画笔 */private Graphics redPen, bluePen;/* 蓝、红笔画时需要旋转角度 */private int blue_angle = 0, red_angle = 0;/* 红色画的图案的中心坐标 */private int a_red = 100, b_red = 100;/* 蓝色画的图案的中心坐标 */private int a_blue = 300, b_blue = 100;/*圆形半径*/private int radius_red = 80;/*椭圆的两个半径*/private int radi
3、us1_blue = 150, radius2_blue = 100;public void init()red_thread = new Thread(this);blue_thread = new Thread(this);redPen = getGraphics();bluePen = getGraphics();setBackground(Color.white);setSize(470, 240);public void start()red_thread.start();/ 两个线程开始blue_thread.start();public void run()while (true
4、)if (Thread.currentThread() = red_thread)redPen.drawOval(25, 25, 160, 160);/ 绘制圆的边框。x,y,width,heightint x = getX(radius_red, red_angle, a_red);int y = getY(radius_red, red_angle, b_red);redPen.setColor(Color.WHITE);redPen.fillOval(x, y, 10, 10);/ 用白色画一次,可以擦出痕迹red_angle += 3;if (red_angle = 360)red_a
5、ngle = 0;x = getX(80, red_angle, 100);y = getY(80, red_angle, 100);redPen.setColor(Color.RED);redPen.fillOval(x, y, 10, 10);/ 使用当前颜色填充外接指定矩形框的椭圆tryThread.sleep(10);catch( InterruptedException e )else if (Thread.currentThread() = blue_thread)bluePen.drawOval(a_blue / 2 + 5, b_blue / 2 - 45, radius1_b
6、lue * 2,radius2_blue * 2);/ 绘制椭圆的边框。x,y,width,heightint x = getX(radius1_blue, blue_angle, a_blue);int y = getY(radius2_blue, blue_angle, b_blue);bluePen.setColor(Color.WHITE);bluePen.fillOval(x, y, 10, 10);/ 擦除痕迹blue_angle += 3;if (blue_angle = 360)blue_angle = 0;x = getX(radius1_blue, blue_angle,
7、300);y = getY(radius2_blue, blue_angle, 100);bluePen.setColor(Color.BLUE);bluePen.fillOval(x, y, 10, 10);/ 擦除痕迹tryThread.sleep(20);catch( InterruptedException e )/* * 用参数方程方法,计算坐标的方法 * param r 圆的半径,椭圆的两个半轴 * param loc x坐标的相对偏移量 * param angle 旋转的角度,单位为角度 * return 计算后的坐标值 */public int getX(int r, int
8、angle, int locX)int x = locX + (int) (r * Math.cos(Math.PI / 180.0 * angle);return x;/* * 用参数方程方法,计算坐标的方法 * param r 圆的半径,椭圆的两个半轴 * param loc Y坐标的相对偏移量 * param angle 旋转的角度,单位为角度 * return 计算后的坐标值 */public int getY(int r, int angle, int locY)int y = locY + (int) (r * Math.sin(Math.PI / 180.0 * angle);r
9、eturn y;6.模拟排队买票,球票5元,购票者持有5,10,20,50元的,售票员手里开始没有零钱。程序运行结果:主窗体源文件:BuyTicketFrame.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;/* * 8.6 线程实现,购票规则,由于我想的算法太过复杂,只写到50元面值的 * author 黎明你好 */public class BuyTicketFrame extends JFrame implements ActionListener, Runnableprivate static fi
10、nal long serialVersionUID = 1L;private Conductor lady;private int array = 10, 10, 5, 20, 50, 5, 5, 5, 5, 5, 5, 10, 20, 10, 5, 10, 20, 5 ;private int number = array.length;private Thread thread = new Threadnumber;static JTextArea text;private JButton begin_button, replay_button;private JPanel panel;p
11、ublic BuyTicketFrame()super(第八章,第六题,线程模拟购票);lady = new Conductor(15);text = new JTextArea();panel = new JPanel();begin_button = new JButton(开始买票);replay_button = new JButton(重新开始);for (int i = 0; i array.length; i+)threadi = new Thread(this);begin_button.addActionListener(this);replay_button.addActi
12、onListener(this);panel.setLayout(new FlowLayout();panel.add(begin_button);panel.add(replay_button);this.add(panel, BorderLayout.NORTH);this.add(new JScrollPane(text), BorderLayout.CENTER);this.setSize(1000, 700);this.setVisible(true);this.validate();this.addWindowListener(new WindowAdapter()/ 窗口监视器p
13、ublic void windowClosing(WindowEvent e)System.exit(0););public void actionPerformed(ActionEvent e)if (e.getSource() = begin_button)/ 线程开始text.setText(* + number + 个人排队买票,共 + lady.getTicketAmount() + 张票,开始卖票:n);tryfor (int i = 0; i array.length; i+)threadi.start();if (threadi.isAlive()text.append( + (i + 1) + 线程开始n);begin_button.setForeground(Color.RED);catch( IllegalThreadStateException ee )text.append(error:重复启动线程 + ee.toString();/ 检查