我现在遇到一个非常棘手的问题,望编程高手解答: 
要对这样一个不规则图形进行操作: 
图形:两个同心圆,从圆心画两条半径(有一定角度)分别交这两个同心圆于A,B,C,D四点。A,B,C,D所围成的图形(像轮胎截取一部分)。 
操作要求: 
1.能够对这个图形进行颜色填充,还根据需要可以变填充色。 
2.在上面画文字而且可以更改。 
3.一定要用java实现!!!! 
多谢高手了!!!

解决方案 »

  1.   

    import java.awt.Choice;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;import javax.swing.JApplet;public class wy extends JApplet {
    Color c;
    Choice choice = new Choice();
    String s[] = { "红", "蓝", "黄" }; public void paint(Graphics g) {
    g.setColor(c);
    g.fillArc(100, 100, 300, 300, -45, 90);
    g.setColor(Color.white);
    g.fillArc(175, 175, 150, 150, -45, 90);
    g.drawLine((int) (100 + 150 + 75 * Math.sin(45 * Math.PI / 180)),
    (int) (100 + 150 + 75 * Math.sin(45 * Math.PI / 180)),
    (int) (100 + 150 + 150 * Math.sin(45 * Math.PI / 180)),
    (int) (100 + 150 + 150 * Math.sin(45 * Math.PI / 180)));
    g.drawLine((int) (100 + 150 + 75 * Math.sin(45 * Math.PI / 180)),
    (int) (100 + 150 - 75 * Math.sin(45 * Math.PI / 180)),
    (int) (100 + 150 + 150 * Math.sin(45 * Math.PI / 180)),
    (int) (100 + 150 - 150 * Math.sin(45 * Math.PI / 180)));
    } public void init() {
    this.setLayout(new FlowLayout());
    resize(500, 500);
    for (int i = 0; i < 3; i++)
    choice.add(s[i]);
    choice.addItemListener(new listener());
    this.add(choice); } class listener implements ItemListener {
    public void itemStateChanged(ItemEvent arg0) {
    if (choice.getSelectedItem().equals("红"))
    c = new Color(255, 0, 0);
    else if (choice.getSelectedItem().equals("蓝"))
    c = new Color(0, 0, 255);
    else if (choice.getSelectedItem().equals("黄"))
    c = new Color(0, 255, 0);
    repaint();
    } }
    }
      

  2.   

    你的第二条要求我是一点思路没有,不过我也做了一个修改颜色的,你参考一下。
    我的思路是做一个继承自Panel的类,然后你可以放到Frame中用,两个圆的半径、起始结束角度、填充颜色都是变量,只要合理的增加seters和geters以及构造函数就可以任意改变颜色、形状等。
    我在main函数中写了一个修改颜色的代码。
    import java.awt.BorderLayout;
    import java.awt.Button;
    import java.awt.Color;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Panel;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import javax.swing.JColorChooser;
    import javax.swing.JFrame;public class SectorPanel extends Panel {
    //JFrame frm;
    Point origin;//圆心
    int bigRadius,smallRadius;//两个圆的半径
    int startAngle,endAngle;//扇区的起始和结束角度
    static Color fillColor;

    public static void main(String[] args) {
    final JFrame frm = new JFrame("绘图测试");
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frm.setSize(400,300);
    final SectorPanel sp = new SectorPanel();
    frm.add(sp,BorderLayout.CENTER);
    Button btnChooserFillColor = new Button("选择填充颜色");
    btnChooserFillColor.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0) {
     Color newColor = JColorChooser.showDialog(frm, "请选择图形的填充颜色", fillColor);
    if (newColor != null){
    sp.changeColor(newColor);
    }
    }

    });
    frm.add(btnChooserFillColor,BorderLayout.SOUTH);
    frm.setVisible(true);
    }

    public SectorPanel(){
    origin = new Point(200,150);
    bigRadius = 100;
    smallRadius = 50;
    startAngle = 60;
    endAngle = 120;
    fillColor = new Color(255,0,0);
    }

    public void changeColor(Color c){
    this.fillColor = c;
    this.repaint();
    }
    public void paint(Graphics g) {
    Color c = g.getColor();

    double x,y,w,h;
    double sX,sY,eX,eY;
    double originX,originY;
    originX = origin.getX();
    originY = origin.getY();

    //画大圆部分
    x = originX - bigRadius;
    y = originY - bigRadius;
    w = 2 * bigRadius;
    h = 2 * bigRadius;

    g.setColor(fillColor);
    g.fillArc(new Double(x).intValue(), new Double(y).intValue(), new Double(w).intValue(), new Double(h).intValue(), startAngle, endAngle - startAngle);

    //用背景色画小圆,清除这个部分
    x = originX - smallRadius;
    y = originY - smallRadius;
    w = 2 * smallRadius;
    h = 2 * smallRadius;

    g.setColor(this.getBackground());
    g.fillArc(new Double(x).intValue(), new Double(y).intValue(), new Double(w).intValue(), new Double(h).intValue(), startAngle, endAngle - startAngle);

    g.setColor(c);
    }
    }
      

  3.   

    import java.awt.*;
    import javax.swing.*;
    import java.awt.geom.*;public class Test extends JComponent
    {
        /** 起始角度和跨度 */
        int argstart = 45, argextent = 90;
        /** 中心坐标 */
        int x = 300, y = 200;
        /** 字符串位置 */
        int sx = 285, sy = 200;
        /** 小半径,大半径 */
        int rmin = 100, rmax = 150;
        public Test()
        {
            this.setSize(600,400);
            this.setPreferredSize(new Dimension(600,400));
        }    public void paint(Graphics g)
        {
            Graphics2D g2 = (Graphics2D)g;
            Arc2D pieArcmin = new Arc2D.Float(Arc2D.PIE);
            pieArcmin.setFrame(x-rmin, y-rmin, rmin*2, rmin*2);
            pieArcmin.setAngleStart(argstart);
            pieArcmin.setAngleExtent(argextent);
            Arc2D pieArcmax = new Arc2D.Float(Arc2D.PIE);
            pieArcmax.setFrame(x-rmax, y-rmax, rmax*2, rmax*2);
            pieArcmax.setAngleStart(argstart);
            pieArcmax.setAngleExtent(argextent);
            Area apieArcmin = new Area(pieArcmin);
            Area apieArcmax = new Area(pieArcmax);
            apieArcmax.subtract(apieArcmin);
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            AffineTransform at = AffineTransform.getTranslateInstance(4, 6);
            g2.setColor(new Color(0,0,0,50));
            g2.fill(at.createTransformedShape(apieArcmax));
            g2.setColor(Color.red);
            g2.fill(apieArcmax);
            GradientPaint p = new GradientPaint(x-rmax, y-rmax, new Color(250,250,250), 
                                                x+rmax, y+rmax, new Color(250,250,250,100));
            g2.setPaint(p);
            g2.fill(apieArcmax);
            g2.setColor(Color.red);
            g2.draw(apieArcmax);
            g2.setColor(new Color(0,0,0,50));
            g2.drawString("COOL", sx+2, sy+2);
            g2.setColor(Color.black);
            g2.drawString("COOL", sx, sy);
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
        }    public static void main(String[] args)
        {
            JFrame f = new JFrame();
            f.getContentPane().setLayout(new BorderLayout());
            f.getContentPane().add(new Test());
            f.pack();
            f.setVisible(true);
        }
    }
      

  4.   

    使用Shape来实现。你想怎么搞就怎么搞。