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

程序功能:

使用drawArc方法绘制同心圆。

参考文章:

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

运行结果:

87a6359c4f54f5cb973878a6c0e2da83.png

源码:

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 Pandenghuang@163.com

* @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()));;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值