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. iOS开发小技巧--修改按钮内部图片和文字之间的间距(xib)

    调整按钮的Edge属性,选择调整图片的Edge还是label的Edge,如图:

  2. java中的this和super的作用和异同和C++中调用父类的构造函数

    来源于:http://www.cnblogs.com/hasse/p/5023392.html 这几天看到类在继承时会用到this和super,这里就做了一点总结,与各位共同交流,有错误请各位指正~ ...

  3. eclipse-搭建maven的war项目集合spring注解方式

    工具:eclipse 4.4.2 版本号:20150219-0600 jdk:1.7 1.下图创建maven工程,然后next 下图选择工程保存位置(这里选择默认),next 下图选择webapp项目 ...

  4. eclipse中maven install和build,clean

    eclipse插件,m2eclipse 1.maven install相当于maven原生的命令: mvn install 2.aven build是 m2eclipse这个插件自己创造的概念,需要你 ...

  5. Html+js 控制input输入框提示

    <input type="text" class="fl plsearch_txt" id="key" value="请输入 ...

  6. 表单提交中get和post方式的区别

    表单提交中get和post方式的区别有5点 1.get是从服务器上获取数据,post是向服务器传送数据. 2.get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一 ...

  7. Java反射机制详解

    Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为Java语言的反 ...

  8. Android Studio打包全攻略

    转载:http://www.2cto.com/kf/201606/517300.html 初出茅庐 手动打包 怎么手动打包 项目写完了,现在需要把应用上传到市场,问题出现-怎么把代码变成.apk(An ...

  9. 在Eclipse中如何关联spring-framework的文档和源代码

    1.到官方网站去下载spring-framework的jar包 spring-framework jar包的下载地址是:http://repo.spring.io/release/org/spring ...

  10. 【BZOJ-4561】圆的异或并 set + 扫描线

    4561: [JLoi2016]圆的异或并 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 254  Solved: 118[Submit][Statu ...