• Java制作罗盘


    1. static String background=FileUtil.Desktop+"图片1.png";
    2. static double begincenter=225;//指南针的角度
    3. static String[] zis="午丙巳巽辰乙卯甲寅艮丑癸子壬亥乾戌辛酉庚申坤未丁".split("");
    4. //写死,适用本电脑
    5. static int width=620,height=620;
    6. static double maxwidth=1250,maxheight=620;
    7. //自动计算
    8. ImageIcon imageIcon=null;
    9. static int circle_center_x=300,circle_center_y=300,circle_radius=150;
    10. public Luopan(){
    11. setBackground(Color.red);
    12. if(background!=null){
    13. imageIcon = new ImageIcon(new ImageIcon(background).getImage());
    14. }
    15. addMouseListener(new MouseAdapter() {
    16. @Override
    17. public void mousePressed(MouseEvent e){
    18. // pressx=e.getX();
    19. // pressy=e.getY();
    20. // repaint();
    21. }
    22. @Override
    23. public void mouseReleased(MouseEvent e){
    24. // pressx=e.getX();
    25. // pressy=e.getY();
    26. // repaint();
    27. }
    28. });
    29. }
    30. @Override
    31. public void paintComponent (Graphics g){
    32. super.paintComponent(g);
    33. //绘制基本图像
    34. Graphics2D g2 = (Graphics2D) g;
    35. g2.setColor(Color.black);
    36. //消除文字锯齿
    37. g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    38. //消除画图锯齿
    39. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    40. if(null!=imageIcon)
    41. g2.drawImage(imageIcon.getImage(), 0, 0, width,height, imageIcon.getImageObserver());
    42. g2.drawOval(circle_center_x-circle_radius, circle_center_y-circle_radius, circle_radius*2, circle_radius*2);
    43. //从右开始 逆时针
    44. double qusize=360.0/zis.length;
    45. for (int idx=0;idx
    46. g2.setColor(Color.black);
    47. g2.drawArc(circle_center_x-circle_radius, circle_center_y-circle_radius, 2*circle_radius, 2*circle_radius, Math.floorMod((int)(begincenter-(qusize/2.0)+idx*qusize),360), (int)qusize);
    48. g2.setColor(Color.red);
    49. writeShuOnFan(g2, circle_center_x, circle_center_y, circle_radius, Math.floorMod((int)(begincenter-(qusize/2.0)+idx*qusize),360),qusize, zis[idx]);
    50. }
    51. }
    52. public static void main(String [] args){
    53. if(null!=background){
    54. width=ImageUtil.getImageWidthNum(background);
    55. height=ImageUtil.getImageHeightNum(background);
    56. double scale=maxwidth/width;
    57. double scale2=maxheight/height;
    58. scale=Math.min(scale, scale2);
    59. if(height!=maxheight){
    60. // 获取缩放后的长和宽
    61. width = (int) (scale * width);
    62. height = (int) (scale * height);
    63. }
    64. }
    65. circle_center_x=width/2;
    66. circle_center_y=height/2;
    67. circle_radius=Math.min(width, height)/2;
    68. EventQueue.invokeLater(new Runnable(){
    69. @Override
    70. public void run(){
    71. JFrame frame = new JFrame();
    72. frame.add(new Luopan());
    73. frame.setSize(width+15,height+38);
    74. frame.setLocation(0, 0);
    75. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    76. frame.setVisible(true);
    77. }
    78. });
    79. }

    只需要输入背景图路径以及指南针方向,就可以自动计算出罗盘

    效果如图

     

  • 相关阅读:
    Linux命令:tr和xargs
    品牌平面设计如何让自己有脑洞大开的创意 优漫动游
    【Vue 开发实战】基础篇 # 3:Vue组件的核心概念:事件
    红外特征吸收峰特征总结(主要基团的红外特征吸收峰)
    Modsecurity安装+Nginx+腾讯云CentOS+XSS-Labs靶场+WAF规则
    leetcode回溯算法总结
    【iOS】引用计数
    做亚马逊店铺怎么解决网络问题?
    【Spring Security 系列】(三)剖析基础组件之授权功能
    实现一个会动的鸿蒙 LOGO
  • 原文地址:https://blog.csdn.net/ak01_10/article/details/128200343