我写了一个程序 是画一个国旗 要在applet中显示 并且是要居中的。随着applet窗口的改变(变大变小变宽变粗)图形都要在中间 下面是我写的代码:import java.applet.Applet;
import java.awt.*;
 
public class CanadianFlag extends Applet
{
    /** 
     * method that is called automatically when the applet is to be painted
     * (i.e. when it is first displayed and when the window is resized)
     */
    public void paint( Graphics page )
    {
        super.paint(page);
    
        // get current width of the applet window
        int width = getWidth();
        int height = getHeight();
 
        // now draw the Canadian flag
        
       Polygon topBorder = new Polygon();
        topBorder.addPoint(0,0 );
        topBorder.addPoint(160,0 );
        topBorder.addPoint(0,1);
        topBorder.addPoint(160,1);
        page.setColor( Color.black);
        page.fillPolygon( topBorder);
        
       Polygon bottomBorder = new Polygon();
        bottomBorder.addPoint(160,90 );
        bottomBorder.addPoint(0,90 );
        bottomBorder.addPoint(0,89);
        bottomBorder.addPoint(160,89);
        page.setColor( Color.black);
        page.fillPolygon( bottomBorder);
        
       Polygon leftBorder = new Polygon();
        leftBorder.addPoint(0,0 );
        leftBorder.addPoint(1,0 );
        leftBorder.addPoint(1,90);
        leftBorder.addPoint(0,90);
        page.setColor( Color.black);
        page.fillPolygon( leftBorder);
        
        Polygon rightBorder = new Polygon();
        rightBorder.addPoint(159,0 );
        rightBorder.addPoint(160,0 );
        rightBorder.addPoint(160,90);
        rightBorder.addPoint(159,90);
        page.setColor( Color.black);
        page.fillPolygon( rightBorder);
        
        
        
       Polygon leftBar = new Polygon();
        leftBar.addPoint(5,5 );
        leftBar.addPoint(40,5 );
        leftBar.addPoint(40,85 );
        leftBar.addPoint(5,85 );
        page.setColor( Color.red );
        page.fillPolygon( leftBar);
        
        
        
        Polygon rightBar = new Polygon();
        rightBar.addPoint(120,5 );
        rightBar.addPoint(155,5 );
        rightBar.addPoint(155,85 );
        rightBar.addPoint(120,85 );
        page.setColor( Color.red );
        page.fillPolygon( rightBar);
        
        
        Polygon mapleLeaf = new Polygon();
        mapleLeaf.addPoint(80,10);
        mapleLeaf.addPoint(75,23);
        mapleLeaf.addPoint(70,20);          
        mapleLeaf.addPoint(70,50);       
        mapleLeaf.addPoint(60,40);
        mapleLeaf.addPoint(56,45);
        mapleLeaf.addPoint(50,40);
        mapleLeaf.addPoint(54,47);
        mapleLeaf.addPoint(50,50);        
        mapleLeaf.addPoint(63,60);        
        mapleLeaf.addPoint(60,65);
        mapleLeaf.addPoint(78,65);
        mapleLeaf.addPoint(78,80);
        mapleLeaf.addPoint(82,80);
        mapleLeaf.addPoint(82,65);
        mapleLeaf.addPoint(100,65);
        mapleLeaf.addPoint(97,60);
        mapleLeaf.addPoint(110,50);
        mapleLeaf.addPoint(106,47);
        mapleLeaf.addPoint(110,40);
        mapleLeaf.addPoint(102,45);
        mapleLeaf.addPoint(100,40);
        mapleLeaf.addPoint(90,50);
        mapleLeaf.addPoint(90,20);
        mapleLeaf.addPoint(85,23);
        page.setColor( Color.red );
        page.fillPolygon( mapleLeaf);
        
        // center the flag
        int x; 
        int y;                       
                                        
    }
}有限制的 只能用Polygon 希望大家能帮帮忙 谢谢你们

