Java制作罗盘

  • Post author:
  • Post category:java


static String background=FileUtil.Desktop+"图片1.png";
	static double begincenter=225;//指南针的角度
	static String[] zis="午丙巳巽辰乙卯甲寅艮丑癸子壬亥乾戌辛酉庚申坤未丁".split("");
	
	//写死,适用本电脑
	static int width=620,height=620;
	static double maxwidth=1250,maxheight=620;
	
	//自动计算
	ImageIcon imageIcon=null;
	static int circle_center_x=300,circle_center_y=300,circle_radius=150;
	
	
    public Luopan(){
    	setBackground(Color.red);
		if(background!=null){
			imageIcon = new ImageIcon(new ImageIcon(background).getImage());
		}
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e){
//                pressx=e.getX();
//                pressy=e.getY();
//                repaint();
            }
            @Override
            public void mouseReleased(MouseEvent e){
//                pressx=e.getX();
//                pressy=e.getY();
//                repaint();
            }
        });
    }
    
    @Override
    public void paintComponent (Graphics g){
        super.paintComponent(g);
        //绘制基本图像
        Graphics2D g2 = (Graphics2D) g;
		g2.setColor(Color.black);
		//消除文字锯齿
		g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		//消除画图锯齿
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    	if(null!=imageIcon) 
    		g2.drawImage(imageIcon.getImage(), 0, 0, width,height, imageIcon.getImageObserver());
    	g2.drawOval(circle_center_x-circle_radius, circle_center_y-circle_radius, circle_radius*2, circle_radius*2);
    	
    	//从右开始 逆时针

    	double qusize=360.0/zis.length;
    	for (int idx=0;idx<zis.length;idx++) {
    		g2.setColor(Color.black);
    		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);
    		g2.setColor(Color.red);
    		writeShuOnFan(g2, circle_center_x, circle_center_y, circle_radius, Math.floorMod((int)(begincenter-(qusize/2.0)+idx*qusize),360),qusize, zis[idx]);
		}
    }
public static void main(String [] args){
    	if(null!=background){
    		width=ImageUtil.getImageWidthNum(background);
    		height=ImageUtil.getImageHeightNum(background);
			double scale=maxwidth/width;
			double scale2=maxheight/height;
			scale=Math.min(scale, scale2);
			if(height!=maxheight){
				// 获取缩放后的长和宽
				width = (int) (scale * width);
				height = (int) (scale * height);
			}
    	}
    	circle_center_x=width/2;
    	circle_center_y=height/2;
    	circle_radius=Math.min(width, height)/2;
    	
    	EventQueue.invokeLater(new Runnable(){
    		@Override
    		public void run(){
    			JFrame frame = new JFrame();
    			frame.add(new Luopan());
    			frame.setSize(width+15,height+38);
    			frame.setLocation(0, 0);
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			frame.setVisible(true);
    		}
    	});
    }

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

效果如图



版权声明:本文为ak01_10原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。