最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java繪制迷宮動畫并顯示的示例代碼

 更新時間:2022年08月29日 17:02:59   作者:天人合一peng  
這篇文章主要為大家詳細介紹了如何利用Java語言實現繪制迷宮動畫并顯示,文中的示例代碼講解詳細,對我們學習Java有一定幫助,需要的可以參考一下

一次性全部繪制出來

實現代碼

import java.awt.*;
 
public class AlgoVisualizer {
 
    private static int DELAY = 200;
    private static int blockSide = 8;
 
    private MazeData data;
    private AlgoFrame frame;
 
    public AlgoVisualizer(String mazeFile){
 
        // 初始化數據
        data = new MazeData(mazeFile);
        int sceneHeight = data.N() * blockSide;
        int sceneWidth = data.M() * blockSide;
        
 
        // 初始化視圖
        EventQueue.invokeLater(() -> {
            frame = new AlgoFrame("Maze Solver Visualization", sceneWidth, sceneHeight);
 
            new Thread(() -> {
                run();
            }).start();
        });
    }
 
    public void run(){
 
        setData();
    }
 
    private void setData(){
    	
		frame.render(data);
        AlgoVisHelper.pause(DELAY);	 
    	
 
    }
 
    
    public static void main(String[] args) {
 
        String mazeFile = "maze_101_101.txt";
 
        AlgoVisualizer vis = new AlgoVisualizer(mazeFile);
 
    }
}
 
 
 
 
 
 
 
 
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
 
 
public class MazeData {
 
    public static final char ROAD = ' ';
    public static final char WALL = '#';
 
    private int N, M;
    private char[][] maze;
    
 
    public MazeData(String filename){
 
        if(filename == null)
            throw new IllegalArgumentException("Filename can not be null!");
 
        Scanner scanner = null;
        try{
            File file = new File(filename);
            if(!file.exists())
                throw new IllegalArgumentException("File " + filename + " doesn't exist");
 
            FileInputStream fis = new FileInputStream(file);
            scanner = new Scanner(new BufferedInputStream(fis), "UTF-8");
 
            // 讀取第一行
            String nmline = scanner.nextLine();
            String[] nm = nmline.trim().split("\\s+");
            //System.out.print(nm[0] + ' ' + nm[1]);
 
            N = Integer.parseInt(nm[0]);
            // System.out.println("N = " + N);
            M = Integer.parseInt(nm[1]);
            // System.out.println("M = " + M);
 
            // 讀取后續(xù)的N行
            maze = new char[N][M];
            for(int i = 0 ; i < N ; i ++){
                String line = scanner.nextLine();
 
                // 每行保證有M個字符
                if(line.length() != M)
                    throw new IllegalArgumentException("Maze file " + filename + " is invalid");
                for(int j = 0 ; j < M ; j ++)
                    maze[i][j] = line.charAt(j);
            }
        }
        catch(IOException e){
            e.printStackTrace();
        }
        finally {
            if(scanner != null)
                scanner.close();
        }
        
    }
 
    public int N(){ return N; }
    public int M(){ return M; }
    public char getMaze(int i, int j){
        if(!inArea(i,j))
            throw new IllegalArgumentException("i or j is out of index in getMaze!");
 
        return maze[i][j];
    }
 
    public boolean inArea(int x, int y){
        return x >= 0 && x < N && y >= 0 && y < M;
    }
 
    public void print(){
        System.out.println(N + " " + M);
        for(int i = 0 ; i < N ; i ++){
            for(int j = 0 ; j < M ; j ++)
                System.out.print(maze[i][j]);
            System.out.println();
        }
        return;
    }
 
}
 
 
 
 
 
 
 
import java.awt.*;
import java.awt.geom.Ellipse2D;
 
import java.awt.geom.Rectangle2D;
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);
//            System.out.println("Dely");
        }
        catch (InterruptedException e) {
            System.out.println("Error sleeping");
        }
    }
 
}
 
 
 
 
 
import java.awt.*;
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 MazeData data;
    public void render(MazeData data){
        this.data = data;
        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/data.M();
            int h = canvasHeight/data.N();
            
           
            
 
            for(int i = 0 ; i < data.N() ; i ++ )
            {
                for(int j = 0 ; j < data.M() ; j ++){
                    if (data.getMaze(i, j) == MazeData.WALL)
                        AlgoVisHelper.setColor(g2d, AlgoVisHelper.LightBlue);
                    else
                        AlgoVisHelper.setColor(g2d, AlgoVisHelper.White);
                    
                    AlgoVisHelper.fillRectangle(g2d, j * w, i * h, w, h);
                }
            }
        }
 
        @Override
        public Dimension getPreferredSize(){
            return new Dimension(canvasWidth, canvasHeight);
        }
    }
}

