学以致用——Java源码——使用drawArc方法绘制同心圆(Concentric Circles Using Method drawArc)

程序功能:

使用drawArc方法绘制同心圆。

参考文章:

彩色同心圆的绘制(Bull's-eye),https://blog.csdn.net/hpdlzu80100/article/details/51768614

运行结果:

学以致用——Java源码——使用drawArc方法绘制同心圆(Concentric Circles Using Method drawArc)_第1张图片

源码:

1. 实体类

//Creating JFrame to display DrawPanel.
import java.awt.Graphics;

import javax.swing.JPanel;
import java.awt.Color;


/**
 * 13.6 (Concentric Circles Using Method drawArc) Write an application that
 * draws a series of eight concentric circles. The circles should be separated
 * by 10 pixels. Use Graphics method drawArc. 
 * Code snippet from below exercise was reused in this exercise 
 * Graphics Exercise 6.1 Using method fillOval, draw a bull’s-eye that
 * alternates between two random colors, as in Fig. 6.13. Use the constructor
 * Color(int r, int g, int b) with random arguments to generate random colors.
 * 
 * @author [email protected]
 * @Date Jan 22, 2019, 11:47:10 AM
 *
 */
public class ConcentricCirclesJPanel extends JPanel
{
 
	public void paintComponent(Graphics g)
	{
		   super.paintComponent(g);
		   int width = getWidth(); // total width   
		   int height = getHeight(); // total height
		   int rRed=0;
		   int rGreen=0;
		   int rBlue=0;

		   //Color of arc, below color is called "Taibao Lan"
		   rRed = 21;
		   rGreen = 101;
		   rBlue = 192;
		   Color color=new Color(rRed, rGreen, rBlue);
		   
		   //画8个同心圆,每个同心圆相距10个像素
		   for (int i = 8; i > 0;i--){
			   g.setColor(color);
			   g.drawArc(width/2-(i+1)*10, height/2-(i+1)*10, 
		            		 20*i,  20*i, 0, 360);}

	}
		
} 

2. 测试类

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame;
import javax.swing.JTextArea;


public class DrawConcentricCircels {
	
	static JTextArea statusBar = new JTextArea();
	
	public static void main(String[] args)
	{
	 // create a panel that contains our drawing
		ConcentricCirclesJPanel panel = new ConcentricCirclesJPanel();

		
		MouseHandler handler = new MouseHandler(); 
		panel.addMouseMotionListener(handler);

		

	 // create a new frame to hold the panel
	 JFrame application = new JFrame();
	 application.setTitle("绘制同心圆");
	 
	 // set the frame to exit when it is closed
	 application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	 
	 application.add(panel,BorderLayout.CENTER); // add the panel to the frame
	 application.add(statusBar,BorderLayout.SOUTH);
	 application.setSize(280, 280); // set the size of the frame
	 application.setVisible(true); // make the frame visible    
	} 
	
	static class MouseHandler extends  MouseMotionAdapter 
	{
	   
	   // handle event when mouse enters area
		@Override
	   public void mouseMoved(MouseEvent event)
	   {  
			 statusBar.setText(String.format("光标当前坐标:[%d, %d]", 
		 	            event.getX(), event.getY()));;
	   }

	}
	

}

 

你可能感兴趣的:(Java编程(Java,Programming))