2013-12-13 85 views
0

我試圖用線條填充三角形。挑戰不在於使用填充多邊形,而是使用循環並填充線條。無論如何,我還沒有弄清楚這個循環。我將創建ints並使用for循環添加到它們。填充三角形/編譯器錯誤

我還設置了休息了,我得到這個荒謬的編譯器錯誤,所有的地方:

error: not a statement 

error: ';' expected 

這很奇怪,因爲我已經初始化的整數。有 ;在那裏。

import javax.swing.JApplet; 
import java.awt.*; 

public class Tri extends JApplet 
    { 


    public static void main(String[] args) 
    { 

     int 1x = 0; 
     int 1y = 140; 
     int 2x = 120; 
     int 2y = 140; 
     int 3x = 60; 
     int 3y =0; 

       public void paint (Graphics page) 
       { 

        page.drawLine (1x, 1y, 2x, 2y); 
        page.drawLine (2x, 2y, 3x, 3y); 
        page.drawLine (3x, 3y, 1x, 1y); 

       } 

    } 
    } 
+2

'import java.awt。*'?我希望你不打算在你的代碼中的任何地方使用'List' ... – corsiKa

回答

3

您的變量不能以數字開頭。

您不能嵌套方法。 (我想你可能會考慮嵌套類,這是允許的)

您需要確保您聲明的變量在範圍內。

public class App { 
    int x1 = 0;   //<<<<<<------change your variable names 
    int y1 = 140; 
    int x2 = 120; 
    int y2 = 140; 
    int x3 = 60; 
    int y3 =0; 

public static void main(String[] args) 
    { 

     App app = new App();  //instantiate an instance 
     app.paint(g); //dunno where you get g, but paint needs to be its own method. 




    } 

    public void paint (Graphics page) 
       { 

        page.drawLine (1x, 1y, 2x, 2y); 
        page.drawLine (2x, 2y, 3x, 3y); 
        page.drawLine (3x, 3y, 1x, 1y); 

       } 
} 
+0

我拿出公共靜態void main,並運行。 – munchschair