• Java小项目——抽奖系统


    一、需求分析

    面板:用于放置两位抽奖号码——号码展示:1.数字 2.图片

    鼠标监听器:按下鼠标,线程开始,开始号码随机滚动;释放鼠标,判断线程循环的值flag=0,号码停止滚动,获得中奖号码。

    线程:线程开始,绘制遮挡框,号码number随机生成,绘制号码number,休眠0.1s(否则数字绘制过快,看不清楚)

    二、系统实现

    1、面板

    package com.java7.choujiang0722;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class myFrame extends JFrame {
        //主程序入口
        public static void main(String args[]){
            System.out.println("抽奖");
            myFrame mf = new myFrame();
            mf.showUI();
        }
        //展示UI函数
        public void showUI(){
            this.setTitle("抽奖");
            this.setSize(600,600);
            this.setDefaultCloseOperation(3);
            this.setLocationRelativeTo(null);
            this.setVisible(true);
            //在JFrame组件上获取画笔
            Graphics g = this.getGraphics();
            //添加监听器,鼠标按压JFrame,开始抽奖;释放,结束抽奖
            addMouseListener mouse = new addMouseListener(g);
            this.addMouseListener(mouse);
        }
    }
    

    2、鼠标监听器

    按下鼠标:线程开始

    释放鼠标:线程结束

    package com.java7.choujiang0722;
    
    import java.awt.*;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    public class addMouseListener extends MouseAdapter {
    
        public Graphics g;
        private myThread tr ;
        //构造函数,创建鼠标监听器时,将画笔传入addMouseListener
        public addMouseListener(Graphics g){
            this.g = g;
        }
    
        //鼠标按下,开始抽奖
        public void mousePressed(MouseEvent e) {
            System.out.println("开始抽奖");
            tr = new myThread(g);
            tr.start();
        }
        //鼠标释放,停止抽奖
        public void mouseReleased(MouseEvent e) {
            System.out.println("停止抽奖");
            tr.flag = false;//public,公用,此处可以直接调用
        }
    }
    

    3、线程

    package com.java7.choujiang0722;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class myThread extends Thread {
    
        private Graphics g;
        public boolean flag = true;
        //构造函数,创建线程时候,将画笔传入myThread
        public myThread(Graphics g){
            this.g = g;
        }
        ImageIcon icon;
    
        public void run() {
            while(flag){
                System.out.println("run : 抽奖中");
                //绘制抽奖数字
                g.setFont(new Font("宋体",Font.BOLD,100));
                g.setColor(Color.WHITE);
                g.fillRect(100,100,400,400);
                g.setColor(Color.RED);
                g.drawString("幸运抽奖",100,200);
    
                g.setColor(Color.CYAN);
                int number = (int)(Math.random()*10);
                g.drawString(number+"",240,330);
                //根据随机生成数字,获取0-9的图片;绘制图片
                icon = new ImageIcon(this.getClass().getResource(number+".png"));
                g.drawImage(icon.getImage(),150,200,120,200,null);
    
                number = (int)(Math.random()*10);
                g.drawString(number+"",300,330);
                icon = new ImageIcon(this.getClass().getResource(number+".png"));
                g.drawImage(icon.getImage(),350,200,120,200,null);
                try {
                    sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            super.run();
    
        }
    }
    
  • 相关阅读:
    使用CuteFTP登陆FTP(servU)服务器后无法LIST目录和文件的解决方法
    delphi技巧集锦之一
    delphi技巧集锦之二
    关于varchar(max), nvarchar(max)和varbinary(max)
    别告诉我你会用WORD
    Showmodal与show的区别
    SET ANSI_NULLS ON的用法
    {右键我的电脑无法打开计算机管理}解决方法
    Word常用技巧
    Excel 使用技巧集锦——163种技巧
  • 原文地址:https://www.cnblogs.com/iriswang/p/11084647.html
Copyright © 2020-2023  润新知