JAVA小项目之摇奖机

  功能:

  1. 点击”摇杆“开始;
  2. 两种结束滚动方式,A:点击”摇杆“ B:分别点击 对应结果框的按钮;
  3. 实现最后减速停下来效果,模拟真实摇奖机。

  知识点:A.线程的控制,B.图片轮播原理

效果图:

   JAVA小项目之摇奖机

  • 窗口类
 package com.gxlee.lhj;

 import java.awt.Color;
 import java.awt.Container;
 import java.awt.Graphics;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.awt.image.BufferedImage;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Random;

 import javax.swing.JFrame;
 import javax.swing.JPanel;

 @SuppressWarnings("serial")
 public class MainFrame extends JFrame {

     private List<Fruit> fruits;
     private boolean running = false;
     private Random r = new Random();
     private String backimg = "bg.png";
     private int fruitOver = 0;

     public MainFrame() {
         // 构造函数
         this.setTitle("摇奖机  v1.0");
         this.setSize(Utils.WIDTH, Utils.HEIGHT);
         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
         this.setLocationRelativeTo(null);
         this.setResizable(false);

         Container c = getContentPane();
         MyPanel zb = new MyPanel();
         c.add(zb);
         fruits = new ArrayList<Fruit>();
         // 添加3个
         for (int i = 0; i < 3; i++) {
             Fruit fruit = new Fruit();
             fruit.setIndex(r.nextInt(6));
             fruits.add(fruit);
         }
         this.addMouseListener(new MousClick());
         this.setVisible(true);

     }

     public class MyPanel extends JPanel {

         protected void paintComponent(Graphics g) {
             g.fillRect(0, 0, Utils.WIDTH, Utils.HEIGHT);
             g.drawImage(Utils.images.get(backimg), 0, 0, Utils.WIDTH,
                     Utils.HEIGHT, this);
             BufferedImage image = Utils.images.get("fruit.jpg");
             BufferedImage subImg = null;

             // 画水果
             if (!running && fruitOver == 3) {
                 int i = 0;
                 for (Fruit f : fruits) {
                     subImg = image.getSubimage(f.getIndex() * 94, 0, 94, 102);
                     g.drawImage(subImg, 84 + (i++ * (67 + 9)), 313, 67, 100,
                             this);
                 }
             } else {
                 int i = 0;
                 for (Fruit f : fruits) {
                     int index = f.getIndex();
                     int offset = f.getOffset();
                     int nextIndex = 0;
                     // 如果offset == 0 的话 或者已经移动到 就只需要一张
                     if (!f.isAlive() || offset == 0) {
                         subImg = image.getSubimage(f.getIndex() * 94, 0, 94,
                                 102);
                         g.drawImage(subImg, 84 + ((i) * (67 + 9)), 313, 67, 100,
                                 this);
                         i+=1;
                         continue;
                     }
                     // 得到下一张图的index
                     nextIndex = (index == 5) ? 0 : index + 1;
                     // 画第一张
                     subImg = image.getSubimage(index * 94, 0, 94, 102 - offset);
                     g.drawImage(subImg, 84 + (i * (67 + 9)), 313 + offset, 67,
                             100 - offset, this);
                     // 画第二张
                     subImg = image.getSubimage(nextIndex * 94, 102 - offset,
                             94, offset);// 取得子图标
                     g.drawImage(subImg, 84 + (i * (67 + 9)), 313, 67, offset,
                             this);
                     i+=1;
                 }
             }
             g.setColor(new Color(181, 36, 43));
             g.fillRect(84, 358, 220, 6);
         }
     }

     public class MousClick extends MouseAdapter {

         @Override
         public void mouseClicked(MouseEvent e) {
             int x = e.getX();
             int y = e.getY();
             //如果按的是开始摇杆
             if(x<=415 && x>=371){
                 if(y<=200 && y>=160){
                     running = true ^ running;
                     new ShowBackGroud().start();
                     if(running){
                         fruitOver = 0;
                         fruits.clear();
                         for (int i = 0; i < 3; i++) {
                             Fruit f = new Fruit();
                             f.setIndex(r.nextInt(6));
                             f.setRunning(true);
                             fruits.add(f);
                             new MyThread(f, 70 + i * 10).start();
                             f.setAlive(true);
                         }
                     }else{
                         for (Fruit f : fruits) {
                             f.setRunning(false);
                         }
                     }
                 }
                 return;
             }
             int index = -1;
             if(y<=495 && y>=480){
                 if(x>=60 && x<=90){
                     //第一个按钮
                      index = 0;
                 }else if(x>=106 && x<=140){
                     //第二个按钮
                      index = 1;
                 }else if(x>=153 && x<=182){
                     //第三个按钮
                     index = 2;
                 }
                 if(running && index >-1)
                     fruits.get(index).setRunning(false);
             }
         }
     }

     public static void main(String[] args) {
         new MainFrame();
     }

     public class MyThread extends Thread {
         public Fruit f;
         public int speed;//最后慢下来时候的初始速度
         public int sleep = 40;//休眠时长
         public int offset;//当传递一个速度是  要完成N张图片的正好播放完产生一个偏移位置
         public MyThread(Fruit f, int delay) {
             this.f = f;
             this.speed = delay;
             offset = getOffset(delay);

         }
         public int getOffset(int delay){
             int sum = 0;
             for(int i = 1;i<=delay;i++){
                 sum +=i;
             }
             return sum % 102;
         }
         @Override
         public void run() {
             while (f.isRunning()) {
                 f.setSpeed(90);// r.nextInt(70)+10 区别不大
                 try {
                     Thread.sleep(40);
                 } catch (InterruptedException e) {
                 }
                 repaint();
             }
             // 停止的时候
             //修正距离  从速度N开始减 速度每减1
             //70开始的话 能走 2485:2485%102 = 37
             // 80的话 3240%102= 78  90%4095%102=15
             f.setDistance(offset, true);//留一个像素
             int sum = 0;
             while (f.isAlive()) {
                 //缓慢下来的效果
                 f.setSpeed(speed);
                 sum += speed;
                 speed-- ;
                 try {
                     Thread.sleep(sleep+=3);
                 } catch (InterruptedException e) {
                 }
                 repaint();
             }
             synchronized (new Object()) {
                 fruitOver++;
             }
         }

     }

     public class ShowBackGroud extends Thread {

         @Override
         public void run() {
             backimg = "bg2.png";
             try {
                 Thread.sleep(500);
             } catch (InterruptedException e) {
             }
             backimg = "bg.png";
         }

     }

 }
  • 水果类
 package com.gxlee.lhj;

 public class Fruit {
     private int index = 0;
     private int nextIndex = 0;
     private int maxIndex = 5;
     private int width=94 ;//单个图片的宽度 并
     private int height = 102;//图片的高度  后面并没有用到 全部写死
     private int distance;
     private boolean alive = true;
     private boolean running ;
     private int speed;
     private int offset;
     public void setSpeed(int speed){
         if (speed<=0){
             speed = 0;
             alive = false;
         }
         this.speed = speed;
         setDisttance(distance+= speed);
     }

     public boolean isRunning() {
         return running;
     }

     public void setRunning(boolean running) {
         this.running = running;
     }

     public int getNextIndex() {
         return nextIndex;
     }
     public void setNextIndex(int nextIndex) {
         this.nextIndex = nextIndex;
     }
     public int getOffset() {
         return offset;
     }
     public void setOffset(int offset) {
         this.offset = offset;
     }

     public int getSpeed() {
         return speed;
     }
     public boolean isAlive() {
         return alive;
     }

     public void setAlive(boolean alive) {
         this.alive = alive;
     }

     public int getDistance() {
         return distance;
     }
     public void setDistance(int offset, boolean flag ) {
         int nextIndex = index==5?0:index+1;
         int dis = nextIndex*102+102-(offset);
         setDisttance(dis);
     }
     public void setDisttance(int distance){
         if(distance>=6*102){distance = distance % 102;}
         this.distance = distance;
         index = distance / 102;
         offset = distance % 102;
         nextIndex = (index == 5) ? 0 : index + 1;
     }

     public void setIndex(int index){
         this.index = index;
         distance = index*height;
     }

     public int getHeight(){
         return height;
     }
     public int getIndex() {
         return index;
     }

     public int getMaxIndex() {
         return maxIndex;
     }
     public void setMaxIndex(int maxIndex) {
         this.maxIndex = maxIndex;
     }
     public int getWidth() {
         return width;
     }

 }
  • 工具类
 package com.gxlee.lhj;

 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;

 import javax.imageio.ImageIO;

 public class Utils {
     public static Map<String, BufferedImage> images = new HashMap<String,BufferedImage>();
     public static final int WIDTH = 440;
     public static final int HEIGHT =600;

     public static final int FRUIT_WIDTH = 94;
     public static final int FRUIT_HEIGHT = 102;

     static{
       File dir = new File("img");
       File[] files = dir.listFiles();
       for (File file:files) {
         try {
             BufferedImage image = ImageIO.read(file);
             images.put(file.getName(), image);
         } catch (IOException e) {
             //e.printStackTrace();
         }
     }
     }
 }

素材:

  • 主界面:

JAVA小项目之摇奖机JAVA小项目之摇奖机

  • 水果:

JAVA小项目之摇奖机

上一篇:20145213《Java程序设计》实验二Java面向对象程序设计实验报告


下一篇:ASP.NET MVC4中的Model验证 移除指定验证信息