zoukankan      html  css  js  c++  java
  • 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()  方法。


  • 相关阅读:
    如何设计工作流引擎?
    产品特点概述驰骋工作流
    驰骋工作流程如何与您的系统进行耦合
    进制转换
    DNS欺骗(转) Anny
    Remember My Account Number(2 schema) Anny
    Mysql中的临时表使用方法(转) Anny
    Mysql任务调度(Event Schedular) Anny
    Using ssh connect to Amazon EC2 instance Anny
    Amazon EC2名词项目笔记(转) Anny
  • 原文地址:https://www.cnblogs.com/young-for-you/p/7286903.html
Copyright © 2011-2022 走看看