欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java利用隨機分錢模擬財富變化

 更新時間:2022年12月21日 09:04:01   作者:天人合一peng  
這篇文章主要為大家詳細介紹了Java如何利用隨機分錢思想模擬財富的變化,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

效果圖

如果財富為負(fù)值時可以通過鍵盤和鼠標(biāo)事件讓其反轉(zhuǎn)方向

示例代碼

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Arrays;
 
public class AlgoVisualizer {
 
    private static int DELAY = 40;
    private int[] money;
    private AlgoFrame frame;
 
    public AlgoVisualizer(int sceneWidth, int sceneHeight){
 
        // 初始化數(shù)據(jù)
        money = new int[100];
        for(int i = 0 ; i < money.length ; i ++)
            money[i] = 100;
 
        // 初始化視圖
        EventQueue.invokeLater(() -> {
            frame = new AlgoFrame("Money Problem", sceneWidth, sceneHeight);
            this.frame.addKeyListener(new AlgoKeyListener());
            frame.addMouseListener(new AlgoMouseListener());
            new Thread(() -> {
                run();
            }).start();
        });
    }
 
    public void run(){
 
        while(true){
 
            // 改進2:是否排序
            Arrays.sort(money);
            frame.render(money);
            AlgoVisHelper.pause(DELAY);
 
            // 改進1:每一幀執(zhí)行的輪數(shù)
            for(int k = 0 ; k < 50 ; k ++){
                for(int i = 0 ; i < money.length; i ++){
                    // 改進3:允許money為負(fù)值
                    //if(money[i] > 0){
                        int j = (int)(Math.random() * money.length);
                        money[i] -= 1;
                        money[j] += 1;
                    //}
                }
            }
        }
    }
 
    public static void main(String[] args) {
 
        int sceneWidth = 1000;
        int sceneHeight = 800;
 
        AlgoVisualizer vis = new AlgoVisualizer(sceneWidth, sceneHeight);
    }
 
 
 
    private class AlgoKeyListener extends KeyAdapter {
        private AlgoKeyListener() {}
 
        public void keyReleased(KeyEvent event) {
            if (event.getKeyChar() == 'u') {
                System.out.println("字母u");
                AlgoVisHelper.is_change = true;
            }
            if (event.getKeyChar() == 'd') {
                System.out.println("字母d");
 
                AlgoVisHelper.is_change = false;
            }
 
            if (event.getKeyChar() == ' ') {
                System.out.println("空格");
                AlgoVisHelper.is_change = !AlgoVisHelper.is_change ;
            }
 
 
        }
    }
 
 
    private class AlgoMouseListener extends MouseAdapter {
        private AlgoMouseListener() {
        }
 
        public void mousePressed(MouseEvent event) {
//            event.translatePoint(0, -(AlgoVisualizer.this.frame.getBounds().height - AlgoVisualizer.this.frame.getCanvasHeight()));
 
//            left
           if (event.getButton() == 1)
           {
               System.out.println("Left button pressed");
               AlgoVisHelper.is_change = true;
 
           }
//         right
           else if( event.getButton() == 3)
           {
               System.out.println("right button pressed");
 
               AlgoVisHelper.is_change = false;
 
           }
//            System.out.println("mousePressed");
//            AlgoVisHelper.is_change = !AlgoVisHelper.is_change;
 
        }
    }
 
 
}
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.lang.InterruptedException;
 
public class AlgoVisHelper {
 
