<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
一次性全部繪製出來
實現程式碼
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); // 讀取後續的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迷宮內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45