学以致用——Java源码——使用Ellipse2D.Double类绘制同心圆(Concentric Circles Using Class Ellipse2D.Double)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hpdlzu80100/article/details/86594779

运行结果:

源码:

1. 实体类

//Creating JFrame to display DrawPanel.
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;

/**
 * 13.7 (Concentric Circles Using Class Ellipse2D.Double) Modify your solution
 * to Exercise 13.6 to draw the ovals by using class Ellipse2D.Double and method
 * draw of class Graphics2D.
 * @author [email protected]
 * @Date Jan 22, 2019, 1:44:03 PM
 *
 */
public class ConcentricCirclesEllipse2DJPanel extends JPanel
{
 
	public void paint(Graphics g)
	{
	       Graphics2D g2d = (Graphics2D) g;	
		   super.paintComponents(g);
		   double width = getWidth(); // total width   
		   double 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--){
			   g2d.setColor(color);
			   g2d.draw(new Ellipse2D.Double(width/2-(i+1)*10, height/2-(i+1)*10, 
					   20*i,  20*i));

			   }	   

	}
		
} 

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 DrawConcentricCircelsEllipse2D {
	
	static JTextArea statusBar = new JTextArea();
	
	public static void main(String[] args)
	{
	 // create a panel that contains our drawing
		ConcentricCirclesEllipse2DJPanel panel = new ConcentricCirclesEllipse2DJPanel();
		
		MouseHandler handler = new MouseHandler(); 
		panel.addMouseMotionListener(handler);

		
	 // create a new frame to hold the panel
	 JFrame application = new JFrame();
	 application.setTitle("使用Ellipse2D.Double类绘制同心圆");
	 
	 // 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); // add the statusBar to the frame
	 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()));;
	   }

	}
	

}

猜你喜欢

转载自blog.csdn.net/hpdlzu80100/article/details/86594779
今日推荐