Java繪制迷宮動畫并顯示的示例代碼
一次性全部繪制出來
實現(xiàn)代碼
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){ // 初始化數(shù)據(jù) 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){ // 初始化數(shù)據(jù) 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){ // 初始化數(shù)據(jù) 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(); 先判斷是不是已經(jīng)繪制了 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); } } }
到此這篇關(guān)于Java繪制迷宮動畫并顯示的示例代碼的文章就介紹到這了,更多相關(guān)Java迷宮內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot基于Sentinel在服務(wù)上實現(xiàn)接口限流
這篇文章主要介紹了SpringBoot基于Sentinel在服務(wù)上實現(xiàn)接口限流,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10SpringSecurity 默認(rèn)表單登錄頁展示流程源碼
本篇主要講解 SpringSecurity提供的默認(rèn)表單登錄頁 它是如何展示流程,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友參考下吧2020-01-01只用400行Java代碼就能實現(xiàn)的飛翔的小鳥游戲
今天給大家?guī)淼氖顷P(guān)于Java實戰(zhàn)的相關(guān)知識,文章圍繞著只用400行Java代碼就能實現(xiàn)的飛翔的小鳥游戲展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06dom4j創(chuàng)建和解析xml文檔的實現(xiàn)方法
下面小編就為大家?guī)硪黄猟om4j創(chuàng)建和解析xml文檔的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06SpringBoot導(dǎo)出Excel的四種實現(xiàn)方式
近期接到了一個小需求,要將系統(tǒng)中的數(shù)據(jù)導(dǎo)出為Excel,且能將Excel數(shù)據(jù)導(dǎo)入到系統(tǒng),對于大多數(shù)研發(fā)人員來說,這算是一個最基本的操作了,本文就給大家總結(jié)一下SpringBoot導(dǎo)出Excel的四種實現(xiàn)方式,需要的朋友可以參考下2024-01-01