一個一個的動畫顯示

DELAY時間不能太小,小了會繪制時出錯,可能是線程出問題了???

import java.awt.*;
 
public class AlgoVisualizer {
 
    private static int DELAY = 10;
    private static int blockSide = 8;
 
    private MazeData data;
    private AlgoFrame frame;
 
    public AlgoVisualizer(String mazeFile){
 
        // 初始化數據
        data = new MazeData(mazeFile);
        int sceneHeight = data.N() * blockSide;
        int sceneWidth = data.M() * blockSide;
        
 
        // 初始化視圖
        EventQueue.invokeLater(() -> {
            frame = new AlgoFrame("Maze Solver Visualization", sceneWidth, sceneHeight);
 
            new Thread(() -> {
                run();
            }).start();
        });
    }
 
    public void run(){
    	      
        for (int i = 0; i < data.N(); i++) {
        	
        	for (int j = 0; j < data.M(); j++) {
        		setData(i, j); 
			}
		}      
    }
 
    private void setData(int i, int j){
    	
      	data.currentN = i;
    	data.currentM = j;
  
		frame.render(data);
        AlgoVisHelper.pause(DELAY);	 
 
    }
 
    
    public static void main(String[] args) {
 
        String mazeFile = "maze_101_101.txt";
 
        AlgoVisualizer vis = new AlgoVisualizer(mazeFile);
 
    }
}
 
 
 
 
 
 
 
 
 
import java.awt.*;
 
public class AlgoVisualizer {
 
    private static int DELAY = 10;
    private static int blockSide = 8;
 
    private MazeData data;
    private AlgoFrame frame;
 
    public AlgoVisualizer(String mazeFile){
 
        // 初始化數據
        data = new MazeData(mazeFile);
        int sceneHeight = data.N() * blockSide;
        int sceneWidth = data.M() * blockSide;
        
 
        // 初始化視圖
        EventQueue.invokeLater(() -> {
            frame = new AlgoFrame("Maze Solver Visualization", sceneWidth, sceneHeight);
 
            new Thread(() -> {
                run();
            }).start();
        });
    }
 
    public void run(){
    	      
        for (int i = 0; i < data.N(); i++) {
        	
        	for (int j = 0; j < data.M(); j++) {
        		setData(i, j); 
			}
		}      
    }
 
    private void setData(int i, int j){
    	
      	data.currentN = i;
    	data.currentM = j;
  
		frame.render(data);
        AlgoVisHelper.pause(DELAY);	 
 
    }
 
    
    public static void main(String[] args) {
 
        String mazeFile = "maze_101_101.txt";
 
        AlgoVisualizer vis = new AlgoVisualizer(mazeFile);
 
    }
}
 
 
 
 
 
 
 
import java.awt.*;
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 MazeData data;
    public void render(MazeData data){
        this.data = data;
        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/data.M();
            int h = canvasHeight/data.N();
                                              
         先判斷是不是已經繪制了 
            for(int n = 0; n < data.N(); n ++ )
            {
                for(int m = 0 ; m < data.M()  ; m ++){
                	     
              	  if (data.drawFinshed[n][m]) {
              		  
                      if (data.getMaze(n, m) == MazeData.WALL)
                          AlgoVisHelper.setColor(g2d, AlgoVisHelper.LightBlue);
                      else
                          AlgoVisHelper.setColor(g2d, AlgoVisHelper.White);
                                             
                      AlgoVisHelper.fillRectangle(g2d, m * w, n * h, w, h);
  				 }
                }
            }
        
            	          	  
      	      
              for(int i = data.currentN, j = 0 ; j < data.currentM + 1 ; j ++){
            	              	                         	             	
                  if (data.getMaze(i, j) == MazeData.WALL)
                      AlgoVisHelper.setColor(g2d, AlgoVisHelper.LightBlue);
                  else
                      AlgoVisHelper.setColor(g2d, AlgoVisHelper.White);
                                         
                  AlgoVisHelper.fillRectangle(g2d, j * w, i * h, w, h);
                  data.drawFinshed[i][j] = true;
              }
                               
            
            
以前一次性全部繪制顯示出來
//            for(int i = 0 ; i < data.N() ; i ++ )
//            {
//                for(int j = 0 ; j < data.M() ; j ++){
//                    if (data.getMaze(i, j) == MazeData.WALL)
//                        AlgoVisHelper.setColor(g2d, AlgoVisHelper.LightBlue);
//                    else
//                        AlgoVisHelper.setColor(g2d, AlgoVisHelper.White);
//                    
//                    AlgoVisHelper.fillRectangle(g2d, j * w, i * h, w, h);
//                }
//            }
            
        }
 
        @Override
        public Dimension getPreferredSize(){
            return new Dimension(canvasWidth, canvasHeight);
        }
    }
}