    private AlgoVisHelper(){}
 
 
    public static final Color Red = new Color(0xF44336);
    public static final Color Pink = new Color(0xE91E63);
    public static final Color Purple = new Color(0x9C27B0);
    public static final Color DeepPurple = new Color(0x673AB7);
    public static final Color Indigo = new Color(0x3F51B5);
    public static final Color Blue = new Color(0x2196F3);
    public static final Color LightBlue = new Color(0x03A9F4);
    public static final Color Cyan = new Color(0x00BCD4);
    public static final Color Teal = new Color(0x009688);
    public static final Color Green = new Color(0x4CAF50);
    public static final Color LightGreen = new Color(0x8BC34A);
    public static final Color Lime = new Color(0xCDDC39);
    public static final Color Yellow = new Color(0xFFEB3B);
    public static final Color Amber = new Color(0xFFC107);
    public static final Color Orange = new Color(0xFF9800);
    public static final Color DeepOrange = new Color(0xFF5722);
    public static final Color Brown = new Color(0x795548);
    public static final Color Grey = new Color(0x9E9E9E);
    public static final Color BlueGrey = new Color(0x607D8B);
    public static final Color Black = new Color(0x000000);
    public static final Color White = new Color(0xFFFFFF);
 
 
    public static void strokeCircle(Graphics2D g, int x, int y, int r){
 
        Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
        g.draw(circle);
    }
 
    public static void fillCircle(Graphics2D g, int x, int y, int r){
 
        Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
        g.fill(circle);
    }
 
    public static void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
 
        Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
        g.draw(rectangle);
    }
 
    public static void fillRectangle(Graphics2D g, int x, int y, int w, int h){
 
        Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
        g.fill(rectangle);
    }
 
    public static void setColor(Graphics2D g, Color color){
        g.setColor(color);
    }
 
    public static void setStrokeWidth(Graphics2D g, int w){
        int strokeWidth = w;
        g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
    }
 
    public static void pause(int t) {
        try {
            Thread.sleep(t);
        }
        catch (InterruptedException e) {
            System.out.println("Error sleeping");
        }
    }
 
    public static void putImage(Graphics2D g, int x, int y, String imageURL){
 
        ImageIcon icon = new ImageIcon(imageURL);
        Image image = icon.getImage();
 
        g.drawImage(image, x, y, null);
    }
 
    public static void drawText(Graphics2D g, String text, int centerx, int centery){
 
        if(text == null)
            throw new IllegalArgumentException("Text is null in drawText function!");
 
        FontMetrics metrics = g.getFontMetrics();
        int w = metrics.stringWidth(text);
        int h = metrics.getDescent();
        g.drawString(text, centerx - w/2, centery + h);
    }
 
    public  static  int changeDir(int change)
    {
//        System.out.println("bollen");
        change += 2;
 
        return change;
    }
 
    public static boolean is_change = false;
 
}
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.RenderingHints;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
 
import javax.swing.*;
 
public class AlgoFrame extends JFrame{
 
    private int canvasWidth;
    private int canvasHeight;
 
    public AlgoFrame(String title, int canvasWidth, int canvasHeight){
 
        super(title);
 
        this.canvasWidth = canvasWidth;
        this.canvasHeight = canvasHeight;
 
        AlgoCanvas canvas = new AlgoCanvas();
        setContentPane(canvas);
        pack();
 
 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
 
        setVisible(true);
    }
 
    public AlgoFrame(String title){
 
        this(title, 1024, 768);
    }
 
    public int getCanvasWidth(){return canvasWidth;}
    public int getCanvasHeight(){return canvasHeight;}
 
    // data
    private int[] money;
    public void render(int[] money){
        this.money = money;
        repaint();
    }
 
    private class AlgoCanvas extends JPanel{
 
        public AlgoCanvas(){
            // 雙緩存
            super(true);
        }
 
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
 
            Graphics2D g2d = (Graphics2D)g;
 
