JAVA 基本绘图——利用JFrame JPanel 绘制扇形

利用JFrame制作幕布(容器),创建JPanel部件,将JPanel部件添加到JFrame容器中,利用了基本的java绘制图形方法,步骤也是基本的套路。

利用了API   ———   fillArc()  

fillArc()方法定义如下:

public abstract void fillArc(int x, int y,
int width, int height, int startAngle, int arcAngle) 

参数说明

x:要绘制填充扇形的左上角的x坐标。

y:要绘制填充扇形的左上角的y坐标。

width:要绘制填充制扇形的宽度。

height:要绘制填充扇形的高度。

startAngle:开始角度。

arcAngle:相对于开始角度而言,填充扇形的弧跨越的角度。

参数的理解很重要,一开始因为参数理解的错误导致绘制失误,另外,角度应使用整数。

源代码如下:

package 绘图;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class firstFrame extends JFrame{
	public firstFrame(){
		panel p = new panel();
		this.add(p);
		this.setTitle("my first");
		this.setLocationRelativeTo(null);
		this.setSize(500,600);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}

	public static void main(String[] args) {
		new firstFrame();
	}
}
class panel extends JPanel{
	protected void paintComponent(Graphics g){
		int CenterX,CenterY;
		int r;
		CenterX = this.getWidth();
		CenterY = this.getHeight();
		r = this.getHeight()/4;
		super.paintComponent(g);
		g.setColor(Color.blue);
		//g.fillArc(CenterX, CenterY, r, r, (int)(Math.PI/6), (int)(Math.PI));
		g.fillArc(CenterX/6, CenterY/6, 2*r, 2*r, 90, 60);
		g.fillArc(CenterX/6, CenterY/6, 2*r, 2*r, 180, 60);
		g.fillArc(CenterX/6, CenterY/6, 2*r, 2*r, 280, 60);
		g.fillArc(CenterX/6, CenterY/6, 2*r, 2*r, 360, 60);
		g.drawString(""+CenterX+" "+CenterY, 0, CenterY);
	}
}

在panel类里重写了  paintComponent()  方法。


posted @ 2017-05-27 22:53  我是王博  阅读(839)  评论(0编辑  收藏  举报