JAVA小项目之摇奖机

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體:   |    |    |  超大  

功能:

点击”摇杆“开始;两种结束滚动方式,A:点击”摇杆“ B:分别点击 对应结果框的按钮;实现最后减速停下来效果,模拟真实摇奖机。知识点:A.线程的控制,B.图片轮播原理

效果图:

窗口类1packagecom.gxlee.lhj;23importjava.awt.Color;4importjava.awt.Container;5importjava.awt.Graphics;6importjava.awt.event.MouseAdapter;7importjava.awt.event.MouseEvent;8importjava.awt.image.BufferedImage;9importjava.util.ArrayList;10importjava.util.List;11importjava.util.Random;1213importjavax.swing.JFrame;14importjavax.swing.JPanel;1516@SupPRessWarnings("serial")17publicclassMainFrameextendsJFrame {1819privateList<Fruit>fruits;20privatebooleanrunning =false;21privateRandom r =newRandom();22privateString backimg = "bg.png";23privateintfruitOver = 0;2425publicMainFrame() {26//构造函数27this.setTitle("摇奖机 v1.0");28this.setSize(Utils.WIDTH, Utils.HEIGHT);29this.setDefaultCloSEOperation(EXIT_ON_CLOSE);30this.setLocationRelativeTo(null);31this.setResizable(false);3233Container c =getContentPane();34MyPanel zb =newMyPanel();35c.add(zb);36fruits =newArrayList<Fruit>();37//添加3个38for(inti = 0; i < 3; i++) {39Fruit fruit =newFruit();40fruit.setIndex(r.nextInt(6));41fruits.add(fruit);42}43this.addMouseListener(newMousClick());44this.setVisible(true);4546}4748publicclassMyPanelextendsJPanel {4950protectedvoidpaintComponent(Graphics g) {51g.fillRect(0, 0, Utils.WIDTH, Utils.HEIGHT);52g.drawImage(Utils.images.get(backimg), 0, 0, Utils.WIDTH,53Utils.HEIGHT,this);54BufferedImage image = Utils.images.get("fruit.jpg");55BufferedImage subImg =null;5657//画水果58if(!running && fruitOver == 3) {59inti = 0;60for(Fruit f : fruits) {61subImg = image.getSubimage(f.getIndex() * 94, 0, 94, 102);62g.drawImage(subImg, 84 + (i++ * (67 + 9)), 313, 67, 100,63this);64}65}else{66inti = 0;67for(Fruit f : fruits) {68intindex =f.getIndex();69intoffset =f.getOffset();70intnextIndex = 0;71//如果offset == 0 的话 或者已经移动到 就只需要一张72if(!f.isAlive() || offset == 0) {73subImg = image.getSubimage(f.getIndex() * 94, 0, 94,74102);75g.drawImage(subImg, 84 + ((i) * (67 + 9)), 313, 67, 100,76this);77i+=1;78continue;79}80//得到下一张图的index81nextIndex = (index == 5) ? 0 : index + 1;82//画第一张83subImg = image.getSubimage(index * 94, 0, 94, 102 -offset);84g.drawImage(subImg, 84 + (i * (67 + 9)), 313 + offset, 67,85100 - offset,this);86//画第二张87subImg = image.getSubimage(nextIndex * 94, 102 -offset,8894, offset);//取得子图标89g.drawImage(subImg, 84 + (i * (67 + 9)), 313, 67, offset,90this);91i+=1;92}93}94g.setColor(newColor(181, 36, 43));95g.fillRect(84, 358, 220, 6);96}97}9899publicclassMousClickextendsMouseAdapter {100101@Override102publicvoidmouseClicked(MouseEvent e) {103intx =e.getX();104inty =e.getY();105//如果按的是开始摇杆106if(x<=415 && x>=371){107if(y<=200 && y>=160){108running =true^running;109newShowBackGroud().start();110if(running){111fruitOver = 0;112fruits.clear();113for(inti = 0; i < 3; i++) {114Fruit f =newFruit();115f.setIndex(r.nextInt(6));116f.setRunning(true);117fruits.add(f);118newMyThread(f, 70 + i * 10).start();119f.setAlive(true);120}121}else{122for(Fruit f : fruits) {123f.setRunning(false);124}125}126}127return;128}129intindex = -1;130if(y<=495 && y>=480){131if(x>=60 && x<=90){132//第一个按钮133index = 0;134}elseif(x>=106 && x<=140){135//第二个按钮136index = 1;137}elseif(x>=153 && x<=182){138//第三个按钮139index = 2;140}141if(running && index >-1)142fruits.get(index).setRunning(false);143}144}145}146147publicstaticvoidmain(String[] args) {148newMainFrame();149}150151publicclassMyThreadextendsThread {152publicFruit f;153publicintspeed;//最后慢下来时候的初始速度154publicintsleep = 40;//休眠时长155publicintoffset;//当传递一个速度是 要完成N张图片的正好播放完产生一个偏移位置156publicMyThread(Fruit f,intdelay) {157this.f =f;158this.speed =delay;159offset =getOffset(delay);160161}162publicintgetOffset(intdelay){163intsum = 0;164for(inti = 1;i<=delay;i++){165sum +=i;166}167returnsum % 102;168}169@Override170publicvoidrun() {171while(f.isRunning()) {172f.setSpeed(90);//r.nextInt(70)+10 区别不大173try{174Thread.sleep(40);175}catch(InterruptedException e) {176}177repaint();178}179//停止的时候180//修正距离 从速度N开始减 速度每减1181//70开始的话 能走 2485:2485%102 = 37182//80的话 3240%102= 78 90%4095%102=15183f.setDistance(offset,true);//留一个像素184intsum = 0;185while(f.isAlive()) {186//缓慢下来的效果187f.setSpeed(speed);188sum +=speed;189speed--;190try{191Thread.sleep(sleep+=3);192}catch(InterruptedException e) {193}194repaint();195}196synchronized(newObject()) {197fruitOver++;198}199}200201}202203publicclassShowBackGroudextendsThread {204205@Override206publicvoidrun() {207backimg = "bg2.png";208try{209Thread.sleep(500);210}catch(InterruptedException e) {211}212backimg = "bg.png";213}214215}216217}

View Code

水果类1packagecom.gxlee.lhj;234publicclassFruit {5privateintindex = 0;6privateintnextIndex = 0;7privateintmaxIndex = 5;8privateintwidth=94 ;//单个图片的宽度 并9privateintheight = 102;//图片的高度 后面并没有用到 全部写死10privateintdistance;11privatebooleanalive =true;12privatebooleanrunning ;13privateintspeed;14privateintoffset;15publicvoidsetSpeed(intspeed){16if(speed<=0){17speed = 0;18alive =false;19}20this.speed =speed;21setDisttance(distance+=speed);22}2324publicbooleanisRunning() {25returnrunning;26}2728publicvoidsetRunning(booleanrunning) {29this.running =running;30}3132publicintgetNextIndex() {33returnnextIndex;34}35publicvoidsetNextIndex(intnextIndex) {36this.nextIndex =nextIndex;37}38publicintgetOffset() {39returnoffset;40}41publicvoidsetOffset(intoffset) {42this.offset =offset;43}4445publicintgetSpeed() {46returnspeed;47}48publicbooleanisAlive() {49returnalive;50}5152publicvoidsetAlive(booleanalive) {53this.alive =alive;54}5556publicintgetDistance() {57returndistance;58}59publicvoidsetDistance(intoffset,booleanflag ) {60intnextIndex = index==5?0:index+1;61intdis = nextIndex*102+102-(offset);62setDisttance(dis);63}64publicvoidsetDisttance(intdistance){65if(distance>=6*102){distance = distance % 102;}66this.distance =distance;67index = distance / 102;68offset = distance % 102;69nextIndex = (index == 5) ? 0 : index + 1;70}7172publicvoidsetIndex(intindex){73this.index =index;74distance = index*height;75}7677publicintgetHeight(){78returnheight;79}80publicintgetIndex() {81returnindex;82}8384publicintgetMaxIndex() {85returnmaxIndex;86}87publicvoidsetMaxIndex(intmaxIndex) {88this.maxIndex =maxIndex;89}90publicintgetWidth() {91returnwidth;92}93949596}

View Code

工具类1packagecom.gxlee.lhj;23importjava.awt.image.BufferedImage;4importjava.io.File;5importjava.io.IOException;6importjava.util.HashMap;7importjava.util.Map;89importjavax.imageio.ImageIO;1011publicclassUtils {12publicstaticMap<String, BufferedImage> images =newHashMap<String,BufferedImage>();13publicstaticfinalintWIDTH = 440;14publicstaticfinalintHEIGHT =600;1516publicstaticfinalintFRUIT_WIDTH = 94;17publicstaticfinalintFRUIT_HEIGHT = 102;181920static{21File dir =newFile("img");22File[] files =dir.listFiles();23for(File file:files) {24try{25BufferedImage image =ImageIO.read(file);26images.put(file.getName(), image);27}catch(IOException e) {28//e.printStackTrace();29}30}31}32}

View Code

素材:

主界面:

水果:

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航