            // 抗鋸齒
            RenderingHints hints = new RenderingHints(
                    RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2d.addRenderingHints(hints);
 
            // 具體繪制
            int w = canvasWidth / money.length;
            for(int i = 0 ; i < money.length ; i ++)
 
                if(money[i] > 0) {
                    AlgoVisHelper.setColor(g2d, AlgoVisHelper.Blue);
                    AlgoVisHelper.fillRectangle(g2d,
                            i * w + 1, canvasHeight / 2 - money[i], w - 1, money[i]);
                }
                else{
 
                    if(AlgoVisHelper.is_change == true)
                    {
                        AlgoVisHelper.setColor(g2d, AlgoVisHelper.Red);
                        AlgoVisHelper.fillRectangle(g2d,
                                i * w + 1, canvasHeight / 2 + money[i] , w - 1, -money[i]);
                    }
                    else
                    {
                        AlgoVisHelper.setColor(g2d, AlgoVisHelper.Red);
                        AlgoVisHelper.fillRectangle(g2d,
                                i * w + 1, canvasHeight / 2 , w - 1, -money[i]);
                    }
 
 
                }
 
 
                下移至圖像下面開始
//            if(money[i] > 0) {
//                AlgoVisHelper.setColor(g2d, AlgoVisHelper.Blue);
//                AlgoVisHelper.fillRectangle(g2d,
//                        i * w + 1, canvasHeight  - money[i], w - 1, money[i]);
//            }
//            else{
//
//                AlgoVisHelper.setColor(g2d, AlgoVisHelper.Red);
//                AlgoVisHelper.fillRectangle(g2d,
//                        i * w + 1, canvasHeight + money[i] , w - 1, -money[i]);
//            }
//
        }
 
        @Override
        public Dimension getPreferredSize(){
            return new Dimension(canvasWidth, canvasHeight);
        }
    }
}

到此這篇關(guān)于Java利用隨機分錢模擬財富變化的文章就介紹到這了,更多相關(guān)Java隨機分錢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • mybatis輸出SQL格式化方式

    mybatis輸出SQL格式化方式

    這篇文章主要介紹了mybatis輸出SQL格式化方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • java前后端加密解密crypto-js的實現(xiàn)

    java前后端加密解密crypto-js的實現(xiàn)

    這篇文章主要介紹了java前后端加密解密crypto-js的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • JDBC PreparedStatement Like參數(shù)報錯解決方案

    JDBC PreparedStatement Like參數(shù)報錯解決方案

    這篇文章主要介紹了JDBC PreparedStatement Like參數(shù)報錯解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • Java基礎(chǔ)-Java變量的聲明和作用域

    Java基礎(chǔ)-Java變量的聲明和作用域

    這篇文章主要介紹了Java變量的聲明和作用域,變量其實就是內(nèi)存中的一個存儲空間,用來存儲數(shù)據(jù),具體的相關(guān)內(nèi)容,需要的小伙伴可以參考下面文章內(nèi)容
    2022-01-01
  • Java基礎(chǔ)教程之?dāng)?shù)組的定義與使用

    Java基礎(chǔ)教程之?dāng)?shù)組的定義與使用

    Java語言的數(shù)組是一個由固定長度的特定類型元素組成的集合,它們的數(shù)據(jù)類型必須相同,聲明變量的時候,必須要指定參數(shù)類型,這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)教程之?dāng)?shù)組的定義與使用的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • 集合框架及背后的數(shù)據(jù)結(jié)構(gòu)

    集合框架及背后的數(shù)據(jù)結(jié)構(gòu)

    本文主要介紹了Java的集合框架Java?Collection?Framework,接口interface以及背后的數(shù)據(jù)結(jié)構(gòu),感興趣的同學(xué)可以閱讀參考
    2023-03-03
  • java 文件流的處理方式 文件打包成zip

    java 文件流的處理方式 文件打包成zip

    這篇文章主要介紹了java 文件流的處理方式 文件打包成zip,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java并發(fā)中的ABA問題學(xué)習(xí)與解決方案

    Java并發(fā)中的ABA問題學(xué)習(xí)與解決方案

    這篇文章主要介紹了Java并發(fā)中的ABA問題學(xué)習(xí)與解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • SpringBoot AOP使用筆記

    SpringBoot AOP使用筆記

    今天小編就為大家分享一篇關(guān)于SpringBoot AOP使用筆記,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • 在webservice里調(diào)用耗時方法出錯的解決方案

    在webservice里調(diào)用耗時方法出錯的解決方案

    這篇文章主要介紹了在webservice里調(diào)用耗時方法出錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07

最新評論