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);
    		}
    	});
    }

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

效果如图

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ak01_10

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值