您的位置:首页 > 编程语言 > Java开发

Java 一个简单的画图程序

2011-07-25 14:04 399 查看
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;

public class DrawArcs extends JFrame {
public DrawArcs() {
setTitle("画弧形");
getContentPane().add(new ArcsPanel());
}

/** 主方法 */
public static void main(String[] args) {
DrawArcs frame = new DrawArcs();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(250, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

// 在面板上画弧形的类
class ArcsPanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE); //设置弧形的颜色为蓝色

int i=0;
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
int radius = (int)(Math.min(getWidth(), getHeight()) * 0.4);

int x = xCenter - radius;
int y = yCenter - radius;

//使用while循环画弧形
while(i<360){
g.fillArc(x, y, 2 * radius, 2 * radius, i, 30);
i+=90;
}
}
}

输出的效果图:





本文出自 “IT的点点滴滴” 博客,请务必保留此出处http://liangruijun.blog.51cto.com/3061169/622706
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: