用点 给一个正方形里的四个等腰三角形填上不同的颜色
这四个等腰三角形是由正方形的两条对角线连接而成的。
可以选用任何一种语言 或给出算法 给这四个三角形 填充上不同颜色 如 红  蓝  黑 绿

解决方案 »

  1.   

    写了个例子,代码如下,楼主看看是否符合你的需求package demo;import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Polygon;
    import java.awt.event.ComponentAdapter;
    import java.awt.event.ComponentEvent;import javax.swing.JFrame;
    import javax.swing.JPanel;public class DemoForDrawGrap extends JFrame { MyPanel myPanel;
    /**
     * Launch the application
     * @param args
     */
    public static void main(String args[]) {
    try {
    DemoForDrawGrap frame = new DemoForDrawGrap();
    frame.setVisible(true);

    } catch (Exception e) {
    e.printStackTrace();
    }
    } /**
     * Create the frame
     */
    public DemoForDrawGrap() {
    super();
    getContentPane().setLayout(null);
    setBounds(100, 100, 396, 289);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myPanel = new MyPanel();
    myPanel.setBounds(0, 0, 388, 255);
    getContentPane().add(myPanel);
    }
    class MyPanel extends JPanel
    {
    public void paintComponent(Graphics g)
    {
    //x,y为矩形形左上角顶点,w为宽,h为高这里是根据当前面板的顶点及宽和高做的,可以更改试试
    int x=getX();
    int y=getY();
    int w=getWidth();
    int h=getHeight();
    //计算四个三角形的坐标
    int[] xpoints1={x,(w+x)/2,w+x};
    int[] ypoints1={y,(h+y)/2,y};
    int npoints1=3;
    Color color1=Color.red;
    Polygon mPolygon1=new Polygon(xpoints1,ypoints1, npoints1);
    int[] xpoints2={x,(w+x)/2,x};
    int[] ypoints2={y,(h+y)/2,y+h};
    int npoints2=3;
    Color color2=Color.blue;
    Polygon mPolygon2=new Polygon(xpoints2,ypoints2, npoints2);
    int[] xpoints3={x,(w+x)/2,x+w};
    int[] ypoints3={y+h,(y+h)/2,y+h};
    int npoints3=3;
    Color color3=Color.yellow;
    Polygon mPolygon3=new Polygon(xpoints3,ypoints3, npoints3);
    int[] xpoints4={x+w,(x+w)/2,x+w};
    int[] ypoints4={y,(y+h)/2,y+h};
    int npoints4=3;
    Color color4=Color.green;
    Polygon mPolygon4=new Polygon(xpoints4,ypoints4, npoints4);
    g.setColor(color1);
    g.fillPolygon(mPolygon1);
    g.setColor(color2);
    g.fillPolygon(mPolygon2);
    g.setColor(color3);
    g.fillPolygon(mPolygon3);
    g.setColor(color4);
    g.fillPolygon(mPolygon4);
    }
    }
    }
      

  2.   

    1楼的兄弟给出的代码是能够运行出来的,而且结果很类似 ,但是我对AWT 或 SWING 不太理解,如果能给出每个方法的注释,那就再好不过了。。 谢谢
      

  3.   

    类似 int[] xpoints1={x,(w+x)/2,w+x}; 中的
    第二个x的坐标,应该是 x+(w/2) 吧
      

  4.   

    恩,是我算错了,每个数组中第二个都应该是正方形的中点,至于注释,其实你看看JDK中java.awt.Graphics和java.awt.Graphics2D的注释应该就差不多了,