有關於JAVA程式 畫圖 使用applet語法

目前我還只是個初學者只懂得打出方型與圓形 三角形和平行四邊形(屋頂)就無所適從 這邊的語法 該怎麼寫呢 如果可以的話 可以整個寫出來嗎?

http://farm4.static.flickr.com/3388/5747238482_02f...

1 Answer

Rating
  • Cola
    Lv 5
    9 years ago
    Favorite Answer

    import javax.swing.*;

    import java.awt.*;

    import java.awt.RenderingHints.*;

    public class DrawDemo extends JApplet{

    public DrawDemo(){}

    public void init(){}

    public void paint(Graphics g){

    Graphics2D g2d = (Graphics2D)g;

    //消除圖形鋸齒

    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

    RenderingHints.VALUE_ANTIALIAS_ON);

    //變更原點座標

    g2d.translate(20,20);

    //填滿

    g2d.setColor(Color.CYAN);

    g2d.fillPolygon(new int[]{0,44,88},new int[]{42,0,42},3);//三角形屋頂

    g2d.setColor(Color.red);

    g2d.fillPolygon(new int[]{44,88,306,262},new int[]{0,42,42,0},4);//四邊形屋頂

    g2d.setColor(Color.BLUE);

    g2d.fillRect(0,42,88,108);//左側四方形

    g2d.setColor(Color.yellow);

    g2d.fillRect(88,42,218,108);//右側四方形

    g2d.setColor(Color.black);

    g2d.fillRect(163,87,68,63);//門

    g2d.setColor(Color.white);

    g2d.fillRect(20,64,44,44);//窗 //設定畫筆

    g2d.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

    g2d.setColor(Color.black);

    //輪廓

    g2d.drawPolygon(new int[]{0,44,88},new int[]{42,0,42},3);//三角形屋頂

    g2d.drawPolygon(new int[]{44,88,306,262},new int[]{0,42,42,0},4);//四邊形屋頂

    g2d.drawRect(0,42,88,108);//左側四方形

    g2d.drawRect(88,42,218,108);//右側四方形

    g2d.drawRect(163,87,68,63);//門

    g2d.drawRect(20,64,44,44);//窗 g2d.drawLine(42,64,42,108);

    g2d.drawLine(20,86,64,86);

    }

    }

Still have questions? Get your answers by asking now.