到此這篇關于Java繪制迷宮動畫并顯示的示例代碼的文章就介紹到這了,更多相關Java迷宮內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Hibernate懶加載之<class>標簽上的lazy

    Hibernate懶加載之<class>標簽上的lazy

    這篇文章主要介紹了Hibernate懶加載之<class>標簽上的lazy,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • 詳細介紹使用Java調用Python的四種方法

    詳細介紹使用Java調用Python的四種方法

    這篇文章主要給大家介紹了關于使用Java調用Python的四種方法,每種方法根據實際項目需求有其適用場景,其中,推薦使用Runtime.getRuntime()方法,因為它更為簡潔且易于實現,需要的朋友可以參考下
    2024-10-10
  • 解決IDEA使用maven創(chuàng)建Web項目,出現500錯誤的問題

    解決IDEA使用maven創(chuàng)建Web項目,出現500錯誤的問題

    本文主要介紹了在使用Maven創(chuàng)建項目并導入依賴寫完測試代碼后運行出現500錯誤的解決步驟,這種問題的根本原因是Tomcat啟動后缺少某些支持的jar包,導致運行出錯,解決方法是在項目結構中找到Artifacts,點擊要編輯的項目
    2024-10-10
  • Java中增強for循環(huán)在一維數組和二維數組中的使用方法

    Java中增強for循環(huán)在一維數組和二維數組中的使用方法

    下面小編就為大家?guī)硪黄狫ava中增強for循環(huán)在一維數組和二維數組中的使用方法。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10
  • java 中鎖的性能提高辦法

    java 中鎖的性能提高辦法

    這篇文章主要介紹了java 中鎖的性能提高辦法的相關資料,需要的朋友可以參考下
    2017-02-02
  • Java實現對華北、華南、華東和華中四個區(qū)域的劃分

    Java實現對華北、華南、華東和華中四個區(qū)域的劃分

    在Java中,通過定義枚舉類、編寫主程序和進行測試,本文詳細介紹了如何劃分華北、華南、華東和華中四個區(qū)域,首先定義枚舉類標識區(qū)域,然后通過主程序接收用戶輸入并返回相應區(qū)域,最后通過測試用例確保正確性,文章還介紹了甘特圖和餅狀圖的使用
    2024-09-09
  • java構造方法的作用總結

    java構造方法的作用總結

    在本篇文章里小編給大家整理了關于java構造方法的相關知識點以及實例代碼,有需要的朋友們可以學習下。
    2019-07-07
  • MyBatis-plus 模糊查詢的使用

    MyBatis-plus 模糊查詢的使用

    這篇文章主要介紹了MyBatis-plus 模糊查詢的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • 簡單談談JVM、JRE和JDK的區(qū)別與聯系

    簡單談談JVM、JRE和JDK的區(qū)別與聯系

    簡單的說JDK是用于開發(fā)的而JRE是用于運行Java程序的。JDK和JRE都包含了JVM,從而使得我們可以運行Java程序。JVM是Java編程語言的核心并且具有平臺獨立性。
    2016-05-05
  • spring cloud如何集成nacos配置中心

    spring cloud如何集成nacos配置中心

    這篇文章主要介紹了spring cloud如何集成nacos配置中心操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09

最新評論

赤峰市| 高唐县| 新平| 乌鲁木齐市| 日喀则市| 嘉峪关市| 苍溪县| 涟水县| 日土县| 仙桃市| 景东| 克什克腾旗| 米林县| 甘泉县| 东乌珠穆沁旗| 华阴市| 屏东市| 喀喇| 休宁县| 桂林市| 河津市| 忻州市| 西乌珠穆沁旗| 台山市| 蒙山县| 沈丘县| 巫溪县| 义马市| 上饶县| 新宁县| 舒城县| 兴安盟| 伽师县| 大方县| 白玉县| 夏邑县| 清水河县| 胶南市| 都安| 麦盖提县| 石柱|