环球资讯:利用Java实现带GUI的气泡诗词特效
(资料图片)
目录
实现效果示例代码AlgoVisualizer.javaAlgoFrame.javaCircle.javaAlgoVisHelper.java实现效果
实现第个气泡中心显示一个字,在框中随意运动,用空格键按下运行停止,再按下左键运动继续。用鼠标左键按下选中的圆变为填充的,再次按下又变为不填充的。参考:https://github.com/liuyubobobo/
示例代码
AlgoVisualizer.java
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); } }
AlgoFrame.java
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); } } }
Circle.java
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; } }
AlgoVisHelper.java
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气泡诗词的资料请关注脚本之家其它相关文章!
X 关闭
X 关闭
- 1亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 2现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 3如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 4AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 5转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 6充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 7好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 8名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?
- 9亚马逊云科技成立量子网络中心致力解决量子计算领域的挑战
- 10京东绿色建材线上平台上线 新增用户70%来自下沉市场