解决方案 »

  1.   

    上面的代码有些繁琐了 我改过了 :import java.applet.Applet;
    import java.awt.*;
     
    public class CanadianFlag extends Applet
    {
        /** 
         * method that is called automatically when the applet is to be painted
         * (i.e. when it is first displayed and when the window is resized)
         */
        public void paint( Graphics page )
        {
            super.paint(page);
        
            // get current width of the applet window
            int width = getWidth();
            int height = getHeight();
     
            // now draw the Canadian flag
         
          page.drawRect(0,0,160,90);
          page.setColor( Color.red );
          page.fillRect(5,5,35,80);  
          page.fillRect(120,5,35,80);
            
            
            Polygon mapleLeaf = new Polygon();
            mapleLeaf.addPoint(80,10);
            mapleLeaf.addPoint(75,23);
            mapleLeaf.addPoint(70,20);          
            mapleLeaf.addPoint(70,50);       
            mapleLeaf.addPoint(60,40);
            mapleLeaf.addPoint(56,45);
            mapleLeaf.addPoint(50,40);
            mapleLeaf.addPoint(54,47);
            mapleLeaf.addPoint(50,50);        
            mapleLeaf.addPoint(63,60);        
            mapleLeaf.addPoint(60,65);
            mapleLeaf.addPoint(78,65);
            mapleLeaf.addPoint(78,80);
            mapleLeaf.addPoint(82,80);
            mapleLeaf.addPoint(82,65);
            mapleLeaf.addPoint(100,65);
            mapleLeaf.addPoint(97,60);
            mapleLeaf.addPoint(110,50);
            mapleLeaf.addPoint(106,47);
            mapleLeaf.addPoint(110,40);
            mapleLeaf.addPoint(102,45);
            mapleLeaf.addPoint(100,40);
            mapleLeaf.addPoint(90,50);
            mapleLeaf.addPoint(90,20);
            mapleLeaf.addPoint(85,23);
            page.setColor( Color.red );
            page.fillPolygon( mapleLeaf);
            
            // center the flag
            int x; 
            int y;                       
                                            
        }
    }
      

  2.   

    搞定了 想了很久 查了很多资料 终于知道咋写了 嘿嘿 挺高兴import java.applet.Applet;
    import java.awt.*;
     
    public class CanadianFlag extends Applet
    {
        /** 
         * method that is called automatically when the applet is to be painted
         * (i.e. when it is first displayed and when the window is resized)
         */
        public void paint( Graphics page )
        {
            super.paint(page);
        
            // get current width of the applet window
            int width = getWidth();
            int height = getHeight();
     
             int x; 
             int y;
             x = (width - 160)/2;
             y = (height - 90)/2;        // now draw the Canadian flag
         
            page.drawRect(x,y,160,90);
            page.setColor( Color.red );
            page.fillRect(x + 5,y + 5,35,80);  
            page.fillRect(x + 120,y + 5,35,80);
            
           Polygon mapleLeaf = new Polygon();
            mapleLeaf.addPoint(x+80,y+10);
            mapleLeaf.addPoint(x+75,y+23);
            mapleLeaf.addPoint(x+70,y+20);          
            mapleLeaf.addPoint(x+70,y+50);       
            mapleLeaf.addPoint(x+60,y+40);
            mapleLeaf.addPoint(x+56,y+45);
            mapleLeaf.addPoint(x+50,y+40);
            mapleLeaf.addPoint(x+54,y+47);
            mapleLeaf.addPoint(x+50,y+50);        
            mapleLeaf.addPoint(x+63,y+60);        
            mapleLeaf.addPoint(x+60,y+65);
            mapleLeaf.addPoint(x+78,y+65);
            mapleLeaf.addPoint(x+78,y+80);
            mapleLeaf.addPoint(x+82,y+80);
            mapleLeaf.addPoint(x+82,y+65);
            mapleLeaf.addPoint(x+100,y+65);
            mapleLeaf.addPoint(x+97,y+60);
            mapleLeaf.addPoint(x+110,y+50);
            mapleLeaf.addPoint(x+106,y+47);
            mapleLeaf.addPoint(x+110,y+40);
            mapleLeaf.addPoint(x+102,y+45);
            mapleLeaf.addPoint(x+100,y+40);
            mapleLeaf.addPoint(x+90,y+50);
            mapleLeaf.addPoint(x+90,y+20);
            mapleLeaf.addPoint(x+85,y+23);
            page.setColor( Color.red );
            page.fillPolygon( mapleLeaf);
            
            // center the flag
           
          
         }
    }