import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TestSanmo extends JFrame {
public TestSanmo() {
add(new Sanmo());
}

public static void main(String[] args) {
JFrame frame = new TestSanmo();
frame.setTitle("TestSanmo");
frame.setSize(210, 330);
frame.setLocationRelativeTo(null); // Center the frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}


class Sanmo extends JPanel{
//定义多边形坐标数组
int polygon_x[]={5,20,25,175,180,195,5};
int polygon_y[]={300,270,230,230,270,300,300};
int polygon_pt_num=7;

public void paintComponent(Graphics g){
int red,green,blue;

//画头部,椭圆方法drawOval
g.setColor(Color.black); //setColor用来设置要使用的颜色
g.drawOval(40,40,120,150);
//画眼睛,椭圆方法drawOval
g.drawOval(57,75,30,20);
g.drawOval(110,75,30,20);
//画眼珠,填充椭圆方法fillOval
g.fillOval(68,81,10,10);
g.fillOval(121,81,10,10);

//画鼻子,椭圆方法drawOval
g.drawOval(85,100,30,30);

//画头发,直线方法drawLine,画弧方法drawArc
g.drawLine(100,10,100,60);
g.drawArc(110,20,100,80,90,90);
//drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
g.drawArc(10,30,80,60,0,80);

//画嘴巴,填充画弧方法fillArc
g.setColor(Color.red);
g.fillArc(60,130,80,40,180,180);

//画耳朵,填充椭圆方法fillOval,复制图形方法copyArea
g.setColor(Color.blue);
g.fillOval(25,92,15,30);

g.copyArea(25,92,15,30,136,0); //拷贝某区域的图形至另一区域
//copyArea(int x, int y, int width, int height, int dx, int dy)
//Copies an area of the component by a distance specified by dx and dy.
//136--宽度差dx; 0--高度差dy;

//画脖子,填充矩形方法fillRect
g.setColor(Color.pink);
g.fillRect(85,190,30,40);

//画身体,多边形方法fillPolygon
red=30;
blue=100;
green=200;
g.setColor(new Color(red,green,blue));
g.fillPolygon(polygon_x,polygon_y,polygon_pt_num);
}
}


在面板上“画”三毛(Java)_填充矩形

在面板上“画”三毛(Java)_java_02

在面板上“画”三毛(Java)_数组_03