<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
實現第個氣泡中心顯示一個字,在框中隨意運動,用空格鍵按下執行停止,再按下左鍵運動繼續。用滑鼠左鍵按下選中的圓變為填充的,再次按下又變為不填充的。參考:https://github.com/liuyubobobo/
import java.awt.*; import java.awt.event.*; public class AlgoVisualizer { private Object data; private Circle[] circles; private AlgoFrame frame; private boolean isAnmiated = true; public AlgoVisualizer(int sceneWidth, int sceneHeight, int N){ circles = new Circle[N]; int R = 50; for(int i = 0; i < N; i++) { int x = (int)(Math.random()*(sceneWidth-2*R)) + R; int y = (int)(Math.random()*(sceneHeight-2*R)) + R; int vx = (int)(Math.random()*11) - 5; int vy = (int)(Math.random()*11) - 5; circles[i] = new Circle(x, y, R, vx, vy); } EventQueue.invokeLater(()->{ frame = new AlgoFrame("Welcome-Java", sceneWidth, sceneHeight); frame.addKeyListener(new AlgoKeyListener()); frame.addMouseListener(new AlgoMouseListener()); new Thread(()->{run();}).start(); }); } public AlgoVisualizer(int sceneWidth, int sceneHeight, int N, String centerLael){ Circle.showLabel = true; circles = new Circle[N]; int R = 50; for(int i = 0; i < N; i++) { int x = (int)(Math.random()*(sceneWidth-2*R)) + R; int y = (int)(Math.random()*(sceneHeight-2*R)) + R; int vx = (int)(Math.random()*11) - 5; int vy = (int)(Math.random()*11) - 5; circles[i] = new Circle(x, y, R, vx, vy); circles[i] = new Circle(x, y, R, vx, vy, centerLael.charAt(i) + ""); } EventQueue.invokeLater(()->{ frame = new AlgoFrame("Welcome-Java", sceneWidth, sceneHeight); frame.addKeyListener(new AlgoKeyListener()); frame.addMouseListener(new AlgoMouseListener()); new Thread(()->{ run(); }).start(); }); } private void run(){ while(true) { //繪製當前資料 frame.render(circles); AlgoVisHelper.pause(20); //更新資料 if(isAnmiated) { for(Circle circle:circles) circle.move(0, 0, frame.getCanvasWidth(), frame.getCanvasHeight()); } } } private class AlgoKeyListener extends KeyAdapter { @Override public void keyReleased(KeyEvent event) { if(event.getKeyChar() == ' ') { isAnmiated = !isAnmiated; } } } private class AlgoMouseListener extends MouseAdapter{ @Override public void mousePressed (MouseEvent event) { event.translatePoint(0, // (frame.getBounds().height -frame.getCanvasHeight())); -(frame.getBounds().height -frame.getCanvasHeight())); // System.out.println(event.getPoint()); for(Circle circle:circles) { if(circle.contain(event.getPoint())){ circle.isFilled = !circle.isFilled; } } } } public static void main(String[] args) { String poemData = "三月七日沙湖道中遇雨。雨具先去,同行皆狼狽,餘獨不覺。已而遂晴,故作此詞 n" + "莫聽穿林打葉聲,何妨吟嘯且徐行。竹杖芒鞋輕勝馬,誰怕? 一蓑煙雨任平生。n" + "料峭春風吹酒醒,微冷,山頭斜照卻相迎。回首向來蕭瑟處,歸去,也無風雨也無晴。"; int sceneWidth = 800; int sceneHeight = 800; int N = 15; // AlgoVisualizer visualizer = new AlgoVisualizer(sceneWidth, sceneHeight, N); AlgoVisualizer visualizer = new AlgoVisualizer(sceneWidth, sceneHeight, N, poemData); } }
import javax.swing.*; import java.awt.*; public class AlgoFrame extends JFrame { private int canvasWidth; private int canvasHeight; public AlgoFrame(String title, int canvasWidth, int canvasHeight){ super(title); this.canvasHeight = canvasHeight; this.canvasWidth = canvasWidth; AlgoCanvas canvas = new AlgoCanvas(); setContentPane(canvas); pack(); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public AlgoFrame(String title){ this(title, 1024, 768); } public int getCanvasWidth(){return canvasWidth;} public int getCanvasHeight() {return canvasHeight;} private Circle[] circles; public void render(Circle[] circles) { this.circles = circles; repaint(); } private Object data; public void render(Object 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); g2d.addRenderingHints(hints); AlgoVisHelper.setStrokeWidth(g2d, 1); AlgoVisHelper.setColor(g2d, Color.RED); g2d.setFont( new Font("SimSun", Font.BOLD, 16)); for (Circle circle: circles) { if(!circle.isFilled) { AlgoVisHelper.strokeCircle(g2d, circle.x, circle.y, circle.getR() ); } else { AlgoVisHelper.fillCircle(g2d, circle.x, circle.y, circle.getR()); } if(Circle.showLabel) { // AlgoVisHelper.showPoem(g2d,circle.x, circle.y, circle.centerLabel); AlgoVisHelper.drawText(g2d,circle.centerLabel,circle.x, circle.y); } } } @Override public Dimension getPreferredSize(){ // System.out.println("Run getPreferredSize()"); return new Dimension(canvasWidth, canvasHeight); } } }
import java.awt.*; public class Circle { public int x, y; private int r; public int vx, vy; public String centerLabel; static public boolean showLabel = false; public boolean isFilled = false; public Circle(int x, int y, int r, int vx, int vy) { this.x = x; this.y = y; this.r = r; this.vx = vx; this.vy = vy; } public Circle(int x, int y, int r, int vx, int vy, String centerLabel) { this.x = x; this.y = y; this.r = r; this.vx = vx; this.vy = vy; this.centerLabel = centerLabel; } public int getR(){return r;} public void move(int minx, int miny, int maxx, int maxy){ x += vx; y += vy; checkCollision(minx, miny, maxx, maxy); } private void checkCollision(int minx, int miny, int maxx, int maxy) { if (x - r < minx) { x = r; vx = -vx; } if (x + r >= maxx) { x = maxx - r; vx = -vx; } if (y - r < miny) { y = r; vy = -vy; } if (y + r >= maxy) { y = maxy - r; vy = -vy; } } public boolean contain(Point p) { return (x - p.x) * (x - p.x) + (y - p.y)*(y - p.y) <= r*r; } }
import javax.swing.*; import java.awt.*; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; public class AlgoVisHelper { private AlgoVisHelper(){} public static void setStrokeWidth(Graphics2D g2d, int w){ int strokeWidth = w; g2d.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); } public static void strokeCircle(Graphics2D g2d, int x, int y, int r){ Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r); g2d.draw(circle); } public static void fillCircle(Graphics2D g2d, int x, int y, int r){ Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r); g2d.fill(circle); } public static void strokeRectangle(Graphics2D g2d, int x, int y, int w, int h) { Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h); g2d.draw(rectangle); } public static void fillRectangle(Graphics2D g2d, int x, int y, int w, int h) { Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h); g2d.fill(rectangle); } public static void putImage(Graphics2D g2d, int x, int y, String imageURL){ ImageIcon icon = new ImageIcon(imageURL); Image image = icon.getImage(); g2d.drawImage(image, x, y, null); } public static void drawText(Graphics2D g2d, String text, int centerx, int centery) { if(text == null) throw new IllegalArgumentException("Text is null"); FontMetrics metrics = g2d.getFontMetrics(); int w = metrics.stringWidth(text); int h = metrics.getDescent(); g2d.drawString(text, centerx - w/2, centery + h); } public static void showPoem(Graphics2D g2d, int x, int y, String poem){ g2d.drawString(poem,x, y); // g2d.drawString("醉", x, y); } public static void setColor(Graphics2D g2d, Color color){ g2d.setColor(color); } public static void pause(int t) { try{ Thread.sleep(t); } catch (InterruptedException e){ System.out.println("Error in sleepping"); } } }
以上就是利用Java實現帶GUI的氣泡詩詞特效的詳細內容,更多關於Java氣泡詩詞的資料請關注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