import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Exercise16_22 extends JFrame {
final static int NUMBER_OF_SLOTS = 9;
final static int NUMBER_OF_ROWS = NUMBER_OF_SLOTS - 2;

private int shift = 0;
private int[] slots = new int[NUMBER_OF_SLOTS];
private int numberOfBallsDropped = 0;
private int moveCount = 0;
private int position = 0;

private BeanMachinePanel paintPanel = new BeanMachinePanel();
private Timer timer = new Timer(200, new ActionListener() {
public void actionPerformed(ActionEvent e) {
moveCount++;
if (moveCount <= NUMBER_OF_ROWS) {
if (Math.random() < 0.5)
paintPanel.moveRedBallLeft();
else {
paintPanel.moveRedBallRight();
position++;
}
}
else {
slots[position]++;
paintPanel.startRedBall();
shift = 0;
moveCount = 0;
position = 0;

numberOfBallsDropped++;
if (numberOfBallsDropped == 10) {
timer.stop();
paintPanel.hideRedBall();
}
}
}
});

public Exercise16_22() {
add(paintPanel);
timer.start();
}

class BeanMachinePanel extends JPanel {
final static int HGAP = 20;
final static int VGAP = 20;
final static int RADIUS = 5;
final static int LENGTH_OF_SLOTS = 40;
final static int LENGTH_OF_OPENNING = 15;
final static int Y_FOR_FIRST_NAIL = 50;
final static int RED_BALL_START_Y = Y_FOR_FIRST_NAIL - RADIUS;

private int yRed = RED_BALL_START_Y;
private boolean hideRedBall = false;

/** Move the red ball down left */
public void moveRedBallLeft() {
shift -= HGAP / 2;
yRed += VGAP;
repaint();
}

/** Move the red ball down right */
public void moveRedBallRight() {
shift += HGAP / 2;
yRed += VGAP;
repaint();
}

/** Move the red ball down right */
public void startRedBall() {
yRed = RED_BALL_START_Y;
hideRedBall = false;
repaint();
}

/** Move the red ball down right */
public void hideRedBall() {
hideRedBall = true;
repaint();
}

/** Paint message */
protected void paintComponent(Graphics g) {
super.paintComponent(g);

int y = Y_FOR_FIRST_NAIL;
int xCenter = getWidth() / 2;

// Draw the red ball
if (!hideRedBall) {
g.setColor(Color.RED);
int xRed = xCenter + shift;
g.fillOval(xRed - RADIUS, yRed - RADIUS, 2 * RADIUS, 2 * RADIUS);
}

// Draw pegs in multiple lines
g.setColor(Color.GREEN);
for (int i = 0; i < NUMBER_OF_ROWS; i++) {
y += VGAP;
for (int k = 0; k <= i; k++) {
g.fillOval(xCenter - i * HGAP / 2 + k * HGAP - RADIUS, y - RADIUS, 2 * RADIUS, 2 * RADIUS);
}
}

// Draw vertical lines for slots
g.setColor(Color.BLACK);
y = y + RADIUS;
for (int i = 0; i < NUMBER_OF_SLOTS; i++) {
int x = xCenter - (NUMBER_OF_ROWS - 1) * HGAP / 2 + (i - 1) * HGAP;
g.drawLine(x, y, x, y + LENGTH_OF_SLOTS);
}

// Draw a horizontal line for bottom
g.drawLine(xCenter - (NUMBER_OF_ROWS - 1) * HGAP / 2 - HGAP, y + LENGTH_OF_SLOTS,
xCenter - (NUMBER_OF_ROWS - 1) * HGAP / 2 + NUMBER_OF_ROWS * HGAP, y + LENGTH_OF_SLOTS);
// Draw two side lines
g.drawLine(xCenter + HGAP / 2, Y_FOR_FIRST_NAIL + RADIUS, xCenter - (NUMBER_OF_ROWS - 1) * HGAP / 2 + NUMBER_OF_ROWS * HGAP, y);
g.drawLine(xCenter - HGAP / 2, Y_FOR_FIRST_NAIL + RADIUS, xCenter - (NUMBER_OF_ROWS - 1) * HGAP / 2 - HGAP, y);
// Draw two vertical lines for the openning
g.drawLine(xCenter - HGAP / 2, Y_FOR_FIRST_NAIL + RADIUS, xCenter - HGAP / 2, Y_FOR_FIRST_NAIL - LENGTH_OF_OPENNING);
g.drawLine(xCenter + HGAP / 2, Y_FOR_FIRST_NAIL + RADIUS, xCenter + HGAP / 2, Y_FOR_FIRST_NAIL - LENGTH_OF_OPENNING);

// Paint the balls in the slots
g.setColor(Color.RED);
for (int i = 0; i < slots.length; i++) {
int x = xCenter - (NUMBER_OF_ROWS) * HGAP / 2 + i * HGAP;
for (int j = 0; j < slots[i]; j++) {
g.fillOval(x - RADIUS, y + LENGTH_OF_SLOTS - 2 * RADIUS - j * 2 * RADIUS, 2 * RADIUS, 2 * RADIUS);
}
}
}
}

public static void main(String[] args) {
JFrame frame = new Exercise16_22();
frame.setTitle("Exercise16_22");
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

Exercise16_22.java的更多相关文章

  1. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  2. 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题

    背景起因: 记起以前的另一次也是关于内存的调优分享下   有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...

  3. Elasticsearch之java的基本操作一

    摘要   接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...

  4. 论:开发者信仰之“天下IT是一家“(Java .NET篇)

    比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...

  5. 故障重现, JAVA进程内存不够时突然挂掉模拟

    背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...

  6. 死磕内存篇 --- JAVA进程和linux内存间的大小关系

    运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...

  7. 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用

    有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...

  8. Java多线程基础学习(二)

    9. 线程安全/共享变量——同步 当多个线程用到同一个变量时,在修改值时存在同时修改的可能性,而此时该变量只能被赋值一次.这就会导致出现“线程安全”问题,这个被多个线程共用的变量称之为“共享变量”. ...

  9. Java多线程基础学习(一)

    1. 创建线程    1.1 通过构造函数:public Thread(Runnable target, String name){}  或:public Thread(Runnable target ...

随机推荐

  1. Swift开发小技巧--自定义Log

    Swift中的自定义Log OC中有宏的定义,可以定义自己的Log,但是Swif中没有宏的定义,想要实现类似OC中的自定义Log,必须实现以下操作 1.在AppDelegate.swift文件中定义一 ...

  2. DOCTYPE是什么鬼?文档模式又是什么鬼?

    !DOCTYPE !DOCTYPE是什么: 在所有 HTML 文档中规定 DOCTYPE 是非常重要的,这样浏览器就能了解预期的文档类型, 告诉浏览器要通过哪一种规范(文档类型定义 DTD)解析文档( ...

  3. JAVA程序员一定知道的优秀第三方库(2016版)

    几乎每个程序员都知道要“避免重复发明轮子”的道理——尽可能使用那些优秀的第三方框架或库,但当真正进入开发时,我却经常发现他们有时并不知道那些轮子在哪里.最近,我在业余时间带几个年轻的程序员一起做了一个 ...

  4. Hibernate @Formula 注解方式

    1.Formula的作用 Formula的作用就是用一个查询语句动态的生成一个类的属性 就是一条select count(*)...构成的虚拟列,而不是存储在数据库里的一个字段.用比较标准的说法就是: ...

  5. ajax技术应用(转)

    ajax简介 AJAX即"Asynchronous Javascript And XML"(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术.Ajax不 ...

  6. 控件(文本类): TextBox, PasswordBox

    介绍背水一战 Windows 10 之 控件(文本类) TextBox PasswordBox 示例1.TextBox 的示例 1Controls/TextControl/TextBoxDemo1.x ...

  7. linux下使用 du查看某个文件或目录占用磁盘空间的大小

    du -ah --max-depth=1     这个是我想要的结果  a显示目录占用的磁盘空间大小,还要显示其下目录和文件占用磁盘空间的大小但是由于用了--max-depth选项,表示显示目录下所有 ...

  8. Mac OS 下 eclipse中文乱码解决方法(eclipse for mac 中文乱码)

    由于一些java源码是从其他人那里拷贝过来,放入Mac os 版本的eclipse下,发现中文都是乱码.经过小试,可以解决. 1.打开eclipse 偏好设置 2.General ——>Cont ...

  9. this action could not be completed.try again登陆appstore错误提示

    今天升级10.11后登陆appstore的时候发现报错了: this action could not be completed.try again 解决办法,终端敲入: sudo mkdir -p ...

  10. python3 连接SQLserver

    pymssql 是Python语言用来连接微软 SQL SERVER 数据库的类库,实现了 Python DB API 2.0 .官网介绍. 最简事例: #!/usr/bin/env python # ...